From 8c8af4e9bfd8eba66ff524d60b23a3dcbb40cd99 Mon Sep 17 00:00:00 2001 From: gschambial Date: Fri, 10 Nov 2017 17:52:21 +0530 Subject: [PATCH 01/28] 1. Constructor and Setter injection explained with example of Computer, Processor and Memory --- spring-boot/.factorypath | 111 ++++++++++-------- .../SpringAutowireConstructorContext.java | 10 ++ .../config/SpringBeanConstructorContext.java | 28 +++++ .../factory/ComputerAutowireFactory.java | 28 +++++ .../factory/ComputerBeanFactory.java | 29 +++++ .../factory/ComputerXmlFactory.java | 31 +++++ .../constructor/model/autowire/Computer.java | 34 ++++++ .../di/constructor/model/autowire/Memory.java | 19 +++ .../constructor/model/autowire/Processor.java | 19 +++ .../di/constructor/model/bean/Computer.java | 29 +++++ .../di/constructor/model/bean/Memory.java | 16 +++ .../di/constructor/model/bean/Processor.java | 16 +++ .../di/constructor/model/xml/Computer.java | 29 +++++ .../di/constructor/model/xml/Memory.java | 20 ++++ .../di/constructor/model/xml/Processor.java | 16 +++ .../config/SpringAutowireSetterContext.java | 10 ++ .../config/SpringBeanSetterContext.java | 31 +++++ .../factory/ComputerAutowireFactory.java | 29 +++++ .../setter/factory/ComputerBeanFactory.java | 30 +++++ .../di/setter/factory/ComputerXmlFactory.java | 30 +++++ .../di/setter/model/autowire/Computer.java | 30 +++++ .../di/setter/model/autowire/Memory.java | 19 +++ .../di/setter/model/autowire/Processor.java | 19 +++ .../di/setter/model/bean/Computer.java | 24 ++++ .../baeldung/di/setter/model/bean/Memory.java | 16 +++ .../di/setter/model/bean/Processor.java | 16 +++ .../di/setter/model/xml/Computer.java | 24 ++++ .../baeldung/di/setter/model/xml/Memory.java | 20 ++++ .../di/setter/model/xml/Processor.java | 16 +++ .../application-di-constructor-context.xml | 23 ++++ .../application-di-setter-context.xml | 22 ++++ .../AutowireAnnotationInjectionTest.java | 28 +++++ .../BeanAnnotationInjectionTest.java | 28 +++++ .../di/constructor/XmlInjectionTest.java | 27 +++++ .../AutowireAnnotationInjectionTest.java | 28 +++++ .../setter/BeanAnnotationInjectionTest.java | 28 +++++ .../baeldung/di/setter/XmlInjectionTest.java | 27 +++++ 37 files changed, 910 insertions(+), 50 deletions(-) create mode 100644 spring-core/src/main/java/com/baeldung/di/constructor/config/SpringAutowireConstructorContext.java create mode 100644 spring-core/src/main/java/com/baeldung/di/constructor/config/SpringBeanConstructorContext.java create mode 100644 spring-core/src/main/java/com/baeldung/di/constructor/factory/ComputerAutowireFactory.java create mode 100644 spring-core/src/main/java/com/baeldung/di/constructor/factory/ComputerBeanFactory.java create mode 100644 spring-core/src/main/java/com/baeldung/di/constructor/factory/ComputerXmlFactory.java create mode 100644 spring-core/src/main/java/com/baeldung/di/constructor/model/autowire/Computer.java create mode 100644 spring-core/src/main/java/com/baeldung/di/constructor/model/autowire/Memory.java create mode 100644 spring-core/src/main/java/com/baeldung/di/constructor/model/autowire/Processor.java create mode 100644 spring-core/src/main/java/com/baeldung/di/constructor/model/bean/Computer.java create mode 100644 spring-core/src/main/java/com/baeldung/di/constructor/model/bean/Memory.java create mode 100644 spring-core/src/main/java/com/baeldung/di/constructor/model/bean/Processor.java create mode 100644 spring-core/src/main/java/com/baeldung/di/constructor/model/xml/Computer.java create mode 100644 spring-core/src/main/java/com/baeldung/di/constructor/model/xml/Memory.java create mode 100644 spring-core/src/main/java/com/baeldung/di/constructor/model/xml/Processor.java create mode 100644 spring-core/src/main/java/com/baeldung/di/setter/config/SpringAutowireSetterContext.java create mode 100644 spring-core/src/main/java/com/baeldung/di/setter/config/SpringBeanSetterContext.java create mode 100644 spring-core/src/main/java/com/baeldung/di/setter/factory/ComputerAutowireFactory.java create mode 100644 spring-core/src/main/java/com/baeldung/di/setter/factory/ComputerBeanFactory.java create mode 100644 spring-core/src/main/java/com/baeldung/di/setter/factory/ComputerXmlFactory.java create mode 100644 spring-core/src/main/java/com/baeldung/di/setter/model/autowire/Computer.java create mode 100644 spring-core/src/main/java/com/baeldung/di/setter/model/autowire/Memory.java create mode 100644 spring-core/src/main/java/com/baeldung/di/setter/model/autowire/Processor.java create mode 100644 spring-core/src/main/java/com/baeldung/di/setter/model/bean/Computer.java create mode 100644 spring-core/src/main/java/com/baeldung/di/setter/model/bean/Memory.java create mode 100644 spring-core/src/main/java/com/baeldung/di/setter/model/bean/Processor.java create mode 100644 spring-core/src/main/java/com/baeldung/di/setter/model/xml/Computer.java create mode 100644 spring-core/src/main/java/com/baeldung/di/setter/model/xml/Memory.java create mode 100644 spring-core/src/main/java/com/baeldung/di/setter/model/xml/Processor.java create mode 100644 spring-core/src/main/resources/application-di-constructor-context.xml create mode 100644 spring-core/src/main/resources/application-di-setter-context.xml create mode 100644 spring-core/src/test/java/com/baeldung/di/constructor/AutowireAnnotationInjectionTest.java create mode 100644 spring-core/src/test/java/com/baeldung/di/constructor/BeanAnnotationInjectionTest.java create mode 100644 spring-core/src/test/java/com/baeldung/di/constructor/XmlInjectionTest.java create mode 100644 spring-core/src/test/java/com/baeldung/di/setter/AutowireAnnotationInjectionTest.java create mode 100644 spring-core/src/test/java/com/baeldung/di/setter/BeanAnnotationInjectionTest.java create mode 100644 spring-core/src/test/java/com/baeldung/di/setter/XmlInjectionTest.java diff --git a/spring-boot/.factorypath b/spring-boot/.factorypath index aa15485f5c..97062c74a4 100644 --- a/spring-boot/.factorypath +++ b/spring-boot/.factorypath @@ -1,46 +1,30 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + @@ -48,81 +32,105 @@ - + + + + + - - + - - - + + + + + + + + + - - - - - + + + + + + + + + + + + + + + + + + + + - + + - @@ -133,7 +141,10 @@ + + + @@ -143,7 +154,7 @@ - + diff --git a/spring-core/src/main/java/com/baeldung/di/constructor/config/SpringAutowireConstructorContext.java b/spring-core/src/main/java/com/baeldung/di/constructor/config/SpringAutowireConstructorContext.java new file mode 100644 index 0000000000..d3c43d843e --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/di/constructor/config/SpringAutowireConstructorContext.java @@ -0,0 +1,10 @@ +package com.baeldung.di.constructor.config; + +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; + +@Configuration +@ComponentScan(basePackages= { "com.baeldung.di.constructor.model.autowire" }) +public class SpringAutowireConstructorContext { + +} diff --git a/spring-core/src/main/java/com/baeldung/di/constructor/config/SpringBeanConstructorContext.java b/spring-core/src/main/java/com/baeldung/di/constructor/config/SpringBeanConstructorContext.java new file mode 100644 index 0000000000..0ac1138e78 --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/di/constructor/config/SpringBeanConstructorContext.java @@ -0,0 +1,28 @@ +package com.baeldung.di.constructor.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import com.baeldung.di.constructor.model.bean.Computer; +import com.baeldung.di.constructor.model.bean.Memory; +import com.baeldung.di.constructor.model.bean.Processor; + +@Configuration +public class SpringBeanConstructorContext { + + @Bean + public Computer computer(){ + return new Computer(processor(), memory()); + } + + @Bean + public Processor processor(){ + return new Processor(); + } + + @Bean + public Memory memory(){ + return new Memory(); + } + +} diff --git a/spring-core/src/main/java/com/baeldung/di/constructor/factory/ComputerAutowireFactory.java b/spring-core/src/main/java/com/baeldung/di/constructor/factory/ComputerAutowireFactory.java new file mode 100644 index 0000000000..a86733c3d1 --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/di/constructor/factory/ComputerAutowireFactory.java @@ -0,0 +1,28 @@ +package com.baeldung.di.constructor.factory; + +import org.springframework.context.ApplicationContext; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; + +import com.baeldung.di.constructor.config.SpringAutowireConstructorContext; +import com.baeldung.di.constructor.model.autowire.Computer; + +public class ComputerAutowireFactory { + + public static void main(String[] args) { + // Create Spring Application Context + ApplicationContext context = new AnnotationConfigApplicationContext(SpringAutowireConstructorContext.class); + + // Get Spring bean "computer" created + Computer computer = (Computer) context.getBean("computer"); + System.out.println("---------------- Constructor Injection via @Autowire------------------"); + System.out.println("Processor cores : " + computer.getProcessor().getCores()); + System.out.println("Processor frequency : " + computer.getProcessor().getFrequency()); + System.out.println("Memory Size in GB : " + computer.getMemory().getSizeInGb()); + System.out.println("Memory format : " + computer.getMemory().getFormat()); + System.out.println("---------------- Constructor Injection via @Autowire------------------"); + // Close Spring Application Context + ((ConfigurableApplicationContext) context).close(); + } + +} diff --git a/spring-core/src/main/java/com/baeldung/di/constructor/factory/ComputerBeanFactory.java b/spring-core/src/main/java/com/baeldung/di/constructor/factory/ComputerBeanFactory.java new file mode 100644 index 0000000000..b518438521 --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/di/constructor/factory/ComputerBeanFactory.java @@ -0,0 +1,29 @@ +package com.baeldung.di.constructor.factory; + +import org.springframework.context.ApplicationContext; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; + +import com.baeldung.di.constructor.config.SpringBeanConstructorContext; +import com.baeldung.di.constructor.model.bean.Computer; + +public class ComputerBeanFactory { + + public static void main(String[] args) { + // Create Spring Application Context + ApplicationContext context = new AnnotationConfigApplicationContext(SpringBeanConstructorContext.class); + + // Get Spring bean "computer" created + Computer computer = (Computer) context.getBean("computer"); + System.out.println("---------------- Constructor Injection via @Bean ------------------"); + System.out.println("Processor cores : " + computer.getProcessor().getCores()); + System.out.println("Processor frequency : " + computer.getProcessor().getFrequency()); + System.out.println("Memory Size in GB : " + computer.getMemory().getSizeInGb()); + System.out.println("Memory format : " + computer.getMemory().getFormat()); + System.out.println("---------------- Constructor Injection via @Bean ------------------"); + + // Close Spring Application Context + ((ConfigurableApplicationContext) context).close(); + } + +} diff --git a/spring-core/src/main/java/com/baeldung/di/constructor/factory/ComputerXmlFactory.java b/spring-core/src/main/java/com/baeldung/di/constructor/factory/ComputerXmlFactory.java new file mode 100644 index 0000000000..2fa12525df --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/di/constructor/factory/ComputerXmlFactory.java @@ -0,0 +1,31 @@ +package com.baeldung.di.constructor.factory; + +import org.springframework.context.ApplicationContext; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +import com.baeldung.di.constructor.model.xml.Computer; + + + +public class ComputerXmlFactory { + + public static void main(String[] args) { + // Create Spring Application Context + ApplicationContext context = new ClassPathXmlApplicationContext("application-di-constructor-context.xml"); + + // Get Spring bean "computer" created + Computer computer = (Computer) context.getBean("computer"); + System.out.println("---------------- Constructor Injection via XML ------------------"); + System.out.println("Processor cores : " + computer.getProcessor().getCores()); + System.out.println("Processor frequency : " + computer.getProcessor().getFrequency()); + System.out.println("Memory Size in GB : " + computer.getMemory().getSizeInGb()); + System.out.println("Memory format : " + computer.getMemory().getFormat()); + System.out.println("---------------- Constructor Injection via XML ------------------"); + + // Close Spring Application Context + ((ConfigurableApplicationContext) context).close(); + + } + +} diff --git a/spring-core/src/main/java/com/baeldung/di/constructor/model/autowire/Computer.java b/spring-core/src/main/java/com/baeldung/di/constructor/model/autowire/Computer.java new file mode 100644 index 0000000000..b470aedeed --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/di/constructor/model/autowire/Computer.java @@ -0,0 +1,34 @@ +package com.baeldung.di.constructor.model.autowire; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +@Component +public class Computer { + + private Processor processor; + private Memory memory; + + @Autowired + public Computer(Processor processor, Memory memory) { + this.processor = processor; + this.memory = memory; + } + + public Processor getProcessor() { + return processor; + } + + public void setProcessor(Processor processor) { + this.processor = processor; + } + + public Memory getMemory() { + return memory; + } + + public void setMemory(Memory memory) { + this.memory = memory; + } + +} diff --git a/spring-core/src/main/java/com/baeldung/di/constructor/model/autowire/Memory.java b/spring-core/src/main/java/com/baeldung/di/constructor/model/autowire/Memory.java new file mode 100644 index 0000000000..29633dd5ca --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/di/constructor/model/autowire/Memory.java @@ -0,0 +1,19 @@ +package com.baeldung.di.constructor.model.autowire; + +import org.springframework.stereotype.Component; + +@Component +public class Memory { + + private Integer sizeInGb = 16; + private String format = "DDR3"; + + public Integer getSizeInGb() { + return sizeInGb; + } + + public String getFormat() { + return format; + } + +} diff --git a/spring-core/src/main/java/com/baeldung/di/constructor/model/autowire/Processor.java b/spring-core/src/main/java/com/baeldung/di/constructor/model/autowire/Processor.java new file mode 100644 index 0000000000..0d81e2a005 --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/di/constructor/model/autowire/Processor.java @@ -0,0 +1,19 @@ +package com.baeldung.di.constructor.model.autowire; + +import org.springframework.stereotype.Component; + +@Component +public class Processor { + + private Integer cores = 2; + private Double frequency = 1.4; + + public Integer getCores() { + return cores; + } + + public Double getFrequency() { + return frequency; + } + +} diff --git a/spring-core/src/main/java/com/baeldung/di/constructor/model/bean/Computer.java b/spring-core/src/main/java/com/baeldung/di/constructor/model/bean/Computer.java new file mode 100644 index 0000000000..d8856420d0 --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/di/constructor/model/bean/Computer.java @@ -0,0 +1,29 @@ +package com.baeldung.di.constructor.model.bean; + +public class Computer { + + private Processor processor; + private Memory memory; + + public Computer(Processor processor, Memory memory) { + this.processor = processor; + this.memory = memory; + } + + public Processor getProcessor() { + return processor; + } + + public void setProcessor(Processor processor) { + this.processor = processor; + } + + public Memory getMemory() { + return memory; + } + + public void setMemory(Memory memory) { + this.memory = memory; + } + +} diff --git a/spring-core/src/main/java/com/baeldung/di/constructor/model/bean/Memory.java b/spring-core/src/main/java/com/baeldung/di/constructor/model/bean/Memory.java new file mode 100644 index 0000000000..8e748bb1a0 --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/di/constructor/model/bean/Memory.java @@ -0,0 +1,16 @@ +package com.baeldung.di.constructor.model.bean; + +public class Memory { + + private Integer sizeInGb = 16; + private String format = "DDR3"; + + public Integer getSizeInGb() { + return sizeInGb; + } + + public String getFormat() { + return format; + } + +} diff --git a/spring-core/src/main/java/com/baeldung/di/constructor/model/bean/Processor.java b/spring-core/src/main/java/com/baeldung/di/constructor/model/bean/Processor.java new file mode 100644 index 0000000000..1566606148 --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/di/constructor/model/bean/Processor.java @@ -0,0 +1,16 @@ +package com.baeldung.di.constructor.model.bean; + +public class Processor { + + private Integer cores = 2; + private Double frequency = 1.4; + + public Integer getCores() { + return cores; + } + + public Double getFrequency() { + return frequency; + } + +} diff --git a/spring-core/src/main/java/com/baeldung/di/constructor/model/xml/Computer.java b/spring-core/src/main/java/com/baeldung/di/constructor/model/xml/Computer.java new file mode 100644 index 0000000000..dbd70cf6a4 --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/di/constructor/model/xml/Computer.java @@ -0,0 +1,29 @@ +package com.baeldung.di.constructor.model.xml; + +public class Computer { + + private Processor processor; + private Memory memory; + + public Computer(Processor processor, Memory memory) { + this.processor = processor; + this.memory = memory; + } + + public Processor getProcessor() { + return processor; + } + + public void setProcessor(Processor processor) { + this.processor = processor; + } + + public Memory getMemory() { + return memory; + } + + public void setMemory(Memory memory) { + this.memory = memory; + } + +} diff --git a/spring-core/src/main/java/com/baeldung/di/constructor/model/xml/Memory.java b/spring-core/src/main/java/com/baeldung/di/constructor/model/xml/Memory.java new file mode 100644 index 0000000000..f5ea3b4ec9 --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/di/constructor/model/xml/Memory.java @@ -0,0 +1,20 @@ +package com.baeldung.di.constructor.model.xml; + +public class Memory { + + private Integer sizeInGb = 16; + private String format = "DDR3"; + + public Memory() { + + } + + public Integer getSizeInGb() { + return sizeInGb; + } + + public String getFormat() { + return format; + } + +} diff --git a/spring-core/src/main/java/com/baeldung/di/constructor/model/xml/Processor.java b/spring-core/src/main/java/com/baeldung/di/constructor/model/xml/Processor.java new file mode 100644 index 0000000000..72733829d8 --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/di/constructor/model/xml/Processor.java @@ -0,0 +1,16 @@ +package com.baeldung.di.constructor.model.xml; + +public class Processor { + + private Integer cores = 2; + private Double frequency = 1.4; + + public Integer getCores() { + return cores; + } + + public Double getFrequency() { + return frequency; + } + +} diff --git a/spring-core/src/main/java/com/baeldung/di/setter/config/SpringAutowireSetterContext.java b/spring-core/src/main/java/com/baeldung/di/setter/config/SpringAutowireSetterContext.java new file mode 100644 index 0000000000..4e0ac0df28 --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/di/setter/config/SpringAutowireSetterContext.java @@ -0,0 +1,10 @@ +package com.baeldung.di.setter.config; + +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; + +@Configuration +@ComponentScan(basePackages= { "com.baeldung.di.setter.model.autowire" }) +public class SpringAutowireSetterContext { + +} diff --git a/spring-core/src/main/java/com/baeldung/di/setter/config/SpringBeanSetterContext.java b/spring-core/src/main/java/com/baeldung/di/setter/config/SpringBeanSetterContext.java new file mode 100644 index 0000000000..885173f5b4 --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/di/setter/config/SpringBeanSetterContext.java @@ -0,0 +1,31 @@ +package com.baeldung.di.setter.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import com.baeldung.di.setter.model.bean.Computer; +import com.baeldung.di.setter.model.bean.Memory; +import com.baeldung.di.setter.model.bean.Processor; + +@Configuration +public class SpringBeanSetterContext { + + @Bean + public Computer computer(){ + Computer computer = new Computer(); + computer.setProcessor(processor()); + computer.setMemory(memory()); + return computer; + } + + @Bean + public Processor processor(){ + return new Processor(); + } + + @Bean + public Memory memory(){ + return new Memory(); + } + +} diff --git a/spring-core/src/main/java/com/baeldung/di/setter/factory/ComputerAutowireFactory.java b/spring-core/src/main/java/com/baeldung/di/setter/factory/ComputerAutowireFactory.java new file mode 100644 index 0000000000..98c3fccb27 --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/di/setter/factory/ComputerAutowireFactory.java @@ -0,0 +1,29 @@ +package com.baeldung.di.setter.factory; + +import org.springframework.context.ApplicationContext; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; + +import com.baeldung.di.setter.config.SpringAutowireSetterContext; +import com.baeldung.di.setter.model.autowire.Computer; + + +public class ComputerAutowireFactory { + + public static void main(String[] args) { + // Create Spring Application Context + ApplicationContext context = new AnnotationConfigApplicationContext(SpringAutowireSetterContext.class); + + // Get Spring bean "computer" created + Computer computer = (Computer) context.getBean("computer"); + System.out.println("---------------- Setter Injection via @Autowire ------------------"); + System.out.println("Processor cores : " + computer.getProcessor().getCores()); + System.out.println("Processor frequency : " + computer.getProcessor().getFrequency()); + System.out.println("Memory Size in GB : " + computer.getMemory().getSizeInGb()); + System.out.println("Memory format : " + computer.getMemory().getFormat()); + System.out.println("---------------- Setter Injection via @Autowire ------------------"); + // Close Spring Application Context + ((ConfigurableApplicationContext) context).close(); + } + +} diff --git a/spring-core/src/main/java/com/baeldung/di/setter/factory/ComputerBeanFactory.java b/spring-core/src/main/java/com/baeldung/di/setter/factory/ComputerBeanFactory.java new file mode 100644 index 0000000000..6341029e95 --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/di/setter/factory/ComputerBeanFactory.java @@ -0,0 +1,30 @@ +package com.baeldung.di.setter.factory; + +import org.springframework.context.ApplicationContext; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; + +import com.baeldung.di.setter.config.SpringBeanSetterContext; +import com.baeldung.di.setter.model.bean.Computer; + + +public class ComputerBeanFactory { + + public static void main(String[] args) { + // Create Spring Application Context + ApplicationContext context = new AnnotationConfigApplicationContext(SpringBeanSetterContext.class); + + // Get Spring bean "computer" created + Computer computer = (Computer) context.getBean("computer"); + System.out.println("---------------- Setter Injection via @Bean ------------------"); + System.out.println("Processor cores : " + computer.getProcessor().getCores()); + System.out.println("Processor frequency : " + computer.getProcessor().getFrequency()); + System.out.println("Memory Size in GB : " + computer.getMemory().getSizeInGb()); + System.out.println("Memory format : " + computer.getMemory().getFormat()); + System.out.println("---------------- Setter Injection via @Bean ------------------"); + + // Close Spring Application Context + ((ConfigurableApplicationContext) context).close(); + } + +} diff --git a/spring-core/src/main/java/com/baeldung/di/setter/factory/ComputerXmlFactory.java b/spring-core/src/main/java/com/baeldung/di/setter/factory/ComputerXmlFactory.java new file mode 100644 index 0000000000..707725d340 --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/di/setter/factory/ComputerXmlFactory.java @@ -0,0 +1,30 @@ +package com.baeldung.di.setter.factory; + +import org.springframework.context.ApplicationContext; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +import com.baeldung.di.setter.model.xml.Computer; + + + +public class ComputerXmlFactory { + + public static void main(String[] args) { + // Create Spring Application Context + ApplicationContext context = new ClassPathXmlApplicationContext("application-setter-context.xml"); + + // Get Spring bean "computer" created + Computer computer = (Computer) context.getBean("computer"); + System.out.println("---------------- Setter Injection via XML ------------------"); + System.out.println("Processor cores : " + computer.getProcessor().getCores()); + System.out.println("Processor frequency : " + computer.getProcessor().getFrequency()); + System.out.println("Memory Size in GB : " + computer.getMemory().getSizeInGb()); + System.out.println("Memory format : " + computer.getMemory().getFormat()); + System.out.println("---------------- Setter Injection via XML ------------------"); + // Close Spring Application Context + ((ConfigurableApplicationContext) context).close(); + + } + +} diff --git a/spring-core/src/main/java/com/baeldung/di/setter/model/autowire/Computer.java b/spring-core/src/main/java/com/baeldung/di/setter/model/autowire/Computer.java new file mode 100644 index 0000000000..4d8a3b1dd3 --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/di/setter/model/autowire/Computer.java @@ -0,0 +1,30 @@ +package com.baeldung.di.setter.model.autowire; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +@Component +public class Computer { + + private Processor processor; + private Memory memory; + + public Processor getProcessor() { + return processor; + } + + @Autowired + public void setProcessor(Processor processor) { + this.processor = processor; + } + + public Memory getMemory() { + return memory; + } + + @Autowired + public void setMemory(Memory memory) { + this.memory = memory; + } + +} diff --git a/spring-core/src/main/java/com/baeldung/di/setter/model/autowire/Memory.java b/spring-core/src/main/java/com/baeldung/di/setter/model/autowire/Memory.java new file mode 100644 index 0000000000..0953a42b35 --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/di/setter/model/autowire/Memory.java @@ -0,0 +1,19 @@ +package com.baeldung.di.setter.model.autowire; + +import org.springframework.stereotype.Component; + +@Component +public class Memory { + + private Integer sizeInGb = 16; + private String format = "DDR3"; + + public Integer getSizeInGb() { + return sizeInGb; + } + + public String getFormat() { + return format; + } + +} diff --git a/spring-core/src/main/java/com/baeldung/di/setter/model/autowire/Processor.java b/spring-core/src/main/java/com/baeldung/di/setter/model/autowire/Processor.java new file mode 100644 index 0000000000..ee5ef3b6cd --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/di/setter/model/autowire/Processor.java @@ -0,0 +1,19 @@ +package com.baeldung.di.setter.model.autowire; + +import org.springframework.stereotype.Component; + +@Component +public class Processor { + + private Integer cores = 2; + private Double frequency = 1.4; + + public Integer getCores() { + return cores; + } + + public Double getFrequency() { + return frequency; + } + +} diff --git a/spring-core/src/main/java/com/baeldung/di/setter/model/bean/Computer.java b/spring-core/src/main/java/com/baeldung/di/setter/model/bean/Computer.java new file mode 100644 index 0000000000..01c87a6c73 --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/di/setter/model/bean/Computer.java @@ -0,0 +1,24 @@ +package com.baeldung.di.setter.model.bean; + +public class Computer { + + private Processor processor; + private Memory memory; + + public Processor getProcessor() { + return processor; + } + + public void setProcessor(Processor processor) { + this.processor = processor; + } + + public Memory getMemory() { + return memory; + } + + public void setMemory(Memory memory) { + this.memory = memory; + } + +} diff --git a/spring-core/src/main/java/com/baeldung/di/setter/model/bean/Memory.java b/spring-core/src/main/java/com/baeldung/di/setter/model/bean/Memory.java new file mode 100644 index 0000000000..bfe94aa8e7 --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/di/setter/model/bean/Memory.java @@ -0,0 +1,16 @@ +package com.baeldung.di.setter.model.bean; + +public class Memory { + + private Integer sizeInGb = 16; + private String format = "DDR3"; + + public Integer getSizeInGb() { + return sizeInGb; + } + + public String getFormat() { + return format; + } + +} diff --git a/spring-core/src/main/java/com/baeldung/di/setter/model/bean/Processor.java b/spring-core/src/main/java/com/baeldung/di/setter/model/bean/Processor.java new file mode 100644 index 0000000000..982b56f3d0 --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/di/setter/model/bean/Processor.java @@ -0,0 +1,16 @@ +package com.baeldung.di.setter.model.bean; + +public class Processor { + + private Integer cores = 2; + private Double frequency = 1.4; + + public Integer getCores() { + return cores; + } + + public Double getFrequency() { + return frequency; + } + +} diff --git a/spring-core/src/main/java/com/baeldung/di/setter/model/xml/Computer.java b/spring-core/src/main/java/com/baeldung/di/setter/model/xml/Computer.java new file mode 100644 index 0000000000..3e7fa58ea8 --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/di/setter/model/xml/Computer.java @@ -0,0 +1,24 @@ +package com.baeldung.di.setter.model.xml; + +public class Computer { + + private Processor processor; + private Memory memory; + + public Processor getProcessor() { + return processor; + } + + public void setProcessor(Processor processor) { + this.processor = processor; + } + + public Memory getMemory() { + return memory; + } + + public void setMemory(Memory memory) { + this.memory = memory; + } + +} diff --git a/spring-core/src/main/java/com/baeldung/di/setter/model/xml/Memory.java b/spring-core/src/main/java/com/baeldung/di/setter/model/xml/Memory.java new file mode 100644 index 0000000000..cb11c59583 --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/di/setter/model/xml/Memory.java @@ -0,0 +1,20 @@ +package com.baeldung.di.setter.model.xml; + +public class Memory { + + private Integer sizeInGb = 16; + private String format = "DDR3"; + + public Memory() { + + } + + public Integer getSizeInGb() { + return sizeInGb; + } + + public String getFormat() { + return format; + } + +} diff --git a/spring-core/src/main/java/com/baeldung/di/setter/model/xml/Processor.java b/spring-core/src/main/java/com/baeldung/di/setter/model/xml/Processor.java new file mode 100644 index 0000000000..07405e13ea --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/di/setter/model/xml/Processor.java @@ -0,0 +1,16 @@ +package com.baeldung.di.setter.model.xml; + +public class Processor { + + private Integer cores = 2; + private Double frequency = 1.4; + + public Integer getCores() { + return cores; + } + + public Double getFrequency() { + return frequency; + } + +} diff --git a/spring-core/src/main/resources/application-di-constructor-context.xml b/spring-core/src/main/resources/application-di-constructor-context.xml new file mode 100644 index 0000000000..46d1256988 --- /dev/null +++ b/spring-core/src/main/resources/application-di-constructor-context.xml @@ -0,0 +1,23 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/spring-core/src/main/resources/application-di-setter-context.xml b/spring-core/src/main/resources/application-di-setter-context.xml new file mode 100644 index 0000000000..3831041e01 --- /dev/null +++ b/spring-core/src/main/resources/application-di-setter-context.xml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/spring-core/src/test/java/com/baeldung/di/constructor/AutowireAnnotationInjectionTest.java b/spring-core/src/test/java/com/baeldung/di/constructor/AutowireAnnotationInjectionTest.java new file mode 100644 index 0000000000..87cb549ace --- /dev/null +++ b/spring-core/src/test/java/com/baeldung/di/constructor/AutowireAnnotationInjectionTest.java @@ -0,0 +1,28 @@ +package com.baeldung.di.constructor; + +import static org.junit.Assert.assertNotNull; + +import org.junit.Before; +import org.junit.Test; +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; + +import com.baeldung.di.constructor.config.SpringAutowireConstructorContext; +import com.baeldung.di.constructor.model.autowire.Computer; + +public class AutowireAnnotationInjectionTest { + + private ApplicationContext applicationContext; + + @Before + public void setUp() throws Exception { + applicationContext = new AnnotationConfigApplicationContext(SpringAutowireConstructorContext.class); + } + + @Test + public void getComputer_() { + final Computer computer = applicationContext.getBean("computer", Computer.class); + assertNotNull(computer); + } + +} diff --git a/spring-core/src/test/java/com/baeldung/di/constructor/BeanAnnotationInjectionTest.java b/spring-core/src/test/java/com/baeldung/di/constructor/BeanAnnotationInjectionTest.java new file mode 100644 index 0000000000..61f718d4ba --- /dev/null +++ b/spring-core/src/test/java/com/baeldung/di/constructor/BeanAnnotationInjectionTest.java @@ -0,0 +1,28 @@ +package com.baeldung.di.constructor; + +import static org.junit.Assert.assertNotNull; + +import org.junit.Before; +import org.junit.Test; +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; + +import com.baeldung.di.constructor.config.SpringBeanConstructorContext; +import com.baeldung.di.constructor.model.bean.Computer; + +public class BeanAnnotationInjectionTest { + + private ApplicationContext applicationContext; + + @Before + public void setUp() throws Exception { + applicationContext = new AnnotationConfigApplicationContext(SpringBeanConstructorContext.class); + } + + @Test + public void getComputer_() { + final Computer computer = applicationContext.getBean("computer", Computer.class); + assertNotNull(computer); + } + +} diff --git a/spring-core/src/test/java/com/baeldung/di/constructor/XmlInjectionTest.java b/spring-core/src/test/java/com/baeldung/di/constructor/XmlInjectionTest.java new file mode 100644 index 0000000000..4787c19cbf --- /dev/null +++ b/spring-core/src/test/java/com/baeldung/di/constructor/XmlInjectionTest.java @@ -0,0 +1,27 @@ +package com.baeldung.di.constructor; + +import static org.junit.Assert.assertNotNull; + +import org.junit.Before; +import org.junit.Test; +import org.springframework.context.ApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +import com.baeldung.di.constructor.model.xml.Computer; + +public class XmlInjectionTest { + + private ApplicationContext applicationContext; + + @Before + public void setUp() throws Exception { + applicationContext = new ClassPathXmlApplicationContext("application-di-constructor-context.xml"); + } + + @Test + public void getComputer_() { + final Computer computer = applicationContext.getBean("computer", Computer.class); + assertNotNull(computer); + } + +} diff --git a/spring-core/src/test/java/com/baeldung/di/setter/AutowireAnnotationInjectionTest.java b/spring-core/src/test/java/com/baeldung/di/setter/AutowireAnnotationInjectionTest.java new file mode 100644 index 0000000000..036c6826e3 --- /dev/null +++ b/spring-core/src/test/java/com/baeldung/di/setter/AutowireAnnotationInjectionTest.java @@ -0,0 +1,28 @@ +package com.baeldung.di.setter; + +import static org.junit.Assert.assertNotNull; + +import org.junit.Before; +import org.junit.Test; +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; + +import com.baeldung.di.setter.config.SpringAutowireSetterContext; +import com.baeldung.di.setter.model.autowire.Computer; + +public class AutowireAnnotationInjectionTest { + + private ApplicationContext applicationContext; + + @Before + public void setUp() throws Exception { + applicationContext = new AnnotationConfigApplicationContext(SpringAutowireSetterContext.class); + } + + @Test + public void getComputer_() { + final Computer computer = applicationContext.getBean("computer", Computer.class); + assertNotNull(computer); + } + +} diff --git a/spring-core/src/test/java/com/baeldung/di/setter/BeanAnnotationInjectionTest.java b/spring-core/src/test/java/com/baeldung/di/setter/BeanAnnotationInjectionTest.java new file mode 100644 index 0000000000..53f1d4e10e --- /dev/null +++ b/spring-core/src/test/java/com/baeldung/di/setter/BeanAnnotationInjectionTest.java @@ -0,0 +1,28 @@ +package com.baeldung.di.setter; + +import static org.junit.Assert.assertNotNull; + +import org.junit.Before; +import org.junit.Test; +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; + +import com.baeldung.di.setter.config.SpringBeanSetterContext; +import com.baeldung.di.setter.model.bean.Computer; + +public class BeanAnnotationInjectionTest { + + private ApplicationContext applicationContext; + + @Before + public void setUp() throws Exception { + applicationContext = new AnnotationConfigApplicationContext(SpringBeanSetterContext.class); + } + + @Test + public void getComputer_() { + final Computer computer = applicationContext.getBean("computer", Computer.class); + assertNotNull(computer); + } + +} diff --git a/spring-core/src/test/java/com/baeldung/di/setter/XmlInjectionTest.java b/spring-core/src/test/java/com/baeldung/di/setter/XmlInjectionTest.java new file mode 100644 index 0000000000..52452be119 --- /dev/null +++ b/spring-core/src/test/java/com/baeldung/di/setter/XmlInjectionTest.java @@ -0,0 +1,27 @@ +package com.baeldung.di.setter; + +import static org.junit.Assert.assertNotNull; + +import org.junit.Before; +import org.junit.Test; +import org.springframework.context.ApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +import com.baeldung.di.setter.model.xml.Computer; + +public class XmlInjectionTest { + + private ApplicationContext applicationContext; + + @Before + public void setUp() throws Exception { + applicationContext = new ClassPathXmlApplicationContext("application-di-setter-context.xml"); + } + + @Test + public void getComputer_() { + final Computer computer = applicationContext.getBean("computer", Computer.class); + assertNotNull(computer); + } + +} From b2dead94841756a35c0743486e4b08b609513888 Mon Sep 17 00:00:00 2001 From: gschambial Date: Fri, 10 Nov 2017 18:14:31 +0530 Subject: [PATCH 02/28] Baeldung eclipse formatter added. --- .../SpringAutowireConstructorContext.java | 4 +- .../config/SpringBeanConstructorContext.java | 32 +++++++-------- .../factory/ComputerAutowireFactory.java | 32 ++++++++------- .../factory/ComputerBeanFactory.java | 32 ++++++++------- .../factory/ComputerXmlFactory.java | 36 +++++++++-------- .../constructor/model/autowire/Computer.java | 40 +++++++++---------- .../di/constructor/model/autowire/Memory.java | 18 ++++----- .../constructor/model/autowire/Processor.java | 16 ++++---- .../di/constructor/model/bean/Computer.java | 38 +++++++++--------- .../di/constructor/model/bean/Memory.java | 18 ++++----- .../di/constructor/model/bean/Processor.java | 16 ++++---- .../di/constructor/model/xml/Computer.java | 40 +++++++++---------- .../di/constructor/model/xml/Memory.java | 20 +++++----- .../di/constructor/model/xml/Processor.java | 16 ++++---- .../config/SpringAutowireSetterContext.java | 4 +- .../config/SpringBeanSetterContext.java | 38 +++++++++--------- .../factory/ComputerAutowireFactory.java | 33 ++++++++------- .../setter/factory/ComputerBeanFactory.java | 33 ++++++++------- .../di/setter/factory/ComputerXmlFactory.java | 36 +++++++++-------- .../di/setter/model/autowire/Computer.java | 36 ++++++++--------- .../di/setter/model/autowire/Memory.java | 18 ++++----- .../di/setter/model/autowire/Processor.java | 16 ++++---- .../di/setter/model/bean/Computer.java | 32 +++++++-------- .../baeldung/di/setter/model/bean/Memory.java | 19 ++++----- .../di/setter/model/bean/Processor.java | 16 ++++---- .../di/setter/model/xml/Computer.java | 30 +++++++------- .../baeldung/di/setter/model/xml/Memory.java | 20 +++++----- .../di/setter/model/xml/Processor.java | 16 ++++---- .../application-di-constructor-context.xml | 6 +-- .../application-di-setter-context.xml | 2 +- 30 files changed, 366 insertions(+), 347 deletions(-) diff --git a/spring-core/src/main/java/com/baeldung/di/constructor/config/SpringAutowireConstructorContext.java b/spring-core/src/main/java/com/baeldung/di/constructor/config/SpringAutowireConstructorContext.java index d3c43d843e..ae67bc7e5e 100644 --- a/spring-core/src/main/java/com/baeldung/di/constructor/config/SpringAutowireConstructorContext.java +++ b/spring-core/src/main/java/com/baeldung/di/constructor/config/SpringAutowireConstructorContext.java @@ -4,7 +4,7 @@ import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; @Configuration -@ComponentScan(basePackages= { "com.baeldung.di.constructor.model.autowire" }) +@ComponentScan(basePackages = { "com.baeldung.di.constructor.model.autowire" }) public class SpringAutowireConstructorContext { - + } diff --git a/spring-core/src/main/java/com/baeldung/di/constructor/config/SpringBeanConstructorContext.java b/spring-core/src/main/java/com/baeldung/di/constructor/config/SpringBeanConstructorContext.java index 0ac1138e78..670f18cb9c 100644 --- a/spring-core/src/main/java/com/baeldung/di/constructor/config/SpringBeanConstructorContext.java +++ b/spring-core/src/main/java/com/baeldung/di/constructor/config/SpringBeanConstructorContext.java @@ -9,20 +9,20 @@ import com.baeldung.di.constructor.model.bean.Processor; @Configuration public class SpringBeanConstructorContext { - - @Bean - public Computer computer(){ - return new Computer(processor(), memory()); - } - - @Bean - public Processor processor(){ - return new Processor(); - } - - @Bean - public Memory memory(){ - return new Memory(); - } - + + @Bean + public Computer computer() { + return new Computer(processor(), memory()); + } + + @Bean + public Processor processor() { + return new Processor(); + } + + @Bean + public Memory memory() { + return new Memory(); + } + } diff --git a/spring-core/src/main/java/com/baeldung/di/constructor/factory/ComputerAutowireFactory.java b/spring-core/src/main/java/com/baeldung/di/constructor/factory/ComputerAutowireFactory.java index a86733c3d1..2a13adf250 100644 --- a/spring-core/src/main/java/com/baeldung/di/constructor/factory/ComputerAutowireFactory.java +++ b/spring-core/src/main/java/com/baeldung/di/constructor/factory/ComputerAutowireFactory.java @@ -9,20 +9,24 @@ import com.baeldung.di.constructor.model.autowire.Computer; public class ComputerAutowireFactory { - public static void main(String[] args) { - // Create Spring Application Context - ApplicationContext context = new AnnotationConfigApplicationContext(SpringAutowireConstructorContext.class); + public static void main(String[] args) { + // Create Spring Application Context + ApplicationContext context = new AnnotationConfigApplicationContext(SpringAutowireConstructorContext.class); - // Get Spring bean "computer" created - Computer computer = (Computer) context.getBean("computer"); - System.out.println("---------------- Constructor Injection via @Autowire------------------"); - System.out.println("Processor cores : " + computer.getProcessor().getCores()); - System.out.println("Processor frequency : " + computer.getProcessor().getFrequency()); - System.out.println("Memory Size in GB : " + computer.getMemory().getSizeInGb()); - System.out.println("Memory format : " + computer.getMemory().getFormat()); - System.out.println("---------------- Constructor Injection via @Autowire------------------"); - // Close Spring Application Context - ((ConfigurableApplicationContext) context).close(); - } + // Get Spring bean "computer" created + Computer computer = (Computer) context.getBean("computer"); + System.out.println("---------------- Constructor Injection via @Autowire------------------"); + System.out.println("Processor cores : " + computer.getProcessor() + .getCores()); + System.out.println("Processor frequency : " + computer.getProcessor() + .getFrequency()); + System.out.println("Memory Size in GB : " + computer.getMemory() + .getSizeInGb()); + System.out.println("Memory format : " + computer.getMemory() + .getFormat()); + System.out.println("---------------- Constructor Injection via @Autowire------------------"); + // Close Spring Application Context + ((ConfigurableApplicationContext) context).close(); + } } diff --git a/spring-core/src/main/java/com/baeldung/di/constructor/factory/ComputerBeanFactory.java b/spring-core/src/main/java/com/baeldung/di/constructor/factory/ComputerBeanFactory.java index b518438521..713ea82b00 100644 --- a/spring-core/src/main/java/com/baeldung/di/constructor/factory/ComputerBeanFactory.java +++ b/spring-core/src/main/java/com/baeldung/di/constructor/factory/ComputerBeanFactory.java @@ -9,21 +9,25 @@ import com.baeldung.di.constructor.model.bean.Computer; public class ComputerBeanFactory { - public static void main(String[] args) { - // Create Spring Application Context - ApplicationContext context = new AnnotationConfigApplicationContext(SpringBeanConstructorContext.class); + public static void main(String[] args) { + // Create Spring Application Context + ApplicationContext context = new AnnotationConfigApplicationContext(SpringBeanConstructorContext.class); - // Get Spring bean "computer" created - Computer computer = (Computer) context.getBean("computer"); - System.out.println("---------------- Constructor Injection via @Bean ------------------"); - System.out.println("Processor cores : " + computer.getProcessor().getCores()); - System.out.println("Processor frequency : " + computer.getProcessor().getFrequency()); - System.out.println("Memory Size in GB : " + computer.getMemory().getSizeInGb()); - System.out.println("Memory format : " + computer.getMemory().getFormat()); - System.out.println("---------------- Constructor Injection via @Bean ------------------"); + // Get Spring bean "computer" created + Computer computer = (Computer) context.getBean("computer"); + System.out.println("---------------- Constructor Injection via @Bean ------------------"); + System.out.println("Processor cores : " + computer.getProcessor() + .getCores()); + System.out.println("Processor frequency : " + computer.getProcessor() + .getFrequency()); + System.out.println("Memory Size in GB : " + computer.getMemory() + .getSizeInGb()); + System.out.println("Memory format : " + computer.getMemory() + .getFormat()); + System.out.println("---------------- Constructor Injection via @Bean ------------------"); - // Close Spring Application Context - ((ConfigurableApplicationContext) context).close(); - } + // Close Spring Application Context + ((ConfigurableApplicationContext) context).close(); + } } diff --git a/spring-core/src/main/java/com/baeldung/di/constructor/factory/ComputerXmlFactory.java b/spring-core/src/main/java/com/baeldung/di/constructor/factory/ComputerXmlFactory.java index 2fa12525df..afe5fa281b 100644 --- a/spring-core/src/main/java/com/baeldung/di/constructor/factory/ComputerXmlFactory.java +++ b/spring-core/src/main/java/com/baeldung/di/constructor/factory/ComputerXmlFactory.java @@ -6,26 +6,28 @@ import org.springframework.context.support.ClassPathXmlApplicationContext; import com.baeldung.di.constructor.model.xml.Computer; - - public class ComputerXmlFactory { - public static void main(String[] args) { - // Create Spring Application Context - ApplicationContext context = new ClassPathXmlApplicationContext("application-di-constructor-context.xml"); + public static void main(String[] args) { + // Create Spring Application Context + ApplicationContext context = new ClassPathXmlApplicationContext("application-di-constructor-context.xml"); - // Get Spring bean "computer" created - Computer computer = (Computer) context.getBean("computer"); - System.out.println("---------------- Constructor Injection via XML ------------------"); - System.out.println("Processor cores : " + computer.getProcessor().getCores()); - System.out.println("Processor frequency : " + computer.getProcessor().getFrequency()); - System.out.println("Memory Size in GB : " + computer.getMemory().getSizeInGb()); - System.out.println("Memory format : " + computer.getMemory().getFormat()); - System.out.println("---------------- Constructor Injection via XML ------------------"); + // Get Spring bean "computer" created + Computer computer = (Computer) context.getBean("computer"); + System.out.println("---------------- Constructor Injection via XML ------------------"); + System.out.println("Processor cores : " + computer.getProcessor() + .getCores()); + System.out.println("Processor frequency : " + computer.getProcessor() + .getFrequency()); + System.out.println("Memory Size in GB : " + computer.getMemory() + .getSizeInGb()); + System.out.println("Memory format : " + computer.getMemory() + .getFormat()); + System.out.println("---------------- Constructor Injection via XML ------------------"); - // Close Spring Application Context - ((ConfigurableApplicationContext) context).close(); - - } + // Close Spring Application Context + ((ConfigurableApplicationContext) context).close(); + + } } diff --git a/spring-core/src/main/java/com/baeldung/di/constructor/model/autowire/Computer.java b/spring-core/src/main/java/com/baeldung/di/constructor/model/autowire/Computer.java index b470aedeed..1dabfd287e 100644 --- a/spring-core/src/main/java/com/baeldung/di/constructor/model/autowire/Computer.java +++ b/spring-core/src/main/java/com/baeldung/di/constructor/model/autowire/Computer.java @@ -6,29 +6,29 @@ import org.springframework.stereotype.Component; @Component public class Computer { - private Processor processor; - private Memory memory; + private Processor processor; + private Memory memory; - @Autowired - public Computer(Processor processor, Memory memory) { - this.processor = processor; - this.memory = memory; - } + @Autowired + public Computer(Processor processor, Memory memory) { + this.processor = processor; + this.memory = memory; + } - public Processor getProcessor() { - return processor; - } - - public void setProcessor(Processor processor) { - this.processor = processor; - } + public Processor getProcessor() { + return processor; + } - public Memory getMemory() { - return memory; - } + public void setProcessor(Processor processor) { + this.processor = processor; + } - public void setMemory(Memory memory) { - this.memory = memory; - } + public Memory getMemory() { + return memory; + } + + public void setMemory(Memory memory) { + this.memory = memory; + } } diff --git a/spring-core/src/main/java/com/baeldung/di/constructor/model/autowire/Memory.java b/spring-core/src/main/java/com/baeldung/di/constructor/model/autowire/Memory.java index 29633dd5ca..d1faecc569 100644 --- a/spring-core/src/main/java/com/baeldung/di/constructor/model/autowire/Memory.java +++ b/spring-core/src/main/java/com/baeldung/di/constructor/model/autowire/Memory.java @@ -5,15 +5,15 @@ import org.springframework.stereotype.Component; @Component public class Memory { - private Integer sizeInGb = 16; - private String format = "DDR3"; - - public Integer getSizeInGb() { - return sizeInGb; - } + private Integer sizeInGb = 16; + private String format = "DDR3"; - public String getFormat() { - return format; - } + public Integer getSizeInGb() { + return sizeInGb; + } + + public String getFormat() { + return format; + } } diff --git a/spring-core/src/main/java/com/baeldung/di/constructor/model/autowire/Processor.java b/spring-core/src/main/java/com/baeldung/di/constructor/model/autowire/Processor.java index 0d81e2a005..7de9d2bf6a 100644 --- a/spring-core/src/main/java/com/baeldung/di/constructor/model/autowire/Processor.java +++ b/spring-core/src/main/java/com/baeldung/di/constructor/model/autowire/Processor.java @@ -5,15 +5,15 @@ import org.springframework.stereotype.Component; @Component public class Processor { - private Integer cores = 2; - private Double frequency = 1.4; + private Integer cores = 2; + private Double frequency = 1.4; - public Integer getCores() { - return cores; - } + public Integer getCores() { + return cores; + } - public Double getFrequency() { - return frequency; - } + public Double getFrequency() { + return frequency; + } } diff --git a/spring-core/src/main/java/com/baeldung/di/constructor/model/bean/Computer.java b/spring-core/src/main/java/com/baeldung/di/constructor/model/bean/Computer.java index d8856420d0..74ec89ce0c 100644 --- a/spring-core/src/main/java/com/baeldung/di/constructor/model/bean/Computer.java +++ b/spring-core/src/main/java/com/baeldung/di/constructor/model/bean/Computer.java @@ -2,28 +2,28 @@ package com.baeldung.di.constructor.model.bean; public class Computer { - private Processor processor; - private Memory memory; + private Processor processor; + private Memory memory; - public Computer(Processor processor, Memory memory) { - this.processor = processor; - this.memory = memory; - } + public Computer(Processor processor, Memory memory) { + this.processor = processor; + this.memory = memory; + } - public Processor getProcessor() { - return processor; - } - - public void setProcessor(Processor processor) { - this.processor = processor; - } + public Processor getProcessor() { + return processor; + } - public Memory getMemory() { - return memory; - } + public void setProcessor(Processor processor) { + this.processor = processor; + } - public void setMemory(Memory memory) { - this.memory = memory; - } + public Memory getMemory() { + return memory; + } + + public void setMemory(Memory memory) { + this.memory = memory; + } } diff --git a/spring-core/src/main/java/com/baeldung/di/constructor/model/bean/Memory.java b/spring-core/src/main/java/com/baeldung/di/constructor/model/bean/Memory.java index 8e748bb1a0..18d4eaf948 100644 --- a/spring-core/src/main/java/com/baeldung/di/constructor/model/bean/Memory.java +++ b/spring-core/src/main/java/com/baeldung/di/constructor/model/bean/Memory.java @@ -2,15 +2,15 @@ package com.baeldung.di.constructor.model.bean; public class Memory { - private Integer sizeInGb = 16; - private String format = "DDR3"; - - public Integer getSizeInGb() { - return sizeInGb; - } + private Integer sizeInGb = 16; + private String format = "DDR3"; - public String getFormat() { - return format; - } + public Integer getSizeInGb() { + return sizeInGb; + } + + public String getFormat() { + return format; + } } diff --git a/spring-core/src/main/java/com/baeldung/di/constructor/model/bean/Processor.java b/spring-core/src/main/java/com/baeldung/di/constructor/model/bean/Processor.java index 1566606148..9dd719c872 100644 --- a/spring-core/src/main/java/com/baeldung/di/constructor/model/bean/Processor.java +++ b/spring-core/src/main/java/com/baeldung/di/constructor/model/bean/Processor.java @@ -2,15 +2,15 @@ package com.baeldung.di.constructor.model.bean; public class Processor { - private Integer cores = 2; - private Double frequency = 1.4; + private Integer cores = 2; + private Double frequency = 1.4; - public Integer getCores() { - return cores; - } + public Integer getCores() { + return cores; + } - public Double getFrequency() { - return frequency; - } + public Double getFrequency() { + return frequency; + } } diff --git a/spring-core/src/main/java/com/baeldung/di/constructor/model/xml/Computer.java b/spring-core/src/main/java/com/baeldung/di/constructor/model/xml/Computer.java index dbd70cf6a4..66c39c66b7 100644 --- a/spring-core/src/main/java/com/baeldung/di/constructor/model/xml/Computer.java +++ b/spring-core/src/main/java/com/baeldung/di/constructor/model/xml/Computer.java @@ -2,28 +2,28 @@ package com.baeldung.di.constructor.model.xml; public class Computer { - private Processor processor; - private Memory memory; + private Processor processor; + private Memory memory; - public Computer(Processor processor, Memory memory) { - this.processor = processor; - this.memory = memory; - } - - public Processor getProcessor() { - return processor; - } - - public void setProcessor(Processor processor) { - this.processor = processor; - } + public Computer(Processor processor, Memory memory) { + this.processor = processor; + this.memory = memory; + } - public Memory getMemory() { - return memory; - } + public Processor getProcessor() { + return processor; + } - public void setMemory(Memory memory) { - this.memory = memory; - } + public void setProcessor(Processor processor) { + this.processor = processor; + } + + public Memory getMemory() { + return memory; + } + + public void setMemory(Memory memory) { + this.memory = memory; + } } diff --git a/spring-core/src/main/java/com/baeldung/di/constructor/model/xml/Memory.java b/spring-core/src/main/java/com/baeldung/di/constructor/model/xml/Memory.java index f5ea3b4ec9..7b4f4fb697 100644 --- a/spring-core/src/main/java/com/baeldung/di/constructor/model/xml/Memory.java +++ b/spring-core/src/main/java/com/baeldung/di/constructor/model/xml/Memory.java @@ -2,19 +2,19 @@ package com.baeldung.di.constructor.model.xml; public class Memory { - private Integer sizeInGb = 16; - private String format = "DDR3"; + private Integer sizeInGb = 16; + private String format = "DDR3"; - public Memory() { + public Memory() { - } + } - public Integer getSizeInGb() { - return sizeInGb; - } + public Integer getSizeInGb() { + return sizeInGb; + } - public String getFormat() { - return format; - } + public String getFormat() { + return format; + } } diff --git a/spring-core/src/main/java/com/baeldung/di/constructor/model/xml/Processor.java b/spring-core/src/main/java/com/baeldung/di/constructor/model/xml/Processor.java index 72733829d8..4e783042fe 100644 --- a/spring-core/src/main/java/com/baeldung/di/constructor/model/xml/Processor.java +++ b/spring-core/src/main/java/com/baeldung/di/constructor/model/xml/Processor.java @@ -2,15 +2,15 @@ package com.baeldung.di.constructor.model.xml; public class Processor { - private Integer cores = 2; - private Double frequency = 1.4; + private Integer cores = 2; + private Double frequency = 1.4; - public Integer getCores() { - return cores; - } + public Integer getCores() { + return cores; + } - public Double getFrequency() { - return frequency; - } + public Double getFrequency() { + return frequency; + } } diff --git a/spring-core/src/main/java/com/baeldung/di/setter/config/SpringAutowireSetterContext.java b/spring-core/src/main/java/com/baeldung/di/setter/config/SpringAutowireSetterContext.java index 4e0ac0df28..9e1d24fc61 100644 --- a/spring-core/src/main/java/com/baeldung/di/setter/config/SpringAutowireSetterContext.java +++ b/spring-core/src/main/java/com/baeldung/di/setter/config/SpringAutowireSetterContext.java @@ -4,7 +4,7 @@ import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; @Configuration -@ComponentScan(basePackages= { "com.baeldung.di.setter.model.autowire" }) +@ComponentScan(basePackages = { "com.baeldung.di.setter.model.autowire" }) public class SpringAutowireSetterContext { - + } diff --git a/spring-core/src/main/java/com/baeldung/di/setter/config/SpringBeanSetterContext.java b/spring-core/src/main/java/com/baeldung/di/setter/config/SpringBeanSetterContext.java index 885173f5b4..14767fb0ca 100644 --- a/spring-core/src/main/java/com/baeldung/di/setter/config/SpringBeanSetterContext.java +++ b/spring-core/src/main/java/com/baeldung/di/setter/config/SpringBeanSetterContext.java @@ -9,23 +9,23 @@ import com.baeldung.di.setter.model.bean.Processor; @Configuration public class SpringBeanSetterContext { - - @Bean - public Computer computer(){ - Computer computer = new Computer(); - computer.setProcessor(processor()); - computer.setMemory(memory()); - return computer; - } - - @Bean - public Processor processor(){ - return new Processor(); - } - - @Bean - public Memory memory(){ - return new Memory(); - } - + + @Bean + public Computer computer() { + Computer computer = new Computer(); + computer.setProcessor(processor()); + computer.setMemory(memory()); + return computer; + } + + @Bean + public Processor processor() { + return new Processor(); + } + + @Bean + public Memory memory() { + return new Memory(); + } + } diff --git a/spring-core/src/main/java/com/baeldung/di/setter/factory/ComputerAutowireFactory.java b/spring-core/src/main/java/com/baeldung/di/setter/factory/ComputerAutowireFactory.java index 98c3fccb27..ef1ac83530 100644 --- a/spring-core/src/main/java/com/baeldung/di/setter/factory/ComputerAutowireFactory.java +++ b/spring-core/src/main/java/com/baeldung/di/setter/factory/ComputerAutowireFactory.java @@ -7,23 +7,26 @@ import org.springframework.context.annotation.AnnotationConfigApplicationContext import com.baeldung.di.setter.config.SpringAutowireSetterContext; import com.baeldung.di.setter.model.autowire.Computer; - public class ComputerAutowireFactory { - public static void main(String[] args) { - // Create Spring Application Context - ApplicationContext context = new AnnotationConfigApplicationContext(SpringAutowireSetterContext.class); + public static void main(String[] args) { + // Create Spring Application Context + ApplicationContext context = new AnnotationConfigApplicationContext(SpringAutowireSetterContext.class); - // Get Spring bean "computer" created - Computer computer = (Computer) context.getBean("computer"); - System.out.println("---------------- Setter Injection via @Autowire ------------------"); - System.out.println("Processor cores : " + computer.getProcessor().getCores()); - System.out.println("Processor frequency : " + computer.getProcessor().getFrequency()); - System.out.println("Memory Size in GB : " + computer.getMemory().getSizeInGb()); - System.out.println("Memory format : " + computer.getMemory().getFormat()); - System.out.println("---------------- Setter Injection via @Autowire ------------------"); - // Close Spring Application Context - ((ConfigurableApplicationContext) context).close(); - } + // Get Spring bean "computer" created + Computer computer = (Computer) context.getBean("computer"); + System.out.println("---------------- Setter Injection via @Autowire ------------------"); + System.out.println("Processor cores : " + computer.getProcessor() + .getCores()); + System.out.println("Processor frequency : " + computer.getProcessor() + .getFrequency()); + System.out.println("Memory Size in GB : " + computer.getMemory() + .getSizeInGb()); + System.out.println("Memory format : " + computer.getMemory() + .getFormat()); + System.out.println("---------------- Setter Injection via @Autowire ------------------"); + // Close Spring Application Context + ((ConfigurableApplicationContext) context).close(); + } } diff --git a/spring-core/src/main/java/com/baeldung/di/setter/factory/ComputerBeanFactory.java b/spring-core/src/main/java/com/baeldung/di/setter/factory/ComputerBeanFactory.java index 6341029e95..87f49bf4cf 100644 --- a/spring-core/src/main/java/com/baeldung/di/setter/factory/ComputerBeanFactory.java +++ b/spring-core/src/main/java/com/baeldung/di/setter/factory/ComputerBeanFactory.java @@ -7,24 +7,27 @@ import org.springframework.context.annotation.AnnotationConfigApplicationContext import com.baeldung.di.setter.config.SpringBeanSetterContext; import com.baeldung.di.setter.model.bean.Computer; - public class ComputerBeanFactory { - public static void main(String[] args) { - // Create Spring Application Context - ApplicationContext context = new AnnotationConfigApplicationContext(SpringBeanSetterContext.class); + public static void main(String[] args) { + // Create Spring Application Context + ApplicationContext context = new AnnotationConfigApplicationContext(SpringBeanSetterContext.class); - // Get Spring bean "computer" created - Computer computer = (Computer) context.getBean("computer"); - System.out.println("---------------- Setter Injection via @Bean ------------------"); - System.out.println("Processor cores : " + computer.getProcessor().getCores()); - System.out.println("Processor frequency : " + computer.getProcessor().getFrequency()); - System.out.println("Memory Size in GB : " + computer.getMemory().getSizeInGb()); - System.out.println("Memory format : " + computer.getMemory().getFormat()); - System.out.println("---------------- Setter Injection via @Bean ------------------"); + // Get Spring bean "computer" created + Computer computer = (Computer) context.getBean("computer"); + System.out.println("---------------- Setter Injection via @Bean ------------------"); + System.out.println("Processor cores : " + computer.getProcessor() + .getCores()); + System.out.println("Processor frequency : " + computer.getProcessor() + .getFrequency()); + System.out.println("Memory Size in GB : " + computer.getMemory() + .getSizeInGb()); + System.out.println("Memory format : " + computer.getMemory() + .getFormat()); + System.out.println("---------------- Setter Injection via @Bean ------------------"); - // Close Spring Application Context - ((ConfigurableApplicationContext) context).close(); - } + // Close Spring Application Context + ((ConfigurableApplicationContext) context).close(); + } } diff --git a/spring-core/src/main/java/com/baeldung/di/setter/factory/ComputerXmlFactory.java b/spring-core/src/main/java/com/baeldung/di/setter/factory/ComputerXmlFactory.java index 707725d340..01af29905c 100644 --- a/spring-core/src/main/java/com/baeldung/di/setter/factory/ComputerXmlFactory.java +++ b/spring-core/src/main/java/com/baeldung/di/setter/factory/ComputerXmlFactory.java @@ -6,25 +6,27 @@ import org.springframework.context.support.ClassPathXmlApplicationContext; import com.baeldung.di.setter.model.xml.Computer; - - public class ComputerXmlFactory { - public static void main(String[] args) { - // Create Spring Application Context - ApplicationContext context = new ClassPathXmlApplicationContext("application-setter-context.xml"); + public static void main(String[] args) { + // Create Spring Application Context + ApplicationContext context = new ClassPathXmlApplicationContext("application-setter-context.xml"); - // Get Spring bean "computer" created - Computer computer = (Computer) context.getBean("computer"); - System.out.println("---------------- Setter Injection via XML ------------------"); - System.out.println("Processor cores : " + computer.getProcessor().getCores()); - System.out.println("Processor frequency : " + computer.getProcessor().getFrequency()); - System.out.println("Memory Size in GB : " + computer.getMemory().getSizeInGb()); - System.out.println("Memory format : " + computer.getMemory().getFormat()); - System.out.println("---------------- Setter Injection via XML ------------------"); - // Close Spring Application Context - ((ConfigurableApplicationContext) context).close(); - - } + // Get Spring bean "computer" created + Computer computer = (Computer) context.getBean("computer"); + System.out.println("---------------- Setter Injection via XML ------------------"); + System.out.println("Processor cores : " + computer.getProcessor() + .getCores()); + System.out.println("Processor frequency : " + computer.getProcessor() + .getFrequency()); + System.out.println("Memory Size in GB : " + computer.getMemory() + .getSizeInGb()); + System.out.println("Memory format : " + computer.getMemory() + .getFormat()); + System.out.println("---------------- Setter Injection via XML ------------------"); + // Close Spring Application Context + ((ConfigurableApplicationContext) context).close(); + + } } diff --git a/spring-core/src/main/java/com/baeldung/di/setter/model/autowire/Computer.java b/spring-core/src/main/java/com/baeldung/di/setter/model/autowire/Computer.java index 4d8a3b1dd3..ed32b34729 100644 --- a/spring-core/src/main/java/com/baeldung/di/setter/model/autowire/Computer.java +++ b/spring-core/src/main/java/com/baeldung/di/setter/model/autowire/Computer.java @@ -6,25 +6,25 @@ import org.springframework.stereotype.Component; @Component public class Computer { - private Processor processor; - private Memory memory; + private Processor processor; + private Memory memory; - public Processor getProcessor() { - return processor; - } - - @Autowired - public void setProcessor(Processor processor) { - this.processor = processor; - } + public Processor getProcessor() { + return processor; + } - public Memory getMemory() { - return memory; - } - - @Autowired - public void setMemory(Memory memory) { - this.memory = memory; - } + @Autowired + public void setProcessor(Processor processor) { + this.processor = processor; + } + + public Memory getMemory() { + return memory; + } + + @Autowired + public void setMemory(Memory memory) { + this.memory = memory; + } } diff --git a/spring-core/src/main/java/com/baeldung/di/setter/model/autowire/Memory.java b/spring-core/src/main/java/com/baeldung/di/setter/model/autowire/Memory.java index 0953a42b35..ffdd63d389 100644 --- a/spring-core/src/main/java/com/baeldung/di/setter/model/autowire/Memory.java +++ b/spring-core/src/main/java/com/baeldung/di/setter/model/autowire/Memory.java @@ -5,15 +5,15 @@ import org.springframework.stereotype.Component; @Component public class Memory { - private Integer sizeInGb = 16; - private String format = "DDR3"; - - public Integer getSizeInGb() { - return sizeInGb; - } + private Integer sizeInGb = 16; + private String format = "DDR3"; - public String getFormat() { - return format; - } + public Integer getSizeInGb() { + return sizeInGb; + } + + public String getFormat() { + return format; + } } diff --git a/spring-core/src/main/java/com/baeldung/di/setter/model/autowire/Processor.java b/spring-core/src/main/java/com/baeldung/di/setter/model/autowire/Processor.java index ee5ef3b6cd..9b01143bd2 100644 --- a/spring-core/src/main/java/com/baeldung/di/setter/model/autowire/Processor.java +++ b/spring-core/src/main/java/com/baeldung/di/setter/model/autowire/Processor.java @@ -5,15 +5,15 @@ import org.springframework.stereotype.Component; @Component public class Processor { - private Integer cores = 2; - private Double frequency = 1.4; + private Integer cores = 2; + private Double frequency = 1.4; - public Integer getCores() { - return cores; - } + public Integer getCores() { + return cores; + } - public Double getFrequency() { - return frequency; - } + public Double getFrequency() { + return frequency; + } } diff --git a/spring-core/src/main/java/com/baeldung/di/setter/model/bean/Computer.java b/spring-core/src/main/java/com/baeldung/di/setter/model/bean/Computer.java index 01c87a6c73..e350cddee8 100644 --- a/spring-core/src/main/java/com/baeldung/di/setter/model/bean/Computer.java +++ b/spring-core/src/main/java/com/baeldung/di/setter/model/bean/Computer.java @@ -2,23 +2,23 @@ package com.baeldung.di.setter.model.bean; public class Computer { - private Processor processor; - private Memory memory; + private Processor processor; + private Memory memory; - public Processor getProcessor() { - return processor; - } - - public void setProcessor(Processor processor) { - this.processor = processor; - } + public Processor getProcessor() { + return processor; + } - public Memory getMemory() { - return memory; - } - - public void setMemory(Memory memory) { - this.memory = memory; - } + public void setProcessor(Processor processor) { + this.processor = processor; + } + + public Memory getMemory() { + return memory; + } + + public void setMemory(Memory memory) { + this.memory = memory; + } } diff --git a/spring-core/src/main/java/com/baeldung/di/setter/model/bean/Memory.java b/spring-core/src/main/java/com/baeldung/di/setter/model/bean/Memory.java index bfe94aa8e7..b34052e066 100644 --- a/spring-core/src/main/java/com/baeldung/di/setter/model/bean/Memory.java +++ b/spring-core/src/main/java/com/baeldung/di/setter/model/bean/Memory.java @@ -2,15 +2,16 @@ package com.baeldung.di.setter.model.bean; public class Memory { - private Integer sizeInGb = 16; - private String format = "DDR3"; - - public Integer getSizeInGb() { - return sizeInGb; - } + private Integer sizeInGb = 16; + private String format = "DDR3"; - public String getFormat() { - return format; - } + // + public Integer getSizeInGb() { + return sizeInGb; + } + + public String getFormat() { + return format; + } } diff --git a/spring-core/src/main/java/com/baeldung/di/setter/model/bean/Processor.java b/spring-core/src/main/java/com/baeldung/di/setter/model/bean/Processor.java index 982b56f3d0..2c9f050232 100644 --- a/spring-core/src/main/java/com/baeldung/di/setter/model/bean/Processor.java +++ b/spring-core/src/main/java/com/baeldung/di/setter/model/bean/Processor.java @@ -2,15 +2,15 @@ package com.baeldung.di.setter.model.bean; public class Processor { - private Integer cores = 2; - private Double frequency = 1.4; + private Integer cores = 2; + private Double frequency = 1.4; - public Integer getCores() { - return cores; - } + public Integer getCores() { + return cores; + } - public Double getFrequency() { - return frequency; - } + public Double getFrequency() { + return frequency; + } } diff --git a/spring-core/src/main/java/com/baeldung/di/setter/model/xml/Computer.java b/spring-core/src/main/java/com/baeldung/di/setter/model/xml/Computer.java index 3e7fa58ea8..a88dc34d07 100644 --- a/spring-core/src/main/java/com/baeldung/di/setter/model/xml/Computer.java +++ b/spring-core/src/main/java/com/baeldung/di/setter/model/xml/Computer.java @@ -2,23 +2,23 @@ package com.baeldung.di.setter.model.xml; public class Computer { - private Processor processor; - private Memory memory; + private Processor processor; + private Memory memory; - public Processor getProcessor() { - return processor; - } - - public void setProcessor(Processor processor) { - this.processor = processor; - } + public Processor getProcessor() { + return processor; + } - public Memory getMemory() { - return memory; - } + public void setProcessor(Processor processor) { + this.processor = processor; + } - public void setMemory(Memory memory) { - this.memory = memory; - } + public Memory getMemory() { + return memory; + } + + public void setMemory(Memory memory) { + this.memory = memory; + } } diff --git a/spring-core/src/main/java/com/baeldung/di/setter/model/xml/Memory.java b/spring-core/src/main/java/com/baeldung/di/setter/model/xml/Memory.java index cb11c59583..93667ff225 100644 --- a/spring-core/src/main/java/com/baeldung/di/setter/model/xml/Memory.java +++ b/spring-core/src/main/java/com/baeldung/di/setter/model/xml/Memory.java @@ -2,19 +2,19 @@ package com.baeldung.di.setter.model.xml; public class Memory { - private Integer sizeInGb = 16; - private String format = "DDR3"; + private Integer sizeInGb = 16; + private String format = "DDR3"; - public Memory() { + public Memory() { - } + } - public Integer getSizeInGb() { - return sizeInGb; - } + public Integer getSizeInGb() { + return sizeInGb; + } - public String getFormat() { - return format; - } + public String getFormat() { + return format; + } } diff --git a/spring-core/src/main/java/com/baeldung/di/setter/model/xml/Processor.java b/spring-core/src/main/java/com/baeldung/di/setter/model/xml/Processor.java index 07405e13ea..1845983338 100644 --- a/spring-core/src/main/java/com/baeldung/di/setter/model/xml/Processor.java +++ b/spring-core/src/main/java/com/baeldung/di/setter/model/xml/Processor.java @@ -2,15 +2,15 @@ package com.baeldung.di.setter.model.xml; public class Processor { - private Integer cores = 2; - private Double frequency = 1.4; + private Integer cores = 2; + private Double frequency = 1.4; - public Integer getCores() { - return cores; - } + public Integer getCores() { + return cores; + } - public Double getFrequency() { - return frequency; - } + public Double getFrequency() { + return frequency; + } } diff --git a/spring-core/src/main/resources/application-di-constructor-context.xml b/spring-core/src/main/resources/application-di-constructor-context.xml index 46d1256988..c1f2d4cdfd 100644 --- a/spring-core/src/main/resources/application-di-constructor-context.xml +++ b/spring-core/src/main/resources/application-di-constructor-context.xml @@ -6,18 +6,18 @@ http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd "> - + - + - + \ No newline at end of file diff --git a/spring-core/src/main/resources/application-di-setter-context.xml b/spring-core/src/main/resources/application-di-setter-context.xml index 3831041e01..658f28b3f3 100644 --- a/spring-core/src/main/resources/application-di-setter-context.xml +++ b/spring-core/src/main/resources/application-di-setter-context.xml @@ -6,7 +6,7 @@ http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd "> - + From 4678e01680ac858d7d5bc17e2eab9e6f17fa2fe2 Mon Sep 17 00:00:00 2001 From: gschambial Date: Fri, 10 Nov 2017 19:44:34 +0530 Subject: [PATCH 03/28] Unit Tests renamed into correct format --- ...ectionTest.java => AutowireAnnotationInjectionUnitTest.java} | 2 +- ...nInjectionTest.java => BeanAnnotationInjectionUnitTest.java} | 2 +- .../{XmlInjectionTest.java => XmlInjectionUnitTest.java} | 2 +- ...ectionTest.java => AutowireAnnotationInjectionUnitTest.java} | 2 +- ...nInjectionTest.java => BeanAnnotationInjectionUnitTest.java} | 2 +- .../setter/{XmlInjectionTest.java => XmlInjectionUnitTest.java} | 2 +- 6 files changed, 6 insertions(+), 6 deletions(-) rename spring-core/src/test/java/com/baeldung/di/constructor/{AutowireAnnotationInjectionTest.java => AutowireAnnotationInjectionUnitTest.java} (93%) rename spring-core/src/test/java/com/baeldung/di/constructor/{BeanAnnotationInjectionTest.java => BeanAnnotationInjectionUnitTest.java} (94%) rename spring-core/src/test/java/com/baeldung/di/constructor/{XmlInjectionTest.java => XmlInjectionUnitTest.java} (94%) rename spring-core/src/test/java/com/baeldung/di/setter/{AutowireAnnotationInjectionTest.java => AutowireAnnotationInjectionUnitTest.java} (93%) rename spring-core/src/test/java/com/baeldung/di/setter/{BeanAnnotationInjectionTest.java => BeanAnnotationInjectionUnitTest.java} (93%) rename spring-core/src/test/java/com/baeldung/di/setter/{XmlInjectionTest.java => XmlInjectionUnitTest.java} (95%) diff --git a/spring-core/src/test/java/com/baeldung/di/constructor/AutowireAnnotationInjectionTest.java b/spring-core/src/test/java/com/baeldung/di/constructor/AutowireAnnotationInjectionUnitTest.java similarity index 93% rename from spring-core/src/test/java/com/baeldung/di/constructor/AutowireAnnotationInjectionTest.java rename to spring-core/src/test/java/com/baeldung/di/constructor/AutowireAnnotationInjectionUnitTest.java index 87cb549ace..348ac1428b 100644 --- a/spring-core/src/test/java/com/baeldung/di/constructor/AutowireAnnotationInjectionTest.java +++ b/spring-core/src/test/java/com/baeldung/di/constructor/AutowireAnnotationInjectionUnitTest.java @@ -10,7 +10,7 @@ import org.springframework.context.annotation.AnnotationConfigApplicationContext import com.baeldung.di.constructor.config.SpringAutowireConstructorContext; import com.baeldung.di.constructor.model.autowire.Computer; -public class AutowireAnnotationInjectionTest { +public class AutowireAnnotationInjectionUnitTest { private ApplicationContext applicationContext; diff --git a/spring-core/src/test/java/com/baeldung/di/constructor/BeanAnnotationInjectionTest.java b/spring-core/src/test/java/com/baeldung/di/constructor/BeanAnnotationInjectionUnitTest.java similarity index 94% rename from spring-core/src/test/java/com/baeldung/di/constructor/BeanAnnotationInjectionTest.java rename to spring-core/src/test/java/com/baeldung/di/constructor/BeanAnnotationInjectionUnitTest.java index 61f718d4ba..3eb276428c 100644 --- a/spring-core/src/test/java/com/baeldung/di/constructor/BeanAnnotationInjectionTest.java +++ b/spring-core/src/test/java/com/baeldung/di/constructor/BeanAnnotationInjectionUnitTest.java @@ -10,7 +10,7 @@ import org.springframework.context.annotation.AnnotationConfigApplicationContext import com.baeldung.di.constructor.config.SpringBeanConstructorContext; import com.baeldung.di.constructor.model.bean.Computer; -public class BeanAnnotationInjectionTest { +public class BeanAnnotationInjectionUnitTest { private ApplicationContext applicationContext; diff --git a/spring-core/src/test/java/com/baeldung/di/constructor/XmlInjectionTest.java b/spring-core/src/test/java/com/baeldung/di/constructor/XmlInjectionUnitTest.java similarity index 94% rename from spring-core/src/test/java/com/baeldung/di/constructor/XmlInjectionTest.java rename to spring-core/src/test/java/com/baeldung/di/constructor/XmlInjectionUnitTest.java index 4787c19cbf..cfcc7ed56e 100644 --- a/spring-core/src/test/java/com/baeldung/di/constructor/XmlInjectionTest.java +++ b/spring-core/src/test/java/com/baeldung/di/constructor/XmlInjectionUnitTest.java @@ -9,7 +9,7 @@ import org.springframework.context.support.ClassPathXmlApplicationContext; import com.baeldung.di.constructor.model.xml.Computer; -public class XmlInjectionTest { +public class XmlInjectionUnitTest { private ApplicationContext applicationContext; diff --git a/spring-core/src/test/java/com/baeldung/di/setter/AutowireAnnotationInjectionTest.java b/spring-core/src/test/java/com/baeldung/di/setter/AutowireAnnotationInjectionUnitTest.java similarity index 93% rename from spring-core/src/test/java/com/baeldung/di/setter/AutowireAnnotationInjectionTest.java rename to spring-core/src/test/java/com/baeldung/di/setter/AutowireAnnotationInjectionUnitTest.java index 036c6826e3..fc8e58f08b 100644 --- a/spring-core/src/test/java/com/baeldung/di/setter/AutowireAnnotationInjectionTest.java +++ b/spring-core/src/test/java/com/baeldung/di/setter/AutowireAnnotationInjectionUnitTest.java @@ -10,7 +10,7 @@ import org.springframework.context.annotation.AnnotationConfigApplicationContext import com.baeldung.di.setter.config.SpringAutowireSetterContext; import com.baeldung.di.setter.model.autowire.Computer; -public class AutowireAnnotationInjectionTest { +public class AutowireAnnotationInjectionUnitTest { private ApplicationContext applicationContext; diff --git a/spring-core/src/test/java/com/baeldung/di/setter/BeanAnnotationInjectionTest.java b/spring-core/src/test/java/com/baeldung/di/setter/BeanAnnotationInjectionUnitTest.java similarity index 93% rename from spring-core/src/test/java/com/baeldung/di/setter/BeanAnnotationInjectionTest.java rename to spring-core/src/test/java/com/baeldung/di/setter/BeanAnnotationInjectionUnitTest.java index 53f1d4e10e..c89c1239ba 100644 --- a/spring-core/src/test/java/com/baeldung/di/setter/BeanAnnotationInjectionTest.java +++ b/spring-core/src/test/java/com/baeldung/di/setter/BeanAnnotationInjectionUnitTest.java @@ -10,7 +10,7 @@ import org.springframework.context.annotation.AnnotationConfigApplicationContext import com.baeldung.di.setter.config.SpringBeanSetterContext; import com.baeldung.di.setter.model.bean.Computer; -public class BeanAnnotationInjectionTest { +public class BeanAnnotationInjectionUnitTest { private ApplicationContext applicationContext; diff --git a/spring-core/src/test/java/com/baeldung/di/setter/XmlInjectionTest.java b/spring-core/src/test/java/com/baeldung/di/setter/XmlInjectionUnitTest.java similarity index 95% rename from spring-core/src/test/java/com/baeldung/di/setter/XmlInjectionTest.java rename to spring-core/src/test/java/com/baeldung/di/setter/XmlInjectionUnitTest.java index 52452be119..0f38b46194 100644 --- a/spring-core/src/test/java/com/baeldung/di/setter/XmlInjectionTest.java +++ b/spring-core/src/test/java/com/baeldung/di/setter/XmlInjectionUnitTest.java @@ -9,7 +9,7 @@ import org.springframework.context.support.ClassPathXmlApplicationContext; import com.baeldung.di.setter.model.xml.Computer; -public class XmlInjectionTest { +public class XmlInjectionUnitTest { private ApplicationContext applicationContext; From 40ceddea2bdc3cf46caa622b19ba1350bd52f055 Mon Sep 17 00:00:00 2001 From: gschambial Date: Mon, 13 Nov 2017 21:01:06 +0530 Subject: [PATCH 04/28] BAEL-1331: Quick Example: Comparator vs Comparable in Java Source code and Unit Tests added. --- .../java/com/baeldung/comparable/Player.java | 51 +++++++++++++++++++ .../com/baeldung/comparable/PlayerSorter.java | 24 +++++++++ .../java/com/baeldung/comparator/Player.java | 46 +++++++++++++++++ .../comparator/PlayerAgeComparator.java | 12 +++++ .../baeldung/comparator/PlayerAgeSorter.java | 26 ++++++++++ .../comparator/PlayerRankingComparator.java | 12 +++++ .../comparator/PlayerRankingSorter.java | 26 ++++++++++ .../comparable/ComparableUnitTest.java | 26 ++++++++++ .../comparator/ComparatorUnitTest.java | 42 +++++++++++++++ 9 files changed, 265 insertions(+) create mode 100644 core-java/src/main/java/com/baeldung/comparable/Player.java create mode 100644 core-java/src/main/java/com/baeldung/comparable/PlayerSorter.java create mode 100644 core-java/src/main/java/com/baeldung/comparator/Player.java create mode 100644 core-java/src/main/java/com/baeldung/comparator/PlayerAgeComparator.java create mode 100644 core-java/src/main/java/com/baeldung/comparator/PlayerAgeSorter.java create mode 100644 core-java/src/main/java/com/baeldung/comparator/PlayerRankingComparator.java create mode 100644 core-java/src/main/java/com/baeldung/comparator/PlayerRankingSorter.java create mode 100644 core-java/src/test/java/com/baeldung/comparable/ComparableUnitTest.java create mode 100644 core-java/src/test/java/com/baeldung/comparator/ComparatorUnitTest.java diff --git a/core-java/src/main/java/com/baeldung/comparable/Player.java b/core-java/src/main/java/com/baeldung/comparable/Player.java new file mode 100644 index 0000000000..68a78980f3 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/comparable/Player.java @@ -0,0 +1,51 @@ +package com.baeldung.comparable; + +public class Player implements Comparable { + + private int ranking; + + private String name; + + private int age; + + public Player(int ranking, String name, int age) { + this.ranking = ranking; + this.name = name; + this.age = age; + } + + public int getRanking() { + return ranking; + } + + public void setRanking(int ranking) { + this.ranking = ranking; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public int getAge() { + return age; + } + + public void setAge(int age) { + this.age = age; + } + + @Override + public String toString() { + return this.name; + } + + @Override + public int compareTo(Player otherPlayer) { + return (this.getRanking() - otherPlayer.getRanking()); + } + +} diff --git a/core-java/src/main/java/com/baeldung/comparable/PlayerSorter.java b/core-java/src/main/java/com/baeldung/comparable/PlayerSorter.java new file mode 100644 index 0000000000..1f64a5eb43 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/comparable/PlayerSorter.java @@ -0,0 +1,24 @@ +package com.baeldung.comparable; + +import java.util.ArrayList; +import java.util.Collections; + +public class PlayerSorter { + + public static void main(String[] args) { + + ArrayList footballTeam = new ArrayList(); + Player player1 = new Player(59, "John", 20); + Player player2 = new Player(67, "Roger", 22); + Player player3 = new Player(45, "Steven", 24); + footballTeam.add(player1); + footballTeam.add(player2); + footballTeam.add(player3); + + System.out.println("Before Sorting : " + footballTeam); + Collections.sort(footballTeam); + System.out.println("After Sorting : " + footballTeam); + + } + +} diff --git a/core-java/src/main/java/com/baeldung/comparator/Player.java b/core-java/src/main/java/com/baeldung/comparator/Player.java new file mode 100644 index 0000000000..e6e9ee0db6 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/comparator/Player.java @@ -0,0 +1,46 @@ +package com.baeldung.comparator; + +public class Player { + + private int ranking; + + private String name; + + private int age; + + public Player(int ranking, String name, int age) { + this.ranking = ranking; + this.name = name; + this.age = age; + } + + public int getRanking() { + return ranking; + } + + public void setRanking(int ranking) { + this.ranking = ranking; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public int getAge() { + return age; + } + + public void setAge(int age) { + this.age = age; + } + + @Override + public String toString() { + return this.name; + } + +} diff --git a/core-java/src/main/java/com/baeldung/comparator/PlayerAgeComparator.java b/core-java/src/main/java/com/baeldung/comparator/PlayerAgeComparator.java new file mode 100644 index 0000000000..d2e7ca1f42 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/comparator/PlayerAgeComparator.java @@ -0,0 +1,12 @@ +package com.baeldung.comparator; + +import java.util.Comparator; + +public class PlayerAgeComparator implements Comparator { + + @Override + public int compare(Player firstPlayer, Player secondPlayer) { + return (firstPlayer.getAge() - secondPlayer.getAge()); + } + +} diff --git a/core-java/src/main/java/com/baeldung/comparator/PlayerAgeSorter.java b/core-java/src/main/java/com/baeldung/comparator/PlayerAgeSorter.java new file mode 100644 index 0000000000..e7d3fd1264 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/comparator/PlayerAgeSorter.java @@ -0,0 +1,26 @@ +package com.baeldung.comparator; + +import java.util.ArrayList; +import java.util.Collections; + +public class PlayerAgeSorter { + + public static void main(String[] args) { + + ArrayList footballTeam = new ArrayList(); + Player player1 = new Player(59, "John", 22); + Player player2 = new Player(67, "Roger", 20); + Player player3 = new Player(45, "Steven", 24); + footballTeam.add(player1); + footballTeam.add(player2); + footballTeam.add(player3); + + System.out.println("Before Sorting : " + footballTeam); + //Instance of PlayerAgeComparator + PlayerAgeComparator playerComparator = new PlayerAgeComparator(); + Collections.sort(footballTeam, playerComparator); + System.out.println("After Sorting by age : " + footballTeam); + + } + +} diff --git a/core-java/src/main/java/com/baeldung/comparator/PlayerRankingComparator.java b/core-java/src/main/java/com/baeldung/comparator/PlayerRankingComparator.java new file mode 100644 index 0000000000..2d42698843 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/comparator/PlayerRankingComparator.java @@ -0,0 +1,12 @@ +package com.baeldung.comparator; + +import java.util.Comparator; + +public class PlayerRankingComparator implements Comparator { + + @Override + public int compare(Player firstPlayer, Player secondPlayer) { + return (firstPlayer.getRanking() - secondPlayer.getRanking()); + } + +} diff --git a/core-java/src/main/java/com/baeldung/comparator/PlayerRankingSorter.java b/core-java/src/main/java/com/baeldung/comparator/PlayerRankingSorter.java new file mode 100644 index 0000000000..a6a971f99e --- /dev/null +++ b/core-java/src/main/java/com/baeldung/comparator/PlayerRankingSorter.java @@ -0,0 +1,26 @@ +package com.baeldung.comparator; + +import java.util.ArrayList; +import java.util.Collections; + +public class PlayerRankingSorter { + + public static void main(String[] args) { + + ArrayList footballTeam = new ArrayList(); + Player player1 = new Player(59, "John", 22); + Player player2 = new Player(67, "Roger", 20); + Player player3 = new Player(45, "Steven", 40); + footballTeam.add(player1); + footballTeam.add(player2); + footballTeam.add(player3); + + System.out.println("Before Sorting : " + footballTeam); + //Instance of PlayerRankingComparator + PlayerRankingComparator playerComparator = new PlayerRankingComparator(); + Collections.sort(footballTeam, playerComparator); + System.out.println("After Sorting by ranking : " + footballTeam); + + } + +} diff --git a/core-java/src/test/java/com/baeldung/comparable/ComparableUnitTest.java b/core-java/src/test/java/com/baeldung/comparable/ComparableUnitTest.java new file mode 100644 index 0000000000..c65c2c3740 --- /dev/null +++ b/core-java/src/test/java/com/baeldung/comparable/ComparableUnitTest.java @@ -0,0 +1,26 @@ +package com.baeldung.comparable; + +import static org.junit.Assert.assertEquals; + +import java.util.ArrayList; +import java.util.Collections; + +import org.junit.Test; + +public class ComparableUnitTest { + + @Test + public void whenUsingComparable_thenSortedList() { + ArrayList footballTeam = new ArrayList(); + Player player1 = new Player(59, "John", 20); + Player player2 = new Player(67, "Roger", 22); + Player player3 = new Player(45, "Steven", 24); + footballTeam.add(player1); + footballTeam.add(player2); + footballTeam.add(player3); + Collections.sort(footballTeam); + assertEquals(footballTeam.get(0).getName(), "Steven"); + assertEquals(footballTeam.get(2).getRanking(), 67); + } + +} diff --git a/core-java/src/test/java/com/baeldung/comparator/ComparatorUnitTest.java b/core-java/src/test/java/com/baeldung/comparator/ComparatorUnitTest.java new file mode 100644 index 0000000000..78f0e0bf33 --- /dev/null +++ b/core-java/src/test/java/com/baeldung/comparator/ComparatorUnitTest.java @@ -0,0 +1,42 @@ +package com.baeldung.comparator; + +import static org.junit.Assert.assertEquals; + +import java.util.ArrayList; +import java.util.Collections; + +import org.junit.Test; + +public class ComparatorUnitTest { + + @Test + public void whenUsingRankingComparator_thenSortedList() { + ArrayList footballTeam = new ArrayList(); + Player player1 = new Player(59, "John", 20); + Player player2 = new Player(67, "Roger", 22); + Player player3 = new Player(45, "Steven", 24); + footballTeam.add(player1); + footballTeam.add(player2); + footballTeam.add(player3); + PlayerRankingComparator playerComparator = new PlayerRankingComparator(); + Collections.sort(footballTeam, playerComparator); + assertEquals(footballTeam.get(0).getName(), "Steven"); + assertEquals(footballTeam.get(2).getRanking(), 67); + } + + @Test + public void whenUsingAgeComparator_thenSortedList() { + ArrayList footballTeam = new ArrayList(); + Player player1 = new Player(59, "John", 20); + Player player2 = new Player(67, "Roger", 22); + Player player3 = new Player(45, "Steven", 24); + footballTeam.add(player1); + footballTeam.add(player2); + footballTeam.add(player3); + PlayerAgeComparator playerComparator = new PlayerAgeComparator(); + Collections.sort(footballTeam, playerComparator); + assertEquals(footballTeam.get(0).getName(), "John"); + assertEquals(footballTeam.get(2).getRanking(), 45); + } + +} From 416bd4a9d5b32585bb9f968f481a266ff0c97df4 Mon Sep 17 00:00:00 2001 From: gschambial Date: Mon, 13 Nov 2017 22:48:09 +0530 Subject: [PATCH 05/28] Removed evaluation article (Dependency Injection) from master branch. --- .../SpringAutowireConstructorContext.java | 10 ------ .../config/SpringBeanConstructorContext.java | 28 --------------- .../factory/ComputerAutowireFactory.java | 32 ----------------- .../factory/ComputerBeanFactory.java | 33 ------------------ .../factory/ComputerXmlFactory.java | 33 ------------------ .../constructor/model/autowire/Computer.java | 34 ------------------- .../di/constructor/model/autowire/Memory.java | 19 ----------- .../constructor/model/autowire/Processor.java | 19 ----------- .../di/constructor/model/bean/Computer.java | 29 ---------------- .../di/constructor/model/bean/Memory.java | 16 --------- .../di/constructor/model/bean/Processor.java | 16 --------- .../di/constructor/model/xml/Computer.java | 29 ---------------- .../di/constructor/model/xml/Memory.java | 20 ----------- .../di/constructor/model/xml/Processor.java | 16 --------- .../config/SpringAutowireSetterContext.java | 10 ------ .../config/SpringBeanSetterContext.java | 31 ----------------- .../factory/ComputerAutowireFactory.java | 32 ----------------- .../setter/factory/ComputerBeanFactory.java | 33 ------------------ .../di/setter/factory/ComputerXmlFactory.java | 32 ----------------- .../di/setter/model/autowire/Computer.java | 30 ---------------- .../di/setter/model/autowire/Memory.java | 19 ----------- .../di/setter/model/autowire/Processor.java | 19 ----------- .../di/setter/model/bean/Computer.java | 24 ------------- .../baeldung/di/setter/model/bean/Memory.java | 17 ---------- .../di/setter/model/bean/Processor.java | 16 --------- .../di/setter/model/xml/Computer.java | 24 ------------- .../baeldung/di/setter/model/xml/Memory.java | 20 ----------- .../di/setter/model/xml/Processor.java | 16 --------- .../application-di-constructor-context.xml | 23 ------------- .../application-di-setter-context.xml | 22 ------------ .../AutowireAnnotationInjectionUnitTest.java | 28 --------------- .../BeanAnnotationInjectionUnitTest.java | 28 --------------- .../di/constructor/XmlInjectionUnitTest.java | 27 --------------- .../AutowireAnnotationInjectionUnitTest.java | 28 --------------- .../BeanAnnotationInjectionUnitTest.java | 28 --------------- .../di/setter/XmlInjectionUnitTest.java | 27 --------------- 36 files changed, 868 deletions(-) delete mode 100644 spring-core/src/main/java/com/baeldung/di/constructor/config/SpringAutowireConstructorContext.java delete mode 100644 spring-core/src/main/java/com/baeldung/di/constructor/config/SpringBeanConstructorContext.java delete mode 100644 spring-core/src/main/java/com/baeldung/di/constructor/factory/ComputerAutowireFactory.java delete mode 100644 spring-core/src/main/java/com/baeldung/di/constructor/factory/ComputerBeanFactory.java delete mode 100644 spring-core/src/main/java/com/baeldung/di/constructor/factory/ComputerXmlFactory.java delete mode 100644 spring-core/src/main/java/com/baeldung/di/constructor/model/autowire/Computer.java delete mode 100644 spring-core/src/main/java/com/baeldung/di/constructor/model/autowire/Memory.java delete mode 100644 spring-core/src/main/java/com/baeldung/di/constructor/model/autowire/Processor.java delete mode 100644 spring-core/src/main/java/com/baeldung/di/constructor/model/bean/Computer.java delete mode 100644 spring-core/src/main/java/com/baeldung/di/constructor/model/bean/Memory.java delete mode 100644 spring-core/src/main/java/com/baeldung/di/constructor/model/bean/Processor.java delete mode 100644 spring-core/src/main/java/com/baeldung/di/constructor/model/xml/Computer.java delete mode 100644 spring-core/src/main/java/com/baeldung/di/constructor/model/xml/Memory.java delete mode 100644 spring-core/src/main/java/com/baeldung/di/constructor/model/xml/Processor.java delete mode 100644 spring-core/src/main/java/com/baeldung/di/setter/config/SpringAutowireSetterContext.java delete mode 100644 spring-core/src/main/java/com/baeldung/di/setter/config/SpringBeanSetterContext.java delete mode 100644 spring-core/src/main/java/com/baeldung/di/setter/factory/ComputerAutowireFactory.java delete mode 100644 spring-core/src/main/java/com/baeldung/di/setter/factory/ComputerBeanFactory.java delete mode 100644 spring-core/src/main/java/com/baeldung/di/setter/factory/ComputerXmlFactory.java delete mode 100644 spring-core/src/main/java/com/baeldung/di/setter/model/autowire/Computer.java delete mode 100644 spring-core/src/main/java/com/baeldung/di/setter/model/autowire/Memory.java delete mode 100644 spring-core/src/main/java/com/baeldung/di/setter/model/autowire/Processor.java delete mode 100644 spring-core/src/main/java/com/baeldung/di/setter/model/bean/Computer.java delete mode 100644 spring-core/src/main/java/com/baeldung/di/setter/model/bean/Memory.java delete mode 100644 spring-core/src/main/java/com/baeldung/di/setter/model/bean/Processor.java delete mode 100644 spring-core/src/main/java/com/baeldung/di/setter/model/xml/Computer.java delete mode 100644 spring-core/src/main/java/com/baeldung/di/setter/model/xml/Memory.java delete mode 100644 spring-core/src/main/java/com/baeldung/di/setter/model/xml/Processor.java delete mode 100644 spring-core/src/main/resources/application-di-constructor-context.xml delete mode 100644 spring-core/src/main/resources/application-di-setter-context.xml delete mode 100644 spring-core/src/test/java/com/baeldung/di/constructor/AutowireAnnotationInjectionUnitTest.java delete mode 100644 spring-core/src/test/java/com/baeldung/di/constructor/BeanAnnotationInjectionUnitTest.java delete mode 100644 spring-core/src/test/java/com/baeldung/di/constructor/XmlInjectionUnitTest.java delete mode 100644 spring-core/src/test/java/com/baeldung/di/setter/AutowireAnnotationInjectionUnitTest.java delete mode 100644 spring-core/src/test/java/com/baeldung/di/setter/BeanAnnotationInjectionUnitTest.java delete mode 100644 spring-core/src/test/java/com/baeldung/di/setter/XmlInjectionUnitTest.java diff --git a/spring-core/src/main/java/com/baeldung/di/constructor/config/SpringAutowireConstructorContext.java b/spring-core/src/main/java/com/baeldung/di/constructor/config/SpringAutowireConstructorContext.java deleted file mode 100644 index ae67bc7e5e..0000000000 --- a/spring-core/src/main/java/com/baeldung/di/constructor/config/SpringAutowireConstructorContext.java +++ /dev/null @@ -1,10 +0,0 @@ -package com.baeldung.di.constructor.config; - -import org.springframework.context.annotation.ComponentScan; -import org.springframework.context.annotation.Configuration; - -@Configuration -@ComponentScan(basePackages = { "com.baeldung.di.constructor.model.autowire" }) -public class SpringAutowireConstructorContext { - -} diff --git a/spring-core/src/main/java/com/baeldung/di/constructor/config/SpringBeanConstructorContext.java b/spring-core/src/main/java/com/baeldung/di/constructor/config/SpringBeanConstructorContext.java deleted file mode 100644 index 670f18cb9c..0000000000 --- a/spring-core/src/main/java/com/baeldung/di/constructor/config/SpringBeanConstructorContext.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.baeldung.di.constructor.config; - -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; - -import com.baeldung.di.constructor.model.bean.Computer; -import com.baeldung.di.constructor.model.bean.Memory; -import com.baeldung.di.constructor.model.bean.Processor; - -@Configuration -public class SpringBeanConstructorContext { - - @Bean - public Computer computer() { - return new Computer(processor(), memory()); - } - - @Bean - public Processor processor() { - return new Processor(); - } - - @Bean - public Memory memory() { - return new Memory(); - } - -} diff --git a/spring-core/src/main/java/com/baeldung/di/constructor/factory/ComputerAutowireFactory.java b/spring-core/src/main/java/com/baeldung/di/constructor/factory/ComputerAutowireFactory.java deleted file mode 100644 index 2a13adf250..0000000000 --- a/spring-core/src/main/java/com/baeldung/di/constructor/factory/ComputerAutowireFactory.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.baeldung.di.constructor.factory; - -import org.springframework.context.ApplicationContext; -import org.springframework.context.ConfigurableApplicationContext; -import org.springframework.context.annotation.AnnotationConfigApplicationContext; - -import com.baeldung.di.constructor.config.SpringAutowireConstructorContext; -import com.baeldung.di.constructor.model.autowire.Computer; - -public class ComputerAutowireFactory { - - public static void main(String[] args) { - // Create Spring Application Context - ApplicationContext context = new AnnotationConfigApplicationContext(SpringAutowireConstructorContext.class); - - // Get Spring bean "computer" created - Computer computer = (Computer) context.getBean("computer"); - System.out.println("---------------- Constructor Injection via @Autowire------------------"); - System.out.println("Processor cores : " + computer.getProcessor() - .getCores()); - System.out.println("Processor frequency : " + computer.getProcessor() - .getFrequency()); - System.out.println("Memory Size in GB : " + computer.getMemory() - .getSizeInGb()); - System.out.println("Memory format : " + computer.getMemory() - .getFormat()); - System.out.println("---------------- Constructor Injection via @Autowire------------------"); - // Close Spring Application Context - ((ConfigurableApplicationContext) context).close(); - } - -} diff --git a/spring-core/src/main/java/com/baeldung/di/constructor/factory/ComputerBeanFactory.java b/spring-core/src/main/java/com/baeldung/di/constructor/factory/ComputerBeanFactory.java deleted file mode 100644 index 713ea82b00..0000000000 --- a/spring-core/src/main/java/com/baeldung/di/constructor/factory/ComputerBeanFactory.java +++ /dev/null @@ -1,33 +0,0 @@ -package com.baeldung.di.constructor.factory; - -import org.springframework.context.ApplicationContext; -import org.springframework.context.ConfigurableApplicationContext; -import org.springframework.context.annotation.AnnotationConfigApplicationContext; - -import com.baeldung.di.constructor.config.SpringBeanConstructorContext; -import com.baeldung.di.constructor.model.bean.Computer; - -public class ComputerBeanFactory { - - public static void main(String[] args) { - // Create Spring Application Context - ApplicationContext context = new AnnotationConfigApplicationContext(SpringBeanConstructorContext.class); - - // Get Spring bean "computer" created - Computer computer = (Computer) context.getBean("computer"); - System.out.println("---------------- Constructor Injection via @Bean ------------------"); - System.out.println("Processor cores : " + computer.getProcessor() - .getCores()); - System.out.println("Processor frequency : " + computer.getProcessor() - .getFrequency()); - System.out.println("Memory Size in GB : " + computer.getMemory() - .getSizeInGb()); - System.out.println("Memory format : " + computer.getMemory() - .getFormat()); - System.out.println("---------------- Constructor Injection via @Bean ------------------"); - - // Close Spring Application Context - ((ConfigurableApplicationContext) context).close(); - } - -} diff --git a/spring-core/src/main/java/com/baeldung/di/constructor/factory/ComputerXmlFactory.java b/spring-core/src/main/java/com/baeldung/di/constructor/factory/ComputerXmlFactory.java deleted file mode 100644 index afe5fa281b..0000000000 --- a/spring-core/src/main/java/com/baeldung/di/constructor/factory/ComputerXmlFactory.java +++ /dev/null @@ -1,33 +0,0 @@ -package com.baeldung.di.constructor.factory; - -import org.springframework.context.ApplicationContext; -import org.springframework.context.ConfigurableApplicationContext; -import org.springframework.context.support.ClassPathXmlApplicationContext; - -import com.baeldung.di.constructor.model.xml.Computer; - -public class ComputerXmlFactory { - - public static void main(String[] args) { - // Create Spring Application Context - ApplicationContext context = new ClassPathXmlApplicationContext("application-di-constructor-context.xml"); - - // Get Spring bean "computer" created - Computer computer = (Computer) context.getBean("computer"); - System.out.println("---------------- Constructor Injection via XML ------------------"); - System.out.println("Processor cores : " + computer.getProcessor() - .getCores()); - System.out.println("Processor frequency : " + computer.getProcessor() - .getFrequency()); - System.out.println("Memory Size in GB : " + computer.getMemory() - .getSizeInGb()); - System.out.println("Memory format : " + computer.getMemory() - .getFormat()); - System.out.println("---------------- Constructor Injection via XML ------------------"); - - // Close Spring Application Context - ((ConfigurableApplicationContext) context).close(); - - } - -} diff --git a/spring-core/src/main/java/com/baeldung/di/constructor/model/autowire/Computer.java b/spring-core/src/main/java/com/baeldung/di/constructor/model/autowire/Computer.java deleted file mode 100644 index 1dabfd287e..0000000000 --- a/spring-core/src/main/java/com/baeldung/di/constructor/model/autowire/Computer.java +++ /dev/null @@ -1,34 +0,0 @@ -package com.baeldung.di.constructor.model.autowire; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; - -@Component -public class Computer { - - private Processor processor; - private Memory memory; - - @Autowired - public Computer(Processor processor, Memory memory) { - this.processor = processor; - this.memory = memory; - } - - public Processor getProcessor() { - return processor; - } - - public void setProcessor(Processor processor) { - this.processor = processor; - } - - public Memory getMemory() { - return memory; - } - - public void setMemory(Memory memory) { - this.memory = memory; - } - -} diff --git a/spring-core/src/main/java/com/baeldung/di/constructor/model/autowire/Memory.java b/spring-core/src/main/java/com/baeldung/di/constructor/model/autowire/Memory.java deleted file mode 100644 index d1faecc569..0000000000 --- a/spring-core/src/main/java/com/baeldung/di/constructor/model/autowire/Memory.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.baeldung.di.constructor.model.autowire; - -import org.springframework.stereotype.Component; - -@Component -public class Memory { - - private Integer sizeInGb = 16; - private String format = "DDR3"; - - public Integer getSizeInGb() { - return sizeInGb; - } - - public String getFormat() { - return format; - } - -} diff --git a/spring-core/src/main/java/com/baeldung/di/constructor/model/autowire/Processor.java b/spring-core/src/main/java/com/baeldung/di/constructor/model/autowire/Processor.java deleted file mode 100644 index 7de9d2bf6a..0000000000 --- a/spring-core/src/main/java/com/baeldung/di/constructor/model/autowire/Processor.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.baeldung.di.constructor.model.autowire; - -import org.springframework.stereotype.Component; - -@Component -public class Processor { - - private Integer cores = 2; - private Double frequency = 1.4; - - public Integer getCores() { - return cores; - } - - public Double getFrequency() { - return frequency; - } - -} diff --git a/spring-core/src/main/java/com/baeldung/di/constructor/model/bean/Computer.java b/spring-core/src/main/java/com/baeldung/di/constructor/model/bean/Computer.java deleted file mode 100644 index 74ec89ce0c..0000000000 --- a/spring-core/src/main/java/com/baeldung/di/constructor/model/bean/Computer.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.baeldung.di.constructor.model.bean; - -public class Computer { - - private Processor processor; - private Memory memory; - - public Computer(Processor processor, Memory memory) { - this.processor = processor; - this.memory = memory; - } - - public Processor getProcessor() { - return processor; - } - - public void setProcessor(Processor processor) { - this.processor = processor; - } - - public Memory getMemory() { - return memory; - } - - public void setMemory(Memory memory) { - this.memory = memory; - } - -} diff --git a/spring-core/src/main/java/com/baeldung/di/constructor/model/bean/Memory.java b/spring-core/src/main/java/com/baeldung/di/constructor/model/bean/Memory.java deleted file mode 100644 index 18d4eaf948..0000000000 --- a/spring-core/src/main/java/com/baeldung/di/constructor/model/bean/Memory.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.baeldung.di.constructor.model.bean; - -public class Memory { - - private Integer sizeInGb = 16; - private String format = "DDR3"; - - public Integer getSizeInGb() { - return sizeInGb; - } - - public String getFormat() { - return format; - } - -} diff --git a/spring-core/src/main/java/com/baeldung/di/constructor/model/bean/Processor.java b/spring-core/src/main/java/com/baeldung/di/constructor/model/bean/Processor.java deleted file mode 100644 index 9dd719c872..0000000000 --- a/spring-core/src/main/java/com/baeldung/di/constructor/model/bean/Processor.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.baeldung.di.constructor.model.bean; - -public class Processor { - - private Integer cores = 2; - private Double frequency = 1.4; - - public Integer getCores() { - return cores; - } - - public Double getFrequency() { - return frequency; - } - -} diff --git a/spring-core/src/main/java/com/baeldung/di/constructor/model/xml/Computer.java b/spring-core/src/main/java/com/baeldung/di/constructor/model/xml/Computer.java deleted file mode 100644 index 66c39c66b7..0000000000 --- a/spring-core/src/main/java/com/baeldung/di/constructor/model/xml/Computer.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.baeldung.di.constructor.model.xml; - -public class Computer { - - private Processor processor; - private Memory memory; - - public Computer(Processor processor, Memory memory) { - this.processor = processor; - this.memory = memory; - } - - public Processor getProcessor() { - return processor; - } - - public void setProcessor(Processor processor) { - this.processor = processor; - } - - public Memory getMemory() { - return memory; - } - - public void setMemory(Memory memory) { - this.memory = memory; - } - -} diff --git a/spring-core/src/main/java/com/baeldung/di/constructor/model/xml/Memory.java b/spring-core/src/main/java/com/baeldung/di/constructor/model/xml/Memory.java deleted file mode 100644 index 7b4f4fb697..0000000000 --- a/spring-core/src/main/java/com/baeldung/di/constructor/model/xml/Memory.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.baeldung.di.constructor.model.xml; - -public class Memory { - - private Integer sizeInGb = 16; - private String format = "DDR3"; - - public Memory() { - - } - - public Integer getSizeInGb() { - return sizeInGb; - } - - public String getFormat() { - return format; - } - -} diff --git a/spring-core/src/main/java/com/baeldung/di/constructor/model/xml/Processor.java b/spring-core/src/main/java/com/baeldung/di/constructor/model/xml/Processor.java deleted file mode 100644 index 4e783042fe..0000000000 --- a/spring-core/src/main/java/com/baeldung/di/constructor/model/xml/Processor.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.baeldung.di.constructor.model.xml; - -public class Processor { - - private Integer cores = 2; - private Double frequency = 1.4; - - public Integer getCores() { - return cores; - } - - public Double getFrequency() { - return frequency; - } - -} diff --git a/spring-core/src/main/java/com/baeldung/di/setter/config/SpringAutowireSetterContext.java b/spring-core/src/main/java/com/baeldung/di/setter/config/SpringAutowireSetterContext.java deleted file mode 100644 index 9e1d24fc61..0000000000 --- a/spring-core/src/main/java/com/baeldung/di/setter/config/SpringAutowireSetterContext.java +++ /dev/null @@ -1,10 +0,0 @@ -package com.baeldung.di.setter.config; - -import org.springframework.context.annotation.ComponentScan; -import org.springframework.context.annotation.Configuration; - -@Configuration -@ComponentScan(basePackages = { "com.baeldung.di.setter.model.autowire" }) -public class SpringAutowireSetterContext { - -} diff --git a/spring-core/src/main/java/com/baeldung/di/setter/config/SpringBeanSetterContext.java b/spring-core/src/main/java/com/baeldung/di/setter/config/SpringBeanSetterContext.java deleted file mode 100644 index 14767fb0ca..0000000000 --- a/spring-core/src/main/java/com/baeldung/di/setter/config/SpringBeanSetterContext.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.baeldung.di.setter.config; - -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; - -import com.baeldung.di.setter.model.bean.Computer; -import com.baeldung.di.setter.model.bean.Memory; -import com.baeldung.di.setter.model.bean.Processor; - -@Configuration -public class SpringBeanSetterContext { - - @Bean - public Computer computer() { - Computer computer = new Computer(); - computer.setProcessor(processor()); - computer.setMemory(memory()); - return computer; - } - - @Bean - public Processor processor() { - return new Processor(); - } - - @Bean - public Memory memory() { - return new Memory(); - } - -} diff --git a/spring-core/src/main/java/com/baeldung/di/setter/factory/ComputerAutowireFactory.java b/spring-core/src/main/java/com/baeldung/di/setter/factory/ComputerAutowireFactory.java deleted file mode 100644 index ef1ac83530..0000000000 --- a/spring-core/src/main/java/com/baeldung/di/setter/factory/ComputerAutowireFactory.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.baeldung.di.setter.factory; - -import org.springframework.context.ApplicationContext; -import org.springframework.context.ConfigurableApplicationContext; -import org.springframework.context.annotation.AnnotationConfigApplicationContext; - -import com.baeldung.di.setter.config.SpringAutowireSetterContext; -import com.baeldung.di.setter.model.autowire.Computer; - -public class ComputerAutowireFactory { - - public static void main(String[] args) { - // Create Spring Application Context - ApplicationContext context = new AnnotationConfigApplicationContext(SpringAutowireSetterContext.class); - - // Get Spring bean "computer" created - Computer computer = (Computer) context.getBean("computer"); - System.out.println("---------------- Setter Injection via @Autowire ------------------"); - System.out.println("Processor cores : " + computer.getProcessor() - .getCores()); - System.out.println("Processor frequency : " + computer.getProcessor() - .getFrequency()); - System.out.println("Memory Size in GB : " + computer.getMemory() - .getSizeInGb()); - System.out.println("Memory format : " + computer.getMemory() - .getFormat()); - System.out.println("---------------- Setter Injection via @Autowire ------------------"); - // Close Spring Application Context - ((ConfigurableApplicationContext) context).close(); - } - -} diff --git a/spring-core/src/main/java/com/baeldung/di/setter/factory/ComputerBeanFactory.java b/spring-core/src/main/java/com/baeldung/di/setter/factory/ComputerBeanFactory.java deleted file mode 100644 index 87f49bf4cf..0000000000 --- a/spring-core/src/main/java/com/baeldung/di/setter/factory/ComputerBeanFactory.java +++ /dev/null @@ -1,33 +0,0 @@ -package com.baeldung.di.setter.factory; - -import org.springframework.context.ApplicationContext; -import org.springframework.context.ConfigurableApplicationContext; -import org.springframework.context.annotation.AnnotationConfigApplicationContext; - -import com.baeldung.di.setter.config.SpringBeanSetterContext; -import com.baeldung.di.setter.model.bean.Computer; - -public class ComputerBeanFactory { - - public static void main(String[] args) { - // Create Spring Application Context - ApplicationContext context = new AnnotationConfigApplicationContext(SpringBeanSetterContext.class); - - // Get Spring bean "computer" created - Computer computer = (Computer) context.getBean("computer"); - System.out.println("---------------- Setter Injection via @Bean ------------------"); - System.out.println("Processor cores : " + computer.getProcessor() - .getCores()); - System.out.println("Processor frequency : " + computer.getProcessor() - .getFrequency()); - System.out.println("Memory Size in GB : " + computer.getMemory() - .getSizeInGb()); - System.out.println("Memory format : " + computer.getMemory() - .getFormat()); - System.out.println("---------------- Setter Injection via @Bean ------------------"); - - // Close Spring Application Context - ((ConfigurableApplicationContext) context).close(); - } - -} diff --git a/spring-core/src/main/java/com/baeldung/di/setter/factory/ComputerXmlFactory.java b/spring-core/src/main/java/com/baeldung/di/setter/factory/ComputerXmlFactory.java deleted file mode 100644 index 01af29905c..0000000000 --- a/spring-core/src/main/java/com/baeldung/di/setter/factory/ComputerXmlFactory.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.baeldung.di.setter.factory; - -import org.springframework.context.ApplicationContext; -import org.springframework.context.ConfigurableApplicationContext; -import org.springframework.context.support.ClassPathXmlApplicationContext; - -import com.baeldung.di.setter.model.xml.Computer; - -public class ComputerXmlFactory { - - public static void main(String[] args) { - // Create Spring Application Context - ApplicationContext context = new ClassPathXmlApplicationContext("application-setter-context.xml"); - - // Get Spring bean "computer" created - Computer computer = (Computer) context.getBean("computer"); - System.out.println("---------------- Setter Injection via XML ------------------"); - System.out.println("Processor cores : " + computer.getProcessor() - .getCores()); - System.out.println("Processor frequency : " + computer.getProcessor() - .getFrequency()); - System.out.println("Memory Size in GB : " + computer.getMemory() - .getSizeInGb()); - System.out.println("Memory format : " + computer.getMemory() - .getFormat()); - System.out.println("---------------- Setter Injection via XML ------------------"); - // Close Spring Application Context - ((ConfigurableApplicationContext) context).close(); - - } - -} diff --git a/spring-core/src/main/java/com/baeldung/di/setter/model/autowire/Computer.java b/spring-core/src/main/java/com/baeldung/di/setter/model/autowire/Computer.java deleted file mode 100644 index ed32b34729..0000000000 --- a/spring-core/src/main/java/com/baeldung/di/setter/model/autowire/Computer.java +++ /dev/null @@ -1,30 +0,0 @@ -package com.baeldung.di.setter.model.autowire; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; - -@Component -public class Computer { - - private Processor processor; - private Memory memory; - - public Processor getProcessor() { - return processor; - } - - @Autowired - public void setProcessor(Processor processor) { - this.processor = processor; - } - - public Memory getMemory() { - return memory; - } - - @Autowired - public void setMemory(Memory memory) { - this.memory = memory; - } - -} diff --git a/spring-core/src/main/java/com/baeldung/di/setter/model/autowire/Memory.java b/spring-core/src/main/java/com/baeldung/di/setter/model/autowire/Memory.java deleted file mode 100644 index ffdd63d389..0000000000 --- a/spring-core/src/main/java/com/baeldung/di/setter/model/autowire/Memory.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.baeldung.di.setter.model.autowire; - -import org.springframework.stereotype.Component; - -@Component -public class Memory { - - private Integer sizeInGb = 16; - private String format = "DDR3"; - - public Integer getSizeInGb() { - return sizeInGb; - } - - public String getFormat() { - return format; - } - -} diff --git a/spring-core/src/main/java/com/baeldung/di/setter/model/autowire/Processor.java b/spring-core/src/main/java/com/baeldung/di/setter/model/autowire/Processor.java deleted file mode 100644 index 9b01143bd2..0000000000 --- a/spring-core/src/main/java/com/baeldung/di/setter/model/autowire/Processor.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.baeldung.di.setter.model.autowire; - -import org.springframework.stereotype.Component; - -@Component -public class Processor { - - private Integer cores = 2; - private Double frequency = 1.4; - - public Integer getCores() { - return cores; - } - - public Double getFrequency() { - return frequency; - } - -} diff --git a/spring-core/src/main/java/com/baeldung/di/setter/model/bean/Computer.java b/spring-core/src/main/java/com/baeldung/di/setter/model/bean/Computer.java deleted file mode 100644 index e350cddee8..0000000000 --- a/spring-core/src/main/java/com/baeldung/di/setter/model/bean/Computer.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.baeldung.di.setter.model.bean; - -public class Computer { - - private Processor processor; - private Memory memory; - - public Processor getProcessor() { - return processor; - } - - public void setProcessor(Processor processor) { - this.processor = processor; - } - - public Memory getMemory() { - return memory; - } - - public void setMemory(Memory memory) { - this.memory = memory; - } - -} diff --git a/spring-core/src/main/java/com/baeldung/di/setter/model/bean/Memory.java b/spring-core/src/main/java/com/baeldung/di/setter/model/bean/Memory.java deleted file mode 100644 index b34052e066..0000000000 --- a/spring-core/src/main/java/com/baeldung/di/setter/model/bean/Memory.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.baeldung.di.setter.model.bean; - -public class Memory { - - private Integer sizeInGb = 16; - private String format = "DDR3"; - - // - public Integer getSizeInGb() { - return sizeInGb; - } - - public String getFormat() { - return format; - } - -} diff --git a/spring-core/src/main/java/com/baeldung/di/setter/model/bean/Processor.java b/spring-core/src/main/java/com/baeldung/di/setter/model/bean/Processor.java deleted file mode 100644 index 2c9f050232..0000000000 --- a/spring-core/src/main/java/com/baeldung/di/setter/model/bean/Processor.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.baeldung.di.setter.model.bean; - -public class Processor { - - private Integer cores = 2; - private Double frequency = 1.4; - - public Integer getCores() { - return cores; - } - - public Double getFrequency() { - return frequency; - } - -} diff --git a/spring-core/src/main/java/com/baeldung/di/setter/model/xml/Computer.java b/spring-core/src/main/java/com/baeldung/di/setter/model/xml/Computer.java deleted file mode 100644 index a88dc34d07..0000000000 --- a/spring-core/src/main/java/com/baeldung/di/setter/model/xml/Computer.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.baeldung.di.setter.model.xml; - -public class Computer { - - private Processor processor; - private Memory memory; - - public Processor getProcessor() { - return processor; - } - - public void setProcessor(Processor processor) { - this.processor = processor; - } - - public Memory getMemory() { - return memory; - } - - public void setMemory(Memory memory) { - this.memory = memory; - } - -} diff --git a/spring-core/src/main/java/com/baeldung/di/setter/model/xml/Memory.java b/spring-core/src/main/java/com/baeldung/di/setter/model/xml/Memory.java deleted file mode 100644 index 93667ff225..0000000000 --- a/spring-core/src/main/java/com/baeldung/di/setter/model/xml/Memory.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.baeldung.di.setter.model.xml; - -public class Memory { - - private Integer sizeInGb = 16; - private String format = "DDR3"; - - public Memory() { - - } - - public Integer getSizeInGb() { - return sizeInGb; - } - - public String getFormat() { - return format; - } - -} diff --git a/spring-core/src/main/java/com/baeldung/di/setter/model/xml/Processor.java b/spring-core/src/main/java/com/baeldung/di/setter/model/xml/Processor.java deleted file mode 100644 index 1845983338..0000000000 --- a/spring-core/src/main/java/com/baeldung/di/setter/model/xml/Processor.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.baeldung.di.setter.model.xml; - -public class Processor { - - private Integer cores = 2; - private Double frequency = 1.4; - - public Integer getCores() { - return cores; - } - - public Double getFrequency() { - return frequency; - } - -} diff --git a/spring-core/src/main/resources/application-di-constructor-context.xml b/spring-core/src/main/resources/application-di-constructor-context.xml deleted file mode 100644 index c1f2d4cdfd..0000000000 --- a/spring-core/src/main/resources/application-di-constructor-context.xml +++ /dev/null @@ -1,23 +0,0 @@ - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/spring-core/src/main/resources/application-di-setter-context.xml b/spring-core/src/main/resources/application-di-setter-context.xml deleted file mode 100644 index 658f28b3f3..0000000000 --- a/spring-core/src/main/resources/application-di-setter-context.xml +++ /dev/null @@ -1,22 +0,0 @@ - - - - - - - - - - - - - - \ No newline at end of file diff --git a/spring-core/src/test/java/com/baeldung/di/constructor/AutowireAnnotationInjectionUnitTest.java b/spring-core/src/test/java/com/baeldung/di/constructor/AutowireAnnotationInjectionUnitTest.java deleted file mode 100644 index 348ac1428b..0000000000 --- a/spring-core/src/test/java/com/baeldung/di/constructor/AutowireAnnotationInjectionUnitTest.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.baeldung.di.constructor; - -import static org.junit.Assert.assertNotNull; - -import org.junit.Before; -import org.junit.Test; -import org.springframework.context.ApplicationContext; -import org.springframework.context.annotation.AnnotationConfigApplicationContext; - -import com.baeldung.di.constructor.config.SpringAutowireConstructorContext; -import com.baeldung.di.constructor.model.autowire.Computer; - -public class AutowireAnnotationInjectionUnitTest { - - private ApplicationContext applicationContext; - - @Before - public void setUp() throws Exception { - applicationContext = new AnnotationConfigApplicationContext(SpringAutowireConstructorContext.class); - } - - @Test - public void getComputer_() { - final Computer computer = applicationContext.getBean("computer", Computer.class); - assertNotNull(computer); - } - -} diff --git a/spring-core/src/test/java/com/baeldung/di/constructor/BeanAnnotationInjectionUnitTest.java b/spring-core/src/test/java/com/baeldung/di/constructor/BeanAnnotationInjectionUnitTest.java deleted file mode 100644 index 3eb276428c..0000000000 --- a/spring-core/src/test/java/com/baeldung/di/constructor/BeanAnnotationInjectionUnitTest.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.baeldung.di.constructor; - -import static org.junit.Assert.assertNotNull; - -import org.junit.Before; -import org.junit.Test; -import org.springframework.context.ApplicationContext; -import org.springframework.context.annotation.AnnotationConfigApplicationContext; - -import com.baeldung.di.constructor.config.SpringBeanConstructorContext; -import com.baeldung.di.constructor.model.bean.Computer; - -public class BeanAnnotationInjectionUnitTest { - - private ApplicationContext applicationContext; - - @Before - public void setUp() throws Exception { - applicationContext = new AnnotationConfigApplicationContext(SpringBeanConstructorContext.class); - } - - @Test - public void getComputer_() { - final Computer computer = applicationContext.getBean("computer", Computer.class); - assertNotNull(computer); - } - -} diff --git a/spring-core/src/test/java/com/baeldung/di/constructor/XmlInjectionUnitTest.java b/spring-core/src/test/java/com/baeldung/di/constructor/XmlInjectionUnitTest.java deleted file mode 100644 index cfcc7ed56e..0000000000 --- a/spring-core/src/test/java/com/baeldung/di/constructor/XmlInjectionUnitTest.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.baeldung.di.constructor; - -import static org.junit.Assert.assertNotNull; - -import org.junit.Before; -import org.junit.Test; -import org.springframework.context.ApplicationContext; -import org.springframework.context.support.ClassPathXmlApplicationContext; - -import com.baeldung.di.constructor.model.xml.Computer; - -public class XmlInjectionUnitTest { - - private ApplicationContext applicationContext; - - @Before - public void setUp() throws Exception { - applicationContext = new ClassPathXmlApplicationContext("application-di-constructor-context.xml"); - } - - @Test - public void getComputer_() { - final Computer computer = applicationContext.getBean("computer", Computer.class); - assertNotNull(computer); - } - -} diff --git a/spring-core/src/test/java/com/baeldung/di/setter/AutowireAnnotationInjectionUnitTest.java b/spring-core/src/test/java/com/baeldung/di/setter/AutowireAnnotationInjectionUnitTest.java deleted file mode 100644 index fc8e58f08b..0000000000 --- a/spring-core/src/test/java/com/baeldung/di/setter/AutowireAnnotationInjectionUnitTest.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.baeldung.di.setter; - -import static org.junit.Assert.assertNotNull; - -import org.junit.Before; -import org.junit.Test; -import org.springframework.context.ApplicationContext; -import org.springframework.context.annotation.AnnotationConfigApplicationContext; - -import com.baeldung.di.setter.config.SpringAutowireSetterContext; -import com.baeldung.di.setter.model.autowire.Computer; - -public class AutowireAnnotationInjectionUnitTest { - - private ApplicationContext applicationContext; - - @Before - public void setUp() throws Exception { - applicationContext = new AnnotationConfigApplicationContext(SpringAutowireSetterContext.class); - } - - @Test - public void getComputer_() { - final Computer computer = applicationContext.getBean("computer", Computer.class); - assertNotNull(computer); - } - -} diff --git a/spring-core/src/test/java/com/baeldung/di/setter/BeanAnnotationInjectionUnitTest.java b/spring-core/src/test/java/com/baeldung/di/setter/BeanAnnotationInjectionUnitTest.java deleted file mode 100644 index c89c1239ba..0000000000 --- a/spring-core/src/test/java/com/baeldung/di/setter/BeanAnnotationInjectionUnitTest.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.baeldung.di.setter; - -import static org.junit.Assert.assertNotNull; - -import org.junit.Before; -import org.junit.Test; -import org.springframework.context.ApplicationContext; -import org.springframework.context.annotation.AnnotationConfigApplicationContext; - -import com.baeldung.di.setter.config.SpringBeanSetterContext; -import com.baeldung.di.setter.model.bean.Computer; - -public class BeanAnnotationInjectionUnitTest { - - private ApplicationContext applicationContext; - - @Before - public void setUp() throws Exception { - applicationContext = new AnnotationConfigApplicationContext(SpringBeanSetterContext.class); - } - - @Test - public void getComputer_() { - final Computer computer = applicationContext.getBean("computer", Computer.class); - assertNotNull(computer); - } - -} diff --git a/spring-core/src/test/java/com/baeldung/di/setter/XmlInjectionUnitTest.java b/spring-core/src/test/java/com/baeldung/di/setter/XmlInjectionUnitTest.java deleted file mode 100644 index 0f38b46194..0000000000 --- a/spring-core/src/test/java/com/baeldung/di/setter/XmlInjectionUnitTest.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.baeldung.di.setter; - -import static org.junit.Assert.assertNotNull; - -import org.junit.Before; -import org.junit.Test; -import org.springframework.context.ApplicationContext; -import org.springframework.context.support.ClassPathXmlApplicationContext; - -import com.baeldung.di.setter.model.xml.Computer; - -public class XmlInjectionUnitTest { - - private ApplicationContext applicationContext; - - @Before - public void setUp() throws Exception { - applicationContext = new ClassPathXmlApplicationContext("application-di-setter-context.xml"); - } - - @Test - public void getComputer_() { - final Computer computer = applicationContext.getBean("computer", Computer.class); - assertNotNull(computer); - } - -} From 1c36955757ef39332307702196667f22b59294b1 Mon Sep 17 00:00:00 2001 From: gschambial Date: Mon, 13 Nov 2017 23:01:05 +0530 Subject: [PATCH 06/28] Removed addition classpath changes from master branch. --- spring-boot/.factorypath | 121 ++++++++++++++++++--------------------- 1 file changed, 55 insertions(+), 66 deletions(-) diff --git a/spring-boot/.factorypath b/spring-boot/.factorypath index 97062c74a4..aa15485f5c 100644 --- a/spring-boot/.factorypath +++ b/spring-boot/.factorypath @@ -1,30 +1,46 @@ - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - + + + + + + + @@ -32,105 +48,81 @@ + - - - - - + - + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + - - - - - - + - + @@ -141,10 +133,7 @@ - - - @@ -154,7 +143,7 @@ - + From d954258a809078b84ea6c16d7c68bf7fdf40f6a9 Mon Sep 17 00:00:00 2001 From: gschambial Date: Tue, 14 Nov 2017 10:25:42 +0530 Subject: [PATCH 07/28] 1. Moved commmon code to @Before method in ComparatorUnitTest. 2. Changed ArrayList to List for declarations --- .../com/baeldung/comparable/PlayerSorter.java | 3 ++- .../baeldung/comparator/PlayerAgeSorter.java | 3 ++- .../comparator/PlayerRankingSorter.java | 3 ++- .../comparable/ComparableUnitTest.java | 3 ++- .../comparator/ComparatorUnitTest.java | 21 ++++++++++--------- 5 files changed, 19 insertions(+), 14 deletions(-) diff --git a/core-java/src/main/java/com/baeldung/comparable/PlayerSorter.java b/core-java/src/main/java/com/baeldung/comparable/PlayerSorter.java index 1f64a5eb43..eeb2c7a883 100644 --- a/core-java/src/main/java/com/baeldung/comparable/PlayerSorter.java +++ b/core-java/src/main/java/com/baeldung/comparable/PlayerSorter.java @@ -2,12 +2,13 @@ package com.baeldung.comparable; import java.util.ArrayList; import java.util.Collections; +import java.util.List; public class PlayerSorter { public static void main(String[] args) { - ArrayList footballTeam = new ArrayList(); + List footballTeam = new ArrayList(); Player player1 = new Player(59, "John", 20); Player player2 = new Player(67, "Roger", 22); Player player3 = new Player(45, "Steven", 24); diff --git a/core-java/src/main/java/com/baeldung/comparator/PlayerAgeSorter.java b/core-java/src/main/java/com/baeldung/comparator/PlayerAgeSorter.java index e7d3fd1264..3bbbcddb80 100644 --- a/core-java/src/main/java/com/baeldung/comparator/PlayerAgeSorter.java +++ b/core-java/src/main/java/com/baeldung/comparator/PlayerAgeSorter.java @@ -2,12 +2,13 @@ package com.baeldung.comparator; import java.util.ArrayList; import java.util.Collections; +import java.util.List; public class PlayerAgeSorter { public static void main(String[] args) { - ArrayList footballTeam = new ArrayList(); + List footballTeam = new ArrayList(); Player player1 = new Player(59, "John", 22); Player player2 = new Player(67, "Roger", 20); Player player3 = new Player(45, "Steven", 24); diff --git a/core-java/src/main/java/com/baeldung/comparator/PlayerRankingSorter.java b/core-java/src/main/java/com/baeldung/comparator/PlayerRankingSorter.java index a6a971f99e..581585fb7e 100644 --- a/core-java/src/main/java/com/baeldung/comparator/PlayerRankingSorter.java +++ b/core-java/src/main/java/com/baeldung/comparator/PlayerRankingSorter.java @@ -2,12 +2,13 @@ package com.baeldung.comparator; import java.util.ArrayList; import java.util.Collections; +import java.util.List; public class PlayerRankingSorter { public static void main(String[] args) { - ArrayList footballTeam = new ArrayList(); + List footballTeam = new ArrayList(); Player player1 = new Player(59, "John", 22); Player player2 = new Player(67, "Roger", 20); Player player3 = new Player(45, "Steven", 40); diff --git a/core-java/src/test/java/com/baeldung/comparable/ComparableUnitTest.java b/core-java/src/test/java/com/baeldung/comparable/ComparableUnitTest.java index c65c2c3740..2b45dd0391 100644 --- a/core-java/src/test/java/com/baeldung/comparable/ComparableUnitTest.java +++ b/core-java/src/test/java/com/baeldung/comparable/ComparableUnitTest.java @@ -4,6 +4,7 @@ import static org.junit.Assert.assertEquals; import java.util.ArrayList; import java.util.Collections; +import java.util.List; import org.junit.Test; @@ -11,7 +12,7 @@ public class ComparableUnitTest { @Test public void whenUsingComparable_thenSortedList() { - ArrayList footballTeam = new ArrayList(); + List footballTeam = new ArrayList(); Player player1 = new Player(59, "John", 20); Player player2 = new Player(67, "Roger", 22); Player player3 = new Player(45, "Steven", 24); diff --git a/core-java/src/test/java/com/baeldung/comparator/ComparatorUnitTest.java b/core-java/src/test/java/com/baeldung/comparator/ComparatorUnitTest.java index 78f0e0bf33..769986edec 100644 --- a/core-java/src/test/java/com/baeldung/comparator/ComparatorUnitTest.java +++ b/core-java/src/test/java/com/baeldung/comparator/ComparatorUnitTest.java @@ -4,20 +4,28 @@ import static org.junit.Assert.assertEquals; import java.util.ArrayList; import java.util.Collections; +import java.util.List; +import org.junit.Before; import org.junit.Test; public class ComparatorUnitTest { - @Test - public void whenUsingRankingComparator_thenSortedList() { - ArrayList footballTeam = new ArrayList(); + List footballTeam; + + @Before + public void setUp(){ + footballTeam = new ArrayList(); Player player1 = new Player(59, "John", 20); Player player2 = new Player(67, "Roger", 22); Player player3 = new Player(45, "Steven", 24); footballTeam.add(player1); footballTeam.add(player2); footballTeam.add(player3); + } + + @Test + public void whenUsingRankingComparator_thenSortedList() { PlayerRankingComparator playerComparator = new PlayerRankingComparator(); Collections.sort(footballTeam, playerComparator); assertEquals(footballTeam.get(0).getName(), "Steven"); @@ -26,13 +34,6 @@ public class ComparatorUnitTest { @Test public void whenUsingAgeComparator_thenSortedList() { - ArrayList footballTeam = new ArrayList(); - Player player1 = new Player(59, "John", 20); - Player player2 = new Player(67, "Roger", 22); - Player player3 = new Player(45, "Steven", 24); - footballTeam.add(player1); - footballTeam.add(player2); - footballTeam.add(player3); PlayerAgeComparator playerComparator = new PlayerAgeComparator(); Collections.sort(footballTeam, playerComparator); assertEquals(footballTeam.get(0).getName(), "John"); From 66a1c32e1329e0b014e13780c7e43ddf685e8c7f Mon Sep 17 00:00:00 2001 From: gschambial Date: Fri, 24 Nov 2017 13:48:55 +0530 Subject: [PATCH 08/28] 1. Java 8 Comparators example added 2. Comparator.comparing example added --- .../com/baeldung/comparable/PlayerSorter.java | 6 +- .../comparable/ComparableUnitTest.java | 8 ++- .../comparator/ComparatorUnitTest.java | 22 ++++--- .../comparator/Java8ComparatorUnitTest.java | 63 +++++++++++++++++++ 4 files changed, 84 insertions(+), 15 deletions(-) create mode 100644 core-java/src/test/java/com/baeldung/comparator/Java8ComparatorUnitTest.java diff --git a/core-java/src/main/java/com/baeldung/comparable/PlayerSorter.java b/core-java/src/main/java/com/baeldung/comparable/PlayerSorter.java index eeb2c7a883..a9b883f579 100644 --- a/core-java/src/main/java/com/baeldung/comparable/PlayerSorter.java +++ b/core-java/src/main/java/com/baeldung/comparable/PlayerSorter.java @@ -7,7 +7,7 @@ import java.util.List; public class PlayerSorter { public static void main(String[] args) { - + List footballTeam = new ArrayList(); Player player1 = new Player(59, "John", 20); Player player2 = new Player(67, "Roger", 22); @@ -15,11 +15,11 @@ public class PlayerSorter { footballTeam.add(player1); footballTeam.add(player2); footballTeam.add(player3); - + System.out.println("Before Sorting : " + footballTeam); Collections.sort(footballTeam); System.out.println("After Sorting : " + footballTeam); - + } } diff --git a/core-java/src/test/java/com/baeldung/comparable/ComparableUnitTest.java b/core-java/src/test/java/com/baeldung/comparable/ComparableUnitTest.java index 2b45dd0391..e8745884b8 100644 --- a/core-java/src/test/java/com/baeldung/comparable/ComparableUnitTest.java +++ b/core-java/src/test/java/com/baeldung/comparable/ComparableUnitTest.java @@ -9,7 +9,7 @@ import java.util.List; import org.junit.Test; public class ComparableUnitTest { - + @Test public void whenUsingComparable_thenSortedList() { List footballTeam = new ArrayList(); @@ -20,8 +20,10 @@ public class ComparableUnitTest { footballTeam.add(player2); footballTeam.add(player3); Collections.sort(footballTeam); - assertEquals(footballTeam.get(0).getName(), "Steven"); - assertEquals(footballTeam.get(2).getRanking(), 67); + assertEquals(footballTeam.get(0) + .getName(), "Steven"); + assertEquals(footballTeam.get(2) + .getRanking(), 67); } } diff --git a/core-java/src/test/java/com/baeldung/comparator/ComparatorUnitTest.java b/core-java/src/test/java/com/baeldung/comparator/ComparatorUnitTest.java index 769986edec..5b7ec3bfe4 100644 --- a/core-java/src/test/java/com/baeldung/comparator/ComparatorUnitTest.java +++ b/core-java/src/test/java/com/baeldung/comparator/ComparatorUnitTest.java @@ -10,11 +10,11 @@ import org.junit.Before; import org.junit.Test; public class ComparatorUnitTest { - + List footballTeam; - + @Before - public void setUp(){ + public void setUp() { footballTeam = new ArrayList(); Player player1 = new Player(59, "John", 20); Player player2 = new Player(67, "Roger", 22); @@ -23,21 +23,25 @@ public class ComparatorUnitTest { footballTeam.add(player2); footballTeam.add(player3); } - + @Test public void whenUsingRankingComparator_thenSortedList() { PlayerRankingComparator playerComparator = new PlayerRankingComparator(); Collections.sort(footballTeam, playerComparator); - assertEquals(footballTeam.get(0).getName(), "Steven"); - assertEquals(footballTeam.get(2).getRanking(), 67); + assertEquals(footballTeam.get(0) + .getName(), "Steven"); + assertEquals(footballTeam.get(2) + .getRanking(), 67); } - + @Test public void whenUsingAgeComparator_thenSortedList() { PlayerAgeComparator playerComparator = new PlayerAgeComparator(); Collections.sort(footballTeam, playerComparator); - assertEquals(footballTeam.get(0).getName(), "John"); - assertEquals(footballTeam.get(2).getRanking(), 45); + assertEquals(footballTeam.get(0) + .getName(), "John"); + assertEquals(footballTeam.get(2) + .getRanking(), 45); } } diff --git a/core-java/src/test/java/com/baeldung/comparator/Java8ComparatorUnitTest.java b/core-java/src/test/java/com/baeldung/comparator/Java8ComparatorUnitTest.java new file mode 100644 index 0000000000..b804573b51 --- /dev/null +++ b/core-java/src/test/java/com/baeldung/comparator/Java8ComparatorUnitTest.java @@ -0,0 +1,63 @@ +package com.baeldung.comparator; + +import static org.junit.Assert.assertEquals; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; + +import org.junit.Before; +import org.junit.Test; + +public class Java8ComparatorUnitTest { + + List footballTeam; + + @Before + public void setUp() { + footballTeam = new ArrayList(); + Player player1 = new Player(59, "John", 20); + Player player2 = new Player(67, "Roger", 22); + Player player3 = new Player(45, "Steven", 24); + footballTeam.add(player1); + footballTeam.add(player2); + footballTeam.add(player3); + } + + @Test + public void whenComparing_UsingJava8_thenSorted() { + System.out.println("************** Java 8 Comaparator **************"); + Comparator byRanking = new Comparator() { + + @Override + public int compare(Player player1, Player player2) { + return player1.getRanking() - player2.getRanking(); + } + }; + + System.out.println("Before Sorting : " + footballTeam); + Collections.sort(footballTeam, byRanking); + System.out.println("After Sorting : " + footballTeam); + assertEquals(footballTeam.get(0) + .getName(), "Steven"); + assertEquals(footballTeam.get(2) + .getRanking(), 67); + } + + @Test + public void whenComparing_UsingComparatorComparing_thenSorted() { + System.out.println("********* Comaparator.comparing method *********"); + Comparator byRanking = + (Player player1, Player player2)->player1.getRanking()-player2.getRanking(); + + System.out.println("Before Sorting : " + footballTeam); + Collections.sort(footballTeam, byRanking); + System.out.println("After Sorting : " + footballTeam); + assertEquals(footballTeam.get(0) + .getName(), "Steven"); + assertEquals(footballTeam.get(2) + .getRanking(), 67); + } + +} From 6536e39622dffac0693c2c79f7debd4a192a078c Mon Sep 17 00:00:00 2001 From: Grzegorz Piwowarek Date: Mon, 27 Nov 2017 10:03:08 +0100 Subject: [PATCH 09/28] Update README.md (#3050) --- noexception/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/noexception/README.md b/noexception/README.md index d840191369..9dd4c11190 100644 --- a/noexception/README.md +++ b/noexception/README.md @@ -1,2 +1,2 @@ ### Relevant Articles: -- [Introduction to NoException](http://www.baeldung.com/intrduction-to-noexception) +- [Introduction to NoException](http://www.baeldung.com/introduction-to-noexception) From ebc25b2b41a765b3503b1de50e17c5fc8090501a Mon Sep 17 00:00:00 2001 From: Eugen Paraschiv Date: Mon, 27 Nov 2017 15:56:07 +0200 Subject: [PATCH 10/28] minor cleanup work --- bootique/dependency-reduced-pom.xml | 8 +++++++- ...yAnnotationJavaConfigMainIntegrationTest.java | 16 ++++++---------- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/bootique/dependency-reduced-pom.xml b/bootique/dependency-reduced-pom.xml index ed18f4e42a..ab09cfb7b1 100644 --- a/bootique/dependency-reduced-pom.xml +++ b/bootique/dependency-reduced-pom.xml @@ -28,8 +28,14 @@ junit junit - 3.8.1 + 4.12 test + + + hamcrest-core + org.hamcrest + + diff --git a/persistence-modules/spring-hibernate-5/src/test/java/com/baeldung/hibernate/manytomany/HibernateManyToManyAnnotationJavaConfigMainIntegrationTest.java b/persistence-modules/spring-hibernate-5/src/test/java/com/baeldung/hibernate/manytomany/HibernateManyToManyAnnotationJavaConfigMainIntegrationTest.java index 61d821e85e..614de6d3ad 100644 --- a/persistence-modules/spring-hibernate-5/src/test/java/com/baeldung/hibernate/manytomany/HibernateManyToManyAnnotationJavaConfigMainIntegrationTest.java +++ b/persistence-modules/spring-hibernate-5/src/test/java/com/baeldung/hibernate/manytomany/HibernateManyToManyAnnotationJavaConfigMainIntegrationTest.java @@ -1,6 +1,5 @@ package com.baeldung.hibernate.manytomany; - import java.util.HashSet; import java.util.Set; import org.hibernate.Session; @@ -17,10 +16,8 @@ import com.baeldung.hibernate.manytomany.model.Employee; import com.baeldung.hibernate.manytomany.model.Project; import com.baeldung.manytomany.spring.PersistenceConfig; - - @RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration(classes = {PersistenceConfig.class }, loader = AnnotationConfigContextLoader.class) +@ContextConfiguration(classes = { PersistenceConfig.class }, loader = AnnotationConfigContextLoader.class) public class HibernateManyToManyAnnotationJavaConfigMainIntegrationTest { @Autowired @@ -28,7 +25,6 @@ public class HibernateManyToManyAnnotationJavaConfigMainIntegrationTest { private Session session; - @Before public final void before() { session = sessionFactory.openSession(); @@ -43,11 +39,11 @@ public class HibernateManyToManyAnnotationJavaConfigMainIntegrationTest { @Test public final void whenEntitiesAreCreated_thenNoExceptions() { - Set projects = new HashSet(); - projects.add(new Project("IT Project")); - projects.add(new Project("Networking Project")); - session.persist(new Employee("Peter", "Oven", projects)); - session.persist(new Employee("Allan", "Norman", projects)); + Set projects = new HashSet(); + projects.add(new Project("IT Project")); + projects.add(new Project("Networking Project")); + session.persist(new Employee("Peter", "Oven", projects)); + session.persist(new Employee("Allan", "Norman", projects)); } } From bac574a7c22d2a20730ebbce70b51a7c90d46f47 Mon Sep 17 00:00:00 2001 From: Eugen Paraschiv Date: Mon, 27 Nov 2017 16:08:53 +0200 Subject: [PATCH 11/28] cleanup work after build --- undertow/dependency-reduced-pom.xml | 40 +++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 undertow/dependency-reduced-pom.xml diff --git a/undertow/dependency-reduced-pom.xml b/undertow/dependency-reduced-pom.xml new file mode 100644 index 0000000000..0654c82b74 --- /dev/null +++ b/undertow/dependency-reduced-pom.xml @@ -0,0 +1,40 @@ + + + 4.0.0 + com.baeldung.undertow + undertow + undertow + 1.0-SNAPSHOT + http://maven.apache.org + + ${project.artifactId} + + + maven-shade-plugin + + + package + + shade + + + + + + maven-jar-plugin + + + + com.baeldung.undertow.SimpleServer + + + + + + + + 1.8 + 1.8 + + + From 68eae8ebf414194419123d2e9a28e5f18517fd13 Mon Sep 17 00:00:00 2001 From: "dhrubajyoti.bhattacharjee" Date: Mon, 27 Nov 2017 21:14:31 +0530 Subject: [PATCH 12/28] BAEL-1160 Introduction to Apache Lucene-Added new sections --- lucene/pom.xml | 2 - .../baeldung/lucene/InMemoryLuceneIndex.java | 54 +++++++- .../lucene/LuceneInMemorySearchTest.java | 131 +++++++++++++++++- 3 files changed, 182 insertions(+), 5 deletions(-) diff --git a/lucene/pom.xml b/lucene/pom.xml index 42b81a7d4a..6659d9ac32 100644 --- a/lucene/pom.xml +++ b/lucene/pom.xml @@ -31,7 +31,5 @@ 4.12 test - - \ No newline at end of file diff --git a/lucene/src/main/java/com/baeldung/lucene/InMemoryLuceneIndex.java b/lucene/src/main/java/com/baeldung/lucene/InMemoryLuceneIndex.java index 40a35fad86..97b1ec7b5d 100644 --- a/lucene/src/main/java/com/baeldung/lucene/InMemoryLuceneIndex.java +++ b/lucene/src/main/java/com/baeldung/lucene/InMemoryLuceneIndex.java @@ -7,18 +7,22 @@ import java.util.List; import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.document.Document; import org.apache.lucene.document.Field; +import org.apache.lucene.document.SortedDocValuesField; import org.apache.lucene.document.TextField; import org.apache.lucene.index.DirectoryReader; import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.IndexWriterConfig; +import org.apache.lucene.index.Term; import org.apache.lucene.queryparser.classic.ParseException; import org.apache.lucene.queryparser.classic.QueryParser; import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.Query; import org.apache.lucene.search.ScoreDoc; +import org.apache.lucene.search.Sort; import org.apache.lucene.search.TopDocs; import org.apache.lucene.store.Directory; +import org.apache.lucene.util.BytesRef; public class InMemoryLuceneIndex { @@ -45,6 +49,7 @@ public class InMemoryLuceneIndex { document.add(new TextField("title", title, Field.Store.YES)); document.add(new TextField("body", body, Field.Store.YES)); + document.add(new SortedDocValuesField("title", new BytesRef(title))); writter.addDocument(document); writter.close(); @@ -73,6 +78,51 @@ public class InMemoryLuceneIndex { } + public void deleteDocument(Term term) { + try { + IndexWriterConfig indexWriterConfig = new IndexWriterConfig(analyzer); + IndexWriter writter = new IndexWriter(memoryIndex, indexWriterConfig); + writter.deleteDocuments(term); + writter.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + public List searchIndex(Query query) { + try { + IndexReader indexReader = DirectoryReader.open(memoryIndex); + IndexSearcher searcher = new IndexSearcher(indexReader); + TopDocs topDocs = searcher.search(query, 10); + List documents = new ArrayList<>(); + for (ScoreDoc scoreDoc : topDocs.scoreDocs) { + documents.add(searcher.doc(scoreDoc.doc)); + } + + return documents; + } catch (IOException e) { + e.printStackTrace(); + } + return null; + + } + + public List searchIndex(Query query, Sort sort) { + try { + IndexReader indexReader = DirectoryReader.open(memoryIndex); + IndexSearcher searcher = new IndexSearcher(indexReader); + TopDocs topDocs = searcher.search(query, 10, sort); + List documents = new ArrayList<>(); + for (ScoreDoc scoreDoc : topDocs.scoreDocs) { + documents.add(searcher.doc(scoreDoc.doc)); + } + + return documents; + } catch (IOException e) { + e.printStackTrace(); + } + return null; + + } + } - - diff --git a/lucene/src/test/java/com/baeldung/lucene/LuceneInMemorySearchTest.java b/lucene/src/test/java/com/baeldung/lucene/LuceneInMemorySearchTest.java index c3a498a4b8..acf688cb99 100644 --- a/lucene/src/test/java/com/baeldung/lucene/LuceneInMemorySearchTest.java +++ b/lucene/src/test/java/com/baeldung/lucene/LuceneInMemorySearchTest.java @@ -4,7 +4,19 @@ import java.util.List; import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.document.Document; +import org.apache.lucene.index.Term; +import org.apache.lucene.search.BooleanClause; +import org.apache.lucene.search.BooleanQuery; +import org.apache.lucene.search.FuzzyQuery; +import org.apache.lucene.search.PhraseQuery; +import org.apache.lucene.search.PrefixQuery; +import org.apache.lucene.search.Query; +import org.apache.lucene.search.Sort; +import org.apache.lucene.search.SortField; +import org.apache.lucene.search.TermQuery; +import org.apache.lucene.search.WildcardQuery; import org.apache.lucene.store.RAMDirectory; +import org.apache.lucene.util.BytesRef; import org.junit.Assert; import org.junit.Test; @@ -20,4 +32,121 @@ public class LuceneInMemorySearchTest { Assert.assertEquals("Hello world", documents.get(0).get("title")); } -} + @Test + public void givenTermQueryWhenFetchedDocumentThenCorrect() { + InMemoryLuceneIndex inMemoryLuceneIndex = new InMemoryLuceneIndex(new RAMDirectory(), new StandardAnalyzer()); + inMemoryLuceneIndex.indexDocument("activity", "running in track"); + inMemoryLuceneIndex.indexDocument("activity", "Cars are running on road"); + + Term term = new Term("body", "running"); + Query query = new TermQuery(term); + + List documents = inMemoryLuceneIndex.searchIndex(query); + Assert.assertEquals(2, documents.size()); + } + + @Test + public void givenPrefixQueryWhenFetchedDocumentThenCorrect() { + InMemoryLuceneIndex inMemoryLuceneIndex = new InMemoryLuceneIndex(new RAMDirectory(), new StandardAnalyzer()); + inMemoryLuceneIndex.indexDocument("article", "Lucene introduction"); + inMemoryLuceneIndex.indexDocument("article", "Introduction to Lucene"); + + Term term = new Term("body", "intro"); + Query query = new PrefixQuery(term); + + List documents = inMemoryLuceneIndex.searchIndex(query); + Assert.assertEquals(2, documents.size()); + } + + @Test + public void givenBooleanQueryWhenFetchedDocumentThenCorrect() { + InMemoryLuceneIndex inMemoryLuceneIndex = new InMemoryLuceneIndex(new RAMDirectory(), new StandardAnalyzer()); + inMemoryLuceneIndex.indexDocument("Destination", "Las Vegas singapore car"); + inMemoryLuceneIndex.indexDocument("Commutes in singapore", "Bus Car Bikes"); + + Term term1 = new Term("body", "singapore"); + Term term2 = new Term("body", "car"); + + TermQuery query1 = new TermQuery(term1); + TermQuery query2 = new TermQuery(term2); + + BooleanQuery booleanQuery = new BooleanQuery.Builder().add(query1, BooleanClause.Occur.MUST) + .add(query2, BooleanClause.Occur.MUST).build(); + + List documents = inMemoryLuceneIndex.searchIndex(booleanQuery); + Assert.assertEquals(1, documents.size()); + } + + @Test + public void givenPhraseQueryWhenFetchedDocumentThenCorrect() { + InMemoryLuceneIndex inMemoryLuceneIndex = new InMemoryLuceneIndex(new RAMDirectory(), new StandardAnalyzer()); + inMemoryLuceneIndex.indexDocument("quotes", "A rose by any other name would smell as sweet."); + + Query query = new PhraseQuery(1, "body", new BytesRef("smell"), new BytesRef("sweet")); + List documents = inMemoryLuceneIndex.searchIndex(query); + + Assert.assertEquals(1, documents.size()); + } + + @Test + public void givenFuzzyQueryWhenFetchedDocumentThenCorrect() { + InMemoryLuceneIndex inMemoryLuceneIndex = new InMemoryLuceneIndex(new RAMDirectory(), new StandardAnalyzer()); + inMemoryLuceneIndex.indexDocument("article", "Halloween Festival"); + inMemoryLuceneIndex.indexDocument("decoration", "Decorations for Halloween"); + + Term term = new Term("body", "hallowen"); + Query query = new FuzzyQuery(term); + + List documents = inMemoryLuceneIndex.searchIndex(query); + Assert.assertEquals(2, documents.size()); + } + + @Test + public void givenWildCardQueryWhenFetchedDocumentThenCorrect() { + InMemoryLuceneIndex inMemoryLuceneIndex = new InMemoryLuceneIndex(new RAMDirectory(), new StandardAnalyzer()); + inMemoryLuceneIndex.indexDocument("article", "Lucene introduction"); + inMemoryLuceneIndex.indexDocument("article", "Introducing Lucene with Spring"); + + Term term = new Term("body", "intro*"); + Query query = new WildcardQuery(term); + + List documents = inMemoryLuceneIndex.searchIndex(query); + Assert.assertEquals(2, documents.size()); + } + + @Test + public void givenSortFieldWhenSortedThenCorrect() { + InMemoryLuceneIndex inMemoryLuceneIndex = new InMemoryLuceneIndex(new RAMDirectory(), new StandardAnalyzer()); + inMemoryLuceneIndex.indexDocument("Ganges", "River in India"); + inMemoryLuceneIndex.indexDocument("Mekong", "This river flows in south Asia"); + inMemoryLuceneIndex.indexDocument("Amazon", "Rain forest river"); + inMemoryLuceneIndex.indexDocument("Rhine", "Belongs to Europe"); + inMemoryLuceneIndex.indexDocument("Nile", "Longest River"); + + Term term = new Term("body", "river"); + Query query = new WildcardQuery(term); + + SortField sortField = new SortField("title", SortField.Type.STRING_VAL, false); + Sort sortByTitle = new Sort(sortField); + + List documents = inMemoryLuceneIndex.searchIndex(query, sortByTitle); + Assert.assertEquals(4, documents.size()); + Assert.assertEquals("Amazon", documents.get(0).getField("title").stringValue()); + } + + @Test + public void whenDocumentDeletedThenCorrect() { + InMemoryLuceneIndex inMemoryLuceneIndex = new InMemoryLuceneIndex(new RAMDirectory(), new StandardAnalyzer()); + inMemoryLuceneIndex.indexDocument("Ganges", "River in India"); + inMemoryLuceneIndex.indexDocument("Mekong", "This river flows in south Asia"); + + Term term = new Term("title", "ganges"); + inMemoryLuceneIndex.deleteDocument(term); + + Query query = new TermQuery(term); + + List documents = inMemoryLuceneIndex.searchIndex(query); + Assert.assertEquals(0, documents.size()); + } + +} \ No newline at end of file From 7f2ace0da913567513f9c4d6a2da15639054be86 Mon Sep 17 00:00:00 2001 From: Grzegorz Piwowarek Date: Mon, 27 Nov 2017 21:02:42 +0100 Subject: [PATCH 13/28] FilesTest fix (#3146) * FilesTest fix * StopThreadTest fix --- .../concurrent/stopping/StopThreadTest.java | 14 ++-- .../baeldung/file/FileOutputStreamTest.java | 36 ---------- .../java/com/baeldung/file/FileUtilsTest.java | 37 ---------- .../com/baeldung/file/FileWriterTest.java | 40 ----------- .../java/com/baeldung/file/FilesTest.java | 68 +++++++++++++++++-- .../java/com/baeldung/file/GuavaTest.java | 48 ------------- 6 files changed, 72 insertions(+), 171 deletions(-) delete mode 100644 core-java/src/test/java/com/baeldung/file/FileOutputStreamTest.java delete mode 100644 core-java/src/test/java/com/baeldung/file/FileUtilsTest.java delete mode 100644 core-java/src/test/java/com/baeldung/file/FileWriterTest.java delete mode 100644 core-java/src/test/java/com/baeldung/file/GuavaTest.java diff --git a/core-java-concurrency/src/test/java/com/baeldung/concurrent/stopping/StopThreadTest.java b/core-java-concurrency/src/test/java/com/baeldung/concurrent/stopping/StopThreadTest.java index 8c1bdbf787..70854f013f 100644 --- a/core-java-concurrency/src/test/java/com/baeldung/concurrent/stopping/StopThreadTest.java +++ b/core-java-concurrency/src/test/java/com/baeldung/concurrent/stopping/StopThreadTest.java @@ -1,7 +1,11 @@ package com.baeldung.concurrent.stopping; +import com.jayway.awaitility.Awaitility; import org.junit.Test; +import java.util.concurrent.TimeUnit; + +import static com.jayway.awaitility.Awaitility.await; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; @@ -22,11 +26,10 @@ public class StopThreadTest { // Stop it and make sure the flags have been reversed controlSubThread.stop(); - Thread.sleep(interval); - assertTrue(controlSubThread.isStopped()); + await() + .until(() -> assertTrue(controlSubThread.isStopped())); } - @Test public void whenInterruptedThreadIsStopped() throws InterruptedException { @@ -44,7 +47,8 @@ public class StopThreadTest { controlSubThread.interrupt(); // Wait less than the time we would normally sleep, and make sure we exited. - Thread.sleep(interval/10); - assertTrue(controlSubThread.isStopped()); + Awaitility.await() + .atMost(interval/ 10, TimeUnit.MILLISECONDS) + .until(controlSubThread::isStopped); } } diff --git a/core-java/src/test/java/com/baeldung/file/FileOutputStreamTest.java b/core-java/src/test/java/com/baeldung/file/FileOutputStreamTest.java deleted file mode 100644 index 451c1b4c4d..0000000000 --- a/core-java/src/test/java/com/baeldung/file/FileOutputStreamTest.java +++ /dev/null @@ -1,36 +0,0 @@ -package com.baeldung.file; - -import static org.assertj.core.api.Assertions.assertThat; - -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.PrintWriter; - -import org.junit.After; -import org.junit.Test; - -import com.baeldung.util.StreamUtils; - -public class FileOutputStreamTest { - - public static final String fileName = "src/main/resources/countries.properties"; - - @Test - public void whenAppendToFileUsingFileOutputStream_thenCorrect() throws Exception { - FileOutputStream fos = new FileOutputStream(fileName, true); - fos.write("Spain\r\n".getBytes()); - fos.close(); - - assertThat(StreamUtils.getStringFromInputStream( - new FileInputStream(fileName))) - .isEqualTo("UK\r\n" + "US\r\n" + "Germany\r\n" + "Spain\r\n"); - } - - @After - public void revertFile() throws IOException { - PrintWriter writer = new PrintWriter(fileName); - writer.print("UK\r\n" + "US\r\n" + "Germany\r\n"); - writer.close(); - } -} diff --git a/core-java/src/test/java/com/baeldung/file/FileUtilsTest.java b/core-java/src/test/java/com/baeldung/file/FileUtilsTest.java deleted file mode 100644 index 9ee8726575..0000000000 --- a/core-java/src/test/java/com/baeldung/file/FileUtilsTest.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.baeldung.file; - -import static org.assertj.core.api.Assertions.assertThat; - -import java.io.File; -import java.io.FileInputStream; -import java.io.IOException; -import java.io.PrintWriter; -import java.nio.charset.StandardCharsets; - -import org.apache.commons.io.FileUtils; -import org.junit.After; -import org.junit.Test; - -import com.baeldung.util.StreamUtils; - -public class FileUtilsTest { - - public static final String fileName = "src/main/resources/countries.properties"; - - @Test - public void whenAppendToFileUsingFiles_thenCorrect() throws IOException { - File file = new File(fileName); - FileUtils.writeStringToFile(file, "Spain\r\n", StandardCharsets.UTF_8, true); - - assertThat(StreamUtils.getStringFromInputStream( - new FileInputStream(fileName))) - .isEqualTo("UK\r\n" + "US\r\n" + "Germany\r\n" + "Spain\r\n"); - } - - @After - public void revertFile() throws IOException { - PrintWriter writer = new PrintWriter(fileName); - writer.print("UK\r\n" + "US\r\n" + "Germany\r\n"); - writer.close(); - } -} diff --git a/core-java/src/test/java/com/baeldung/file/FileWriterTest.java b/core-java/src/test/java/com/baeldung/file/FileWriterTest.java deleted file mode 100644 index 8d2ce4310e..0000000000 --- a/core-java/src/test/java/com/baeldung/file/FileWriterTest.java +++ /dev/null @@ -1,40 +0,0 @@ -package com.baeldung.file; - -import static org.assertj.core.api.Assertions.assertThat; - -import java.io.BufferedWriter; -import java.io.FileInputStream; -import java.io.FileWriter; -import java.io.IOException; -import java.io.PrintWriter; - -import org.junit.After; -import org.junit.Test; - -import com.baeldung.util.StreamUtils; - -public class FileWriterTest { - - public static final String fileName = "src/main/resources/countries.properties"; - - @Test - public void whenAppendToFileUsingFileWriter_thenCorrect() throws IOException { - FileWriter fw = new FileWriter(fileName, true); - BufferedWriter bw = new BufferedWriter(fw); - bw.write("Spain"); - bw.newLine(); - bw.close(); - - assertThat( - StreamUtils.getStringFromInputStream( - new FileInputStream(fileName))) - .isEqualTo("UK\r\n" + "US\r\n" + "Germany\r\n" + "Spain\n"); - } - - @After - public void revertFile() throws IOException { - PrintWriter writer = new PrintWriter(fileName); - writer.print("UK\r\n" + "US\r\n" + "Germany\r\n"); - writer.close(); - } -} diff --git a/core-java/src/test/java/com/baeldung/file/FilesTest.java b/core-java/src/test/java/com/baeldung/file/FilesTest.java index bd39d004d3..e17e8580aa 100644 --- a/core-java/src/test/java/com/baeldung/file/FilesTest.java +++ b/core-java/src/test/java/com/baeldung/file/FilesTest.java @@ -2,14 +2,24 @@ package com.baeldung.file; import static org.assertj.core.api.Assertions.assertThat; +import java.io.BufferedWriter; +import java.io.File; import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; +import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Paths; import java.nio.file.StandardOpenOption; +import com.google.common.base.Charsets; +import com.google.common.io.CharSink; +import com.google.common.io.FileWriteMode; +import org.apache.commons.io.FileUtils; import org.junit.After; +import org.junit.Before; import org.junit.Test; import com.baeldung.util.StreamUtils; @@ -18,6 +28,26 @@ public class FilesTest { public static final String fileName = "src/main/resources/countries.properties"; + @Before + @After + public void setup() throws Exception { + PrintWriter writer = new PrintWriter(fileName); + writer.print("UK\r\n" + "US\r\n" + "Germany\r\n"); + writer.close(); + } + + @Test + public void whenAppendToFileUsingGuava_thenCorrect() throws IOException { + File file = new File(fileName); + CharSink chs = com.google.common.io.Files.asCharSink(file, Charsets.UTF_8, FileWriteMode.APPEND); + chs.write("Spain\r\n"); + + assertThat(StreamUtils.getStringFromInputStream( + new FileInputStream(fileName))) + .isEqualTo("UK\r\n" + "US\r\n" + "Germany\r\n" + "Spain\r\n"); + } + + @Test public void whenAppendToFileUsingFiles_thenCorrect() throws IOException { Files.write(Paths.get(fileName), "Spain\r\n".getBytes(), StandardOpenOption.APPEND); @@ -27,10 +57,38 @@ public class FilesTest { .isEqualTo("UK\r\n" + "US\r\n" + "Germany\r\n" + "Spain\r\n"); } - @After - public void revertFile() throws IOException { - PrintWriter writer = new PrintWriter(fileName); - writer.print("UK\r\n" + "US\r\n" + "Germany\r\n"); - writer.close(); + @Test + public void whenAppendToFileUsingFileUtils_thenCorrect() throws IOException { + File file = new File(fileName); + FileUtils.writeStringToFile(file, "Spain\r\n", StandardCharsets.UTF_8, true); + + assertThat(StreamUtils.getStringFromInputStream( + new FileInputStream(fileName))) + .isEqualTo("UK\r\n" + "US\r\n" + "Germany\r\n" + "Spain\r\n"); + } + + @Test + public void whenAppendToFileUsingFileOutputStream_thenCorrect() throws Exception { + FileOutputStream fos = new FileOutputStream(fileName, true); + fos.write("Spain\r\n".getBytes()); + fos.close(); + + assertThat(StreamUtils.getStringFromInputStream( + new FileInputStream(fileName))) + .isEqualTo("UK\r\n" + "US\r\n" + "Germany\r\n" + "Spain\r\n"); + } + + @Test + public void whenAppendToFileUsingFileWriter_thenCorrect() throws IOException { + FileWriter fw = new FileWriter(fileName, true); + BufferedWriter bw = new BufferedWriter(fw); + bw.write("Spain"); + bw.newLine(); + bw.close(); + + assertThat( + StreamUtils.getStringFromInputStream( + new FileInputStream(fileName))) + .isEqualTo("UK\r\n" + "US\r\n" + "Germany\r\n" + "Spain\n"); } } diff --git a/core-java/src/test/java/com/baeldung/file/GuavaTest.java b/core-java/src/test/java/com/baeldung/file/GuavaTest.java deleted file mode 100644 index 5a7ec6c4a8..0000000000 --- a/core-java/src/test/java/com/baeldung/file/GuavaTest.java +++ /dev/null @@ -1,48 +0,0 @@ -package com.baeldung.file; - -import static org.assertj.core.api.Assertions.assertThat; - -import java.io.File; -import java.io.FileInputStream; -import java.io.IOException; -import java.io.PrintWriter; - -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - -import com.baeldung.util.StreamUtils; -import com.google.common.base.Charsets; -import com.google.common.io.CharSink; -import com.google.common.io.FileWriteMode; -import com.google.common.io.Files; - -public class GuavaTest { - - public static final String fileName = "src/main/resources/countries.properties"; - - @Before - public void setup() throws Exception { - PrintWriter writer = new PrintWriter(fileName); - writer.print("UK\r\n" + "US\r\n" + "Germany\r\n"); - writer.close(); - } - - @Test - public void whenAppendToFileUsingGuava_thenCorrect() throws IOException { - File file = new File(fileName); - CharSink chs = Files.asCharSink(file, Charsets.UTF_8, FileWriteMode.APPEND); - chs.write("Spain\r\n"); - - assertThat(StreamUtils.getStringFromInputStream( - new FileInputStream(fileName))) - .isEqualTo("UK\r\n" + "US\r\n" + "Germany\r\n" + "Spain\r\n"); - } - - @After - public void revertFile() throws IOException { - PrintWriter writer = new PrintWriter(fileName); - writer.print("UK\r\n" + "US\r\n" + "Germany\r\n"); - writer.close(); - } -} From 5f57a6cb4bfb7aecd3bba2245f6caf472015279d Mon Sep 17 00:00:00 2001 From: Eugen Paraschiv Date: Tue, 28 Nov 2017 12:36:54 +0200 Subject: [PATCH 14/28] separating the sun specific functionality into a new module --- core-java-sun/.gitignore | 26 + core-java-sun/README.md | 123 +++++ core-java-sun/pom.xml | 489 ++++++++++++++++++ .../src/main/java/com/baeldung/.gitignore | 13 + .../src/main/java/com/baeldung/README.md | 2 + .../java/com/baeldung/javac/Positive.java | 0 .../com/baeldung/javac/SampleJavacPlugin.java | 0 .../src/main/resources/log4j.properties | 6 + core-java-sun/src/main/resources/logback.xml | 19 + .../SampleJavacPluginIntegrationTest.java | 0 .../com/baeldung/javac/SimpleClassFile.java | 0 .../com/baeldung/javac/SimpleFileManager.java | 0 .../com/baeldung/javac/SimpleSourceFile.java | 0 .../java/com/baeldung/javac/TestCompiler.java | 0 .../java/com/baeldung/javac/TestRunner.java | 0 core-java-sun/src/test/resources/.gitignore | 13 + core-java/pom.xml | 12 - 17 files changed, 691 insertions(+), 12 deletions(-) create mode 100644 core-java-sun/.gitignore create mode 100644 core-java-sun/README.md create mode 100644 core-java-sun/pom.xml create mode 100644 core-java-sun/src/main/java/com/baeldung/.gitignore create mode 100644 core-java-sun/src/main/java/com/baeldung/README.md rename {core-java => core-java-sun}/src/main/java/com/baeldung/javac/Positive.java (100%) rename {core-java => core-java-sun}/src/main/java/com/baeldung/javac/SampleJavacPlugin.java (100%) create mode 100644 core-java-sun/src/main/resources/log4j.properties create mode 100644 core-java-sun/src/main/resources/logback.xml rename {core-java => core-java-sun}/src/test/java/com/baeldung/javac/SampleJavacPluginIntegrationTest.java (100%) rename {core-java => core-java-sun}/src/test/java/com/baeldung/javac/SimpleClassFile.java (100%) rename {core-java => core-java-sun}/src/test/java/com/baeldung/javac/SimpleFileManager.java (100%) rename {core-java => core-java-sun}/src/test/java/com/baeldung/javac/SimpleSourceFile.java (100%) rename {core-java => core-java-sun}/src/test/java/com/baeldung/javac/TestCompiler.java (100%) rename {core-java => core-java-sun}/src/test/java/com/baeldung/javac/TestRunner.java (100%) create mode 100644 core-java-sun/src/test/resources/.gitignore diff --git a/core-java-sun/.gitignore b/core-java-sun/.gitignore new file mode 100644 index 0000000000..3de4cc647e --- /dev/null +++ b/core-java-sun/.gitignore @@ -0,0 +1,26 @@ +*.class + +0.* + +#folders# +/target +/neoDb* +/data +/src/main/webapp/WEB-INF/classes +*/META-INF/* +.resourceCache + +# Packaged files # +*.jar +*.war +*.ear + +# Files generated by integration tests +*.txt +backup-pom.xml +/bin/ +/temp + +#IntelliJ specific +.idea/ +*.iml \ No newline at end of file diff --git a/core-java-sun/README.md b/core-java-sun/README.md new file mode 100644 index 0000000000..1feee4126e --- /dev/null +++ b/core-java-sun/README.md @@ -0,0 +1,123 @@ +========= + +## Core Java Cookbooks and Examples + +### Relevant Articles: +- [Immutable ArrayList in Java](http://www.baeldung.com/java-immutable-list) +- [Java - Reading a Large File Efficiently](http://www.baeldung.com/java-read-lines-large-file) +- [Java InputStream to String](http://www.baeldung.com/convert-input-stream-to-string) +- [Converting between an Array and a List in Java](http://www.baeldung.com/convert-array-to-list-and-list-to-array) +- [Converting between an Array and a Set in Java](http://www.baeldung.com/convert-array-to-set-and-set-to-array) +- [Converting between a List and a Set in Java](http://www.baeldung.com/convert-list-to-set-and-set-to-list) +- [Convert a Map to an Array, List or Set in Java](http://www.baeldung.com/convert-map-values-to-array-list-set) +- [Java – Write to File](http://www.baeldung.com/java-write-to-file) +- [Java - Convert File to InputStream](http://www.baeldung.com/convert-file-to-input-stream) +- [Java – Random Long, Float, Integer and Double](http://www.baeldung.com/java-generate-random-long-float-integer-double) +- [Java – Generate Random String](http://www.baeldung.com/java-random-string) +- [Java Scanner](http://www.baeldung.com/java-scanner) +- [Java Timer](http://www.baeldung.com/java-timer-and-timertask) +- [Java – Byte Array to Writer](http://www.baeldung.com/java-convert-byte-array-to-writer) +- [How to Run a Shell Command in Java](http://www.baeldung.com/run-shell-command-in-java) +- [MD5 Hashing in Java](http://www.baeldung.com/java-md5) +- [Guide to the Java ArrayList](http://www.baeldung.com/java-arraylist) +- [Guide to Java Reflection](http://www.baeldung.com/java-reflection) +- [A Guide to Java Sockets](http://www.baeldung.com/a-guide-to-java-sockets) +- [Convert char to String in Java](http://www.baeldung.com/java-convert-char-to-string) +- [Random List Element](http://www.baeldung.com/java-random-list-element) +- [Convert String to int or Integer in Java](http://www.baeldung.com/java-convert-string-to-int-or-integer) +- [Java – Directory Size](http://www.baeldung.com/java-folder-size) +- [Java – Try with Resources](http://www.baeldung.com/java-try-with-resources) +- [Guide to the Fork/Join Framework in Java](http://www.baeldung.com/java-fork-join) +- [How to Print Screen in Java](http://www.baeldung.com/print-screen-in-java) +- [How to Convert String to different data types in Java](http://www.baeldung.com/java-string-conversions) +- [Introduction to Java Generics](http://www.baeldung.com/java-generics) +- [Generate equals() and hashCode() with Eclipse](http://www.baeldung.com/java-eclipse-equals-and-hashcode) +- [A Guide To Java Regular Expressions API](http://www.baeldung.com/regular-expressions-java) +- [Sorting in Java](http://www.baeldung.com/java-sorting) +- [Getting Started with Java Properties](http://www.baeldung.com/java-properties) +- [Grep in Java](http://www.baeldung.com/grep-in-java) +- [Java - Combine Multiple Collections](http://www.baeldung.com/java-combine-multiple-collections) +- [Simulated Annealing for Travelling Salesman Problem](http://www.baeldung.com/java-simulated-annealing-for-traveling-salesman) +- [Slope One Algorithm: Collaborative Filtering Recommendation Systems](http://www.baeldung.com/java-collaborative-filtering-recommendations) +- [Differences Between the Java WatchService API and the Apache Commons IO Monitor Library](http://www.baeldung.com/java-watchservice-vs-apache-commons-io-monitor-library) +- [Pattern Search with Grep in Java](http://www.baeldung.com/grep-in-java) +- [URL Encoding and Decoding in Java](http://www.baeldung.com/java-url-encoding-decoding) +- [Calculate the Size of a File in Java](http://www.baeldung.com/java-file-size) +- [The Basics of Java Generics](http://www.baeldung.com/java-generics) +- [The Traveling Salesman Problem in Java](http://www.baeldung.com/java-simulated-annealing-for-traveling-salesman) +- [How to Create an Executable JAR with Maven](http://www.baeldung.com/executable-jar-with-maven) +- [How to Design a Genetic Algorithm in Java](http://www.baeldung.com/java-genetic-algorithm) +- [Guide to WeakHashMap in Java](http://www.baeldung.com/java-weakhashmap) +- [Spring Security – Cache Control Headers](http://www.baeldung.com/spring-security-cache-control-headers) +- [Basic Introduction to JMX](http://www.baeldung.com/java-management-extensions) +- [AWS Lambda With Java](http://www.baeldung.com/java-aws-lambda) +- [Introduction to Nashorn](http://www.baeldung.com/java-nashorn) +- [Guide to the Guava BiMap](http://www.baeldung.com/guava-bimap) +- [Iterable to Stream in Java](http://www.baeldung.com/java-iterable-to-stream) +- [Chained Exceptions in Java](http://www.baeldung.com/java-chained-exceptions) +- [The Java HashMap Under the Hood](http://www.baeldung.com/java-hashmap) +- [A Guide to LinkedHashMap in Java](http://www.baeldung.com/java-linked-hashmap) +- [A Guide to TreeMap in Java](http://www.baeldung.com/java-treemap) +- [A Quick JUnit vs TestNG Comparison](http://www.baeldung.com/junit-vs-testng) +- [Java Primitive Conversions](http://www.baeldung.com/java-primitive-conversions) +- [Java Money and the Currency API](http://www.baeldung.com/java-money-and-currency) +- [Removing all nulls from a List in Java](http://www.baeldung.com/java-remove-nulls-from-list) +- [Removing all duplicates from a List in Java](http://www.baeldung.com/java-remove-duplicates-from-list) +- [Using Math.pow in Java](http://www.baeldung.com/java-math-pow) +- [Converting Strings to Enums in Java](http://www.baeldung.com/java-string-to-enum) +- [Flattening Nested Collections in Java](http://www.baeldung.com/java-flatten-nested-collections) +- [Quick Guide to the Java StringTokenizer](http://www.baeldung.com/java-stringtokenizer) +- [JVM Log Forging](http://www.baeldung.com/jvm-log-forging) +- [Guide to sun.misc.Unsafe](http://www.baeldung.com/java-unsafe) +- [HashSet and TreeSet Comparison](http://www.baeldung.com/java-hashset-vs-treeset) +- [How to Perform a Simple HTTP Request in Java](http://www.baeldung.com/java-http-request) +- [Call Methods at Runtime Using Java Reflection](http://www.baeldung.com/java-method-reflection) +- [Guide to UUID in JAVA](http://www.baeldung.com/guide-to-uuid-in-java) +- [Comparing getPath(), getAbsolutePath(), and getCanonicalPath() in Java](http://www.baeldung.com/java-path) +- [How to Add a Single Element to a Stream](http://www.baeldung.com/java-stream-append-prepend) +- [Iterating Over Enum Values in Java](http://www.baeldung.com/java-enum-iteration) +- [Kotlin Java Interoperability](http://www.baeldung.com/kotlin-java-interoperability) +- [Using Java MappedByteBuffer](http://www.baeldung.com/java-mapped-byte-buffer) +- [How to Round a Number to N Decimal Places in Java](http://www.baeldung.com/java-round-decimal-number) +- [Changing Annotation Parameters At Runtime](http://www.baeldung.com/java-reflection-change-annotation-params) +- [How to Find all Getters Returning Null](http://www.baeldung.com/java-getters-returning-null) +- [Converting String to Stream of chars](http://www.baeldung.com/java-string-to-stream) +- [Changing the Order in a Sum Operation Can Produce Different Results?](http://www.baeldung.com/java-floating-point-sum-order) +- [How to Get a Name of a Method Being Executed?](http://www.baeldung.com/java-name-of-executing-method) +- [Iterate over a Map in Java](http://www.baeldung.com/java-iterate-map) +- [Dynamic Proxies in Java](http://www.baeldung.com/java-dynamic-proxies) +- [How to Copy an Array in Java](http://www.baeldung.com/java-array-copy) +- [Introduction to JDBC](http://www.baeldung.com/java-jdbc) +- [Period and Duration in Java](http://www.baeldung.com/java-period-duration) +- [Converting a Stack Trace to a String in Java](http://www.baeldung.com/java-stacktrace-to-string) +- [Count Occurrences of a Char in a String](http://www.baeldung.com/java-count-chars) +- [Java Double Brace Initialization](http://www.baeldung.com/java-double-brace-initialization) +- [The StackOverflowError in Java](http://www.baeldung.com/java-stack-overflow-error) +- [Split a String in Java](http://www.baeldung.com/java-split-string) +- [Introduction to Java Serialization](http://www.baeldung.com/java-serialization) +- [How to Remove the Last Character of a String?](http://www.baeldung.com/java-remove-last-character-of-string) +- [ClassNotFoundException vs NoClassDefFoundError](http://www.baeldung.com/java-classnotfoundexception-and-noclassdeffounderror) +- [Guide to UUID in Java](http://www.baeldung.com/java-uuid) +- [Guide to Escaping Characters in Java RegExps](http://www.baeldung.com/java-regexp-escape-char) +- [Guide to hashCode() in Java](http://www.baeldung.com/java-hashcode) +- [Collect a Java Stream to an Immutable Collection](http://www.baeldung.com/java-stream-immutable-collection) +- [Difference between URL and URI](http://www.baeldung.com/java-url-vs-uri) +- [Broadcasting and Multicasting in Java](http://www.baeldung.com/java-broadcast-multicast) +- [Converting a List to String in Java](http://www.baeldung.com/java-list-to-string) +- [CharSequence vs. String in Java](http://www.baeldung.com/java-char-sequence-string) +- [Period and Duration in Java](http://www.baeldung.com/java-period-duration) +- [Guide to the Diamond Operator in Java](http://www.baeldung.com/java-diamond-operator) +- [Singletons in Java](http://www.baeldung.com/java-singleton) +- [“Sneaky Throws” in Java](http://www.baeldung.com/java-sneaky-throws) +- [OutOfMemoryError: GC Overhead Limit Exceeded](http://www.baeldung.com/java-gc-overhead-limit-exceeded) +- [How to Iterate Over a Stream With Indices](http://www.baeldung.com/java-stream-indices) +- [StringBuilder and StringBuffer in Java](http://www.baeldung.com/java-string-builder-string-buffer) +- [Number of Digits in an Integer in Java](http://www.baeldung.com/java-number-of-digits-in-int) +- [Proxy, Decorator, Adapter and Bridge Patterns](http://www.baeldung.com/java-structural-design-patterns) +- [Creating a Java Compiler Plugin](http://www.baeldung.com/java-build-compiler-plugin) +- [A Guide to the Static Keyword in Java](http://www.baeldung.com/java-static) +- [Initializing Arrays in Java](http://www.baeldung.com/java-initialize-array) +- [Guide to Java String Pool](http://www.baeldung.com/java-string-pool) +- [Copy a File with Java](http://www.baeldung.com/java-copy-file) +- [Introduction to Creational Design Patterns](http://www.baeldung.com/creational-design-patterns) + diff --git a/core-java-sun/pom.xml b/core-java-sun/pom.xml new file mode 100644 index 0000000000..3997f47d19 --- /dev/null +++ b/core-java-sun/pom.xml @@ -0,0 +1,489 @@ + + 4.0.0 + com.baeldung + core-java-sun + 0.1.0-SNAPSHOT + jar + + core-java-sun + + + + + + net.sourceforge.collections + collections-generic + ${collections-generic.version} + + + com.google.guava + guava + ${guava.version} + + + + org.apache.commons + commons-collections4 + ${commons-collections4.version} + + + + commons-io + commons-io + ${commons-io.version} + + + + org.apache.commons + commons-lang3 + ${commons-lang3.version} + + + + org.apache.commons + commons-math3 + ${commons-math3.version} + + + + org.decimal4j + decimal4j + ${decimal4j.version} + + + + org.bouncycastle + bcprov-jdk15on + ${bouncycastle.version} + + + + org.unix4j + unix4j-command + ${unix4j.version} + + + + com.googlecode.grep4j + grep4j + ${grep4j.version} + + + + + + + com.fasterxml.jackson.core + jackson-databind + ${jackson.version} + + + + + log4j + log4j + 1.2.17 + + + org.slf4j + slf4j-api + ${org.slf4j.version} + + + ch.qos.logback + logback-classic + ${logback.version} + + + + org.slf4j + jcl-over-slf4j + ${org.slf4j.version} + + + + org.slf4j + log4j-over-slf4j + ${org.slf4j.version} + + + org.projectlombok + lombok + ${lombok.version} + provided + + + + + + org.hamcrest + hamcrest-all + 1.3 + test + + + + junit + junit + ${junit.version} + test + + + + org.hamcrest + hamcrest-core + ${org.hamcrest.version} + test + + + org.hamcrest + hamcrest-library + ${org.hamcrest.version} + test + + + + org.assertj + assertj-core + ${assertj.version} + test + + + + org.mockito + mockito-core + ${mockito.version} + test + + + com.jayway.awaitility + awaitility + ${avaitility.version} + test + + + + commons-codec + commons-codec + ${commons-codec.version} + + + + org.javamoney + moneta + 1.1 + + + + org.owasp.esapi + esapi + 2.1.0.1 + + + + com.sun.messaging.mq + fscontext + ${fscontext.version} + + + com.codepoetics + protonpack + ${protonpack.version} + + + one.util + streamex + ${streamex.version} + + + io.vavr + vavr + ${vavr.version} + + + org.openjdk.jmh + jmh-core + 1.19 + + + org.openjdk.jmh + jmh-generator-annprocess + 1.19 + + + org.springframework + spring-web + 4.3.4.RELEASE + + + com.sun + tools + 1.8.0 + system + ${java.home}/../lib/tools.jar + + + + + core-java + + + src/main/resources + true + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + ${maven-compiler-plugin.version} + + 1.8 + 1.8 + + + + + org.apache.maven.plugins + maven-surefire-plugin + + + **/*LiveTest.java + **/*IntegrationTest.java + **/*LongRunningUnitTest.java + **/*ManualTest.java + + + + + + + org.apache.maven.plugins + maven-dependency-plugin + + + copy-dependencies + prepare-package + + copy-dependencies + + + ${project.build.directory}/libs + + + + + + + org.apache.maven.plugins + maven-jar-plugin + + + + true + libs/ + org.baeldung.executable.ExecutableMavenJar + + + + + + + org.apache.maven.plugins + maven-assembly-plugin + + + package + + single + + + ${project.basedir} + + + org.baeldung.executable.ExecutableMavenJar + + + + jar-with-dependencies + + + + + + + + org.apache.maven.plugins + maven-shade-plugin + + + + shade + + + true + + + org.baeldung.executable.ExecutableMavenJar + + + + + + + + + com.jolira + onejar-maven-plugin + + + + org.baeldung.executable.ExecutableMavenJar + true + ${project.build.finalName}-onejar.${project.packaging} + + + one-jar + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + repackage + + + spring-boot + org.baeldung.executable.ExecutableMavenJar + + + + + + + org.codehaus.mojo + exec-maven-plugin + 1.6.0 + + java + com.baeldung.outofmemoryerror.OutOfMemoryGCLimitExceed + + -Xmx300m + -XX:+UseParallelGC + -classpath + + com.baeldung.outofmemoryerror.OutOfMemoryGCLimitExceed + + + + + + + + + + + + integration + + + + org.apache.maven.plugins + maven-surefire-plugin + + + integration-test + + test + + + + **/*ManualTest.java + + + **/*IntegrationTest.java + + + + + + + json + + + + + org.codehaus.mojo + exec-maven-plugin + + + + run-benchmarks + + none + + exec + + + test + java + + -classpath + + org.openjdk.jmh.Main + .* + + + + + + + + + + + + + 2.8.5 + + + 1.7.21 + 1.1.7 + + + 23.0 + 3.5 + 1.55 + 1.10 + 3.6.1 + 1.0.3 + 2.5 + 4.1 + 4.01 + 0.4 + 1.8.7 + 1.16.12 + 4.6-b01 + 1.13 + 0.6.5 + 0.9.0 + + + 1.3 + 4.12 + 2.8.9 + 3.6.1 + 1.7.0 + + + 3.6.0 + 2.19.1 + + \ No newline at end of file diff --git a/core-java-sun/src/main/java/com/baeldung/.gitignore b/core-java-sun/src/main/java/com/baeldung/.gitignore new file mode 100644 index 0000000000..83c05e60c8 --- /dev/null +++ b/core-java-sun/src/main/java/com/baeldung/.gitignore @@ -0,0 +1,13 @@ +*.class + +#folders# +/target +/neoDb* +/data +/src/main/webapp/WEB-INF/classes +*/META-INF/* + +# Packaged files # +*.jar +*.war +*.ear \ No newline at end of file diff --git a/core-java-sun/src/main/java/com/baeldung/README.md b/core-java-sun/src/main/java/com/baeldung/README.md new file mode 100644 index 0000000000..51809b2882 --- /dev/null +++ b/core-java-sun/src/main/java/com/baeldung/README.md @@ -0,0 +1,2 @@ +### Relevant Articles: +- [SHA-256 Hashing in Java](http://www.baeldung.com/sha-256-hashing-java) diff --git a/core-java/src/main/java/com/baeldung/javac/Positive.java b/core-java-sun/src/main/java/com/baeldung/javac/Positive.java similarity index 100% rename from core-java/src/main/java/com/baeldung/javac/Positive.java rename to core-java-sun/src/main/java/com/baeldung/javac/Positive.java diff --git a/core-java/src/main/java/com/baeldung/javac/SampleJavacPlugin.java b/core-java-sun/src/main/java/com/baeldung/javac/SampleJavacPlugin.java similarity index 100% rename from core-java/src/main/java/com/baeldung/javac/SampleJavacPlugin.java rename to core-java-sun/src/main/java/com/baeldung/javac/SampleJavacPlugin.java diff --git a/core-java-sun/src/main/resources/log4j.properties b/core-java-sun/src/main/resources/log4j.properties new file mode 100644 index 0000000000..621cf01735 --- /dev/null +++ b/core-java-sun/src/main/resources/log4j.properties @@ -0,0 +1,6 @@ +log4j.rootLogger=DEBUG, A1 + +log4j.appender.A1=org.apache.log4j.ConsoleAppender + +log4j.appender.A1.layout=org.apache.log4j.PatternLayout +log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n \ No newline at end of file diff --git a/core-java-sun/src/main/resources/logback.xml b/core-java-sun/src/main/resources/logback.xml new file mode 100644 index 0000000000..ec0dc2469a --- /dev/null +++ b/core-java-sun/src/main/resources/logback.xml @@ -0,0 +1,19 @@ + + + + + web - %date [%thread] %-5level %logger{36} - %message%n + + + + + + + + + + + + + + \ No newline at end of file diff --git a/core-java/src/test/java/com/baeldung/javac/SampleJavacPluginIntegrationTest.java b/core-java-sun/src/test/java/com/baeldung/javac/SampleJavacPluginIntegrationTest.java similarity index 100% rename from core-java/src/test/java/com/baeldung/javac/SampleJavacPluginIntegrationTest.java rename to core-java-sun/src/test/java/com/baeldung/javac/SampleJavacPluginIntegrationTest.java diff --git a/core-java/src/test/java/com/baeldung/javac/SimpleClassFile.java b/core-java-sun/src/test/java/com/baeldung/javac/SimpleClassFile.java similarity index 100% rename from core-java/src/test/java/com/baeldung/javac/SimpleClassFile.java rename to core-java-sun/src/test/java/com/baeldung/javac/SimpleClassFile.java diff --git a/core-java/src/test/java/com/baeldung/javac/SimpleFileManager.java b/core-java-sun/src/test/java/com/baeldung/javac/SimpleFileManager.java similarity index 100% rename from core-java/src/test/java/com/baeldung/javac/SimpleFileManager.java rename to core-java-sun/src/test/java/com/baeldung/javac/SimpleFileManager.java diff --git a/core-java/src/test/java/com/baeldung/javac/SimpleSourceFile.java b/core-java-sun/src/test/java/com/baeldung/javac/SimpleSourceFile.java similarity index 100% rename from core-java/src/test/java/com/baeldung/javac/SimpleSourceFile.java rename to core-java-sun/src/test/java/com/baeldung/javac/SimpleSourceFile.java diff --git a/core-java/src/test/java/com/baeldung/javac/TestCompiler.java b/core-java-sun/src/test/java/com/baeldung/javac/TestCompiler.java similarity index 100% rename from core-java/src/test/java/com/baeldung/javac/TestCompiler.java rename to core-java-sun/src/test/java/com/baeldung/javac/TestCompiler.java diff --git a/core-java/src/test/java/com/baeldung/javac/TestRunner.java b/core-java-sun/src/test/java/com/baeldung/javac/TestRunner.java similarity index 100% rename from core-java/src/test/java/com/baeldung/javac/TestRunner.java rename to core-java-sun/src/test/java/com/baeldung/javac/TestRunner.java diff --git a/core-java-sun/src/test/resources/.gitignore b/core-java-sun/src/test/resources/.gitignore new file mode 100644 index 0000000000..83c05e60c8 --- /dev/null +++ b/core-java-sun/src/test/resources/.gitignore @@ -0,0 +1,13 @@ +*.class + +#folders# +/target +/neoDb* +/data +/src/main/webapp/WEB-INF/classes +*/META-INF/* + +# Packaged files # +*.jar +*.war +*.ear \ No newline at end of file diff --git a/core-java/pom.xml b/core-java/pom.xml index 77000b8741..2c4cbfc37b 100644 --- a/core-java/pom.xml +++ b/core-java/pom.xml @@ -181,11 +181,6 @@ 2.1.0.1 - - com.sun.messaging.mq - fscontext - ${fscontext.version} - com.codepoetics protonpack @@ -216,13 +211,6 @@ spring-web 4.3.4.RELEASE - - com.sun - tools - 1.8.0 - system - ${java.home}/../lib/tools.jar - From 1854175cb9b0ba85685abb88bed4de6b8fdd8587 Mon Sep 17 00:00:00 2001 From: Eugen Date: Tue, 28 Nov 2017 12:39:39 +0200 Subject: [PATCH 15/28] Update README.md --- core-java-sun/README.md | 117 ---------------------------------------- 1 file changed, 117 deletions(-) diff --git a/core-java-sun/README.md b/core-java-sun/README.md index 1feee4126e..9cf8b26f1b 100644 --- a/core-java-sun/README.md +++ b/core-java-sun/README.md @@ -3,121 +3,4 @@ ## Core Java Cookbooks and Examples ### Relevant Articles: -- [Immutable ArrayList in Java](http://www.baeldung.com/java-immutable-list) -- [Java - Reading a Large File Efficiently](http://www.baeldung.com/java-read-lines-large-file) -- [Java InputStream to String](http://www.baeldung.com/convert-input-stream-to-string) -- [Converting between an Array and a List in Java](http://www.baeldung.com/convert-array-to-list-and-list-to-array) -- [Converting between an Array and a Set in Java](http://www.baeldung.com/convert-array-to-set-and-set-to-array) -- [Converting between a List and a Set in Java](http://www.baeldung.com/convert-list-to-set-and-set-to-list) -- [Convert a Map to an Array, List or Set in Java](http://www.baeldung.com/convert-map-values-to-array-list-set) -- [Java – Write to File](http://www.baeldung.com/java-write-to-file) -- [Java - Convert File to InputStream](http://www.baeldung.com/convert-file-to-input-stream) -- [Java – Random Long, Float, Integer and Double](http://www.baeldung.com/java-generate-random-long-float-integer-double) -- [Java – Generate Random String](http://www.baeldung.com/java-random-string) -- [Java Scanner](http://www.baeldung.com/java-scanner) -- [Java Timer](http://www.baeldung.com/java-timer-and-timertask) -- [Java – Byte Array to Writer](http://www.baeldung.com/java-convert-byte-array-to-writer) -- [How to Run a Shell Command in Java](http://www.baeldung.com/run-shell-command-in-java) -- [MD5 Hashing in Java](http://www.baeldung.com/java-md5) -- [Guide to the Java ArrayList](http://www.baeldung.com/java-arraylist) -- [Guide to Java Reflection](http://www.baeldung.com/java-reflection) -- [A Guide to Java Sockets](http://www.baeldung.com/a-guide-to-java-sockets) -- [Convert char to String in Java](http://www.baeldung.com/java-convert-char-to-string) -- [Random List Element](http://www.baeldung.com/java-random-list-element) -- [Convert String to int or Integer in Java](http://www.baeldung.com/java-convert-string-to-int-or-integer) -- [Java – Directory Size](http://www.baeldung.com/java-folder-size) -- [Java – Try with Resources](http://www.baeldung.com/java-try-with-resources) -- [Guide to the Fork/Join Framework in Java](http://www.baeldung.com/java-fork-join) -- [How to Print Screen in Java](http://www.baeldung.com/print-screen-in-java) -- [How to Convert String to different data types in Java](http://www.baeldung.com/java-string-conversions) -- [Introduction to Java Generics](http://www.baeldung.com/java-generics) -- [Generate equals() and hashCode() with Eclipse](http://www.baeldung.com/java-eclipse-equals-and-hashcode) -- [A Guide To Java Regular Expressions API](http://www.baeldung.com/regular-expressions-java) -- [Sorting in Java](http://www.baeldung.com/java-sorting) -- [Getting Started with Java Properties](http://www.baeldung.com/java-properties) -- [Grep in Java](http://www.baeldung.com/grep-in-java) -- [Java - Combine Multiple Collections](http://www.baeldung.com/java-combine-multiple-collections) -- [Simulated Annealing for Travelling Salesman Problem](http://www.baeldung.com/java-simulated-annealing-for-traveling-salesman) -- [Slope One Algorithm: Collaborative Filtering Recommendation Systems](http://www.baeldung.com/java-collaborative-filtering-recommendations) -- [Differences Between the Java WatchService API and the Apache Commons IO Monitor Library](http://www.baeldung.com/java-watchservice-vs-apache-commons-io-monitor-library) -- [Pattern Search with Grep in Java](http://www.baeldung.com/grep-in-java) -- [URL Encoding and Decoding in Java](http://www.baeldung.com/java-url-encoding-decoding) -- [Calculate the Size of a File in Java](http://www.baeldung.com/java-file-size) -- [The Basics of Java Generics](http://www.baeldung.com/java-generics) -- [The Traveling Salesman Problem in Java](http://www.baeldung.com/java-simulated-annealing-for-traveling-salesman) -- [How to Create an Executable JAR with Maven](http://www.baeldung.com/executable-jar-with-maven) -- [How to Design a Genetic Algorithm in Java](http://www.baeldung.com/java-genetic-algorithm) -- [Guide to WeakHashMap in Java](http://www.baeldung.com/java-weakhashmap) -- [Spring Security – Cache Control Headers](http://www.baeldung.com/spring-security-cache-control-headers) -- [Basic Introduction to JMX](http://www.baeldung.com/java-management-extensions) -- [AWS Lambda With Java](http://www.baeldung.com/java-aws-lambda) -- [Introduction to Nashorn](http://www.baeldung.com/java-nashorn) -- [Guide to the Guava BiMap](http://www.baeldung.com/guava-bimap) -- [Iterable to Stream in Java](http://www.baeldung.com/java-iterable-to-stream) -- [Chained Exceptions in Java](http://www.baeldung.com/java-chained-exceptions) -- [The Java HashMap Under the Hood](http://www.baeldung.com/java-hashmap) -- [A Guide to LinkedHashMap in Java](http://www.baeldung.com/java-linked-hashmap) -- [A Guide to TreeMap in Java](http://www.baeldung.com/java-treemap) -- [A Quick JUnit vs TestNG Comparison](http://www.baeldung.com/junit-vs-testng) -- [Java Primitive Conversions](http://www.baeldung.com/java-primitive-conversions) -- [Java Money and the Currency API](http://www.baeldung.com/java-money-and-currency) -- [Removing all nulls from a List in Java](http://www.baeldung.com/java-remove-nulls-from-list) -- [Removing all duplicates from a List in Java](http://www.baeldung.com/java-remove-duplicates-from-list) -- [Using Math.pow in Java](http://www.baeldung.com/java-math-pow) -- [Converting Strings to Enums in Java](http://www.baeldung.com/java-string-to-enum) -- [Flattening Nested Collections in Java](http://www.baeldung.com/java-flatten-nested-collections) -- [Quick Guide to the Java StringTokenizer](http://www.baeldung.com/java-stringtokenizer) -- [JVM Log Forging](http://www.baeldung.com/jvm-log-forging) -- [Guide to sun.misc.Unsafe](http://www.baeldung.com/java-unsafe) -- [HashSet and TreeSet Comparison](http://www.baeldung.com/java-hashset-vs-treeset) -- [How to Perform a Simple HTTP Request in Java](http://www.baeldung.com/java-http-request) -- [Call Methods at Runtime Using Java Reflection](http://www.baeldung.com/java-method-reflection) -- [Guide to UUID in JAVA](http://www.baeldung.com/guide-to-uuid-in-java) -- [Comparing getPath(), getAbsolutePath(), and getCanonicalPath() in Java](http://www.baeldung.com/java-path) -- [How to Add a Single Element to a Stream](http://www.baeldung.com/java-stream-append-prepend) -- [Iterating Over Enum Values in Java](http://www.baeldung.com/java-enum-iteration) -- [Kotlin Java Interoperability](http://www.baeldung.com/kotlin-java-interoperability) -- [Using Java MappedByteBuffer](http://www.baeldung.com/java-mapped-byte-buffer) -- [How to Round a Number to N Decimal Places in Java](http://www.baeldung.com/java-round-decimal-number) -- [Changing Annotation Parameters At Runtime](http://www.baeldung.com/java-reflection-change-annotation-params) -- [How to Find all Getters Returning Null](http://www.baeldung.com/java-getters-returning-null) -- [Converting String to Stream of chars](http://www.baeldung.com/java-string-to-stream) -- [Changing the Order in a Sum Operation Can Produce Different Results?](http://www.baeldung.com/java-floating-point-sum-order) -- [How to Get a Name of a Method Being Executed?](http://www.baeldung.com/java-name-of-executing-method) -- [Iterate over a Map in Java](http://www.baeldung.com/java-iterate-map) -- [Dynamic Proxies in Java](http://www.baeldung.com/java-dynamic-proxies) -- [How to Copy an Array in Java](http://www.baeldung.com/java-array-copy) -- [Introduction to JDBC](http://www.baeldung.com/java-jdbc) -- [Period and Duration in Java](http://www.baeldung.com/java-period-duration) -- [Converting a Stack Trace to a String in Java](http://www.baeldung.com/java-stacktrace-to-string) -- [Count Occurrences of a Char in a String](http://www.baeldung.com/java-count-chars) -- [Java Double Brace Initialization](http://www.baeldung.com/java-double-brace-initialization) -- [The StackOverflowError in Java](http://www.baeldung.com/java-stack-overflow-error) -- [Split a String in Java](http://www.baeldung.com/java-split-string) -- [Introduction to Java Serialization](http://www.baeldung.com/java-serialization) -- [How to Remove the Last Character of a String?](http://www.baeldung.com/java-remove-last-character-of-string) -- [ClassNotFoundException vs NoClassDefFoundError](http://www.baeldung.com/java-classnotfoundexception-and-noclassdeffounderror) -- [Guide to UUID in Java](http://www.baeldung.com/java-uuid) -- [Guide to Escaping Characters in Java RegExps](http://www.baeldung.com/java-regexp-escape-char) -- [Guide to hashCode() in Java](http://www.baeldung.com/java-hashcode) -- [Collect a Java Stream to an Immutable Collection](http://www.baeldung.com/java-stream-immutable-collection) -- [Difference between URL and URI](http://www.baeldung.com/java-url-vs-uri) -- [Broadcasting and Multicasting in Java](http://www.baeldung.com/java-broadcast-multicast) -- [Converting a List to String in Java](http://www.baeldung.com/java-list-to-string) -- [CharSequence vs. String in Java](http://www.baeldung.com/java-char-sequence-string) -- [Period and Duration in Java](http://www.baeldung.com/java-period-duration) -- [Guide to the Diamond Operator in Java](http://www.baeldung.com/java-diamond-operator) -- [Singletons in Java](http://www.baeldung.com/java-singleton) -- [“Sneaky Throws” in Java](http://www.baeldung.com/java-sneaky-throws) -- [OutOfMemoryError: GC Overhead Limit Exceeded](http://www.baeldung.com/java-gc-overhead-limit-exceeded) -- [How to Iterate Over a Stream With Indices](http://www.baeldung.com/java-stream-indices) -- [StringBuilder and StringBuffer in Java](http://www.baeldung.com/java-string-builder-string-buffer) -- [Number of Digits in an Integer in Java](http://www.baeldung.com/java-number-of-digits-in-int) -- [Proxy, Decorator, Adapter and Bridge Patterns](http://www.baeldung.com/java-structural-design-patterns) - [Creating a Java Compiler Plugin](http://www.baeldung.com/java-build-compiler-plugin) -- [A Guide to the Static Keyword in Java](http://www.baeldung.com/java-static) -- [Initializing Arrays in Java](http://www.baeldung.com/java-initialize-array) -- [Guide to Java String Pool](http://www.baeldung.com/java-string-pool) -- [Copy a File with Java](http://www.baeldung.com/java-copy-file) -- [Introduction to Creational Design Patterns](http://www.baeldung.com/creational-design-patterns) - From d10a8d7f474d47345b7ef7118c6303644bab30cf Mon Sep 17 00:00:00 2001 From: Marcos Lopez Gonzalez Date: Wed, 29 Nov 2017 09:53:43 +0100 Subject: [PATCH 16/28] spring junit config (#3124) --- .../jupiter/SpringJUnitConfigTest.java | 33 ++++++++++++++++++ .../jupiter/SpringJUnitWebConfigTest.java | 34 +++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 spring-5/src/test/java/com/baeldung/jupiter/SpringJUnitConfigTest.java create mode 100644 spring-5/src/test/java/com/baeldung/jupiter/SpringJUnitWebConfigTest.java diff --git a/spring-5/src/test/java/com/baeldung/jupiter/SpringJUnitConfigTest.java b/spring-5/src/test/java/com/baeldung/jupiter/SpringJUnitConfigTest.java new file mode 100644 index 0000000000..6b0a6f9808 --- /dev/null +++ b/spring-5/src/test/java/com/baeldung/jupiter/SpringJUnitConfigTest.java @@ -0,0 +1,33 @@ +package com.baeldung.jupiter; + +import static org.junit.Assert.assertNotNull; + +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.Configuration; +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; + +/** + * @SpringJUnitConfig(SpringJUnitConfigTest.Config.class) is equivalent to: + * + * @ExtendWith(SpringExtension.class) + * @ContextConfiguration(classes = SpringJUnitConfigTest.Config.class ) + * + */ +@SpringJUnitConfig(SpringJUnitConfigTest.Config.class) +public class SpringJUnitConfigTest { + + @Configuration + static class Config { + } + + @Autowired + private ApplicationContext applicationContext; + + @Test + void givenAppContext_WhenInjected_ThenItShouldNotBeNull() { + assertNotNull(applicationContext); + } + +} diff --git a/spring-5/src/test/java/com/baeldung/jupiter/SpringJUnitWebConfigTest.java b/spring-5/src/test/java/com/baeldung/jupiter/SpringJUnitWebConfigTest.java new file mode 100644 index 0000000000..c679dce77f --- /dev/null +++ b/spring-5/src/test/java/com/baeldung/jupiter/SpringJUnitWebConfigTest.java @@ -0,0 +1,34 @@ +package com.baeldung.jupiter; + +import static org.junit.Assert.assertNotNull; + +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Configuration; +import org.springframework.test.context.junit.jupiter.web.SpringJUnitWebConfig; +import org.springframework.web.context.WebApplicationContext; + +/** + * @SpringJUnitWebConfig(SpringJUnitWebConfigTest.Config.class) is equivalent to: + * + * @ExtendWith(SpringExtension.class) + * @WebAppConfiguration + * @ContextConfiguration(classes = SpringJUnitWebConfigTest.Config.class ) + * + */ +@SpringJUnitWebConfig(SpringJUnitWebConfigTest.Config.class) +public class SpringJUnitWebConfigTest { + + @Configuration + static class Config { + } + + @Autowired + private WebApplicationContext webAppContext; + + @Test + void givenWebAppContext_WhenInjected_ThenItShouldNotBeNull() { + assertNotNull(webAppContext); + } + +} From 3feaee499a0b5e7489b479ebe3e17710835b4f84 Mon Sep 17 00:00:00 2001 From: Grzegorz Piwowarek Date: Wed, 29 Nov 2017 12:07:35 +0100 Subject: [PATCH 17/28] Update README.md (#3044) --- logging-modules/log4j/README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/logging-modules/log4j/README.md b/logging-modules/log4j/README.md index 3c0258142c..8aae1b5826 100644 --- a/logging-modules/log4j/README.md +++ b/logging-modules/log4j/README.md @@ -2,6 +2,5 @@ - [Introduction to Java Logging](http://www.baeldung.com/java-logging-intro) - [Introduction to SLF4J](http://www.baeldung.com/slf4j-with-log4j2-logback) - [Generate equals() and hashCode() with Eclipse](http://www.baeldung.com/java-eclipse-equals-and-hashcode) -- [A Guide To Java Regular Expressions API](http://www.baeldung.com/regular-expressions-java) - [Introduction to SLF4J](http://www.baeldung.com/slf4j-with-log4j2-logback) - [A Guide to Rolling File Appenders](http://www.baeldung.com/java-logging-rolling-file-appenders) From fb8f07c852e2eb0027854f97cf38e36188f3e6f8 Mon Sep 17 00:00:00 2001 From: Grzegorz Piwowarek Date: Wed, 29 Nov 2017 12:10:22 +0100 Subject: [PATCH 18/28] Update PolygonFactory.java (#3108) --- .../designpatterns/creational/factory/PolygonFactory.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core-java/src/main/java/com/baeldung/designpatterns/creational/factory/PolygonFactory.java b/core-java/src/main/java/com/baeldung/designpatterns/creational/factory/PolygonFactory.java index 406f0f5274..9f34fe77b9 100644 --- a/core-java/src/main/java/com/baeldung/designpatterns/creational/factory/PolygonFactory.java +++ b/core-java/src/main/java/com/baeldung/designpatterns/creational/factory/PolygonFactory.java @@ -11,7 +11,7 @@ public class PolygonFactory { if(numberOfSides == 5) { return new Pentagon(); } - if(numberOfSides == 4) { + if(numberOfSides == 7) { return new Heptagon(); } else if(numberOfSides == 8) { @@ -19,4 +19,4 @@ public class PolygonFactory { } return null; } -} \ No newline at end of file +} From 8af39bf73021cd19aca167cef7e0cbf477ce316a Mon Sep 17 00:00:00 2001 From: gschambial Date: Wed, 29 Nov 2017 16:46:37 +0530 Subject: [PATCH 19/28] 1. Comparator example using Lambda and Comparator.comparing corrected. --- .../comparator/Java8ComparatorUnitTest.java | 29 +++++++++++-------- 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/core-java/src/test/java/com/baeldung/comparator/Java8ComparatorUnitTest.java b/core-java/src/test/java/com/baeldung/comparator/Java8ComparatorUnitTest.java index b804573b51..49c8749309 100644 --- a/core-java/src/test/java/com/baeldung/comparator/Java8ComparatorUnitTest.java +++ b/core-java/src/test/java/com/baeldung/comparator/Java8ComparatorUnitTest.java @@ -17,8 +17,8 @@ public class Java8ComparatorUnitTest { @Before public void setUp() { footballTeam = new ArrayList(); - Player player1 = new Player(59, "John", 20); - Player player2 = new Player(67, "Roger", 22); + Player player1 = new Player(59, "John", 22); + Player player2 = new Player(67, "Roger", 20); Player player3 = new Player(45, "Steven", 24); footballTeam.add(player1); footballTeam.add(player2); @@ -26,15 +26,9 @@ public class Java8ComparatorUnitTest { } @Test - public void whenComparing_UsingJava8_thenSorted() { + public void whenComparing_UsingLambda_thenSorted() { System.out.println("************** Java 8 Comaparator **************"); - Comparator byRanking = new Comparator() { - - @Override - public int compare(Player player1, Player player2) { - return player1.getRanking() - player2.getRanking(); - } - }; + Comparator byRanking = (Player player1, Player player2) -> player1.getRanking() - player2.getRanking(); System.out.println("Before Sorting : " + footballTeam); Collections.sort(footballTeam, byRanking); @@ -48,8 +42,8 @@ public class Java8ComparatorUnitTest { @Test public void whenComparing_UsingComparatorComparing_thenSorted() { System.out.println("********* Comaparator.comparing method *********"); - Comparator byRanking = - (Player player1, Player player2)->player1.getRanking()-player2.getRanking(); + System.out.println("********* byRanking *********"); + Comparator byRanking = Comparator.comparing(Player::getRanking); System.out.println("Before Sorting : " + footballTeam); Collections.sort(footballTeam, byRanking); @@ -58,6 +52,17 @@ public class Java8ComparatorUnitTest { .getName(), "Steven"); assertEquals(footballTeam.get(2) .getRanking(), 67); + + System.out.println("********* byAge *********"); + Comparator byAge = Comparator.comparing(Player::getAge); + + System.out.println("Before Sorting : " + footballTeam); + Collections.sort(footballTeam, byAge); + System.out.println("After Sorting : " + footballTeam); + assertEquals(footballTeam.get(0) + .getName(), "Roger"); + assertEquals(footballTeam.get(2) + .getRanking(), 45); } } From 9f42f62187d9d87d823fe622aa8f9eca6cfd1d2c Mon Sep 17 00:00:00 2001 From: Eugen Paraschiv Date: Wed, 29 Nov 2017 14:57:56 +0200 Subject: [PATCH 20/28] new spring context test --- .../baeldung/SpringContextIntegrationTest.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 spring-security-mvc-boot/src/test/java/org/baeldung/SpringContextIntegrationTest.java diff --git a/spring-security-mvc-boot/src/test/java/org/baeldung/SpringContextIntegrationTest.java b/spring-security-mvc-boot/src/test/java/org/baeldung/SpringContextIntegrationTest.java new file mode 100644 index 0000000000..cf1ac7de89 --- /dev/null +++ b/spring-security-mvc-boot/src/test/java/org/baeldung/SpringContextIntegrationTest.java @@ -0,0 +1,15 @@ +package org.baeldung; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@SpringBootTest(classes = Application.class) +public class SpringContextIntegrationTest { + + @Test + public void whenSpringContextIsBootstrapped_thenNoExceptions() { + } +} From d433add90d978bdf1d77dec4c9745a0cf2e9e942 Mon Sep 17 00:00:00 2001 From: Eugen Date: Wed, 29 Nov 2017 16:36:08 +0200 Subject: [PATCH 21/28] Create LICENSE (#3159) Setting the MIT license on the repo. --- LICENSE | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 LICENSE diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000000..40f5c88746 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2017 Eugen Paraschiv + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. From 064ea79490bc1c8e53c6d62c43ee2330a97c7f2e Mon Sep 17 00:00:00 2001 From: araknoid Date: Wed, 29 Nov 2017 20:49:08 +0100 Subject: [PATCH 22/28] BAEL-1203 Apache POI PowerPoint (#3090) * Added Apache-POI PowerPoint * Adjusted format + retrieve all placeholder --- .../poi/powerpoint/PowerPointHelper.java | 224 ++++++++++++++++++ .../powerpoint/PowerPointIntegrationTest.java | 77 ++++++ 2 files changed, 301 insertions(+) create mode 100644 apache-poi/src/main/java/com/baeldung/poi/powerpoint/PowerPointHelper.java create mode 100644 apache-poi/src/test/java/com/baeldung/poi/powerpoint/PowerPointIntegrationTest.java diff --git a/apache-poi/src/main/java/com/baeldung/poi/powerpoint/PowerPointHelper.java b/apache-poi/src/main/java/com/baeldung/poi/powerpoint/PowerPointHelper.java new file mode 100644 index 0000000000..e2af4f8808 --- /dev/null +++ b/apache-poi/src/main/java/com/baeldung/poi/powerpoint/PowerPointHelper.java @@ -0,0 +1,224 @@ +package com.baeldung.poi.powerpoint; + +import org.apache.poi.sl.usermodel.AutoNumberingScheme; +import org.apache.poi.sl.usermodel.PictureData; +import org.apache.poi.sl.usermodel.TableCell; +import org.apache.poi.sl.usermodel.TextParagraph; +import org.apache.poi.util.IOUtils; +import org.apache.poi.xslf.usermodel.*; + +import java.awt.*; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +/** + * Helper class for the PowerPoint presentation creation + */ +public class PowerPointHelper { + + /** + * Read an existing presentation + * + * @param fileLocation + * File location of the presentation + * @return instance of {@link XMLSlideShow} + * @throws IOException + */ + public XMLSlideShow readingExistingSlideShow(String fileLocation) throws IOException { + return new XMLSlideShow(new FileInputStream(fileLocation)); + } + + /** + * Create a sample presentation + * + * @param fileLocation + * File location of the presentation + * @throws IOException + */ + public void createPresentation(String fileLocation) throws IOException { + // Create presentation + XMLSlideShow ppt = new XMLSlideShow(); + + XSLFSlideMaster defaultMaster = ppt.getSlideMasters().get(0); + + // Retriving the slide layout + XSLFSlideLayout layout = defaultMaster.getLayout(SlideLayout.TITLE_ONLY); + + // Creating the 1st slide + XSLFSlide slide1 = ppt.createSlide(layout); + XSLFTextShape title = slide1.getPlaceholder(0); + // Clearing text to remove the predefined one in the template + title.clearText(); + XSLFTextParagraph p = title.addNewTextParagraph(); + + XSLFTextRun r1 = p.addNewTextRun(); + r1.setText("Baeldung"); + r1.setFontColor(new Color(78, 147, 89)); + r1.setFontSize(48.); + + // Add Image + ClassLoader classLoader = getClass().getClassLoader(); + byte[] pictureData = IOUtils.toByteArray(new FileInputStream(classLoader.getResource("logo-leaf.png").getFile())); + + XSLFPictureData pd = ppt.addPicture(pictureData, PictureData.PictureType.PNG); + XSLFPictureShape picture = slide1.createPicture(pd); + picture.setAnchor(new Rectangle(320, 230, 100, 92)); + + // Creating 2nd slide + layout = defaultMaster.getLayout(SlideLayout.TITLE_AND_CONTENT); + XSLFSlide slide2 = ppt.createSlide(layout); + + // setting the tile + title = slide2.getPlaceholder(0); + title.clearText(); + XSLFTextRun r = title.addNewTextParagraph().addNewTextRun(); + r.setText("Baeldung"); + + // Adding the link + XSLFHyperlink link = r.createHyperlink(); + link.setAddress("http://www.baeldung.com"); + + // setting the content + XSLFTextShape content = slide2.getPlaceholder(1); + content.clearText(); // unset any existing text + content.addNewTextParagraph().addNewTextRun().setText("First paragraph"); + content.addNewTextParagraph().addNewTextRun().setText("Second paragraph"); + content.addNewTextParagraph().addNewTextRun().setText("Third paragraph"); + + // Creating 3rd slide - List + layout = defaultMaster.getLayout(SlideLayout.TITLE_AND_CONTENT); + XSLFSlide slide3 = ppt.createSlide(layout); + title = slide3.getPlaceholder(0); + title.clearText(); + r = title.addNewTextParagraph().addNewTextRun(); + r.setText("Lists"); + + content = slide3.getPlaceholder(1); + content.clearText(); + XSLFTextParagraph p1 = content.addNewTextParagraph(); + p1.setIndentLevel(0); + p1.setBullet(true); + r1 = p1.addNewTextRun(); + r1.setText("Bullet"); + + // the next three paragraphs form an auto-numbered list + XSLFTextParagraph p2 = content.addNewTextParagraph(); + p2.setBulletAutoNumber(AutoNumberingScheme.alphaLcParenRight, 1); + p2.setIndentLevel(1); + XSLFTextRun r2 = p2.addNewTextRun(); + r2.setText("Numbered List Item - 1"); + + // Creating 4th slide + XSLFSlide slide4 = ppt.createSlide(); + createTable(slide4); + + // Save presentation + FileOutputStream out = new FileOutputStream(fileLocation); + ppt.write(out); + out.close(); + + // Closing presentation + ppt.close(); + } + + /** + * Delete a slide from the presentation + * + * @param ppt + * The presentation + * @param slideNumber + * The number of the slide to be deleted (0-based) + */ + public void deleteSlide(XMLSlideShow ppt, int slideNumber) { + ppt.removeSlide(slideNumber); + } + + /** + * Re-order the slides inside a presentation + * + * @param ppt + * The presentation + * @param slideNumber + * The number of the slide to move + * @param newSlideNumber + * The new position of the slide (0-base) + */ + public void reorderSlide(XMLSlideShow ppt, int slideNumber, int newSlideNumber) { + List slides = ppt.getSlides(); + + XSLFSlide secondSlide = slides.get(slideNumber); + ppt.setSlideOrder(secondSlide, newSlideNumber); + } + + /** + * Retrieve the placeholder inside a slide + * + * @param slide + * The slide + * @return List of placeholder inside a slide + */ + public List retrieveTemplatePlaceholders(XSLFSlide slide) { + List placeholders = new ArrayList<>(); + + for (XSLFShape shape : slide.getShapes()) { + if (shape instanceof XSLFAutoShape) { + placeholders.add(shape); + } + } + return placeholders; + } + + /** + * Create a table + * + * @param slide + * Slide + */ + private void createTable(XSLFSlide slide) { + + XSLFTable tbl = slide.createTable(); + tbl.setAnchor(new Rectangle(50, 50, 450, 300)); + + int numColumns = 3; + int numRows = 5; + + // header + XSLFTableRow headerRow = tbl.addRow(); + headerRow.setHeight(50); + for (int i = 0; i < numColumns; i++) { + XSLFTableCell th = headerRow.addCell(); + XSLFTextParagraph p = th.addNewTextParagraph(); + p.setTextAlign(TextParagraph.TextAlign.CENTER); + XSLFTextRun r = p.addNewTextRun(); + r.setText("Header " + (i + 1)); + r.setBold(true); + r.setFontColor(Color.white); + th.setFillColor(new Color(79, 129, 189)); + th.setBorderWidth(TableCell.BorderEdge.bottom, 2.0); + th.setBorderColor(TableCell.BorderEdge.bottom, Color.white); + // all columns are equally sized + tbl.setColumnWidth(i, 150); + } + + // data + for (int rownum = 0; rownum < numRows; rownum++) { + XSLFTableRow tr = tbl.addRow(); + tr.setHeight(50); + for (int i = 0; i < numColumns; i++) { + XSLFTableCell cell = tr.addCell(); + XSLFTextParagraph p = cell.addNewTextParagraph(); + XSLFTextRun r = p.addNewTextRun(); + + r.setText("Cell " + (i * rownum + 1)); + if (rownum % 2 == 0) { + cell.setFillColor(new Color(208, 216, 232)); + } else { + cell.setFillColor(new Color(233, 247, 244)); + } + } + } + } +} diff --git a/apache-poi/src/test/java/com/baeldung/poi/powerpoint/PowerPointIntegrationTest.java b/apache-poi/src/test/java/com/baeldung/poi/powerpoint/PowerPointIntegrationTest.java new file mode 100644 index 0000000000..5319208e85 --- /dev/null +++ b/apache-poi/src/test/java/com/baeldung/poi/powerpoint/PowerPointIntegrationTest.java @@ -0,0 +1,77 @@ +package com.baeldung.poi.powerpoint; + +import org.apache.poi.xslf.usermodel.XMLSlideShow; +import org.apache.poi.xslf.usermodel.XSLFShape; +import org.apache.poi.xslf.usermodel.XSLFSlide; +import org.junit.After; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +import java.io.File; +import java.util.List; + +public class PowerPointIntegrationTest { + + private PowerPointHelper pph; + private String fileLocation; + private static final String FILE_NAME = "presentation.pptx"; + + @Before + public void setUp() throws Exception { + File currDir = new File("."); + String path = currDir.getAbsolutePath(); + fileLocation = path.substring(0, path.length() - 1) + FILE_NAME; + + pph = new PowerPointHelper(); + pph.createPresentation(fileLocation); + } + + @Test + public void whenReadingAPresentation_thenOK() throws Exception { + XMLSlideShow xmlSlideShow = pph.readingExistingSlideShow(fileLocation); + + Assert.assertNotNull(xmlSlideShow); + Assert.assertEquals(4, xmlSlideShow.getSlides().size()); + } + + @Test + public void whenRetrievingThePlaceholdersForEachSlide_thenOK() throws Exception { + XMLSlideShow xmlSlideShow = pph.readingExistingSlideShow(fileLocation); + + List onlyTitleSlidePlaceholders = pph.retrieveTemplatePlaceholders(xmlSlideShow.getSlides().get(0)); + List titleAndBodySlidePlaceholders = pph.retrieveTemplatePlaceholders(xmlSlideShow.getSlides().get(1)); + List emptySlidePlaceholdes = pph.retrieveTemplatePlaceholders(xmlSlideShow.getSlides().get(3)); + + Assert.assertEquals(1, onlyTitleSlidePlaceholders.size()); + Assert.assertEquals(2, titleAndBodySlidePlaceholders.size()); + Assert.assertEquals(0, emptySlidePlaceholdes.size()); + + } + + @Test + public void whenSortingSlides_thenOK() throws Exception { + XMLSlideShow xmlSlideShow = pph.readingExistingSlideShow(fileLocation); + XSLFSlide slide4 = xmlSlideShow.getSlides().get(3); + pph.reorderSlide(xmlSlideShow, 3, 1); + + Assert.assertEquals(slide4, xmlSlideShow.getSlides().get(1)); + } + + @Test + public void givenPresentation_whenDeletingASlide_thenOK() throws Exception { + XMLSlideShow xmlSlideShow = pph.readingExistingSlideShow(fileLocation); + pph.deleteSlide(xmlSlideShow, 3); + + Assert.assertEquals(3, xmlSlideShow.getSlides().size()); + } + + @After + public void tearDown() throws Exception { + File testFile = new File(fileLocation); + if (testFile.exists()) { + testFile.delete(); + } + pph = null; + } +} From 67c9b1e86f5fff5fe03f04e44135a8018bd813d6 Mon Sep 17 00:00:00 2001 From: Holger Date: Wed, 29 Nov 2017 22:37:54 +0000 Subject: [PATCH 23/28] BAEL-1376: Varargs Code Example (#3129) * BAEL-1376: Varargs Code Example * BAEL-1376: Varargs Code Example Moving the "logic" into a test and removing the unnecessary class. --- .../com/baeldung/varargs/FormatterTest.java | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 core-java/src/test/java/com/baeldung/varargs/FormatterTest.java diff --git a/core-java/src/test/java/com/baeldung/varargs/FormatterTest.java b/core-java/src/test/java/com/baeldung/varargs/FormatterTest.java new file mode 100644 index 0000000000..509c8764d2 --- /dev/null +++ b/core-java/src/test/java/com/baeldung/varargs/FormatterTest.java @@ -0,0 +1,60 @@ +package com.baeldung.varargs; + +import org.junit.Test; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; + +public class FormatterTest { + + private final static String FORMAT = "%s %s %s"; + + @Test + public void givenNoArgument_thenEmptyAndTwoSpacesAreReturned() { + String actualResult = format(); + + assertThat(actualResult, is("empty ")); + } + + @Test + public void givenOneArgument_thenResultHasTwoTrailingSpace() { + String actualResult = format("baeldung"); + + assertThat(actualResult, is("baeldung ")); + } + + @Test + public void givenTwoArguments_thenOneTrailingSpaceExists() { + String actualResult = format("baeldung", "rocks"); + + assertThat(actualResult, is("baeldung rocks ")); + } + + @Test + public void givenMoreThanThreeArguments_thenTheFirstThreeAreUsed() { + String actualResult = formatWithVarArgs("baeldung", "rocks", "java", "and", "spring"); + + assertThat(actualResult, is("baeldung rocks java")); + } + + public String format() { + return format("empty", ""); + } + + public String format(String value) { + return format(value, ""); + } + + public String format(String val1, String val2) { + return String.format(FORMAT, val1, val2, ""); + } + + public String formatWithVarArgs(String... values) { + if (values.length == 0) { + return "no arguments given"; + } + + return String.format(FORMAT, values); + } + +} \ No newline at end of file From ba566a81cb6dd26afd258b9ebf6640a99e052ead Mon Sep 17 00:00:00 2001 From: abirkhan04 Date: Thu, 30 Nov 2017 09:34:24 +0600 Subject: [PATCH 24/28] BAEL-1254 Getting Started With Mule ESB (#3153) --- muleesb/.gitignore | 1 + .../partition-descriptor | 1 + muleesb/.mule/queue-tx-log/tx1.log | 0 muleesb/.mule/queue-tx-log/tx2.log | 0 muleesb/.mule/queue-xa-tx-log/tx1.log | 0 muleesb/.mule/queue-xa-tx-log/tx2.log | 0 muleesb/mule-project.xml | 5 + muleesb/pom.xml | 214 ++++++++++++++++++ muleesb/src/main/app/mule-app.properties | 0 muleesb/src/main/app/mule-deploy.properties | 6 + muleesb/src/main/app/variablescopetest.xml | 36 +++ .../transformer/FromFlow2Component.java | 18 ++ .../InitializationTransformer.java | 29 +++ .../transformer/InvokingMessageComponent.java | 16 ++ muleesb/src/main/resources/log4j2.xml | 33 +++ .../munit/variablescopetest-test-suite.xml | 40 ++++ muleesb/src/test/resources/log4j2-test.xml | 35 +++ 17 files changed, 434 insertions(+) create mode 100644 muleesb/.gitignore create mode 100644 muleesb/.mule/objectstore/b2fe1a90-c473-11e7-8eb5-98e7f44e8ac8/partition-descriptor create mode 100644 muleesb/.mule/queue-tx-log/tx1.log create mode 100644 muleesb/.mule/queue-tx-log/tx2.log create mode 100644 muleesb/.mule/queue-xa-tx-log/tx1.log create mode 100644 muleesb/.mule/queue-xa-tx-log/tx2.log create mode 100644 muleesb/mule-project.xml create mode 100644 muleesb/pom.xml create mode 100644 muleesb/src/main/app/mule-app.properties create mode 100644 muleesb/src/main/app/mule-deploy.properties create mode 100644 muleesb/src/main/app/variablescopetest.xml create mode 100644 muleesb/src/main/java/com/baeldung/transformer/FromFlow2Component.java create mode 100644 muleesb/src/main/java/com/baeldung/transformer/InitializationTransformer.java create mode 100644 muleesb/src/main/java/com/baeldung/transformer/InvokingMessageComponent.java create mode 100644 muleesb/src/main/resources/log4j2.xml create mode 100644 muleesb/src/test/munit/variablescopetest-test-suite.xml create mode 100644 muleesb/src/test/resources/log4j2-test.xml diff --git a/muleesb/.gitignore b/muleesb/.gitignore new file mode 100644 index 0000000000..541f92c42e --- /dev/null +++ b/muleesb/.gitignore @@ -0,0 +1 @@ +# Add any directories, files, or patterns you don't want to be tracked by version control \ No newline at end of file diff --git a/muleesb/.mule/objectstore/b2fe1a90-c473-11e7-8eb5-98e7f44e8ac8/partition-descriptor b/muleesb/.mule/objectstore/b2fe1a90-c473-11e7-8eb5-98e7f44e8ac8/partition-descriptor new file mode 100644 index 0000000000..0b8060f303 --- /dev/null +++ b/muleesb/.mule/objectstore/b2fe1a90-c473-11e7-8eb5-98e7f44e8ac8/partition-descriptor @@ -0,0 +1 @@ +DEFAULT_PARTITION \ No newline at end of file diff --git a/muleesb/.mule/queue-tx-log/tx1.log b/muleesb/.mule/queue-tx-log/tx1.log new file mode 100644 index 0000000000..e69de29bb2 diff --git a/muleesb/.mule/queue-tx-log/tx2.log b/muleesb/.mule/queue-tx-log/tx2.log new file mode 100644 index 0000000000..e69de29bb2 diff --git a/muleesb/.mule/queue-xa-tx-log/tx1.log b/muleesb/.mule/queue-xa-tx-log/tx1.log new file mode 100644 index 0000000000..e69de29bb2 diff --git a/muleesb/.mule/queue-xa-tx-log/tx2.log b/muleesb/.mule/queue-xa-tx-log/tx2.log new file mode 100644 index 0000000000..e69de29bb2 diff --git a/muleesb/mule-project.xml b/muleesb/mule-project.xml new file mode 100644 index 0000000000..0d522b0141 --- /dev/null +++ b/muleesb/mule-project.xml @@ -0,0 +1,5 @@ + + + muleesb + + diff --git a/muleesb/pom.xml b/muleesb/pom.xml new file mode 100644 index 0000000000..2c88bf83da --- /dev/null +++ b/muleesb/pom.xml @@ -0,0 +1,214 @@ + + + + 4.0.0 + com.mycompany + muleesb + 1.0.0-SNAPSHOT + mule + Mule muleesb Application + + + UTF-8 + UTF-8 + + 3.8.1 + 1.2 + 1.3.6 + 3.9.0 + + + + + + org.mule.tools.maven + mule-app-maven-plugin + ${mule.tools.version} + true + + true + + + + org.mule.tools + muleesb-maven-plugin + 1.0 + + 3.7.0 + /home/abir/AnypointStudio/workspace/variablescopetest + + + + deploy + + start + + + + + + org.codehaus.mojo + build-helper-maven-plugin + 1.7 + + + add-resource + generate-resources + + add-resource + + + + + src/main/app/ + + + mappings/ + + + src/main/api/ + + + + + + + + com.mulesoft.munit.tools + munit-maven-plugin + ${munit.version} + + + test + test + + test + + + + + + true + + html + + + + + + + + src/test/munit + + + src/test/resources + + + + + + + + + org.mule.modules + mule-module-spring-config + ${mule.version} + provided + + + + org.mule.transports + mule-transport-file + ${mule.version} + provided + + + org.mule.transports + mule-transport-http + ${mule.version} + provided + + + org.mule.transports + mule-transport-jdbc + ${mule.version} + provided + + + org.mule.transports + mule-transport-jms + ${mule.version} + provided + + + org.mule.transports + mule-transport-vm + ${mule.version} + provided + + + + org.mule.modules + mule-module-scripting + ${mule.version} + provided + + + org.mule.modules + mule-module-xml + ${mule.version} + provided + + + + org.mule.tests + mule-tests-functional + ${mule.version} + test + + + org.mule.modules + mule-module-apikit + ${mule.version} + provided + + + com.mulesoft.munit + mule-munit-support + ${mule.munit.support.version} + test + + + com.mulesoft.munit + munit-runner + ${munit.version} + test + + + + + + Central + Central + http://repo1.maven.org/maven2/ + default + + + mulesoft-releases + MuleSoft Releases Repository + http://repository.mulesoft.org/releases/ + default + + + + + mulesoft-release + mulesoft release repository + default + http://repository.mulesoft.org/releases/ + + false + + + + diff --git a/muleesb/src/main/app/mule-app.properties b/muleesb/src/main/app/mule-app.properties new file mode 100644 index 0000000000..e69de29bb2 diff --git a/muleesb/src/main/app/mule-deploy.properties b/muleesb/src/main/app/mule-deploy.properties new file mode 100644 index 0000000000..07eabe9cc6 --- /dev/null +++ b/muleesb/src/main/app/mule-deploy.properties @@ -0,0 +1,6 @@ +#** GENERATED CONTENT ** Mule Application Deployment Descriptor +#Mon Nov 06 15:54:37 BDT 2017 +redeployment.enabled=true +encoding=UTF-8 +domain=default +config.resources=variablescopetest.xml diff --git a/muleesb/src/main/app/variablescopetest.xml b/muleesb/src/main/app/variablescopetest.xml new file mode 100644 index 0000000000..518b901084 --- /dev/null +++ b/muleesb/src/main/app/variablescopetest.xml @@ -0,0 +1,36 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/muleesb/src/main/java/com/baeldung/transformer/FromFlow2Component.java b/muleesb/src/main/java/com/baeldung/transformer/FromFlow2Component.java new file mode 100644 index 0000000000..0e180062a7 --- /dev/null +++ b/muleesb/src/main/java/com/baeldung/transformer/FromFlow2Component.java @@ -0,0 +1,18 @@ +package com.baeldung.transformer; + +import org.mule.api.MuleEventContext; +import org.mule.api.MuleMessage; +import org.mule.api.lifecycle.Callable; + +public class FromFlow2Component implements Callable { + + @Override + public Object onCall(MuleEventContext eventContext) throws Exception { + + MuleMessage message = eventContext.getMessage(); + message.setPayload("Converted in flow 2"); + + return message; + } + +} diff --git a/muleesb/src/main/java/com/baeldung/transformer/InitializationTransformer.java b/muleesb/src/main/java/com/baeldung/transformer/InitializationTransformer.java new file mode 100644 index 0000000000..1e1ad15be8 --- /dev/null +++ b/muleesb/src/main/java/com/baeldung/transformer/InitializationTransformer.java @@ -0,0 +1,29 @@ +package com.baeldung.transformer; + +import org.mule.api.MuleMessage; +import org.mule.api.transformer.TransformerException; +import org.mule.api.transport.PropertyScope; +import org.mule.transformer.AbstractMessageTransformer; + +public class InitializationTransformer extends AbstractMessageTransformer { + + @Override + public Object transformMessage(MuleMessage message, String outputEncoding) throws TransformerException { + // TODO Auto-generated method stub + + String payload = null; + + try { + payload = message.getPayloadAsString(); + }catch (Exception e) { + e.printStackTrace(); + } + + System.out.println("Logged Payload: "+payload); + message.setPayload("Payload from Initialization"); + message.setProperty("outboundKey", "outboundpropertyvalue",PropertyScope.OUTBOUND); + + + return message; + } +} \ No newline at end of file diff --git a/muleesb/src/main/java/com/baeldung/transformer/InvokingMessageComponent.java b/muleesb/src/main/java/com/baeldung/transformer/InvokingMessageComponent.java new file mode 100644 index 0000000000..105522e5b4 --- /dev/null +++ b/muleesb/src/main/java/com/baeldung/transformer/InvokingMessageComponent.java @@ -0,0 +1,16 @@ +package com.baeldung.transformer; + +import org.mule.api.MuleMessage; +import org.mule.api.transformer.TransformerException; +import org.mule.transformer.AbstractMessageTransformer; + +public class InvokingMessageComponent extends AbstractMessageTransformer { + + @Override + public Object transformMessage(MuleMessage message, String outputEncoding) throws TransformerException { + // TODO Auto-generated method stub + String InboundProp = (String) message.getInboundProperty("outboundKey"); + System.out.println("InboundProp:" + InboundProp); + return InboundProp; + } +} \ No newline at end of file diff --git a/muleesb/src/main/resources/log4j2.xml b/muleesb/src/main/resources/log4j2.xml new file mode 100644 index 0000000000..98c4b02433 --- /dev/null +++ b/muleesb/src/main/resources/log4j2.xml @@ -0,0 +1,33 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/muleesb/src/test/munit/variablescopetest-test-suite.xml b/muleesb/src/test/munit/variablescopetest-test-suite.xml new file mode 100644 index 0000000000..43e410a327 --- /dev/null +++ b/muleesb/src/test/munit/variablescopetest-test-suite.xml @@ -0,0 +1,40 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/muleesb/src/test/resources/log4j2-test.xml b/muleesb/src/test/resources/log4j2-test.xml new file mode 100644 index 0000000000..6351ae041c --- /dev/null +++ b/muleesb/src/test/resources/log4j2-test.xml @@ -0,0 +1,35 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file From 882ccae19096c2a871b4f683afa5fed8959a00bc Mon Sep 17 00:00:00 2001 From: Eugen Date: Thu, 30 Nov 2017 15:44:36 +0200 Subject: [PATCH 25/28] Update README.md --- README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.md b/README.md index d94a786bc2..271aea0767 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,14 @@ The "REST with Spring" Classes ============================== + After 5 months of work, here's the Master Class of REST With Spring:
**[>> THE REST WITH SPRING MASTER CLASS](http://www.baeldung.com/rest-with-spring-course?utm_source=github&utm_medium=social&utm_content=tutorials&utm_campaign=rws#master-class)** +And here's the Master Class of Learn Spring Security:
+**[>> LEARN SPRING SECURITY MASTER CLASS](http://www.baeldung.com/learn-spring-security-course?utm_source=github&utm_medium=social&utm_content=tutorials&utm_campaign=lss#master-class)** + + Spring Tutorials ================ From a8bbb67c53226b522ef7ec32e4646e562474dd76 Mon Sep 17 00:00:00 2001 From: Doha2012 Date: Thu, 30 Nov 2017 16:22:43 +0200 Subject: [PATCH 26/28] minor fix (#3163) * move security content from spring-security-rest-full * swagger update * move query language to new module * rename spring-security-rest-full to spring-rest-full * group persistence modules * group testing modules * try fix conflict * cleanup * group and cleanup * add readme to grouping modules * move spring-jpa to persistence-modules * minor fix --- .../src/main/java/org/baeldung/Application.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/spring-security-mvc-boot/src/main/java/org/baeldung/Application.java b/spring-security-mvc-boot/src/main/java/org/baeldung/Application.java index b3c98c3e71..8a40744bdc 100644 --- a/spring-security-mvc-boot/src/main/java/org/baeldung/Application.java +++ b/spring-security-mvc-boot/src/main/java/org/baeldung/Application.java @@ -5,12 +5,13 @@ import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.web.support.SpringBootServletInitializer; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.FilterType; @Configuration @EnableAutoConfiguration -@ComponentScan(excludeFilters = { @ComponentScan.Filter(type = FilterType.REGEX, pattern = "org.baeldung.voter.*"), @ComponentScan.Filter(type = FilterType.REGEX, pattern = "org.baeldung.multipleauthproviders.*"), - @ComponentScan.Filter(type = FilterType.REGEX, pattern = "org.baeldung.multiplelogin.*"), @ComponentScan.Filter(type = FilterType.REGEX, pattern = "org.baeldung.multipleentrypoints.*") }) +@ComponentScan({ "org.baeldung.config", "org.baeldung.persistence", "org.baeldung.security", "org.baeldung.web" }) +// @ComponentScan(excludeFilters = { @ComponentScan.Filter(type = FilterType.REGEX, pattern = "org.baeldung.voter.*"), @ComponentScan.Filter(type = FilterType.REGEX, pattern = "org.baeldung.multipleauthproviders.*"), +// @ComponentScan.Filter(type = FilterType.REGEX, pattern = "org.baeldung.multiplelogin.*"), @ComponentScan.Filter(type = FilterType.REGEX, pattern = "org.baeldung.multipleentrypoints.*"), +// @ComponentScan.Filter(type = FilterType.REGEX, pattern = "org.baeldung.rolesauthorities.*"), @ComponentScan.Filter(type = FilterType.REGEX, pattern = "org.baeldung.acl.*") }) public class Application extends SpringBootServletInitializer { public static void main(String[] args) { SpringApplication.run(Application.class, args); From 58f8e6163fc0bfc6c6ddbfb8d712fc930b11a0a4 Mon Sep 17 00:00:00 2001 From: ramansahasi Date: Fri, 1 Dec 2017 00:31:44 +0530 Subject: [PATCH 27/28] BAEL-1327 Java Threads: notify and wait (initial commit) (#3160) --- .../concurrent/waitandnotify/Data.java | 33 ++++++++++ .../waitandnotify/NetworkDriver.java | 12 ++++ .../concurrent/waitandnotify/Receiver.java | 25 +++++++ .../concurrent/waitandnotify/Sender.java | 30 +++++++++ .../waitandnotify/NetworkIntegrationTest.java | 65 +++++++++++++++++++ 5 files changed, 165 insertions(+) create mode 100644 core-java-concurrency/src/main/java/com/baeldung/concurrent/waitandnotify/Data.java create mode 100644 core-java-concurrency/src/main/java/com/baeldung/concurrent/waitandnotify/NetworkDriver.java create mode 100644 core-java-concurrency/src/main/java/com/baeldung/concurrent/waitandnotify/Receiver.java create mode 100644 core-java-concurrency/src/main/java/com/baeldung/concurrent/waitandnotify/Sender.java create mode 100644 core-java-concurrency/src/test/java/com/baeldung/concurrent/waitandnotify/NetworkIntegrationTest.java diff --git a/core-java-concurrency/src/main/java/com/baeldung/concurrent/waitandnotify/Data.java b/core-java-concurrency/src/main/java/com/baeldung/concurrent/waitandnotify/Data.java new file mode 100644 index 0000000000..9b850c4153 --- /dev/null +++ b/core-java-concurrency/src/main/java/com/baeldung/concurrent/waitandnotify/Data.java @@ -0,0 +1,33 @@ +package com.baeldung.concurrent.waitandnotify; + +public class Data { + private String packet; + + // True if receiver should wait + // False if sender should wait + private boolean transfer = true; + + public synchronized String receive() { + while (transfer) { + try { + wait(); + } catch (InterruptedException e) {} + } + transfer = true; + + notifyAll(); + return packet; + } + + public synchronized void send(String packet) { + while (!transfer) { + try { + wait(); + } catch (InterruptedException e) {} + } + transfer = false; + + this.packet = packet; + notifyAll(); + } +} \ No newline at end of file diff --git a/core-java-concurrency/src/main/java/com/baeldung/concurrent/waitandnotify/NetworkDriver.java b/core-java-concurrency/src/main/java/com/baeldung/concurrent/waitandnotify/NetworkDriver.java new file mode 100644 index 0000000000..d4fd1574c6 --- /dev/null +++ b/core-java-concurrency/src/main/java/com/baeldung/concurrent/waitandnotify/NetworkDriver.java @@ -0,0 +1,12 @@ +package com.baeldung.concurrent.waitandnotify; + +public class NetworkDriver { + public static void main(String[] args) { + Data data = new Data(); + Thread sender = new Thread(new Sender(data)); + Thread receiver = new Thread(new Receiver(data)); + + sender.start(); + receiver.start(); + } +} \ No newline at end of file diff --git a/core-java-concurrency/src/main/java/com/baeldung/concurrent/waitandnotify/Receiver.java b/core-java-concurrency/src/main/java/com/baeldung/concurrent/waitandnotify/Receiver.java new file mode 100644 index 0000000000..63f48b8031 --- /dev/null +++ b/core-java-concurrency/src/main/java/com/baeldung/concurrent/waitandnotify/Receiver.java @@ -0,0 +1,25 @@ +package com.baeldung.concurrent.waitandnotify; + +import java.util.concurrent.ThreadLocalRandom; + +public class Receiver implements Runnable { + private Data load; + + public Receiver(Data load) { + this.load = load; + } + + public void run() { + for(String receivedMessage = load.receive(); + !"End".equals(receivedMessage) ; + receivedMessage = load.receive()) { + + System.out.println(receivedMessage); + + //Thread.sleep() to mimic heavy server-side processing + try { + Thread.sleep(ThreadLocalRandom.current().nextInt(1000, 5000)); + } catch (InterruptedException e) {} + } + } +} \ No newline at end of file diff --git a/core-java-concurrency/src/main/java/com/baeldung/concurrent/waitandnotify/Sender.java b/core-java-concurrency/src/main/java/com/baeldung/concurrent/waitandnotify/Sender.java new file mode 100644 index 0000000000..b7d782c3f5 --- /dev/null +++ b/core-java-concurrency/src/main/java/com/baeldung/concurrent/waitandnotify/Sender.java @@ -0,0 +1,30 @@ +package com.baeldung.concurrent.waitandnotify; + +import java.util.concurrent.ThreadLocalRandom; + +public class Sender implements Runnable { + private Data data; + + public Sender(Data data) { + this.data = data; + } + + public void run() { + String packets[] = { + "First packet", + "Second packet", + "Third packet", + "Fourth packet", + "End" + }; + + for (String packet : packets) { + data.send(packet); + + //Thread.sleep() to mimic heavy server-side processing + try { + Thread.sleep(ThreadLocalRandom.current().nextInt(1000, 5000)); + } catch (InterruptedException e) {} + } + } +} \ No newline at end of file diff --git a/core-java-concurrency/src/test/java/com/baeldung/concurrent/waitandnotify/NetworkIntegrationTest.java b/core-java-concurrency/src/test/java/com/baeldung/concurrent/waitandnotify/NetworkIntegrationTest.java new file mode 100644 index 0000000000..49f4313e9d --- /dev/null +++ b/core-java-concurrency/src/test/java/com/baeldung/concurrent/waitandnotify/NetworkIntegrationTest.java @@ -0,0 +1,65 @@ +package com.baeldung.concurrent.waitandnotify; + +import static org.junit.Assert.assertEquals; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.io.PrintWriter; +import java.io.StringWriter; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class NetworkIntegrationTest { + + private final ByteArrayOutputStream outContent = new ByteArrayOutputStream(); + private final ByteArrayOutputStream errContent = new ByteArrayOutputStream(); + private String expected; + + @Before + public void setUpStreams() { + System.setOut(new PrintStream(outContent)); + System.setErr(new PrintStream(errContent)); + } + + @Before + public void setUpExpectedOutput() { + StringWriter expectedStringWriter = new StringWriter(); + + PrintWriter printWriter = new PrintWriter(expectedStringWriter); + printWriter.println("First packet"); + printWriter.println("Second packet"); + printWriter.println("Third packet"); + printWriter.println("Fourth packet"); + printWriter.close(); + + expected = expectedStringWriter.toString(); + } + + @After + public void cleanUpStreams() { + System.setOut(null); + System.setErr(null); + } + + @Test + public void givenSenderAndReceiver_whenSendingPackets_thenNetworkSynchronized() { + Data data = new Data(); + Thread sender = new Thread(new Sender(data)); + Thread receiver = new Thread(new Receiver(data)); + + sender.start(); + receiver.start(); + + //wait for sender and receiver to finish before we test against expected + try { + sender.join(); + receiver.join(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + + assertEquals(expected, outContent.toString()); + } +} From d45a891d4c2b6e295eb8d6324b0d6493c897b786 Mon Sep 17 00:00:00 2001 From: ocheja Date: Fri, 1 Dec 2017 05:07:23 +0900 Subject: [PATCH 28/28] BAEL-1261 Polymorphism in Java (#2762) * Define beans for handling different message types in a lean chat app * Add class based spring beans configuration * Define spring configuration in XML for constructor based bean injection * Refactor package structure to separate constructor based bean injection code set from setter based bean injection code set * Define configuration and classes specific to setter-based bean injection. * Implement tests for constructor-based and setter-based bean injections * develop codes for explaining type erasure * Write unit tests for type erasure examples * Remove evaluation article code * Modify type erasure examples and unit tests * Modify type erasure examples and unit tests * Add expected exception in TypeErasureUnitTest * Correct grammar in class name * Implement File Manager app to demonstrate Polymorphism. Develop unit tests for Polymorphism article code * Add examples for static polymorphism * Change sysout statments to slf4j log info statements * Add assertions and expected errors check on Test * Add assertions and expected errors check on Test * Correct compile time error of symbol not found * Removed commented out non-compiling test. --- .../baeldung/polymorphism/FileManager.java | 38 +++++++++++ .../baeldung/polymorphism/GenericFile.java | 63 +++++++++++++++++++ .../com/baeldung/polymorphism/ImageFile.java | 41 ++++++++++++ .../com/baeldung/polymorphism/TextFile.java | 44 +++++++++++++ .../polymorphism/PolymorphismUnitTest.java | 34 ++++++++++ 5 files changed, 220 insertions(+) create mode 100644 core-java/src/main/java/com/baeldung/polymorphism/FileManager.java create mode 100644 core-java/src/main/java/com/baeldung/polymorphism/GenericFile.java create mode 100644 core-java/src/main/java/com/baeldung/polymorphism/ImageFile.java create mode 100644 core-java/src/main/java/com/baeldung/polymorphism/TextFile.java create mode 100644 core-java/src/test/java/com/baeldung/polymorphism/PolymorphismUnitTest.java diff --git a/core-java/src/main/java/com/baeldung/polymorphism/FileManager.java b/core-java/src/main/java/com/baeldung/polymorphism/FileManager.java new file mode 100644 index 0000000000..7f2665ff2d --- /dev/null +++ b/core-java/src/main/java/com/baeldung/polymorphism/FileManager.java @@ -0,0 +1,38 @@ +package com.baeldung.polymorphism; + +import java.awt.image.BufferedImage; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class FileManager { + + final static Logger logger = LoggerFactory.getLogger(FileManager.class); + + public static void main(String[] args) { + GenericFile file1 = new TextFile("SampleTextFile", "This is a sample text content", "v1.0.0"); + logger.info("File Info: \n" + file1.getFileInfo() + "\n"); + ImageFile imageFile = new ImageFile("SampleImageFile", 200, 100, new BufferedImage(100, 200, BufferedImage.TYPE_INT_RGB).toString() + .getBytes(), "v1.0.0"); + logger.info("File Info: \n" + imageFile.getFileInfo()); + } + + public static ImageFile createImageFile(String name, int height, int width, byte[] content, String version) { + ImageFile imageFile = new ImageFile(name, height, width, content, version); + logger.info("File 2 Info: \n" + imageFile.getFileInfo()); + return imageFile; + } + + public static GenericFile createTextFile(String name, String content, String version) { + GenericFile file1 = new TextFile(name, content, version); + logger.info("File 1 Info: \n" + file1.getFileInfo() + "\n"); + return file1; + } + + public static TextFile createTextFile2(String name, String content, String version) { + TextFile file1 = new TextFile(name, content, version); + logger.info("File 1 Info: \n" + file1.getFileInfo() + "\n"); + return file1; + } + +} diff --git a/core-java/src/main/java/com/baeldung/polymorphism/GenericFile.java b/core-java/src/main/java/com/baeldung/polymorphism/GenericFile.java new file mode 100644 index 0000000000..4075083c49 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/polymorphism/GenericFile.java @@ -0,0 +1,63 @@ +package com.baeldung.polymorphism; + +import java.util.Date; + +public class GenericFile { + private String name; + private String extension; + private Date dateCreated; + private String version; + private byte[] content; + + public GenericFile() { + this.setDateCreated(new Date()); + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getExtension() { + return extension; + } + + public void setExtension(String extension) { + this.extension = extension; + } + + public Date getDateCreated() { + return dateCreated; + } + + public void setDateCreated(Date dateCreated) { + this.dateCreated = dateCreated; + } + + public String getVersion() { + return version; + } + + public void setVersion(String version) { + this.version = version; + } + + public byte[] getContent() { + return content; + } + + public void setContent(byte[] content) { + this.content = content; + } + + public String getFileInfo() { + return "File Name: " + this.getName() + "\n" + "Extension: " + this.getExtension() + "\n" + "Date Created: " + this.getDateCreated() + "\n" + "Version: " + this.getVersion() + "\n"; + } + + public Object read() { + return content; + } +} diff --git a/core-java/src/main/java/com/baeldung/polymorphism/ImageFile.java b/core-java/src/main/java/com/baeldung/polymorphism/ImageFile.java new file mode 100644 index 0000000000..ac72a40993 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/polymorphism/ImageFile.java @@ -0,0 +1,41 @@ +package com.baeldung.polymorphism; + +public class ImageFile extends GenericFile { + private int height; + private int width; + + public ImageFile(String name, int height, int width, byte[] content, String version) { + this.setHeight(height); + this.setWidth(width); + this.setContent(content); + this.setName(name); + this.setVersion(version); + this.setExtension(".jpg"); + } + + public int getHeight() { + return height; + } + + public void setHeight(int height) { + this.height = height; + } + + public int getWidth() { + return width; + } + + public void setWidth(int width) { + this.width = width; + } + + public String getFileInfo() { + return super.getFileInfo() + "Height: " + this.getHeight() + "\n" + "Width: " + this.getWidth(); + } + + public String read() { + return this.getContent() + .toString(); + } + +} diff --git a/core-java/src/main/java/com/baeldung/polymorphism/TextFile.java b/core-java/src/main/java/com/baeldung/polymorphism/TextFile.java new file mode 100644 index 0000000000..8280b4ee95 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/polymorphism/TextFile.java @@ -0,0 +1,44 @@ +package com.baeldung.polymorphism; + +public class TextFile extends GenericFile { + private int wordCount; + + public TextFile(String name, String content, String version) { + String[] words = content.split(" "); + this.setWordCount(words.length > 0 ? words.length : 1); + this.setContent(content.getBytes()); + this.setName(name); + this.setVersion(version); + this.setExtension(".txt"); + } + + public int getWordCount() { + return wordCount; + } + + public void setWordCount(int wordCount) { + this.wordCount = wordCount; + } + + public String getFileInfo() { + return super.getFileInfo() + "Word Count: " + wordCount; + } + + public String read() { + return this.getContent() + .toString(); + } + + public String read(int limit) { + return this.getContent() + .toString() + .substring(0, limit); + } + + public String read(int start, int stop) { + return this.getContent() + .toString() + .substring(start, stop); + } + +} diff --git a/core-java/src/test/java/com/baeldung/polymorphism/PolymorphismUnitTest.java b/core-java/src/test/java/com/baeldung/polymorphism/PolymorphismUnitTest.java new file mode 100644 index 0000000000..8fb606c2fc --- /dev/null +++ b/core-java/src/test/java/com/baeldung/polymorphism/PolymorphismUnitTest.java @@ -0,0 +1,34 @@ +package com.baeldung.polymorphism; + +import static org.junit.Assert.*; + +import java.awt.image.BufferedImage; + +import org.junit.Ignore; +import org.junit.Test; + +public class PolymorphismUnitTest { + + @Test + public void givenImageFile_whenFileCreated_shouldSucceed() { + ImageFile imageFile = FileManager.createImageFile("SampleImageFile", 200, 100, new BufferedImage(100, 200, BufferedImage.TYPE_INT_RGB).toString() + .getBytes(), "v1.0.0"); + assertEquals(200, imageFile.getHeight()); + } + + // Downcasting then Upcasting + @Test + public void givenTextFile_whenTextFileCreatedAndAssignedToGenericFileAndCastBackToTextFileOnGetWordCount_shouldSucceed() { + GenericFile textFile = FileManager.createTextFile("SampleTextFile", "This is a sample text content", "v1.0.0"); + TextFile textFile2 = (TextFile) textFile; + assertEquals(6, textFile2.getWordCount()); + } + + // Downcasting + @Test(expected = ClassCastException.class) + public void givenGenericFile_whenCastToTextFileAndInvokeGetWordCount_shouldFail() { + GenericFile genericFile = new GenericFile(); + TextFile textFile = (TextFile) genericFile; + System.out.println(textFile.getWordCount()); + } +}