From 0dab47c76d3a5c8e6a8239929301e86b7a993545 Mon Sep 17 00:00:00 2001 From: Senthil Kumar Subramanian Date: Sun, 25 Mar 2018 21:06:38 +0530 Subject: [PATCH 1/4] Spring-dependency injection --- Spring-DependencyInjection/pom.xml | 30 +++++++++ .../src/main/java/beans.xml | 31 ++++++++++ .../com/baeldung/spring/core/Address.java | 53 ++++++++++++++++ .../java/com/baeldung/spring/core/Car.java | 30 +++++++++ .../com/baeldung/spring/core/Customer.java | 61 +++++++++++++++++++ .../com/baeldung/spring/core/Department.java | 31 ++++++++++ .../com/baeldung/spring/core/Employee.java | 23 +++++++ .../com/baeldung/spring/core/Product.java | 48 +++++++++++++++ .../com/baeldung/spring/core/Student.java | 40 ++++++++++++ .../com/baeldung/spring/core/Subject.java | 30 +++++++++ .../spring/core/config/AppConfig.java | 30 +++++++++ .../AnnotationApplicationContextTest.java | 17 ++++++ .../JavaConfigApplicationContextTest.java | 23 +++++++ ...MLConfigurationApplicationContextTest.java | 28 +++++++++ .../Spring_DependencyInjection/AppTest.java | 38 ++++++++++++ 15 files changed, 513 insertions(+) create mode 100644 Spring-DependencyInjection/pom.xml create mode 100644 Spring-DependencyInjection/src/main/java/beans.xml create mode 100644 Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/Address.java create mode 100644 Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/Car.java create mode 100644 Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/Customer.java create mode 100644 Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/Department.java create mode 100644 Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/Employee.java create mode 100644 Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/Product.java create mode 100644 Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/Student.java create mode 100644 Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/Subject.java create mode 100644 Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/config/AppConfig.java create mode 100644 Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/test/AnnotationApplicationContextTest.java create mode 100644 Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/test/JavaConfigApplicationContextTest.java create mode 100644 Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/test/XMLConfigurationApplicationContextTest.java create mode 100644 Spring-DependencyInjection/src/test/java/com/spring/core/Spring_DependencyInjection/AppTest.java diff --git a/Spring-DependencyInjection/pom.xml b/Spring-DependencyInjection/pom.xml new file mode 100644 index 0000000000..cc818f948b --- /dev/null +++ b/Spring-DependencyInjection/pom.xml @@ -0,0 +1,30 @@ + + 4.0.0 + + com.spring.core + Spring-DependencyInjection + 0.0.1-SNAPSHOT + jar + + Spring-DependencyInjection + http://maven.apache.org + + + UTF-8 + + + + + org.springframework + spring-context + 4.3.14.RELEASE + + + junit + junit + 3.8.1 + test + + + diff --git a/Spring-DependencyInjection/src/main/java/beans.xml b/Spring-DependencyInjection/src/main/java/beans.xml new file mode 100644 index 0000000000..71de8d3293 --- /dev/null +++ b/Spring-DependencyInjection/src/main/java/beans.xml @@ -0,0 +1,31 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/Address.java b/Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/Address.java new file mode 100644 index 0000000000..9e6fe29c0c --- /dev/null +++ b/Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/Address.java @@ -0,0 +1,53 @@ +package com.baeldung.spring.core; + +import org.springframework.stereotype.Component; + +@Component +public class Address { + + private String address1; + + private String city; + + private String state; + + private String country; + + public String getAddress1() { + return address1; + } + + public void setAddress1(String address1) { + this.address1 = address1; + } + + public String getCity() { + return city; + } + + public void setCity(String city) { + this.city = city; + } + + public String getState() { + return state; + } + + public void setState(String state) { + this.state = state; + } + + public String getCountry() { + return country; + } + + public void setCountry(String country) { + this.country = country; + } + + @Override + public String toString() { + return "Address [address1=" + address1 + ", city=" + city + ", state=" + state + ", country=" + country + "]"; + } + +} diff --git a/Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/Car.java b/Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/Car.java new file mode 100644 index 0000000000..3f940e1e28 --- /dev/null +++ b/Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/Car.java @@ -0,0 +1,30 @@ +package com.baeldung.spring.core; + +public class Car { + + private String carName; + + private String carType; + + public String getCarName() { + return carName; + } + + public void setCarName(String carName) { + this.carName = carName; + } + + public String getCarType() { + return carType; + } + + public void setCarType(String carType) { + this.carType = carType; + } + + @Override + public String toString() { + return "Car [carName=" + carName + ", carType=" + carType + "]"; + } + +} diff --git a/Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/Customer.java b/Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/Customer.java new file mode 100644 index 0000000000..da9ac13021 --- /dev/null +++ b/Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/Customer.java @@ -0,0 +1,61 @@ +package com.baeldung.spring.core; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; + +@Component +public class Customer { + + @Value("Thomas") + private String customerName; + + @Value("Cust123") + private String customerId; + + private Product product; + + private Address address; + + @Autowired + public Customer(Address address) { + super(); + this.address = address; + } + + public String getCustomerName() { + return customerName; + } + + public void setCustomerName(String customerName) { + this.customerName = customerName; + } + + public String getCustomerId() { + return customerId; + } + + public void setCustomerId(String customerId) { + this.customerId = customerId; + } + + public Product getProduct() { + return product; + } + + @Autowired + public void setProduct(Product product) { + this.product = product; + } + + public Address getAddress() { + return address; + } + + @Override + public String toString() { + return "Customer [customerName=" + customerName + ", customerId=" + customerId + ", product=" + product + + ", address=" + address + "]"; + } + +} diff --git a/Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/Department.java b/Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/Department.java new file mode 100644 index 0000000000..f7da2d560a --- /dev/null +++ b/Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/Department.java @@ -0,0 +1,31 @@ +package com.baeldung.spring.core; + +import org.springframework.stereotype.Component; + +@Component +public class Department { + + private String departmentName; + + private String departmentId; + + public Department(String departmentName, String departmentNumber) { + super(); + this.departmentName = departmentName; + this.departmentId = departmentNumber; + } + + public String getDepartmentName() { + return departmentName; + } + + public String getDepartmentId() { + return departmentId; + } + + @Override + public String toString() { + return "Department [departmentName=" + departmentName + ", departmentNumber=" + departmentId + "]"; + } + +} diff --git a/Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/Employee.java b/Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/Employee.java new file mode 100644 index 0000000000..de450b8e6b --- /dev/null +++ b/Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/Employee.java @@ -0,0 +1,23 @@ +package com.baeldung.spring.core; + +import org.springframework.stereotype.Component; + +@Component +public class Employee { + + private Department department; + + public Employee(Department department) { + super(); + this.department = department; + } + + public Department getDepartment() { + return department; + } + + @Override + public String toString() { + return "Employee [department=" + department + "]"; + } +} diff --git a/Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/Product.java b/Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/Product.java new file mode 100644 index 0000000000..162c9b3429 --- /dev/null +++ b/Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/Product.java @@ -0,0 +1,48 @@ +package com.baeldung.spring.core; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; + +@Component +public class Product { + + @Value("product1") + private String productName; + + @Value("Pro123") + private String productId; + + @Value("100") + private String productCost; + + public String getProductName() { + return productName; + } + + public void setProductName(String productName) { + this.productName = productName; + } + + public String getProductId() { + return productId; + } + + public void setProductId(String productId) { + this.productId = productId; + } + + public String getProductCost() { + return productCost; + } + + public void setProductCost(String productCost) { + this.productCost = productCost; + } + + @Override + public String toString() { + return "Product [productName=" + productName + ", productId=" + productId + ", productCost=" + productCost + + "]"; + } + +} diff --git a/Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/Student.java b/Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/Student.java new file mode 100644 index 0000000000..f2563f01ec --- /dev/null +++ b/Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/Student.java @@ -0,0 +1,40 @@ +package com.baeldung.spring.core; + +public class Student { + + private String studentName; + + private String studentId; + + private Subject subject; + + public String getStudentName() { + return studentName; + } + + public void setStudentName(String studentName) { + this.studentName = studentName; + } + + public String getStudentId() { + return studentId; + } + + public void setStudentId(String studentId) { + this.studentId = studentId; + } + + public Subject getSubject() { + return subject; + } + + public void setSubject(Subject subject) { + this.subject = subject; + } + + @Override + public String toString() { + return "Student [studentName=" + studentName + ", studentId=" + studentId + ", subject=" + subject + "]"; + } + +} diff --git a/Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/Subject.java b/Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/Subject.java new file mode 100644 index 0000000000..b160bbef18 --- /dev/null +++ b/Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/Subject.java @@ -0,0 +1,30 @@ +package com.baeldung.spring.core; + +public class Subject { + + private String subjectName; + + private String subjectId; + + public String getSubjectName() { + return subjectName; + } + + public void setSubjectName(String subjectName) { + this.subjectName = subjectName; + } + + public String getSubjectId() { + return subjectId; + } + + public void setSubjectId(String subjectId) { + this.subjectId = subjectId; + } + + @Override + public String toString() { + return "Subject [subjectName=" + subjectName + ", subjectId=" + subjectId + "]"; + } + +} diff --git a/Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/config/AppConfig.java b/Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/config/AppConfig.java new file mode 100644 index 0000000000..496191d758 --- /dev/null +++ b/Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/config/AppConfig.java @@ -0,0 +1,30 @@ +package com.baeldung.spring.core.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import com.baeldung.spring.core.Car; +import com.baeldung.spring.core.Department; +import com.baeldung.spring.core.Employee; + +@Configuration +public class AppConfig { + + @Bean + public Department getDepartment() { + return new Department("Sales", "S01"); + } + + @Bean + public Employee getEmployee() { + return new Employee(getDepartment()); + } + + @Bean + public Car getCar() { + Car car = new Car(); + car.setCarName("Honda"); + car.setCarType("SUV"); + return car; + } +} diff --git a/Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/test/AnnotationApplicationContextTest.java b/Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/test/AnnotationApplicationContextTest.java new file mode 100644 index 0000000000..02daf26eb2 --- /dev/null +++ b/Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/test/AnnotationApplicationContextTest.java @@ -0,0 +1,17 @@ +package com.baeldung.spring.core.test; + +import org.springframework.context.support.AbstractApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +import com.baeldung.spring.core.Customer; + +public class AnnotationApplicationContextTest { + + public static void main(String[] args) { + AbstractApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml"); + Customer customer = applicationContext.getBean(Customer.class); + System.out.println("Customer is :: " + customer); + System.out.println("Product is :: " + customer.getProduct()); + applicationContext.close(); + } +} diff --git a/Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/test/JavaConfigApplicationContextTest.java b/Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/test/JavaConfigApplicationContextTest.java new file mode 100644 index 0000000000..01b0c67675 --- /dev/null +++ b/Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/test/JavaConfigApplicationContextTest.java @@ -0,0 +1,23 @@ +package com.baeldung.spring.core.test; + +import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.context.support.AbstractApplicationContext; + +import com.baeldung.spring.core.Car; +import com.baeldung.spring.core.Department; +import com.baeldung.spring.core.Employee; +import com.baeldung.spring.core.config.AppConfig; + +public class JavaConfigApplicationContextTest { + + public static void main(String[] args) { + AbstractApplicationContext applicationContext = new AnnotationConfigApplicationContext(AppConfig.class); + Department department = applicationContext.getBean(Department.class); + Employee employee = applicationContext.getBean(Employee.class); + System.out.println("Department is :: " + department); + System.out.println("Employee is :: " + employee); + Car car = applicationContext.getBean(Car.class); + System.out.println("Car is :: " + car); + applicationContext.close(); + } +} diff --git a/Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/test/XMLConfigurationApplicationContextTest.java b/Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/test/XMLConfigurationApplicationContextTest.java new file mode 100644 index 0000000000..ead4226f1b --- /dev/null +++ b/Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/test/XMLConfigurationApplicationContextTest.java @@ -0,0 +1,28 @@ +package com.baeldung.spring.core.test; + +import org.springframework.context.support.AbstractApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +import com.baeldung.spring.core.Department; +import com.baeldung.spring.core.Employee; +import com.baeldung.spring.core.Student; +import com.baeldung.spring.core.Subject; + +public class XMLConfigurationApplicationContextTest { + + public static void main(String[] args) { + AbstractApplicationContext abstractApplicationContext = new ClassPathXmlApplicationContext("beans.xml"); + // Constructor Injection + Department department = (Department) abstractApplicationContext.getBean("department"); + Employee employee = (Employee) abstractApplicationContext.getBean("employee"); + System.out.println("Department is :: " + department); + System.out.println("Employee is :: " + employee); + + // Setter Injection + Subject subject = (Subject) abstractApplicationContext.getBean("subject"); + Student student = (Student) abstractApplicationContext.getBean("student"); + System.out.println("Subject is :: " + subject); + System.out.println("Student is :: " + student); + abstractApplicationContext.close(); + } +} diff --git a/Spring-DependencyInjection/src/test/java/com/spring/core/Spring_DependencyInjection/AppTest.java b/Spring-DependencyInjection/src/test/java/com/spring/core/Spring_DependencyInjection/AppTest.java new file mode 100644 index 0000000000..5b3c551257 --- /dev/null +++ b/Spring-DependencyInjection/src/test/java/com/spring/core/Spring_DependencyInjection/AppTest.java @@ -0,0 +1,38 @@ +package com.spring.core.Spring_DependencyInjection; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +/** + * Unit test for simple App. + */ +public class AppTest + extends TestCase +{ + /** + * Create the test case + * + * @param testName name of the test case + */ + public AppTest( String testName ) + { + super( testName ); + } + + /** + * @return the suite of tests being tested + */ + public static Test suite() + { + return new TestSuite( AppTest.class ); + } + + /** + * Rigourous Test :-) + */ + public void testApp() + { + assertTrue( true ); + } +} From b2dfcd9469ea94c3d339e145d6df5aa17d79d63f Mon Sep 17 00:00:00 2001 From: Senthil Kumar Subramanian Date: Sun, 8 Apr 2018 16:39:10 +0530 Subject: [PATCH 2/4] BAEL-1683-Guide to Java Clock class --- .../java/com/baeldung/clock/ClockExample.java | 83 +++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 core-java/src/main/java/com/baeldung/clock/ClockExample.java diff --git a/core-java/src/main/java/com/baeldung/clock/ClockExample.java b/core-java/src/main/java/com/baeldung/clock/ClockExample.java new file mode 100644 index 0000000000..c28f9102a6 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/clock/ClockExample.java @@ -0,0 +1,83 @@ +package com.baeldung.clock; + +import java.time.Clock; +import java.time.Duration; +import java.time.Instant; +import java.time.ZoneId; + +public class ClockExample { + + public static void main(String[] args) { + + // gets systemUTC + Clock clockUTC = Clock.systemUTC(); + System.out.println("UTC time :: " + clockUTC.instant()); + + // using system() + Clock clockSystem = Clock.system(ZoneId.of("Asia/Kolkata")); + System.out.println(clockSystem.instant()); + + // gets systemDefaultZone + Clock clockSystemDefault = Clock.systemDefaultZone(); + System.out.println(clockSystemDefault); + clockSystemDefault = Clock.system(ZoneId.systemDefault()); + System.out.println(clockSystemDefault); + + // gets instant from system default zone + Clock clockInstant = Clock.systemDefaultZone(); + Instant instant = clockInstant.instant(); + System.out.println(instant); + + // gets millis + Clock millis = Clock.systemDefaultZone(); + System.out.println(millis.millis()); + System.out.println(System.currentTimeMillis()); + + // using offset + + Clock baseClock = Clock.systemDefaultZone(); + // result clock will be later than baseClock + Clock clock5 = Clock.offset(baseClock, Duration.ofHours(72)); + System.out.println(clock5.instant()); + + // result clock will be same as baseClock + clock5 = Clock.offset(baseClock, Duration.ZERO); + System.out.println(clock5.instant()); + + // result clock will be earlier than baseClock + clock5 = Clock.offset(baseClock, Duration.ofHours(-72)); + System.out.println(clock5.instant()); + + // using tick + Clock clockDefaultZone = Clock.systemDefaultZone(); + Clock clocktick = Clock.tick(clockDefaultZone, Duration.ofSeconds(30)); + System.out.println("Clock Default Zone : " + clockDefaultZone.instant()); + System.out.println("Clock tick : " + clocktick.instant()); + + // using tickMinutes + Clock tickMinutes = Clock.tickMinutes(ZoneId.of("Asia/Kolkata")); + System.out.println(tickMinutes.instant()); + tickMinutes = Clock.tick(Clock.system(ZoneId.of("Asia/Kolkata")), Duration.ofMinutes(1)); + System.out.println(tickMinutes.instant()); + + // using tickSeconds + ZoneId zoneId = ZoneId.of("Asia/Calcutta"); + Clock tickSeconds = Clock.tickSeconds(zoneId); + System.out.println(tickSeconds.instant()); + tickSeconds = Clock.tick(Clock.system(ZoneId.of("Asia/Kolkata")), Duration.ofSeconds(1)); + System.out.println(tickSeconds.instant()); + + // using withZone + ZoneId zone1 = ZoneId.of("Asia/Singapore"); + Clock clock11 = Clock.system(zone1); + System.out.println(clock11.instant()); + + ZoneId zone2 = ZoneId.of("Asia/Kolkata"); + Clock clock21 = clock11.withZone(zone2); + System.out.println(clock21.instant()); + + ZoneId zone = clockDefaultZone.getZone(); + System.out.println(zone.getId()); + + } +} From b9d21e3210738acd9ea80da2c247f0ff0305dbd7 Mon Sep 17 00:00:00 2001 From: Senthil Kumar Subramanian Date: Thu, 12 Apr 2018 07:42:42 +0530 Subject: [PATCH 3/4] BAEL-1683-Guide to Java Clock class --- .../java/com/baeldung/clock/ClockExample.java | 83 --------- .../baeldung/java/clock/ClockUnitTest.java | 159 ++++++++++++++++++ 2 files changed, 159 insertions(+), 83 deletions(-) delete mode 100644 core-java/src/main/java/com/baeldung/clock/ClockExample.java create mode 100644 core-java/src/test/java/com/baeldung/java/clock/ClockUnitTest.java diff --git a/core-java/src/main/java/com/baeldung/clock/ClockExample.java b/core-java/src/main/java/com/baeldung/clock/ClockExample.java deleted file mode 100644 index c28f9102a6..0000000000 --- a/core-java/src/main/java/com/baeldung/clock/ClockExample.java +++ /dev/null @@ -1,83 +0,0 @@ -package com.baeldung.clock; - -import java.time.Clock; -import java.time.Duration; -import java.time.Instant; -import java.time.ZoneId; - -public class ClockExample { - - public static void main(String[] args) { - - // gets systemUTC - Clock clockUTC = Clock.systemUTC(); - System.out.println("UTC time :: " + clockUTC.instant()); - - // using system() - Clock clockSystem = Clock.system(ZoneId.of("Asia/Kolkata")); - System.out.println(clockSystem.instant()); - - // gets systemDefaultZone - Clock clockSystemDefault = Clock.systemDefaultZone(); - System.out.println(clockSystemDefault); - clockSystemDefault = Clock.system(ZoneId.systemDefault()); - System.out.println(clockSystemDefault); - - // gets instant from system default zone - Clock clockInstant = Clock.systemDefaultZone(); - Instant instant = clockInstant.instant(); - System.out.println(instant); - - // gets millis - Clock millis = Clock.systemDefaultZone(); - System.out.println(millis.millis()); - System.out.println(System.currentTimeMillis()); - - // using offset - - Clock baseClock = Clock.systemDefaultZone(); - // result clock will be later than baseClock - Clock clock5 = Clock.offset(baseClock, Duration.ofHours(72)); - System.out.println(clock5.instant()); - - // result clock will be same as baseClock - clock5 = Clock.offset(baseClock, Duration.ZERO); - System.out.println(clock5.instant()); - - // result clock will be earlier than baseClock - clock5 = Clock.offset(baseClock, Duration.ofHours(-72)); - System.out.println(clock5.instant()); - - // using tick - Clock clockDefaultZone = Clock.systemDefaultZone(); - Clock clocktick = Clock.tick(clockDefaultZone, Duration.ofSeconds(30)); - System.out.println("Clock Default Zone : " + clockDefaultZone.instant()); - System.out.println("Clock tick : " + clocktick.instant()); - - // using tickMinutes - Clock tickMinutes = Clock.tickMinutes(ZoneId.of("Asia/Kolkata")); - System.out.println(tickMinutes.instant()); - tickMinutes = Clock.tick(Clock.system(ZoneId.of("Asia/Kolkata")), Duration.ofMinutes(1)); - System.out.println(tickMinutes.instant()); - - // using tickSeconds - ZoneId zoneId = ZoneId.of("Asia/Calcutta"); - Clock tickSeconds = Clock.tickSeconds(zoneId); - System.out.println(tickSeconds.instant()); - tickSeconds = Clock.tick(Clock.system(ZoneId.of("Asia/Kolkata")), Duration.ofSeconds(1)); - System.out.println(tickSeconds.instant()); - - // using withZone - ZoneId zone1 = ZoneId.of("Asia/Singapore"); - Clock clock11 = Clock.system(zone1); - System.out.println(clock11.instant()); - - ZoneId zone2 = ZoneId.of("Asia/Kolkata"); - Clock clock21 = clock11.withZone(zone2); - System.out.println(clock21.instant()); - - ZoneId zone = clockDefaultZone.getZone(); - System.out.println(zone.getId()); - - } -} diff --git a/core-java/src/test/java/com/baeldung/java/clock/ClockUnitTest.java b/core-java/src/test/java/com/baeldung/java/clock/ClockUnitTest.java new file mode 100644 index 0000000000..e83ba7afc8 --- /dev/null +++ b/core-java/src/test/java/com/baeldung/java/clock/ClockUnitTest.java @@ -0,0 +1,159 @@ +package com.baeldung.java.clock; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.time.Clock; +import java.time.Duration; +import java.time.ZoneId; +import java.time.ZoneOffset; + +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class ClockUnitTest { + + private static final Logger LOGGER = LoggerFactory.getLogger(ClockUnitTest.class); + + @Test + public void givenClock_withSytemUTC_retrievesInstant() { + + Clock clockUTC = Clock.systemUTC(); + + assertEquals(clockUTC.getZone(), ZoneOffset.UTC); + assertEquals(clockUTC.instant().equals(null), false); + + LOGGER.debug("UTC instant :: " + clockUTC.instant()); + } + + @Test + public void givenClock_withSytem_retrievesInstant() { + + Clock clockSystem = Clock.system(ZoneId.of("Asia/Calcutta")); + + assertEquals(clockSystem.getZone(), ZoneId.of("Asia/Calcutta")); + assertEquals(clockSystem.instant().equals(null), false); + + LOGGER.debug("System zone :: " + clockSystem.getZone()); + } + + @Test + public void givenClock_withSytemDefaultZone_retrievesInstant() { + + Clock clockSystemDefault = Clock.systemDefaultZone(); + + assertEquals(clockSystemDefault.getZone().equals(null), false); + assertEquals(clockSystemDefault.instant().equals(null), false); + + LOGGER.debug("System Default instant :: " + clockSystemDefault.instant()); + } + + @Test + public void givenClock_withSytemUTC_retrievesTimeInMillis() { + + Clock clockMillis = Clock.systemDefaultZone(); + + assertEquals(clockMillis.instant().equals(null), false); + assertTrue(clockMillis.millis() > 0); + + LOGGER.debug("System Default millis :: " + clockMillis.millis()); + } + + @Test + public void givenClock_usingOffset_retrievesFutureDate() { + + Clock baseClock = Clock.systemDefaultZone(); + + // result clock will be later than baseClock + Clock futureClock = Clock.offset(baseClock, Duration.ofHours(72)); + + assertEquals(futureClock.instant().equals(null), false); + assertTrue(futureClock.millis() > baseClock.millis()); + + LOGGER.debug("Future Clock instant :: " + futureClock.instant()); + } + + @Test + public void givenClock_usingOffset_retrievesPastDate() { + Clock baseClock = Clock.systemDefaultZone(); + + // result clock will be later than baseClock + Clock pastClock = Clock.offset(baseClock, Duration.ofHours(-72)); + + assertEquals(pastClock.instant().equals(null), false); + assertTrue(pastClock.millis() < baseClock.millis()); + + LOGGER.debug("Past Clock instant :: " + pastClock.instant()); + } + + @Test + public void givenClock_usingTick_retrievesInstant() { + Clock clockDefaultZone = Clock.systemDefaultZone(); + Clock clocktick = Clock.tick(clockDefaultZone, Duration.ofSeconds(300)); + + assertEquals(clockDefaultZone.instant().equals(null), false); + assertEquals(clocktick.instant().equals(null), false); + assertTrue(clockDefaultZone.millis() > clocktick.millis()); + + LOGGER.debug("Clock Default Zone instant : " + clockDefaultZone.instant()); + LOGGER.debug("Clock tick instant: " + clocktick.instant()); + } + + @Test(expected=IllegalArgumentException.class) + public void givenClock_usingTickDurationNegative_throwsException() { + + Clock clockDefaultZone = Clock.systemDefaultZone(); + Clock.tick(clockDefaultZone, Duration.ofSeconds(-300)); + + } + + @Test + public void givenClock_usingTickSeconds_retrievesInstant() { + ZoneId zoneId = ZoneId.of("Asia/Calcutta"); + Clock tickSeconds = Clock.tickSeconds(zoneId); + + assertEquals(tickSeconds.instant().equals(null), false); + LOGGER.debug("Clock tick seconds instant :: " + tickSeconds.instant()); + + tickSeconds = Clock.tick(Clock.system(ZoneId.of("Asia/Calcutta")), Duration.ofSeconds(100)); + assertEquals(tickSeconds.instant().equals(null), false); + } + + @Test + public void givenClock_usingTickMinutes_retrievesInstant() { + + Clock tickMinutes = Clock.tickMinutes(ZoneId.of("Asia/Calcutta")); + + assertEquals(tickMinutes.instant().equals(null), false); + LOGGER.debug("Clock tick seconds instant :: " + tickMinutes.instant()); + + tickMinutes = Clock.tick(Clock.system(ZoneId.of("Asia/Calcutta")), Duration.ofMinutes(5)); + assertEquals(tickMinutes.instant().equals(null), false); + } + + @Test + public void givenClock_usingWithZone_retrievesInstant() { + + ZoneId zoneSingapore = ZoneId.of("Asia/Singapore"); + Clock clockSingapore = Clock.system(zoneSingapore); + + assertEquals(clockSingapore.instant().equals(null), false); + LOGGER.debug("clockSingapore instant : " + clockSingapore.instant()); + + ZoneId zoneCalcutta = ZoneId.of("Asia/Calcutta"); + Clock clockCalcutta = clockSingapore.withZone(zoneCalcutta); + assertEquals(clockCalcutta.instant().equals(null), false); + LOGGER.debug("clockCalcutta instant : " + clockSingapore.instant()); + } + + @Test + public void givenClock_usingGetZone_retrievesZoneId() { + + Clock clockDefaultZone = Clock.systemDefaultZone(); + ZoneId zone = clockDefaultZone.getZone(); + + assertEquals(zone.getId().equals(null), false); + LOGGER.debug("Default zone instant : " + clockDefaultZone.instant()); + } +} From 8e0b36a8f90a93d91512185139e6efa573d2b626 Mon Sep 17 00:00:00 2001 From: Tom Hombergs Date: Sat, 21 Apr 2018 22:49:05 +0200 Subject: [PATCH 4/4] removed folder "SpringDependencyInjection" e Please enter the commit message for your changes. Lines starting --- Spring-DependencyInjection/pom.xml | 30 --------- .../src/main/java/beans.xml | 31 ---------- .../com/baeldung/spring/core/Address.java | 53 ---------------- .../java/com/baeldung/spring/core/Car.java | 30 --------- .../com/baeldung/spring/core/Customer.java | 61 ------------------- .../com/baeldung/spring/core/Department.java | 31 ---------- .../com/baeldung/spring/core/Employee.java | 23 ------- .../com/baeldung/spring/core/Product.java | 48 --------------- .../com/baeldung/spring/core/Student.java | 40 ------------ .../com/baeldung/spring/core/Subject.java | 30 --------- .../spring/core/config/AppConfig.java | 30 --------- .../AnnotationApplicationContextTest.java | 17 ------ .../JavaConfigApplicationContextTest.java | 23 ------- ...MLConfigurationApplicationContextTest.java | 28 --------- .../Spring_DependencyInjection/AppTest.java | 38 ------------ 15 files changed, 513 deletions(-) delete mode 100644 Spring-DependencyInjection/pom.xml delete mode 100644 Spring-DependencyInjection/src/main/java/beans.xml delete mode 100644 Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/Address.java delete mode 100644 Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/Car.java delete mode 100644 Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/Customer.java delete mode 100644 Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/Department.java delete mode 100644 Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/Employee.java delete mode 100644 Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/Product.java delete mode 100644 Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/Student.java delete mode 100644 Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/Subject.java delete mode 100644 Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/config/AppConfig.java delete mode 100644 Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/test/AnnotationApplicationContextTest.java delete mode 100644 Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/test/JavaConfigApplicationContextTest.java delete mode 100644 Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/test/XMLConfigurationApplicationContextTest.java delete mode 100644 Spring-DependencyInjection/src/test/java/com/spring/core/Spring_DependencyInjection/AppTest.java diff --git a/Spring-DependencyInjection/pom.xml b/Spring-DependencyInjection/pom.xml deleted file mode 100644 index cc818f948b..0000000000 --- a/Spring-DependencyInjection/pom.xml +++ /dev/null @@ -1,30 +0,0 @@ - - 4.0.0 - - com.spring.core - Spring-DependencyInjection - 0.0.1-SNAPSHOT - jar - - Spring-DependencyInjection - http://maven.apache.org - - - UTF-8 - - - - - org.springframework - spring-context - 4.3.14.RELEASE - - - junit - junit - 3.8.1 - test - - - diff --git a/Spring-DependencyInjection/src/main/java/beans.xml b/Spring-DependencyInjection/src/main/java/beans.xml deleted file mode 100644 index 71de8d3293..0000000000 --- a/Spring-DependencyInjection/src/main/java/beans.xml +++ /dev/null @@ -1,31 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/Address.java b/Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/Address.java deleted file mode 100644 index 9e6fe29c0c..0000000000 --- a/Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/Address.java +++ /dev/null @@ -1,53 +0,0 @@ -package com.baeldung.spring.core; - -import org.springframework.stereotype.Component; - -@Component -public class Address { - - private String address1; - - private String city; - - private String state; - - private String country; - - public String getAddress1() { - return address1; - } - - public void setAddress1(String address1) { - this.address1 = address1; - } - - public String getCity() { - return city; - } - - public void setCity(String city) { - this.city = city; - } - - public String getState() { - return state; - } - - public void setState(String state) { - this.state = state; - } - - public String getCountry() { - return country; - } - - public void setCountry(String country) { - this.country = country; - } - - @Override - public String toString() { - return "Address [address1=" + address1 + ", city=" + city + ", state=" + state + ", country=" + country + "]"; - } - -} diff --git a/Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/Car.java b/Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/Car.java deleted file mode 100644 index 3f940e1e28..0000000000 --- a/Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/Car.java +++ /dev/null @@ -1,30 +0,0 @@ -package com.baeldung.spring.core; - -public class Car { - - private String carName; - - private String carType; - - public String getCarName() { - return carName; - } - - public void setCarName(String carName) { - this.carName = carName; - } - - public String getCarType() { - return carType; - } - - public void setCarType(String carType) { - this.carType = carType; - } - - @Override - public String toString() { - return "Car [carName=" + carName + ", carType=" + carType + "]"; - } - -} diff --git a/Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/Customer.java b/Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/Customer.java deleted file mode 100644 index da9ac13021..0000000000 --- a/Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/Customer.java +++ /dev/null @@ -1,61 +0,0 @@ -package com.baeldung.spring.core; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.stereotype.Component; - -@Component -public class Customer { - - @Value("Thomas") - private String customerName; - - @Value("Cust123") - private String customerId; - - private Product product; - - private Address address; - - @Autowired - public Customer(Address address) { - super(); - this.address = address; - } - - public String getCustomerName() { - return customerName; - } - - public void setCustomerName(String customerName) { - this.customerName = customerName; - } - - public String getCustomerId() { - return customerId; - } - - public void setCustomerId(String customerId) { - this.customerId = customerId; - } - - public Product getProduct() { - return product; - } - - @Autowired - public void setProduct(Product product) { - this.product = product; - } - - public Address getAddress() { - return address; - } - - @Override - public String toString() { - return "Customer [customerName=" + customerName + ", customerId=" + customerId + ", product=" + product - + ", address=" + address + "]"; - } - -} diff --git a/Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/Department.java b/Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/Department.java deleted file mode 100644 index f7da2d560a..0000000000 --- a/Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/Department.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.baeldung.spring.core; - -import org.springframework.stereotype.Component; - -@Component -public class Department { - - private String departmentName; - - private String departmentId; - - public Department(String departmentName, String departmentNumber) { - super(); - this.departmentName = departmentName; - this.departmentId = departmentNumber; - } - - public String getDepartmentName() { - return departmentName; - } - - public String getDepartmentId() { - return departmentId; - } - - @Override - public String toString() { - return "Department [departmentName=" + departmentName + ", departmentNumber=" + departmentId + "]"; - } - -} diff --git a/Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/Employee.java b/Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/Employee.java deleted file mode 100644 index de450b8e6b..0000000000 --- a/Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/Employee.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.baeldung.spring.core; - -import org.springframework.stereotype.Component; - -@Component -public class Employee { - - private Department department; - - public Employee(Department department) { - super(); - this.department = department; - } - - public Department getDepartment() { - return department; - } - - @Override - public String toString() { - return "Employee [department=" + department + "]"; - } -} diff --git a/Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/Product.java b/Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/Product.java deleted file mode 100644 index 162c9b3429..0000000000 --- a/Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/Product.java +++ /dev/null @@ -1,48 +0,0 @@ -package com.baeldung.spring.core; - -import org.springframework.beans.factory.annotation.Value; -import org.springframework.stereotype.Component; - -@Component -public class Product { - - @Value("product1") - private String productName; - - @Value("Pro123") - private String productId; - - @Value("100") - private String productCost; - - public String getProductName() { - return productName; - } - - public void setProductName(String productName) { - this.productName = productName; - } - - public String getProductId() { - return productId; - } - - public void setProductId(String productId) { - this.productId = productId; - } - - public String getProductCost() { - return productCost; - } - - public void setProductCost(String productCost) { - this.productCost = productCost; - } - - @Override - public String toString() { - return "Product [productName=" + productName + ", productId=" + productId + ", productCost=" + productCost - + "]"; - } - -} diff --git a/Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/Student.java b/Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/Student.java deleted file mode 100644 index f2563f01ec..0000000000 --- a/Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/Student.java +++ /dev/null @@ -1,40 +0,0 @@ -package com.baeldung.spring.core; - -public class Student { - - private String studentName; - - private String studentId; - - private Subject subject; - - public String getStudentName() { - return studentName; - } - - public void setStudentName(String studentName) { - this.studentName = studentName; - } - - public String getStudentId() { - return studentId; - } - - public void setStudentId(String studentId) { - this.studentId = studentId; - } - - public Subject getSubject() { - return subject; - } - - public void setSubject(Subject subject) { - this.subject = subject; - } - - @Override - public String toString() { - return "Student [studentName=" + studentName + ", studentId=" + studentId + ", subject=" + subject + "]"; - } - -} diff --git a/Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/Subject.java b/Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/Subject.java deleted file mode 100644 index b160bbef18..0000000000 --- a/Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/Subject.java +++ /dev/null @@ -1,30 +0,0 @@ -package com.baeldung.spring.core; - -public class Subject { - - private String subjectName; - - private String subjectId; - - public String getSubjectName() { - return subjectName; - } - - public void setSubjectName(String subjectName) { - this.subjectName = subjectName; - } - - public String getSubjectId() { - return subjectId; - } - - public void setSubjectId(String subjectId) { - this.subjectId = subjectId; - } - - @Override - public String toString() { - return "Subject [subjectName=" + subjectName + ", subjectId=" + subjectId + "]"; - } - -} diff --git a/Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/config/AppConfig.java b/Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/config/AppConfig.java deleted file mode 100644 index 496191d758..0000000000 --- a/Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/config/AppConfig.java +++ /dev/null @@ -1,30 +0,0 @@ -package com.baeldung.spring.core.config; - -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; - -import com.baeldung.spring.core.Car; -import com.baeldung.spring.core.Department; -import com.baeldung.spring.core.Employee; - -@Configuration -public class AppConfig { - - @Bean - public Department getDepartment() { - return new Department("Sales", "S01"); - } - - @Bean - public Employee getEmployee() { - return new Employee(getDepartment()); - } - - @Bean - public Car getCar() { - Car car = new Car(); - car.setCarName("Honda"); - car.setCarType("SUV"); - return car; - } -} diff --git a/Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/test/AnnotationApplicationContextTest.java b/Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/test/AnnotationApplicationContextTest.java deleted file mode 100644 index 02daf26eb2..0000000000 --- a/Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/test/AnnotationApplicationContextTest.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.baeldung.spring.core.test; - -import org.springframework.context.support.AbstractApplicationContext; -import org.springframework.context.support.ClassPathXmlApplicationContext; - -import com.baeldung.spring.core.Customer; - -public class AnnotationApplicationContextTest { - - public static void main(String[] args) { - AbstractApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml"); - Customer customer = applicationContext.getBean(Customer.class); - System.out.println("Customer is :: " + customer); - System.out.println("Product is :: " + customer.getProduct()); - applicationContext.close(); - } -} diff --git a/Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/test/JavaConfigApplicationContextTest.java b/Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/test/JavaConfigApplicationContextTest.java deleted file mode 100644 index 01b0c67675..0000000000 --- a/Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/test/JavaConfigApplicationContextTest.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.baeldung.spring.core.test; - -import org.springframework.context.annotation.AnnotationConfigApplicationContext; -import org.springframework.context.support.AbstractApplicationContext; - -import com.baeldung.spring.core.Car; -import com.baeldung.spring.core.Department; -import com.baeldung.spring.core.Employee; -import com.baeldung.spring.core.config.AppConfig; - -public class JavaConfigApplicationContextTest { - - public static void main(String[] args) { - AbstractApplicationContext applicationContext = new AnnotationConfigApplicationContext(AppConfig.class); - Department department = applicationContext.getBean(Department.class); - Employee employee = applicationContext.getBean(Employee.class); - System.out.println("Department is :: " + department); - System.out.println("Employee is :: " + employee); - Car car = applicationContext.getBean(Car.class); - System.out.println("Car is :: " + car); - applicationContext.close(); - } -} diff --git a/Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/test/XMLConfigurationApplicationContextTest.java b/Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/test/XMLConfigurationApplicationContextTest.java deleted file mode 100644 index ead4226f1b..0000000000 --- a/Spring-DependencyInjection/src/main/java/com/baeldung/spring/core/test/XMLConfigurationApplicationContextTest.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.baeldung.spring.core.test; - -import org.springframework.context.support.AbstractApplicationContext; -import org.springframework.context.support.ClassPathXmlApplicationContext; - -import com.baeldung.spring.core.Department; -import com.baeldung.spring.core.Employee; -import com.baeldung.spring.core.Student; -import com.baeldung.spring.core.Subject; - -public class XMLConfigurationApplicationContextTest { - - public static void main(String[] args) { - AbstractApplicationContext abstractApplicationContext = new ClassPathXmlApplicationContext("beans.xml"); - // Constructor Injection - Department department = (Department) abstractApplicationContext.getBean("department"); - Employee employee = (Employee) abstractApplicationContext.getBean("employee"); - System.out.println("Department is :: " + department); - System.out.println("Employee is :: " + employee); - - // Setter Injection - Subject subject = (Subject) abstractApplicationContext.getBean("subject"); - Student student = (Student) abstractApplicationContext.getBean("student"); - System.out.println("Subject is :: " + subject); - System.out.println("Student is :: " + student); - abstractApplicationContext.close(); - } -} diff --git a/Spring-DependencyInjection/src/test/java/com/spring/core/Spring_DependencyInjection/AppTest.java b/Spring-DependencyInjection/src/test/java/com/spring/core/Spring_DependencyInjection/AppTest.java deleted file mode 100644 index 5b3c551257..0000000000 --- a/Spring-DependencyInjection/src/test/java/com/spring/core/Spring_DependencyInjection/AppTest.java +++ /dev/null @@ -1,38 +0,0 @@ -package com.spring.core.Spring_DependencyInjection; - -import junit.framework.Test; -import junit.framework.TestCase; -import junit.framework.TestSuite; - -/** - * Unit test for simple App. - */ -public class AppTest - extends TestCase -{ - /** - * Create the test case - * - * @param testName name of the test case - */ - public AppTest( String testName ) - { - super( testName ); - } - - /** - * @return the suite of tests being tested - */ - public static Test suite() - { - return new TestSuite( AppTest.class ); - } - - /** - * Rigourous Test :-) - */ - public void testApp() - { - assertTrue( true ); - } -}