From d2ac20185e636245bc0ae0b4ccb952965de88e28 Mon Sep 17 00:00:00 2001 From: Tomasz Date: Tue, 28 Feb 2017 21:58:39 +0100 Subject: [PATCH 1/9] injecting beans --- injecting-beans/pom.xml | 32 +++++++++++ .../src/main/java/com/baeldung/App.java | 16 ++++++ .../src/main/java/com/baeldung/Computer.java | 55 +++++++++++++++++++ .../java/com/baeldung/model/GraphicsCard.java | 23 ++++++++ .../java/com/baeldung/model/HardDisk.java | 23 ++++++++ .../com/baeldung/model/OperatingSystem.java | 23 ++++++++ .../java/com/baeldung/model/Processor.java | 23 ++++++++ .../main/java/com/baeldung/model/Screen.java | 24 ++++++++ .../src/main/resources/Spring-Module.xml | 37 +++++++++++++ 9 files changed, 256 insertions(+) create mode 100644 injecting-beans/pom.xml create mode 100644 injecting-beans/src/main/java/com/baeldung/App.java create mode 100644 injecting-beans/src/main/java/com/baeldung/Computer.java create mode 100644 injecting-beans/src/main/java/com/baeldung/model/GraphicsCard.java create mode 100644 injecting-beans/src/main/java/com/baeldung/model/HardDisk.java create mode 100644 injecting-beans/src/main/java/com/baeldung/model/OperatingSystem.java create mode 100644 injecting-beans/src/main/java/com/baeldung/model/Processor.java create mode 100644 injecting-beans/src/main/java/com/baeldung/model/Screen.java create mode 100644 injecting-beans/src/main/resources/Spring-Module.xml diff --git a/injecting-beans/pom.xml b/injecting-beans/pom.xml new file mode 100644 index 000000000000..db3d4c4c2075 --- /dev/null +++ b/injecting-beans/pom.xml @@ -0,0 +1,32 @@ + + 4.0.0 + com.baeldung.common + SpringExample + jar + 1.0-SNAPSHOT + SpringExample + http://maven.apache.org + + + + junit + junit + 4.12 + test + + + + + org.springframework + spring-core + 4.1.4.RELEASE + + + org.springframework + spring-context + 4.1.4.RELEASE + + + + diff --git a/injecting-beans/src/main/java/com/baeldung/App.java b/injecting-beans/src/main/java/com/baeldung/App.java new file mode 100644 index 000000000000..2afc005c7692 --- /dev/null +++ b/injecting-beans/src/main/java/com/baeldung/App.java @@ -0,0 +1,16 @@ +package com.baeldung; + +import org.springframework.context.ApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +/** + * Application presenting various possibilities of bean injections in Spring. + */ +public class App { + public static void main(String[] args) { + ApplicationContext context = new ClassPathXmlApplicationContext("Spring-Module.xml"); + + Computer obj = (Computer) context.getBean("computer"); + obj.print(); + } +} diff --git a/injecting-beans/src/main/java/com/baeldung/Computer.java b/injecting-beans/src/main/java/com/baeldung/Computer.java new file mode 100644 index 000000000000..b9551ad34db3 --- /dev/null +++ b/injecting-beans/src/main/java/com/baeldung/Computer.java @@ -0,0 +1,55 @@ +package com.baeldung; + +import com.baeldung.model.*; +import org.springframework.beans.factory.annotation.Autowired; + +/** + * Model for Computer. + */ +public class Computer { + + @Autowired + private GraphicsCard graphicsCard; + private Processor processor; + private HardDisk hardDisk; + private Screen screen; + private OperatingSystem operatingSystem; + + public Computer(Processor processor) { + this.processor = processor; + } + + @Autowired + public Computer(OperatingSystem operatingSystem, Processor processor) { + this.operatingSystem = operatingSystem; + this.processor = processor; + } + + public void setHardDisk(HardDisk hardDisk) { + this.hardDisk = hardDisk; + } + + @Autowired + public void setScreen(Screen screen) { + this.screen = screen; + } + + public void print() { + System.out.println("Injected processor by constructor injection: " + processor.getName()); + System.out.println("Injected hard disk by setter injection: " + hardDisk.getName()); + System.out.println("Injected graphics card with field annotation: " + graphicsCard.getName()); + System.out.println("Injected screen with setter annotation: " + screen.getSize()); + System.out.println("Injected operating system name with constructor annotation: " + operatingSystem.getName()); + } + + @Override + public String toString() { + return "Computer{" + + "graphicsCard=" + graphicsCard + + ", processor=" + processor + + ", hardDisk=" + hardDisk + + ", screen=" + screen + + ", operatingSystem=" + operatingSystem + + '}'; + } +} diff --git a/injecting-beans/src/main/java/com/baeldung/model/GraphicsCard.java b/injecting-beans/src/main/java/com/baeldung/model/GraphicsCard.java new file mode 100644 index 000000000000..70bec9b1cde9 --- /dev/null +++ b/injecting-beans/src/main/java/com/baeldung/model/GraphicsCard.java @@ -0,0 +1,23 @@ +package com.baeldung.model; + +/** + * Model for graphics card. + */ +public class GraphicsCard { + String name; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + @Override + public String toString() { + return "GraphicsCard{" + + "name='" + name + '\'' + + '}'; + } +} diff --git a/injecting-beans/src/main/java/com/baeldung/model/HardDisk.java b/injecting-beans/src/main/java/com/baeldung/model/HardDisk.java new file mode 100644 index 000000000000..2117fb67a14d --- /dev/null +++ b/injecting-beans/src/main/java/com/baeldung/model/HardDisk.java @@ -0,0 +1,23 @@ +package com.baeldung.model; + +/** + * Model for hard disk drive. + */ +public class HardDisk { + private String name; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + @Override + public String toString() { + return "HardDisk{" + + "name='" + name + '\'' + + '}'; + } +} diff --git a/injecting-beans/src/main/java/com/baeldung/model/OperatingSystem.java b/injecting-beans/src/main/java/com/baeldung/model/OperatingSystem.java new file mode 100644 index 000000000000..296445e60b7a --- /dev/null +++ b/injecting-beans/src/main/java/com/baeldung/model/OperatingSystem.java @@ -0,0 +1,23 @@ +package com.baeldung.model; + +/** + * Model for operating system. + */ +public class OperatingSystem { + private String name; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + @Override + public String toString() { + return "OperatingSystem{" + + "name='" + name + '\'' + + '}'; + } +} diff --git a/injecting-beans/src/main/java/com/baeldung/model/Processor.java b/injecting-beans/src/main/java/com/baeldung/model/Processor.java new file mode 100644 index 000000000000..ef41b1590293 --- /dev/null +++ b/injecting-beans/src/main/java/com/baeldung/model/Processor.java @@ -0,0 +1,23 @@ +package com.baeldung.model; + +/** + * Model for Processor. + */ +public class Processor { + private String name; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + @Override + public String toString() { + return "Processor{" + + "name='" + name + '\'' + + '}'; + } +} diff --git a/injecting-beans/src/main/java/com/baeldung/model/Screen.java b/injecting-beans/src/main/java/com/baeldung/model/Screen.java new file mode 100644 index 000000000000..08b5da05509d --- /dev/null +++ b/injecting-beans/src/main/java/com/baeldung/model/Screen.java @@ -0,0 +1,24 @@ +package com.baeldung.model; + +/** + * Model for Screen. + */ +public class Screen { + + private String size; + + public String getSize() { + return size; + } + + public void setSize(String size) { + this.size = size; + } + + @Override + public String toString() { + return "Screen{" + + "size='" + size + '\'' + + '}'; + } +} diff --git a/injecting-beans/src/main/resources/Spring-Module.xml b/injecting-beans/src/main/resources/Spring-Module.xml new file mode 100644 index 000000000000..cb0b4c058960 --- /dev/null +++ b/injecting-beans/src/main/resources/Spring-Module.xml @@ -0,0 +1,37 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file From 66471cf0574c85f8ff514ec4caf5ba44ebba1a74 Mon Sep 17 00:00:00 2001 From: Tomasz Date: Mon, 6 Mar 2017 21:15:46 +0100 Subject: [PATCH 2/9] XML-based configuration replaced with Java Config. --- injecting-beans/README.txt | 2 + .../src/main/java/com/baeldung/App.java | 5 +- .../src/main/java/com/baeldung/Computer.java | 17 +++--- .../java/com/baeldung/config/AppConfig.java | 54 +++++++++++++++++++ .../java/com/baeldung/model/GraphicsCard.java | 5 +- .../com/baeldung/model/OperatingSystem.java | 3 ++ .../java/com/baeldung/model/Processor.java | 3 ++ .../main/java/com/baeldung/model/Screen.java | 4 +- 8 files changed, 80 insertions(+), 13 deletions(-) create mode 100644 injecting-beans/README.txt create mode 100644 injecting-beans/src/main/java/com/baeldung/config/AppConfig.java diff --git a/injecting-beans/README.txt b/injecting-beans/README.txt new file mode 100644 index 000000000000..99043a5caeca --- /dev/null +++ b/injecting-beans/README.txt @@ -0,0 +1,2 @@ +If you are interested in XML-based configuration, you can see all the injections done with this approach by +just using Spring-Module.xml in App.java (It's commented by default). \ No newline at end of file diff --git a/injecting-beans/src/main/java/com/baeldung/App.java b/injecting-beans/src/main/java/com/baeldung/App.java index 2afc005c7692..048fb1a35a49 100644 --- a/injecting-beans/src/main/java/com/baeldung/App.java +++ b/injecting-beans/src/main/java/com/baeldung/App.java @@ -1,6 +1,8 @@ package com.baeldung; +import com.baeldung.config.AppConfig; import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** @@ -8,8 +10,9 @@ */ public class App { public static void main(String[] args) { - ApplicationContext context = new ClassPathXmlApplicationContext("Spring-Module.xml"); +// ApplicationContext context = new ClassPathXmlApplicationContext("Spring-Module.xml"); + ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class); Computer obj = (Computer) context.getBean("computer"); obj.print(); } diff --git a/injecting-beans/src/main/java/com/baeldung/Computer.java b/injecting-beans/src/main/java/com/baeldung/Computer.java index b9551ad34db3..29c33fec09ec 100644 --- a/injecting-beans/src/main/java/com/baeldung/Computer.java +++ b/injecting-beans/src/main/java/com/baeldung/Computer.java @@ -15,14 +15,11 @@ public class Computer { private Screen screen; private OperatingSystem operatingSystem; - public Computer(Processor processor) { - this.processor = processor; - } - @Autowired - public Computer(OperatingSystem operatingSystem, Processor processor) { + public Computer(Processor processor, OperatingSystem operatingSystem, HardDisk hardDisk) { this.operatingSystem = operatingSystem; this.processor = processor; + this.hardDisk = hardDisk; } public void setHardDisk(HardDisk hardDisk) { @@ -35,11 +32,11 @@ public void setScreen(Screen screen) { } public void print() { - System.out.println("Injected processor by constructor injection: " + processor.getName()); - System.out.println("Injected hard disk by setter injection: " + hardDisk.getName()); - System.out.println("Injected graphics card with field annotation: " + graphicsCard.getName()); - System.out.println("Injected screen with setter annotation: " + screen.getSize()); - System.out.println("Injected operating system name with constructor annotation: " + operatingSystem.getName()); + System.out.println("Injected processor: " + processor.getName()); + System.out.println("Injected hard disk: " + hardDisk.getName()); + System.out.println("Injected graphics card: " + graphicsCard.getName()); + System.out.println("Injected screen size: " + screen.getSize()); + System.out.println("Injected operating system name: " + operatingSystem.getName()); } @Override diff --git a/injecting-beans/src/main/java/com/baeldung/config/AppConfig.java b/injecting-beans/src/main/java/com/baeldung/config/AppConfig.java new file mode 100644 index 000000000000..eb7b3442a1cc --- /dev/null +++ b/injecting-beans/src/main/java/com/baeldung/config/AppConfig.java @@ -0,0 +1,54 @@ +package com.baeldung.config; + +import com.baeldung.Computer; +import com.baeldung.model.*; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +/** + * Beans configuration. + */ +@Configuration +public class AppConfig { + + @Bean(name = "computer") + public Computer computer() { + return new Computer(processor(), operatingSystem(), hardDisk()); + } + + @Bean(name = "processor") + public Processor processor() { + Processor processor = new Processor(); + processor.setName("Intel Core i7-6650U"); + return processor; + } + + @Bean(name = "hardDisk") + public HardDisk hardDisk() { + HardDisk hardDisk = new HardDisk(); + hardDisk.setName("SSD TRANSCEND MTS400"); + return hardDisk; + } + + @Bean(name = "graphicsCard") + public GraphicsCard graphicsCard() { + GraphicsCard graphicsCard = new GraphicsCard(); + graphicsCard.setName("NVIDIA GeForce GTX980M"); + return graphicsCard; + } + + @Bean(name = "operatingSystem") + public OperatingSystem operatingSystem() { + OperatingSystem operatingSystem = new OperatingSystem(); + operatingSystem.setName("Windows 10 Pro"); + return operatingSystem; + } + + @Bean(name = "screen") + public Screen screen() { + Screen screen = new Screen(); + screen.setSize("17 inches"); + return screen; + } + +} diff --git a/injecting-beans/src/main/java/com/baeldung/model/GraphicsCard.java b/injecting-beans/src/main/java/com/baeldung/model/GraphicsCard.java index 70bec9b1cde9..6ae2d6a89165 100644 --- a/injecting-beans/src/main/java/com/baeldung/model/GraphicsCard.java +++ b/injecting-beans/src/main/java/com/baeldung/model/GraphicsCard.java @@ -4,7 +4,10 @@ * Model for graphics card. */ public class GraphicsCard { - String name; + private String name; + + public GraphicsCard() { + } public String getName() { return name; diff --git a/injecting-beans/src/main/java/com/baeldung/model/OperatingSystem.java b/injecting-beans/src/main/java/com/baeldung/model/OperatingSystem.java index 296445e60b7a..e804d1fd502e 100644 --- a/injecting-beans/src/main/java/com/baeldung/model/OperatingSystem.java +++ b/injecting-beans/src/main/java/com/baeldung/model/OperatingSystem.java @@ -6,6 +6,9 @@ public class OperatingSystem { private String name; + public OperatingSystem(){ + } + public String getName() { return name; } diff --git a/injecting-beans/src/main/java/com/baeldung/model/Processor.java b/injecting-beans/src/main/java/com/baeldung/model/Processor.java index ef41b1590293..f6d4c0969de7 100644 --- a/injecting-beans/src/main/java/com/baeldung/model/Processor.java +++ b/injecting-beans/src/main/java/com/baeldung/model/Processor.java @@ -6,6 +6,9 @@ public class Processor { private String name; + public Processor() { + } + public String getName() { return name; } diff --git a/injecting-beans/src/main/java/com/baeldung/model/Screen.java b/injecting-beans/src/main/java/com/baeldung/model/Screen.java index 08b5da05509d..85658494d1c2 100644 --- a/injecting-beans/src/main/java/com/baeldung/model/Screen.java +++ b/injecting-beans/src/main/java/com/baeldung/model/Screen.java @@ -4,9 +4,11 @@ * Model for Screen. */ public class Screen { - private String size; + public Screen() { + } + public String getSize() { return size; } From 9eb4b9de92770df24f03b96c61ecbe30f62fb675 Mon Sep 17 00:00:00 2001 From: Tomasz Date: Sun, 19 Mar 2017 09:29:38 +0100 Subject: [PATCH 3/9] [BAEL-431] Exploring TestRestTemplate. --- spring-rest/pom.xml | 4 + .../client/TestRestTemplateBasicLiveTest.java | 121 ++++++++++++++++++ 2 files changed, 125 insertions(+) create mode 100644 spring-rest/src/test/java/org/baeldung/client/TestRestTemplateBasicLiveTest.java diff --git a/spring-rest/pom.xml b/spring-rest/pom.xml index da26d8abe91d..a9b208bcd29c 100644 --- a/spring-rest/pom.xml +++ b/spring-rest/pom.xml @@ -29,6 +29,10 @@ org.springframework.boot spring-boot-devtools + + org.springframework.boot + spring-boot-test + diff --git a/spring-rest/src/test/java/org/baeldung/client/TestRestTemplateBasicLiveTest.java b/spring-rest/src/test/java/org/baeldung/client/TestRestTemplateBasicLiveTest.java new file mode 100644 index 000000000000..c4ea0bbf154b --- /dev/null +++ b/spring-rest/src/test/java/org/baeldung/client/TestRestTemplateBasicLiveTest.java @@ -0,0 +1,121 @@ +package org.baeldung.client; + +import okhttp3.Request; +import okhttp3.RequestBody; +import org.junit.Before; +import org.junit.Test; +import org.springframework.boot.test.web.client.TestRestTemplate; +import org.springframework.boot.web.client.RestTemplateBuilder; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.web.client.RestTemplate; + +import java.io.IOException; + +import static org.baeldung.client.Consts.APPLICATION_PORT; +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertTrue; + +public class TestRestTemplateBasicLiveTest { + + private RestTemplate restTemplate; + private static final String FOO_RESOURCE_URL = "http://localhost:" + APPLICATION_PORT + "/spring-rest/myfoos"; + private static final String URL_SECURED_BY_AUTHENTICATION = "http://browserspy.dk/password-ok.php"; + private static final String BASE_URL = "http://localhost:" + APPLICATION_PORT + "/spring-rest"; + + @Before + public void beforeTest() { + restTemplate = new RestTemplate(); + } + + // GET + @Test + public void givenTestRestTemplate_whenSendGetForEntity_thenStatusOk() throws IOException { + TestRestTemplate testRestTemplate = new TestRestTemplate(); + ResponseEntity response = testRestTemplate.getForEntity(FOO_RESOURCE_URL + "/1", String.class); + assertThat(response.getStatusCode(), equalTo(HttpStatus.OK)); + } + + @Test + public void givenRestTemplateWrapper_whenSendGetForEntity_thenStatusOk() throws IOException { + TestRestTemplate testRestTemplate = new TestRestTemplate(restTemplate); + ResponseEntity response = testRestTemplate.getForEntity(FOO_RESOURCE_URL + "/1", String.class); + assertThat(response.getStatusCode(), equalTo(HttpStatus.OK)); + } + + @Test + public void givenRestTemplateBuilderWrapper_whenSendGetForEntity_thenStatusOk() throws IOException { + RestTemplateBuilder restTemplateBuilder = new RestTemplateBuilder(); + restTemplateBuilder.build(); + TestRestTemplate testRestTemplate = new TestRestTemplate(restTemplateBuilder); + ResponseEntity response = testRestTemplate.getForEntity(FOO_RESOURCE_URL + "/1", String.class); + assertThat(response.getStatusCode(), equalTo(HttpStatus.OK)); + } + + @Test + public void givenRestTemplateWrapperWithCredentials_whenSendGetForEntity_thenStatusOk() throws IOException { + TestRestTemplate testRestTemplate = new TestRestTemplate(restTemplate, "test", "test"); + ResponseEntity response = testRestTemplate.getForEntity(URL_SECURED_BY_AUTHENTICATION + "/1", + String.class); + assertThat(response.getStatusCode(), equalTo(HttpStatus.OK)); + } + + @Test + public void givenTestRestTemplateWithCredentials_whenSendGetForEntity_thenStatusOk() throws IOException { + TestRestTemplate testRestTemplate = new TestRestTemplate("test", "test"); + ResponseEntity response = testRestTemplate.getForEntity(URL_SECURED_BY_AUTHENTICATION + "/1", + String.class); + assertThat(response.getStatusCode(), equalTo(HttpStatus.OK)); + } + + @Test + public void givenTestRestTemplateWithBasicAuth_whenSendGetForEntity_thenStatusOk() throws IOException { + TestRestTemplate testRestTemplate = new TestRestTemplate(); + ResponseEntity response = testRestTemplate.withBasicAuth("test", "test"). + getForEntity(URL_SECURED_BY_AUTHENTICATION + "/1", String.class); + assertThat(response.getStatusCode(), equalTo(HttpStatus.OK)); + } + + @Test + public void givenTestRestTemplateWithCredentialsAndEnabledCookies_whenSendGetForEntity_thenStatusOk() + throws IOException { + TestRestTemplate testRestTemplate = new TestRestTemplate("test", "test", TestRestTemplate. + HttpClientOption.ENABLE_COOKIES); + ResponseEntity response = testRestTemplate.getForEntity(URL_SECURED_BY_AUTHENTICATION + "/1", + String.class); + assertThat(response.getStatusCode(), equalTo(HttpStatus.OK)); + } + + // HEAD + @Test + public void givenFooService_whenCallHeadForHeaders_thenReceiveAllHeaders() { + TestRestTemplate testRestTemplate = new TestRestTemplate(); + final HttpHeaders httpHeaders = testRestTemplate.headForHeaders(FOO_RESOURCE_URL); + assertTrue(httpHeaders.getContentType().includes(MediaType.APPLICATION_JSON)); + } + + // POST + @Test + public void givenService_whenPostForObject_thenCreatedObjectIsReturned() { + TestRestTemplate testRestTemplate = new TestRestTemplate("test", "test"); + final RequestBody body = RequestBody.create(okhttp3.MediaType.parse("text/html; charset=utf-8"), + "{\"id\":1,\"name\":\"Jim\"}"); + final Request request = new Request.Builder().url(BASE_URL + "/users/detail").post(body).build(); + Object response = testRestTemplate.postForObject(URL_SECURED_BY_AUTHENTICATION, request, String.class); + assertTrue(response.toString().contains("Success")); + } + + // PUT + @Test + public void givenService_whenPutForObject_thenCreatedObjectIsReturned() { + TestRestTemplate testRestTemplate = new TestRestTemplate("test", "test"); + final RequestBody body = RequestBody.create(okhttp3.MediaType.parse("text/html; charset=utf-8"), + "{\"id\":1,\"name\":\"Jim\"}"); + final Request request = new Request.Builder().url(BASE_URL + "/users/detail").post(body).build(); + testRestTemplate.put(URL_SECURED_BY_AUTHENTICATION, request, String.class); + } + +} From 7a515dfeb882c42e2137893ef920c6f0f4c0e053 Mon Sep 17 00:00:00 2001 From: Tomasz Date: Sun, 19 Mar 2017 09:45:56 +0100 Subject: [PATCH 4/9] Revert of evaluation task "XML-based configuration replaced with Java Config." This reverts commit 66471cf0574c85f8ff514ec4caf5ba44ebba1a74. --- injecting-beans/README.txt | 2 - .../src/main/java/com/baeldung/App.java | 5 +- .../src/main/java/com/baeldung/Computer.java | 17 +++--- .../java/com/baeldung/config/AppConfig.java | 54 ------------------- .../java/com/baeldung/model/GraphicsCard.java | 5 +- .../com/baeldung/model/OperatingSystem.java | 3 -- .../java/com/baeldung/model/Processor.java | 3 -- .../main/java/com/baeldung/model/Screen.java | 4 +- 8 files changed, 13 insertions(+), 80 deletions(-) delete mode 100644 injecting-beans/README.txt delete mode 100644 injecting-beans/src/main/java/com/baeldung/config/AppConfig.java diff --git a/injecting-beans/README.txt b/injecting-beans/README.txt deleted file mode 100644 index 99043a5caeca..000000000000 --- a/injecting-beans/README.txt +++ /dev/null @@ -1,2 +0,0 @@ -If you are interested in XML-based configuration, you can see all the injections done with this approach by -just using Spring-Module.xml in App.java (It's commented by default). \ No newline at end of file diff --git a/injecting-beans/src/main/java/com/baeldung/App.java b/injecting-beans/src/main/java/com/baeldung/App.java index 048fb1a35a49..2afc005c7692 100644 --- a/injecting-beans/src/main/java/com/baeldung/App.java +++ b/injecting-beans/src/main/java/com/baeldung/App.java @@ -1,8 +1,6 @@ package com.baeldung; -import com.baeldung.config.AppConfig; import org.springframework.context.ApplicationContext; -import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; /** @@ -10,9 +8,8 @@ */ public class App { public static void main(String[] args) { -// ApplicationContext context = new ClassPathXmlApplicationContext("Spring-Module.xml"); + ApplicationContext context = new ClassPathXmlApplicationContext("Spring-Module.xml"); - ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class); Computer obj = (Computer) context.getBean("computer"); obj.print(); } diff --git a/injecting-beans/src/main/java/com/baeldung/Computer.java b/injecting-beans/src/main/java/com/baeldung/Computer.java index 29c33fec09ec..b9551ad34db3 100644 --- a/injecting-beans/src/main/java/com/baeldung/Computer.java +++ b/injecting-beans/src/main/java/com/baeldung/Computer.java @@ -15,11 +15,14 @@ public class Computer { private Screen screen; private OperatingSystem operatingSystem; + public Computer(Processor processor) { + this.processor = processor; + } + @Autowired - public Computer(Processor processor, OperatingSystem operatingSystem, HardDisk hardDisk) { + public Computer(OperatingSystem operatingSystem, Processor processor) { this.operatingSystem = operatingSystem; this.processor = processor; - this.hardDisk = hardDisk; } public void setHardDisk(HardDisk hardDisk) { @@ -32,11 +35,11 @@ public void setScreen(Screen screen) { } public void print() { - System.out.println("Injected processor: " + processor.getName()); - System.out.println("Injected hard disk: " + hardDisk.getName()); - System.out.println("Injected graphics card: " + graphicsCard.getName()); - System.out.println("Injected screen size: " + screen.getSize()); - System.out.println("Injected operating system name: " + operatingSystem.getName()); + System.out.println("Injected processor by constructor injection: " + processor.getName()); + System.out.println("Injected hard disk by setter injection: " + hardDisk.getName()); + System.out.println("Injected graphics card with field annotation: " + graphicsCard.getName()); + System.out.println("Injected screen with setter annotation: " + screen.getSize()); + System.out.println("Injected operating system name with constructor annotation: " + operatingSystem.getName()); } @Override diff --git a/injecting-beans/src/main/java/com/baeldung/config/AppConfig.java b/injecting-beans/src/main/java/com/baeldung/config/AppConfig.java deleted file mode 100644 index eb7b3442a1cc..000000000000 --- a/injecting-beans/src/main/java/com/baeldung/config/AppConfig.java +++ /dev/null @@ -1,54 +0,0 @@ -package com.baeldung.config; - -import com.baeldung.Computer; -import com.baeldung.model.*; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; - -/** - * Beans configuration. - */ -@Configuration -public class AppConfig { - - @Bean(name = "computer") - public Computer computer() { - return new Computer(processor(), operatingSystem(), hardDisk()); - } - - @Bean(name = "processor") - public Processor processor() { - Processor processor = new Processor(); - processor.setName("Intel Core i7-6650U"); - return processor; - } - - @Bean(name = "hardDisk") - public HardDisk hardDisk() { - HardDisk hardDisk = new HardDisk(); - hardDisk.setName("SSD TRANSCEND MTS400"); - return hardDisk; - } - - @Bean(name = "graphicsCard") - public GraphicsCard graphicsCard() { - GraphicsCard graphicsCard = new GraphicsCard(); - graphicsCard.setName("NVIDIA GeForce GTX980M"); - return graphicsCard; - } - - @Bean(name = "operatingSystem") - public OperatingSystem operatingSystem() { - OperatingSystem operatingSystem = new OperatingSystem(); - operatingSystem.setName("Windows 10 Pro"); - return operatingSystem; - } - - @Bean(name = "screen") - public Screen screen() { - Screen screen = new Screen(); - screen.setSize("17 inches"); - return screen; - } - -} diff --git a/injecting-beans/src/main/java/com/baeldung/model/GraphicsCard.java b/injecting-beans/src/main/java/com/baeldung/model/GraphicsCard.java index 6ae2d6a89165..70bec9b1cde9 100644 --- a/injecting-beans/src/main/java/com/baeldung/model/GraphicsCard.java +++ b/injecting-beans/src/main/java/com/baeldung/model/GraphicsCard.java @@ -4,10 +4,7 @@ * Model for graphics card. */ public class GraphicsCard { - private String name; - - public GraphicsCard() { - } + String name; public String getName() { return name; diff --git a/injecting-beans/src/main/java/com/baeldung/model/OperatingSystem.java b/injecting-beans/src/main/java/com/baeldung/model/OperatingSystem.java index e804d1fd502e..296445e60b7a 100644 --- a/injecting-beans/src/main/java/com/baeldung/model/OperatingSystem.java +++ b/injecting-beans/src/main/java/com/baeldung/model/OperatingSystem.java @@ -6,9 +6,6 @@ public class OperatingSystem { private String name; - public OperatingSystem(){ - } - public String getName() { return name; } diff --git a/injecting-beans/src/main/java/com/baeldung/model/Processor.java b/injecting-beans/src/main/java/com/baeldung/model/Processor.java index f6d4c0969de7..ef41b1590293 100644 --- a/injecting-beans/src/main/java/com/baeldung/model/Processor.java +++ b/injecting-beans/src/main/java/com/baeldung/model/Processor.java @@ -6,9 +6,6 @@ public class Processor { private String name; - public Processor() { - } - public String getName() { return name; } diff --git a/injecting-beans/src/main/java/com/baeldung/model/Screen.java b/injecting-beans/src/main/java/com/baeldung/model/Screen.java index 85658494d1c2..08b5da05509d 100644 --- a/injecting-beans/src/main/java/com/baeldung/model/Screen.java +++ b/injecting-beans/src/main/java/com/baeldung/model/Screen.java @@ -4,10 +4,8 @@ * Model for Screen. */ public class Screen { - private String size; - public Screen() { - } + private String size; public String getSize() { return size; From 108d0007fa6117f8a6786c808cb23ea8b1ec3af3 Mon Sep 17 00:00:00 2001 From: Tomasz Date: Sun, 19 Mar 2017 09:46:55 +0100 Subject: [PATCH 5/9] Revert of evaluation task "injecting beans" This reverts commit d2ac20185e636245bc0ae0b4ccb952965de88e28. --- injecting-beans/pom.xml | 32 ----------- .../src/main/java/com/baeldung/App.java | 16 ------ .../src/main/java/com/baeldung/Computer.java | 55 ------------------- .../java/com/baeldung/model/GraphicsCard.java | 23 -------- .../java/com/baeldung/model/HardDisk.java | 23 -------- .../com/baeldung/model/OperatingSystem.java | 23 -------- .../java/com/baeldung/model/Processor.java | 23 -------- .../main/java/com/baeldung/model/Screen.java | 24 -------- .../src/main/resources/Spring-Module.xml | 37 ------------- 9 files changed, 256 deletions(-) delete mode 100644 injecting-beans/pom.xml delete mode 100644 injecting-beans/src/main/java/com/baeldung/App.java delete mode 100644 injecting-beans/src/main/java/com/baeldung/Computer.java delete mode 100644 injecting-beans/src/main/java/com/baeldung/model/GraphicsCard.java delete mode 100644 injecting-beans/src/main/java/com/baeldung/model/HardDisk.java delete mode 100644 injecting-beans/src/main/java/com/baeldung/model/OperatingSystem.java delete mode 100644 injecting-beans/src/main/java/com/baeldung/model/Processor.java delete mode 100644 injecting-beans/src/main/java/com/baeldung/model/Screen.java delete mode 100644 injecting-beans/src/main/resources/Spring-Module.xml diff --git a/injecting-beans/pom.xml b/injecting-beans/pom.xml deleted file mode 100644 index db3d4c4c2075..000000000000 --- a/injecting-beans/pom.xml +++ /dev/null @@ -1,32 +0,0 @@ - - 4.0.0 - com.baeldung.common - SpringExample - jar - 1.0-SNAPSHOT - SpringExample - http://maven.apache.org - - - - junit - junit - 4.12 - test - - - - - org.springframework - spring-core - 4.1.4.RELEASE - - - org.springframework - spring-context - 4.1.4.RELEASE - - - - diff --git a/injecting-beans/src/main/java/com/baeldung/App.java b/injecting-beans/src/main/java/com/baeldung/App.java deleted file mode 100644 index 2afc005c7692..000000000000 --- a/injecting-beans/src/main/java/com/baeldung/App.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.baeldung; - -import org.springframework.context.ApplicationContext; -import org.springframework.context.support.ClassPathXmlApplicationContext; - -/** - * Application presenting various possibilities of bean injections in Spring. - */ -public class App { - public static void main(String[] args) { - ApplicationContext context = new ClassPathXmlApplicationContext("Spring-Module.xml"); - - Computer obj = (Computer) context.getBean("computer"); - obj.print(); - } -} diff --git a/injecting-beans/src/main/java/com/baeldung/Computer.java b/injecting-beans/src/main/java/com/baeldung/Computer.java deleted file mode 100644 index b9551ad34db3..000000000000 --- a/injecting-beans/src/main/java/com/baeldung/Computer.java +++ /dev/null @@ -1,55 +0,0 @@ -package com.baeldung; - -import com.baeldung.model.*; -import org.springframework.beans.factory.annotation.Autowired; - -/** - * Model for Computer. - */ -public class Computer { - - @Autowired - private GraphicsCard graphicsCard; - private Processor processor; - private HardDisk hardDisk; - private Screen screen; - private OperatingSystem operatingSystem; - - public Computer(Processor processor) { - this.processor = processor; - } - - @Autowired - public Computer(OperatingSystem operatingSystem, Processor processor) { - this.operatingSystem = operatingSystem; - this.processor = processor; - } - - public void setHardDisk(HardDisk hardDisk) { - this.hardDisk = hardDisk; - } - - @Autowired - public void setScreen(Screen screen) { - this.screen = screen; - } - - public void print() { - System.out.println("Injected processor by constructor injection: " + processor.getName()); - System.out.println("Injected hard disk by setter injection: " + hardDisk.getName()); - System.out.println("Injected graphics card with field annotation: " + graphicsCard.getName()); - System.out.println("Injected screen with setter annotation: " + screen.getSize()); - System.out.println("Injected operating system name with constructor annotation: " + operatingSystem.getName()); - } - - @Override - public String toString() { - return "Computer{" + - "graphicsCard=" + graphicsCard + - ", processor=" + processor + - ", hardDisk=" + hardDisk + - ", screen=" + screen + - ", operatingSystem=" + operatingSystem + - '}'; - } -} diff --git a/injecting-beans/src/main/java/com/baeldung/model/GraphicsCard.java b/injecting-beans/src/main/java/com/baeldung/model/GraphicsCard.java deleted file mode 100644 index 70bec9b1cde9..000000000000 --- a/injecting-beans/src/main/java/com/baeldung/model/GraphicsCard.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.baeldung.model; - -/** - * Model for graphics card. - */ -public class GraphicsCard { - String name; - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - @Override - public String toString() { - return "GraphicsCard{" + - "name='" + name + '\'' + - '}'; - } -} diff --git a/injecting-beans/src/main/java/com/baeldung/model/HardDisk.java b/injecting-beans/src/main/java/com/baeldung/model/HardDisk.java deleted file mode 100644 index 2117fb67a14d..000000000000 --- a/injecting-beans/src/main/java/com/baeldung/model/HardDisk.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.baeldung.model; - -/** - * Model for hard disk drive. - */ -public class HardDisk { - private String name; - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - @Override - public String toString() { - return "HardDisk{" + - "name='" + name + '\'' + - '}'; - } -} diff --git a/injecting-beans/src/main/java/com/baeldung/model/OperatingSystem.java b/injecting-beans/src/main/java/com/baeldung/model/OperatingSystem.java deleted file mode 100644 index 296445e60b7a..000000000000 --- a/injecting-beans/src/main/java/com/baeldung/model/OperatingSystem.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.baeldung.model; - -/** - * Model for operating system. - */ -public class OperatingSystem { - private String name; - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - @Override - public String toString() { - return "OperatingSystem{" + - "name='" + name + '\'' + - '}'; - } -} diff --git a/injecting-beans/src/main/java/com/baeldung/model/Processor.java b/injecting-beans/src/main/java/com/baeldung/model/Processor.java deleted file mode 100644 index ef41b1590293..000000000000 --- a/injecting-beans/src/main/java/com/baeldung/model/Processor.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.baeldung.model; - -/** - * Model for Processor. - */ -public class Processor { - private String name; - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - @Override - public String toString() { - return "Processor{" + - "name='" + name + '\'' + - '}'; - } -} diff --git a/injecting-beans/src/main/java/com/baeldung/model/Screen.java b/injecting-beans/src/main/java/com/baeldung/model/Screen.java deleted file mode 100644 index 08b5da05509d..000000000000 --- a/injecting-beans/src/main/java/com/baeldung/model/Screen.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.baeldung.model; - -/** - * Model for Screen. - */ -public class Screen { - - private String size; - - public String getSize() { - return size; - } - - public void setSize(String size) { - this.size = size; - } - - @Override - public String toString() { - return "Screen{" + - "size='" + size + '\'' + - '}'; - } -} diff --git a/injecting-beans/src/main/resources/Spring-Module.xml b/injecting-beans/src/main/resources/Spring-Module.xml deleted file mode 100644 index cb0b4c058960..000000000000 --- a/injecting-beans/src/main/resources/Spring-Module.xml +++ /dev/null @@ -1,37 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file From f4198fd9de5fd5452a294fa6b460d1d4c70fa12b Mon Sep 17 00:00:00 2001 From: Tomasz Date: Sun, 19 Mar 2017 22:08:29 +0100 Subject: [PATCH 6/9] [BAEL-431] fix to the tests in TestRestTemplateBasicLiveTest. --- .../client/TestRestTemplateBasicLiveTest.java | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/spring-rest/src/test/java/org/baeldung/client/TestRestTemplateBasicLiveTest.java b/spring-rest/src/test/java/org/baeldung/client/TestRestTemplateBasicLiveTest.java index c4ea0bbf154b..9f4319d85702 100644 --- a/spring-rest/src/test/java/org/baeldung/client/TestRestTemplateBasicLiveTest.java +++ b/spring-rest/src/test/java/org/baeldung/client/TestRestTemplateBasicLiveTest.java @@ -12,8 +12,6 @@ import org.springframework.http.ResponseEntity; import org.springframework.web.client.RestTemplate; -import java.io.IOException; - import static org.baeldung.client.Consts.APPLICATION_PORT; import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.MatcherAssert.assertThat; @@ -33,21 +31,21 @@ public void beforeTest() { // GET @Test - public void givenTestRestTemplate_whenSendGetForEntity_thenStatusOk() throws IOException { + public void givenTestRestTemplate_whenSendGetForEntity_thenStatusOk() { TestRestTemplate testRestTemplate = new TestRestTemplate(); ResponseEntity response = testRestTemplate.getForEntity(FOO_RESOURCE_URL + "/1", String.class); assertThat(response.getStatusCode(), equalTo(HttpStatus.OK)); } @Test - public void givenRestTemplateWrapper_whenSendGetForEntity_thenStatusOk() throws IOException { + public void givenRestTemplateWrapper_whenSendGetForEntity_thenStatusOk() { TestRestTemplate testRestTemplate = new TestRestTemplate(restTemplate); ResponseEntity response = testRestTemplate.getForEntity(FOO_RESOURCE_URL + "/1", String.class); assertThat(response.getStatusCode(), equalTo(HttpStatus.OK)); } @Test - public void givenRestTemplateBuilderWrapper_whenSendGetForEntity_thenStatusOk() throws IOException { + public void givenRestTemplateBuilderWrapper_whenSendGetForEntity_thenStatusOk() { RestTemplateBuilder restTemplateBuilder = new RestTemplateBuilder(); restTemplateBuilder.build(); TestRestTemplate testRestTemplate = new TestRestTemplate(restTemplateBuilder); @@ -56,7 +54,7 @@ public void givenRestTemplateBuilderWrapper_whenSendGetForEntity_thenStatusOk() } @Test - public void givenRestTemplateWrapperWithCredentials_whenSendGetForEntity_thenStatusOk() throws IOException { + public void givenRestTemplateWrapperWithCredentials_whenSendGetForEntity_thenStatusOk() { TestRestTemplate testRestTemplate = new TestRestTemplate(restTemplate, "test", "test"); ResponseEntity response = testRestTemplate.getForEntity(URL_SECURED_BY_AUTHENTICATION + "/1", String.class); @@ -64,7 +62,7 @@ public void givenRestTemplateWrapperWithCredentials_whenSendGetForEntity_thenSta } @Test - public void givenTestRestTemplateWithCredentials_whenSendGetForEntity_thenStatusOk() throws IOException { + public void givenTestRestTemplateWithCredentials_whenSendGetForEntity_thenStatusOk() { TestRestTemplate testRestTemplate = new TestRestTemplate("test", "test"); ResponseEntity response = testRestTemplate.getForEntity(URL_SECURED_BY_AUTHENTICATION + "/1", String.class); @@ -72,7 +70,7 @@ public void givenTestRestTemplateWithCredentials_whenSendGetForEntity_thenStatus } @Test - public void givenTestRestTemplateWithBasicAuth_whenSendGetForEntity_thenStatusOk() throws IOException { + public void givenTestRestTemplateWithBasicAuth_whenSendGetForEntity_thenStatusOk() { TestRestTemplate testRestTemplate = new TestRestTemplate(); ResponseEntity response = testRestTemplate.withBasicAuth("test", "test"). getForEntity(URL_SECURED_BY_AUTHENTICATION + "/1", String.class); @@ -80,8 +78,7 @@ public void givenTestRestTemplateWithBasicAuth_whenSendGetForEntity_thenStatusOk } @Test - public void givenTestRestTemplateWithCredentialsAndEnabledCookies_whenSendGetForEntity_thenStatusOk() - throws IOException { + public void givenTestRestTemplateWithCredentialsAndEnabledCookies_whenSendGetForEntity_thenStatusOk() { TestRestTemplate testRestTemplate = new TestRestTemplate("test", "test", TestRestTemplate. HttpClientOption.ENABLE_COOKIES); ResponseEntity response = testRestTemplate.getForEntity(URL_SECURED_BY_AUTHENTICATION + "/1", From 5915d081b7acf7a67a09436bb3a548722ab2d15b Mon Sep 17 00:00:00 2001 From: Tomasz Date: Fri, 31 Mar 2017 17:08:16 +0200 Subject: [PATCH 7/9] [BAEL-431] added more meaningful user and password for auth. --- .../client/TestRestTemplateBasicLiveTest.java | 25 +++++++++---------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/spring-rest/src/test/java/org/baeldung/client/TestRestTemplateBasicLiveTest.java b/spring-rest/src/test/java/org/baeldung/client/TestRestTemplateBasicLiveTest.java index 9f4319d85702..a4d8eb0908ed 100644 --- a/spring-rest/src/test/java/org/baeldung/client/TestRestTemplateBasicLiveTest.java +++ b/spring-rest/src/test/java/org/baeldung/client/TestRestTemplateBasicLiveTest.java @@ -21,7 +21,7 @@ public class TestRestTemplateBasicLiveTest { private RestTemplate restTemplate; private static final String FOO_RESOURCE_URL = "http://localhost:" + APPLICATION_PORT + "/spring-rest/myfoos"; - private static final String URL_SECURED_BY_AUTHENTICATION = "http://browserspy.dk/password-ok.php"; + private static final String URL_SECURED_BY_AUTHENTICATION = "http://httpbin.org/basic-auth/user/passwd"; private static final String BASE_URL = "http://localhost:" + APPLICATION_PORT + "/spring-rest"; @Before @@ -55,16 +55,16 @@ public void givenRestTemplateBuilderWrapper_whenSendGetForEntity_thenStatusOk() @Test public void givenRestTemplateWrapperWithCredentials_whenSendGetForEntity_thenStatusOk() { - TestRestTemplate testRestTemplate = new TestRestTemplate(restTemplate, "test", "test"); - ResponseEntity response = testRestTemplate.getForEntity(URL_SECURED_BY_AUTHENTICATION + "/1", + TestRestTemplate testRestTemplate = new TestRestTemplate(restTemplate, "user", "passwd"); + ResponseEntity response = testRestTemplate.getForEntity(URL_SECURED_BY_AUTHENTICATION, String.class); assertThat(response.getStatusCode(), equalTo(HttpStatus.OK)); } @Test public void givenTestRestTemplateWithCredentials_whenSendGetForEntity_thenStatusOk() { - TestRestTemplate testRestTemplate = new TestRestTemplate("test", "test"); - ResponseEntity response = testRestTemplate.getForEntity(URL_SECURED_BY_AUTHENTICATION + "/1", + TestRestTemplate testRestTemplate = new TestRestTemplate("user", "passwd"); + ResponseEntity response = testRestTemplate.getForEntity(URL_SECURED_BY_AUTHENTICATION, String.class); assertThat(response.getStatusCode(), equalTo(HttpStatus.OK)); } @@ -72,16 +72,16 @@ public void givenTestRestTemplateWithCredentials_whenSendGetForEntity_thenStatus @Test public void givenTestRestTemplateWithBasicAuth_whenSendGetForEntity_thenStatusOk() { TestRestTemplate testRestTemplate = new TestRestTemplate(); - ResponseEntity response = testRestTemplate.withBasicAuth("test", "test"). - getForEntity(URL_SECURED_BY_AUTHENTICATION + "/1", String.class); + ResponseEntity response = testRestTemplate.withBasicAuth("user", "passwd"). + getForEntity(URL_SECURED_BY_AUTHENTICATION, String.class); assertThat(response.getStatusCode(), equalTo(HttpStatus.OK)); } @Test public void givenTestRestTemplateWithCredentialsAndEnabledCookies_whenSendGetForEntity_thenStatusOk() { - TestRestTemplate testRestTemplate = new TestRestTemplate("test", "test", TestRestTemplate. + TestRestTemplate testRestTemplate = new TestRestTemplate("user", "passwd", TestRestTemplate. HttpClientOption.ENABLE_COOKIES); - ResponseEntity response = testRestTemplate.getForEntity(URL_SECURED_BY_AUTHENTICATION + "/1", + ResponseEntity response = testRestTemplate.getForEntity(URL_SECURED_BY_AUTHENTICATION, String.class); assertThat(response.getStatusCode(), equalTo(HttpStatus.OK)); } @@ -97,18 +97,17 @@ public void givenFooService_whenCallHeadForHeaders_thenReceiveAllHeaders() { // POST @Test public void givenService_whenPostForObject_thenCreatedObjectIsReturned() { - TestRestTemplate testRestTemplate = new TestRestTemplate("test", "test"); + TestRestTemplate testRestTemplate = new TestRestTemplate("user", "passwd"); final RequestBody body = RequestBody.create(okhttp3.MediaType.parse("text/html; charset=utf-8"), "{\"id\":1,\"name\":\"Jim\"}"); final Request request = new Request.Builder().url(BASE_URL + "/users/detail").post(body).build(); - Object response = testRestTemplate.postForObject(URL_SECURED_BY_AUTHENTICATION, request, String.class); - assertTrue(response.toString().contains("Success")); + testRestTemplate.postForObject(URL_SECURED_BY_AUTHENTICATION, request, String.class); } // PUT @Test public void givenService_whenPutForObject_thenCreatedObjectIsReturned() { - TestRestTemplate testRestTemplate = new TestRestTemplate("test", "test"); + TestRestTemplate testRestTemplate = new TestRestTemplate("user", "passwd"); final RequestBody body = RequestBody.create(okhttp3.MediaType.parse("text/html; charset=utf-8"), "{\"id\":1,\"name\":\"Jim\"}"); final Request request = new Request.Builder().url(BASE_URL + "/users/detail").post(body).build(); From cb705f22e431d66446ba52a9bc93f9d129059417 Mon Sep 17 00:00:00 2001 From: Tomasz Date: Sat, 22 Apr 2017 19:16:52 +0200 Subject: [PATCH 8/9] [BAEL-820] examples of wait() and sleep() methods. --- .../concurrent/sleepwait/ThreadA.java | 21 ++++++++++++++++++ .../concurrent/sleepwait/ThreadB.java | 20 +++++++++++++++++ .../sleepwait/WaitSleepExample.java | 22 +++++++++++++++++++ 3 files changed, 63 insertions(+) create mode 100644 core-java/src/main/java/com/baeldung/concurrent/sleepwait/ThreadA.java create mode 100644 core-java/src/main/java/com/baeldung/concurrent/sleepwait/ThreadB.java create mode 100644 core-java/src/main/java/com/baeldung/concurrent/sleepwait/WaitSleepExample.java diff --git a/core-java/src/main/java/com/baeldung/concurrent/sleepwait/ThreadA.java b/core-java/src/main/java/com/baeldung/concurrent/sleepwait/ThreadA.java new file mode 100644 index 000000000000..bfa4e0164524 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/concurrent/sleepwait/ThreadA.java @@ -0,0 +1,21 @@ +package com.baeldung.concurrent.sleepwait; + +/*** + * Example of waking up a waiting thread + */ +public class ThreadA { + private static final ThreadB b = new ThreadB(); + + public static void main(String... args) throws InterruptedException { + b.start(); + + synchronized (b) { + while (b.sum == 0) { + System.out.println("Waiting for ThreadB to complete..."); + b.wait(); + } + + System.out.println("ThreadB has completed. Sum from that thread is: " + b.sum); + } + } +} \ No newline at end of file diff --git a/core-java/src/main/java/com/baeldung/concurrent/sleepwait/ThreadB.java b/core-java/src/main/java/com/baeldung/concurrent/sleepwait/ThreadB.java new file mode 100644 index 000000000000..3901fae91b7c --- /dev/null +++ b/core-java/src/main/java/com/baeldung/concurrent/sleepwait/ThreadB.java @@ -0,0 +1,20 @@ +package com.baeldung.concurrent.sleepwait; + +/*** + * Example of waking up a waiting thread + */ +class ThreadB extends Thread { + int sum; + + @Override + public void run() { + synchronized (this) { + int i = 0; + while (i < 100000) { + sum += i; + i++; + } + notify(); + } + } +} \ No newline at end of file diff --git a/core-java/src/main/java/com/baeldung/concurrent/sleepwait/WaitSleepExample.java b/core-java/src/main/java/com/baeldung/concurrent/sleepwait/WaitSleepExample.java new file mode 100644 index 000000000000..ff197783edda --- /dev/null +++ b/core-java/src/main/java/com/baeldung/concurrent/sleepwait/WaitSleepExample.java @@ -0,0 +1,22 @@ +package com.baeldung.concurrent.sleepwait; + +/*** + * Example of wait() and sleep() methods + */ +public class WaitSleepExample { + private static final Object LOCK = new Object(); + + public static void main(String... args) throws InterruptedException { + sleepWaitInSyncronizedBlocks(); + } + + private static void sleepWaitInSyncronizedBlocks() throws InterruptedException { + Thread.sleep(1000); // called on the thread + System.out.println("Thread '" + Thread.currentThread().getName() + "' is woken after sleeping for 1 second"); + + synchronized (LOCK) { + LOCK.wait(1000); // called on the object, synchronization required + System.out.println("Object '" + LOCK + "' is woken after waiting for 1 second"); + } + } +} From c0ce017ae4ee14143d9c712891eeb42de1b7d918 Mon Sep 17 00:00:00 2001 From: Tomasz Date: Sat, 22 Apr 2017 20:27:35 +0200 Subject: [PATCH 9/9] [BAEL-820] wait() and sleep() examples. --- .../main/java/com/baeldung/concurrent/sleepwait/ThreadA.java | 2 +- .../main/java/com/baeldung/concurrent/sleepwait/ThreadB.java | 2 +- .../com/baeldung/concurrent/sleepwait/WaitSleepExample.java | 1 + 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/core-java/src/main/java/com/baeldung/concurrent/sleepwait/ThreadA.java b/core-java/src/main/java/com/baeldung/concurrent/sleepwait/ThreadA.java index bfa4e0164524..b19bc3fe1ac8 100644 --- a/core-java/src/main/java/com/baeldung/concurrent/sleepwait/ThreadA.java +++ b/core-java/src/main/java/com/baeldung/concurrent/sleepwait/ThreadA.java @@ -18,4 +18,4 @@ public static void main(String... args) throws InterruptedException { System.out.println("ThreadB has completed. Sum from that thread is: " + b.sum); } } -} \ No newline at end of file +} diff --git a/core-java/src/main/java/com/baeldung/concurrent/sleepwait/ThreadB.java b/core-java/src/main/java/com/baeldung/concurrent/sleepwait/ThreadB.java index 3901fae91b7c..1fc180a5de72 100644 --- a/core-java/src/main/java/com/baeldung/concurrent/sleepwait/ThreadB.java +++ b/core-java/src/main/java/com/baeldung/concurrent/sleepwait/ThreadB.java @@ -17,4 +17,4 @@ public void run() { notify(); } } -} \ No newline at end of file +} diff --git a/core-java/src/main/java/com/baeldung/concurrent/sleepwait/WaitSleepExample.java b/core-java/src/main/java/com/baeldung/concurrent/sleepwait/WaitSleepExample.java index ff197783edda..e84fe29d8734 100644 --- a/core-java/src/main/java/com/baeldung/concurrent/sleepwait/WaitSleepExample.java +++ b/core-java/src/main/java/com/baeldung/concurrent/sleepwait/WaitSleepExample.java @@ -19,4 +19,5 @@ private static void sleepWaitInSyncronizedBlocks() throws InterruptedException { System.out.println("Object '" + LOCK + "' is woken after waiting for 1 second"); } } + }