From 672be6c7ff62bf3aa0c23c2ab63eaa1c75bbb35b Mon Sep 17 00:00:00 2001 From: Sunil Gulabani Date: Fri, 10 Feb 2017 17:54:00 +0530 Subject: [PATCH 01/68] BAEL-650: Lambda and DynamoDB --- aws/pom.xml | 24 ++++++- .../lambda/dynamodb/SavePersonHandler.java | 57 +++++++++++++++ .../lambda/dynamodb/bean/PersonRequest.java | 71 +++++++++++++++++++ .../lambda/dynamodb/bean/PersonResponse.java | 25 +++++++ 4 files changed, 176 insertions(+), 1 deletion(-) create mode 100644 aws/src/main/java/com/baeldung/lambda/dynamodb/SavePersonHandler.java create mode 100644 aws/src/main/java/com/baeldung/lambda/dynamodb/bean/PersonRequest.java create mode 100644 aws/src/main/java/com/baeldung/lambda/dynamodb/bean/PersonResponse.java diff --git a/aws/pom.xml b/aws/pom.xml index f3ae672a2f..4ea6b8786c 100644 --- a/aws/pom.xml +++ b/aws/pom.xml @@ -14,11 +14,23 @@ 1.1.0 + + com.amazonaws + aws-lambda-java-events + 1.3.0 + + commons-io commons-io 2.5 + + + com.google.code.gson + gson + 2.8.0 + @@ -26,7 +38,7 @@ org.apache.maven.plugins maven-shade-plugin - 2.3 + 3.0.0 false @@ -41,4 +53,14 @@ + + + artifactory-isg-release + https://repository.deere.com/artifactory/isg-release + + + axiom-nexus + http://isgnexus.deere.com/nexus/content/groups/public + + \ No newline at end of file diff --git a/aws/src/main/java/com/baeldung/lambda/dynamodb/SavePersonHandler.java b/aws/src/main/java/com/baeldung/lambda/dynamodb/SavePersonHandler.java new file mode 100644 index 0000000000..47d3569d27 --- /dev/null +++ b/aws/src/main/java/com/baeldung/lambda/dynamodb/SavePersonHandler.java @@ -0,0 +1,57 @@ +/** + * "Unpublished Work © 2017 Deere & Company. All Worldwide Rights Reserved. THIS MATERIAL IS THE PROPERTY OF DEERE & + * COMPANY. ALL USE, ALTERATIONS, DISCLOSURE, DISSEMINATION AND/OR REPRODUCTION NOT SPECIFICALLY AUTHORIZED BY DEERE & + * COMPANY IS PROHIBITED." + */ +package com.baeldung.lambda.dynamodb; + +import com.amazonaws.regions.Region; +import com.amazonaws.regions.Regions; +import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClient; +import com.amazonaws.services.dynamodbv2.document.DynamoDB; +import com.amazonaws.services.dynamodbv2.document.Item; +import com.amazonaws.services.dynamodbv2.document.PutItemOutcome; +import com.amazonaws.services.dynamodbv2.document.spec.PutItemSpec; +import com.amazonaws.services.dynamodbv2.model.ConditionalCheckFailedException; +import com.amazonaws.services.lambda.runtime.Context; +import com.amazonaws.services.lambda.runtime.RequestHandler; +import com.baeldung.lambda.dynamodb.bean.PersonRequest; +import com.baeldung.lambda.dynamodb.bean.PersonResponse; + +public class SavePersonHandler implements RequestHandler { + + private DynamoDB dynamoDb; + + private final String DYNAMODB_TABLE_NAME = "Person"; + private final Regions REGION = Regions.US_WEST_2; + + public PersonResponse handleRequest(PersonRequest personRequest, Context context) { + context.getLogger().log("personRequest: " + personRequest); + this.initDynamoDbClient(); + + persistData(personRequest); + + PersonResponse personResponse = new PersonResponse(); + personResponse.setMessage("Saved Successfully!!!"); + context.getLogger().log("personResponse: " + personResponse); + return personResponse; + } + + private PutItemOutcome persistData(PersonRequest personRequest) throws ConditionalCheckFailedException { + return this.dynamoDb.getTable(DYNAMODB_TABLE_NAME) + .putItem( + new PutItemSpec().withItem(new Item() + .withNumber("id", personRequest.getId()) + .withString("firstName", personRequest.getFirstName()) + .withString("lastName", personRequest.getLastName()) + .withNumber("age", personRequest.getAge()) + .withString("address", personRequest.getAddress()) + )); + } + + private void initDynamoDbClient() { + final AmazonDynamoDBClient client = new AmazonDynamoDBClient(); + client.setRegion(Region.getRegion(REGION)); + this.dynamoDb = new DynamoDB(client); + } +} diff --git a/aws/src/main/java/com/baeldung/lambda/dynamodb/bean/PersonRequest.java b/aws/src/main/java/com/baeldung/lambda/dynamodb/bean/PersonRequest.java new file mode 100644 index 0000000000..1f699d7ee3 --- /dev/null +++ b/aws/src/main/java/com/baeldung/lambda/dynamodb/bean/PersonRequest.java @@ -0,0 +1,71 @@ +/** + * "Unpublished Work © 2017 Deere & Company. All Worldwide Rights Reserved. THIS MATERIAL IS THE PROPERTY OF DEERE & + * COMPANY. ALL USE, ALTERATIONS, DISCLOSURE, DISSEMINATION AND/OR REPRODUCTION NOT SPECIFICALLY AUTHORIZED BY DEERE & + * COMPANY IS PROHIBITED." + */ +package com.baeldung.lambda.dynamodb.bean; + +import com.google.gson.Gson; + +public class PersonRequest { + private int id; + private String firstName; + private String lastName; + private int age; + private String address; + + public static void main(String[] args) { + PersonRequest personRequest = new PersonRequest(); + personRequest.setId(1); + personRequest.setFirstName("John"); + personRequest.setLastName("Doe"); + personRequest.setAge(30); + personRequest.setAddress("United States"); + System.out.println(personRequest); + } + + public String toString() { + final Gson gson = new Gson(); + return gson.toJson(this); + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getFirstName() { + return firstName; + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + public String getLastName() { + return lastName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } + + public int getAge() { + return age; + } + + public void setAge(int age) { + this.age = age; + } + + public String getAddress() { + return address; + } + + public void setAddress(String address) { + this.address = address; + } +} diff --git a/aws/src/main/java/com/baeldung/lambda/dynamodb/bean/PersonResponse.java b/aws/src/main/java/com/baeldung/lambda/dynamodb/bean/PersonResponse.java new file mode 100644 index 0000000000..19724adbfa --- /dev/null +++ b/aws/src/main/java/com/baeldung/lambda/dynamodb/bean/PersonResponse.java @@ -0,0 +1,25 @@ +/** + * "Unpublished Work © 2017 Deere & Company. All Worldwide Rights Reserved. THIS MATERIAL IS THE PROPERTY OF DEERE & + * COMPANY. ALL USE, ALTERATIONS, DISCLOSURE, DISSEMINATION AND/OR REPRODUCTION NOT SPECIFICALLY AUTHORIZED BY DEERE & + * COMPANY IS PROHIBITED." + */ +package com.baeldung.lambda.dynamodb.bean; + +import com.google.gson.Gson; + +public class PersonResponse { + private String message; + + public String toString() { + final Gson gson = new Gson(); + return gson.toJson(this); + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } +} From c9bb4b9e37ca9d4a45a0d90adbd1059ceb7ae27d Mon Sep 17 00:00:00 2001 From: Sunil Gulabani Date: Sun, 12 Feb 2017 13:44:03 +0530 Subject: [PATCH 02/68] BAEL-650: Removed unwanted comments --- aws/pom.xml | 10 ---------- .../baeldung/lambda/dynamodb/SavePersonHandler.java | 5 ----- .../baeldung/lambda/dynamodb/bean/PersonRequest.java | 5 ----- .../baeldung/lambda/dynamodb/bean/PersonResponse.java | 5 ----- 4 files changed, 25 deletions(-) diff --git a/aws/pom.xml b/aws/pom.xml index 4ea6b8786c..881ba6522d 100644 --- a/aws/pom.xml +++ b/aws/pom.xml @@ -53,14 +53,4 @@ - - - artifactory-isg-release - https://repository.deere.com/artifactory/isg-release - - - axiom-nexus - http://isgnexus.deere.com/nexus/content/groups/public - - \ No newline at end of file diff --git a/aws/src/main/java/com/baeldung/lambda/dynamodb/SavePersonHandler.java b/aws/src/main/java/com/baeldung/lambda/dynamodb/SavePersonHandler.java index 47d3569d27..70aa0fd68f 100644 --- a/aws/src/main/java/com/baeldung/lambda/dynamodb/SavePersonHandler.java +++ b/aws/src/main/java/com/baeldung/lambda/dynamodb/SavePersonHandler.java @@ -1,8 +1,3 @@ -/** - * "Unpublished Work © 2017 Deere & Company. All Worldwide Rights Reserved. THIS MATERIAL IS THE PROPERTY OF DEERE & - * COMPANY. ALL USE, ALTERATIONS, DISCLOSURE, DISSEMINATION AND/OR REPRODUCTION NOT SPECIFICALLY AUTHORIZED BY DEERE & - * COMPANY IS PROHIBITED." - */ package com.baeldung.lambda.dynamodb; import com.amazonaws.regions.Region; diff --git a/aws/src/main/java/com/baeldung/lambda/dynamodb/bean/PersonRequest.java b/aws/src/main/java/com/baeldung/lambda/dynamodb/bean/PersonRequest.java index 1f699d7ee3..6071f4448e 100644 --- a/aws/src/main/java/com/baeldung/lambda/dynamodb/bean/PersonRequest.java +++ b/aws/src/main/java/com/baeldung/lambda/dynamodb/bean/PersonRequest.java @@ -1,8 +1,3 @@ -/** - * "Unpublished Work © 2017 Deere & Company. All Worldwide Rights Reserved. THIS MATERIAL IS THE PROPERTY OF DEERE & - * COMPANY. ALL USE, ALTERATIONS, DISCLOSURE, DISSEMINATION AND/OR REPRODUCTION NOT SPECIFICALLY AUTHORIZED BY DEERE & - * COMPANY IS PROHIBITED." - */ package com.baeldung.lambda.dynamodb.bean; import com.google.gson.Gson; diff --git a/aws/src/main/java/com/baeldung/lambda/dynamodb/bean/PersonResponse.java b/aws/src/main/java/com/baeldung/lambda/dynamodb/bean/PersonResponse.java index 19724adbfa..a61294ddea 100644 --- a/aws/src/main/java/com/baeldung/lambda/dynamodb/bean/PersonResponse.java +++ b/aws/src/main/java/com/baeldung/lambda/dynamodb/bean/PersonResponse.java @@ -1,8 +1,3 @@ -/** - * "Unpublished Work © 2017 Deere & Company. All Worldwide Rights Reserved. THIS MATERIAL IS THE PROPERTY OF DEERE & - * COMPANY. ALL USE, ALTERATIONS, DISCLOSURE, DISSEMINATION AND/OR REPRODUCTION NOT SPECIFICALLY AUTHORIZED BY DEERE & - * COMPANY IS PROHIBITED." - */ package com.baeldung.lambda.dynamodb.bean; import com.google.gson.Gson; From b3303dc0cfebe443c1d997900bd8f8cf89d94764 Mon Sep 17 00:00:00 2001 From: Tomasz Lelek Date: Tue, 14 Feb 2017 16:17:55 +0100 Subject: [PATCH 03/68] BAEL-633 craete cglib template --- cglib/pom.xml | 47 +++++++++++++++++++ .../com/baeldung/cglib/proxy/SampleClass.java | 7 +++ .../baeldung/cglib/proxy/SampleClassTest.java | 20 ++++++++ pom.xml | 1 + 4 files changed, 75 insertions(+) create mode 100644 cglib/pom.xml create mode 100644 cglib/src/main/java/com/baeldung/cglib/proxy/SampleClass.java create mode 100644 cglib/src/test/java/com/baeldung/cglib/proxy/SampleClassTest.java diff --git a/cglib/pom.xml b/cglib/pom.xml new file mode 100644 index 0000000000..21309cf514 --- /dev/null +++ b/cglib/pom.xml @@ -0,0 +1,47 @@ + + + + parent-modules + com.baeldung + 1.0.0-SNAPSHOT + + 4.0.0 + + cglib + + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.8 + 1.8 + + + + + + + + + cglib + cglib + ${cglib.version} + + + junit + junit + ${junit.version} + test + + + + + 3.2.4 + 4.12 + + + + \ No newline at end of file diff --git a/cglib/src/main/java/com/baeldung/cglib/proxy/SampleClass.java b/cglib/src/main/java/com/baeldung/cglib/proxy/SampleClass.java new file mode 100644 index 0000000000..3e0d098dd1 --- /dev/null +++ b/cglib/src/main/java/com/baeldung/cglib/proxy/SampleClass.java @@ -0,0 +1,7 @@ +package com.baeldung.cglib.proxy; + +public class SampleClass { + public String test(String input) { + return "Hello world!"; + } +} \ No newline at end of file diff --git a/cglib/src/test/java/com/baeldung/cglib/proxy/SampleClassTest.java b/cglib/src/test/java/com/baeldung/cglib/proxy/SampleClassTest.java new file mode 100644 index 0000000000..15037b3f1c --- /dev/null +++ b/cglib/src/test/java/com/baeldung/cglib/proxy/SampleClassTest.java @@ -0,0 +1,20 @@ +package com.baeldung.cglib.proxy; + +import net.sf.cglib.proxy.Enhancer; +import net.sf.cglib.proxy.FixedValue; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class SampleClassTest { + @Test + public void testFixedValue() throws Exception { + Enhancer enhancer = new Enhancer(); + enhancer.setSuperclass(SampleClass.class); + enhancer.setCallback((FixedValue) () -> "Hello cglib!"); + SampleClass proxy = (SampleClass) enhancer.create(); + assertEquals("Hello cglib!", proxy.test(null)); + + } + +} \ No newline at end of file diff --git a/pom.xml b/pom.xml index 2392e2c594..5bf0d4ad02 100644 --- a/pom.xml +++ b/pom.xml @@ -191,6 +191,7 @@ xstream struts2 + cglib From dd0158836921e4170255dddc3da808ebe47577a5 Mon Sep 17 00:00:00 2001 From: Sunil Gulabani Date: Wed, 15 Feb 2017 18:07:14 +0530 Subject: [PATCH 04/68] BAEL-650: Small refactoring --- .../lambda/dynamodb/SavePersonHandler.java | 23 ++++++++----------- .../lambda/dynamodb/bean/PersonRequest.java | 3 ++- 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/aws/src/main/java/com/baeldung/lambda/dynamodb/SavePersonHandler.java b/aws/src/main/java/com/baeldung/lambda/dynamodb/SavePersonHandler.java index 70aa0fd68f..625da62efd 100644 --- a/aws/src/main/java/com/baeldung/lambda/dynamodb/SavePersonHandler.java +++ b/aws/src/main/java/com/baeldung/lambda/dynamodb/SavePersonHandler.java @@ -17,35 +17,32 @@ public class SavePersonHandler implements RequestHandler Date: Wed, 15 Feb 2017 21:46:45 +0530 Subject: [PATCH 05/68] Changes_for_BAEL-552-Update_SpringDataREST_article --- .../SpringDataRestValidatorTest.java | 186 ++++++++---------- 1 file changed, 86 insertions(+), 100 deletions(-) rename spring-data-rest/src/{main/test => test/java}/com/baeldung/validator/SpringDataRestValidatorTest.java (75%) diff --git a/spring-data-rest/src/main/test/com/baeldung/validator/SpringDataRestValidatorTest.java b/spring-data-rest/src/test/java/com/baeldung/validator/SpringDataRestValidatorTest.java similarity index 75% rename from spring-data-rest/src/main/test/com/baeldung/validator/SpringDataRestValidatorTest.java rename to spring-data-rest/src/test/java/com/baeldung/validator/SpringDataRestValidatorTest.java index b185c6d5ab..300fc081d3 100644 --- a/spring-data-rest/src/main/test/com/baeldung/validator/SpringDataRestValidatorTest.java +++ b/spring-data-rest/src/test/java/com/baeldung/validator/SpringDataRestValidatorTest.java @@ -1,100 +1,86 @@ -package com.baeldung.validator; - -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; -import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.redirectedUrl; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; -import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup; - -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.SpringApplicationConfiguration; -import org.springframework.http.MediaType; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -import org.springframework.test.context.web.WebAppConfiguration; -import org.springframework.test.web.servlet.MockMvc; -import org.springframework.test.web.servlet.ResultActions; -import org.springframework.test.web.servlet.ResultMatcher; -import org.springframework.web.context.WebApplicationContext; - -import com.baeldung.SpringDataRestApplication; -import com.baeldung.models.WebsiteUser; -import com.fasterxml.jackson.databind.ObjectMapper; - -@RunWith(SpringJUnit4ClassRunner.class) -@SpringApplicationConfiguration(classes = SpringDataRestApplication.class) -@WebAppConfiguration -public class SpringDataRestValidatorTest { - public static final String URL = "http://localhost"; - - private MockMvc mockMvc; - - @Autowired - protected WebApplicationContext wac; - - @Before - public void setup() { - mockMvc = webAppContextSetup(wac).build(); - } - - @Test - public void whenStartingApplication_thenCorrectStatusCode() throws Exception { - mockMvc.perform(get("/users")).andExpect(status().is2xxSuccessful()); - }; - - @Test - public void whenAddingNewCorrectUser_thenCorrectStatusCodeAndResponse() throws Exception { - WebsiteUser user = new WebsiteUser(); - user.setEmail("john.doe@john.com"); - user.setName("John Doe"); - - mockMvc.perform(post("/users", user).contentType(MediaType.APPLICATION_JSON).content(new ObjectMapper().writeValueAsString(user))) - .andExpect(status().is2xxSuccessful()) - .andExpect(redirectedUrl("http://localhost/users/1")); - } - - @Test - public void whenAddingNewUserWithoutName_thenErrorStatusCodeAndResponse() throws Exception { - WebsiteUser user = new WebsiteUser(); - user.setEmail("john.doe@john.com"); - - mockMvc.perform(post("/users", user).contentType(MediaType.APPLICATION_JSON).content(new ObjectMapper().writeValueAsString(user))) - .andExpect(status().isNotAcceptable()) - .andExpect(redirectedUrl(null)); - } - - @Test - public void whenAddingNewUserWithEmptyName_thenErrorStatusCodeAndResponse() throws Exception { - WebsiteUser user = new WebsiteUser(); - user.setEmail("john.doe@john.com"); - user.setName(""); - mockMvc.perform(post("/users", user).contentType(MediaType.APPLICATION_JSON).content(new ObjectMapper().writeValueAsString(user))) - .andExpect(status().isNotAcceptable()) - .andExpect(redirectedUrl(null)); - } - - @Test - public void whenAddingNewUserWithoutEmail_thenErrorStatusCodeAndResponse() throws Exception { - WebsiteUser user = new WebsiteUser(); - user.setName("John Doe"); - - mockMvc.perform(post("/users", user).contentType(MediaType.APPLICATION_JSON).content(new ObjectMapper().writeValueAsString(user))) - .andExpect(status().isNotAcceptable()) - .andExpect(redirectedUrl(null)); - } - - @Test - public void whenAddingNewUserWithEmptyEmail_thenErrorStatusCodeAndResponse() throws Exception { - WebsiteUser user = new WebsiteUser(); - user.setName("John Doe"); - user.setEmail(""); - mockMvc.perform(post("/users", user).contentType(MediaType.APPLICATION_JSON).content(new ObjectMapper().writeValueAsString(user))) - .andExpect(status().isNotAcceptable()) - .andExpect(redirectedUrl(null)); - } - -} +package com.baeldung.validator; + +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.redirectedUrl; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.http.MediaType; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.web.WebAppConfiguration; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.web.context.WebApplicationContext; + +import com.baeldung.SpringDataRestApplication; +import com.baeldung.models.WebsiteUser; +import com.fasterxml.jackson.databind.ObjectMapper; + +@RunWith(SpringJUnit4ClassRunner.class) +@SpringBootTest(classes = SpringDataRestApplication.class) +@WebAppConfiguration +public class SpringDataRestValidatorTest { + public static final String URL = "http://localhost"; + + private MockMvc mockMvc; + + @Autowired + protected WebApplicationContext wac; + + @Before + public void setup() { + mockMvc = webAppContextSetup(wac).build(); + } + + @Test + public void whenStartingApplication_thenCorrectStatusCode() throws Exception { + mockMvc.perform(get("/users")).andExpect(status().is2xxSuccessful()); + }; + + @Test + public void whenAddingNewCorrectUser_thenCorrectStatusCodeAndResponse() throws Exception { + WebsiteUser user = new WebsiteUser(); + user.setEmail("john.doe@john.com"); + user.setName("John Doe"); + + mockMvc.perform(post("/users", user).contentType(MediaType.APPLICATION_JSON).content(new ObjectMapper().writeValueAsString(user))).andExpect(status().is2xxSuccessful()).andExpect(redirectedUrl("http://localhost/users/1")); + } + + @Test + public void whenAddingNewUserWithoutName_thenErrorStatusCodeAndResponse() throws Exception { + WebsiteUser user = new WebsiteUser(); + user.setEmail("john.doe@john.com"); + + mockMvc.perform(post("/users", user).contentType(MediaType.APPLICATION_JSON).content(new ObjectMapper().writeValueAsString(user))).andExpect(status().isNotAcceptable()).andExpect(redirectedUrl(null)); + } + + @Test + public void whenAddingNewUserWithEmptyName_thenErrorStatusCodeAndResponse() throws Exception { + WebsiteUser user = new WebsiteUser(); + user.setEmail("john.doe@john.com"); + user.setName(""); + mockMvc.perform(post("/users", user).contentType(MediaType.APPLICATION_JSON).content(new ObjectMapper().writeValueAsString(user))).andExpect(status().isNotAcceptable()).andExpect(redirectedUrl(null)); + } + + @Test + public void whenAddingNewUserWithoutEmail_thenErrorStatusCodeAndResponse() throws Exception { + WebsiteUser user = new WebsiteUser(); + user.setName("John Doe"); + + mockMvc.perform(post("/users", user).contentType(MediaType.APPLICATION_JSON).content(new ObjectMapper().writeValueAsString(user))).andExpect(status().isNotAcceptable()).andExpect(redirectedUrl(null)); + } + + @Test + public void whenAddingNewUserWithEmptyEmail_thenErrorStatusCodeAndResponse() throws Exception { + WebsiteUser user = new WebsiteUser(); + user.setName("John Doe"); + user.setEmail(""); + mockMvc.perform(post("/users", user).contentType(MediaType.APPLICATION_JSON).content(new ObjectMapper().writeValueAsString(user))).andExpect(status().isNotAcceptable()).andExpect(redirectedUrl(null)); + } + +} From 6b0e59afac0777717900f822158aa946ed0c071b Mon Sep 17 00:00:00 2001 From: Abhinab Kanrar Date: Thu, 16 Feb 2017 21:45:08 +0530 Subject: [PATCH 06/68] Guide to the Spring WebUtils and ServletRequestUtils (#1184) * rest with spark java * 4 * Update Application.java * indentation changes * spring @requestmapping shortcuts * removing spring requestmapping and pushing spring-mvc-java * Joining/Splitting Strings with Java and Stream API * adding more join/split functionality * changing package name * testcase change * adding webutils --- core-java/0.004102810554955205 | 0 core-java/0.04832801936270381 | 0 core-java/0.5633433244738808 | 0 core-java/0.6256429734439612 | 0 core-java/0.9799201796740292 | 0 .../baeldung/string/JoinerSplitterTest.java | 5 +- .../java/com/baeldung/utils/Application.java | 18 +++++++ .../utils/controller/UtilsController.java | 49 +++++++++++++++++++ .../src/main/resources/templates/other.html | 16 ++++++ .../src/main/resources/templates/utils.html | 23 +++++++++ 10 files changed, 110 insertions(+), 1 deletion(-) create mode 100644 core-java/0.004102810554955205 create mode 100644 core-java/0.04832801936270381 create mode 100644 core-java/0.5633433244738808 create mode 100644 core-java/0.6256429734439612 create mode 100644 core-java/0.9799201796740292 create mode 100644 spring-boot/src/main/java/com/baeldung/utils/Application.java create mode 100644 spring-boot/src/main/java/com/baeldung/utils/controller/UtilsController.java create mode 100644 spring-boot/src/main/resources/templates/other.html create mode 100644 spring-boot/src/main/resources/templates/utils.html diff --git a/core-java/0.004102810554955205 b/core-java/0.004102810554955205 new file mode 100644 index 0000000000..e69de29bb2 diff --git a/core-java/0.04832801936270381 b/core-java/0.04832801936270381 new file mode 100644 index 0000000000..e69de29bb2 diff --git a/core-java/0.5633433244738808 b/core-java/0.5633433244738808 new file mode 100644 index 0000000000..e69de29bb2 diff --git a/core-java/0.6256429734439612 b/core-java/0.6256429734439612 new file mode 100644 index 0000000000..e69de29bb2 diff --git a/core-java/0.9799201796740292 b/core-java/0.9799201796740292 new file mode 100644 index 0000000000..e69de29bb2 diff --git a/core-java/src/test/java/com/baeldung/string/JoinerSplitterTest.java b/core-java/src/test/java/com/baeldung/string/JoinerSplitterTest.java index 9ccff40558..a89f89b8d5 100644 --- a/core-java/src/test/java/com/baeldung/string/JoinerSplitterTest.java +++ b/core-java/src/test/java/com/baeldung/string/JoinerSplitterTest.java @@ -12,7 +12,7 @@ import com.baeldung.string.JoinerSplitter; public class JoinerSplitterTest { @Test - public void givenArray_transformedToStream_convertToString() { + public void provided_array_convert_to_stream_and_convert_to_string() { String[] programming_languages = {"java", "python", "nodejs", "ruby"}; @@ -24,6 +24,7 @@ public class JoinerSplitterTest { @Test public void givenArray_transformedToStream_convertToPrefixPostfixString() { + String[] programming_languages = {"java", "python", "nodejs", "ruby"}; String expectation = "[java,python,nodejs,ruby]"; @@ -34,6 +35,7 @@ public class JoinerSplitterTest { @Test public void givenString_transformedToStream_convertToList() { + String programming_languages = "java,python,nodejs,ruby"; List expectation = new ArrayList(); @@ -49,6 +51,7 @@ public class JoinerSplitterTest { @Test public void givenString_transformedToStream_convertToListOfChar() { + String programming_languages = "java,python,nodejs,ruby"; List expectation = new ArrayList(); diff --git a/spring-boot/src/main/java/com/baeldung/utils/Application.java b/spring-boot/src/main/java/com/baeldung/utils/Application.java new file mode 100644 index 0000000000..1f637eec11 --- /dev/null +++ b/spring-boot/src/main/java/com/baeldung/utils/Application.java @@ -0,0 +1,18 @@ +package com.baeldung.utils; + +import javax.annotation.security.RolesAllowed; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.ComponentScan; + +@SpringBootApplication +@ComponentScan(basePackages="com.baeldung.utils") +public class Application { + + @RolesAllowed("*") + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + } + +} diff --git a/spring-boot/src/main/java/com/baeldung/utils/controller/UtilsController.java b/spring-boot/src/main/java/com/baeldung/utils/controller/UtilsController.java new file mode 100644 index 0000000000..a14d0b26c6 --- /dev/null +++ b/spring-boot/src/main/java/com/baeldung/utils/controller/UtilsController.java @@ -0,0 +1,49 @@ +package com.baeldung.utils.controller; + +import javax.servlet.http.HttpServletRequest; + +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.ServletRequestBindingException; +import org.springframework.web.bind.ServletRequestUtils; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.util.WebUtils; + +@Controller +public class UtilsController { + + @GetMapping("/utils") + public String webUtils(Model model) { + return "utils"; + } + + @PostMapping("/setParam") + public String post(HttpServletRequest request, Model model) { + String param = ServletRequestUtils.getStringParameter(request, "param", "DEFAULT"); + +// Long param = ServletRequestUtils.getLongParameter(request, "param",1L); +// boolean param = ServletRequestUtils.getBooleanParameter(request, "param", true); +// double param = ServletRequestUtils.getDoubleParameter(request, "param", 1000); +// float param = ServletRequestUtils.getFloatParameter(request, "param", (float) 1.00); +// int param = ServletRequestUtils.getIntParameter(request, "param", 100); + +// try { +// ServletRequestUtils.getRequiredStringParameter(request, "param"); +// } catch (ServletRequestBindingException e) { +// e.printStackTrace(); +// } + + WebUtils.setSessionAttribute(request, "parameter", param); + model.addAttribute("parameter", "You set: "+(String) WebUtils.getSessionAttribute(request, "parameter")); + return "utils"; + } + + @GetMapping("/other") + public String other(HttpServletRequest request, Model model) { + String param = (String) WebUtils.getSessionAttribute(request, "parameter"); + model.addAttribute("parameter", param); + return "other"; + } + +} diff --git a/spring-boot/src/main/resources/templates/other.html b/spring-boot/src/main/resources/templates/other.html new file mode 100644 index 0000000000..d13373f9fe --- /dev/null +++ b/spring-boot/src/main/resources/templates/other.html @@ -0,0 +1,16 @@ + + + + +Spring Utils Demo + + + + Parameter set by you:

+ + \ No newline at end of file diff --git a/spring-boot/src/main/resources/templates/utils.html b/spring-boot/src/main/resources/templates/utils.html new file mode 100644 index 0000000000..93030f424f --- /dev/null +++ b/spring-boot/src/main/resources/templates/utils.html @@ -0,0 +1,23 @@ + + + + +Spring Utils Demo + + + +

+

Set Parameter:

+

+ + +

+
+Another Page + + \ No newline at end of file From 935b815ad4828a184a3f246d595b3fc6dfb42599 Mon Sep 17 00:00:00 2001 From: Nancy Bosecker Date: Thu, 16 Feb 2017 19:59:06 -0800 Subject: [PATCH 07/68] Updated test names and moved document add code to @Before method. (#1187) * Solr w Apache SolrJ * Solr w Apache SolrJ * updated test names and moved add to @before method --- .../solrjava/SolrJavaIntegrationTest.java | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/spring-data-solr/src/test/java/com/baeldung/solrjava/SolrJavaIntegrationTest.java b/spring-data-solr/src/test/java/com/baeldung/solrjava/SolrJavaIntegrationTest.java index 1613ba5480..ce90ccaf16 100644 --- a/spring-data-solr/src/test/java/com/baeldung/solrjava/SolrJavaIntegrationTest.java +++ b/spring-data-solr/src/test/java/com/baeldung/solrjava/SolrJavaIntegrationTest.java @@ -18,24 +18,24 @@ import org.junit.Test; public class SolrJavaIntegrationTest { private HttpSolrClient solr; + private SolrInputDocument document; @Before public void setUp() throws Exception { solr = new HttpSolrClient("http://localhost:8983/solr/bigboxstore"); solr.setParser(new XMLResponseParser()); - } - - @Test - public void givenAdd_thenVerifyAdded() throws SolrServerException, IOException { - - SolrInputDocument document = new SolrInputDocument(); + + document = new SolrInputDocument(); document.addField("id", "123456"); document.addField("name", "Kenmore Dishwasher"); document.addField("price", "599.99"); - solr.add(document); solr.commit(); + } + + @Test + public void whenAdd_thenVerifyAdded() throws SolrServerException, IOException { SolrQuery query = new SolrQuery(); query.set("q", "id:123456"); @@ -53,7 +53,7 @@ public class SolrJavaIntegrationTest { } @Test - public void givenDelete_thenVerifyDeleted() throws SolrServerException, IOException { + public void whenDelete_thenVerifyDeleted() throws SolrServerException, IOException { solr.deleteById("123456"); solr.commit(); From de376eb0fc7fe10c5a552ef1a8043b7fa0dc7bfd Mon Sep 17 00:00:00 2001 From: Abhinab Kanrar Date: Fri, 17 Feb 2017 11:02:53 +0530 Subject: [PATCH 08/68] Update Application.java --- spring-boot/src/main/java/com/baeldung/utils/Application.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spring-boot/src/main/java/com/baeldung/utils/Application.java b/spring-boot/src/main/java/com/baeldung/utils/Application.java index 1f637eec11..a3d9f9130c 100644 --- a/spring-boot/src/main/java/com/baeldung/utils/Application.java +++ b/spring-boot/src/main/java/com/baeldung/utils/Application.java @@ -12,7 +12,7 @@ public class Application { @RolesAllowed("*") public static void main(String[] args) { - SpringApplication.run(Application.class, args); + SpringApplication.run(Application.class, args); } } From 88a97726948c7dc422c1d3aa6087296e3f730c55 Mon Sep 17 00:00:00 2001 From: Abhinab Kanrar Date: Fri, 17 Feb 2017 11:04:33 +0530 Subject: [PATCH 09/68] Update UtilsController.java --- .../java/com/baeldung/utils/controller/UtilsController.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spring-boot/src/main/java/com/baeldung/utils/controller/UtilsController.java b/spring-boot/src/main/java/com/baeldung/utils/controller/UtilsController.java index a14d0b26c6..7b4827cdf2 100644 --- a/spring-boot/src/main/java/com/baeldung/utils/controller/UtilsController.java +++ b/spring-boot/src/main/java/com/baeldung/utils/controller/UtilsController.java @@ -35,7 +35,7 @@ public class UtilsController { // } WebUtils.setSessionAttribute(request, "parameter", param); - model.addAttribute("parameter", "You set: "+(String) WebUtils.getSessionAttribute(request, "parameter")); + model.addAttribute("parameter", "You set: "+(String) WebUtils.getSessionAttribute(request, "parameter")); return "utils"; } From 895bcc1921eb716d6f7bd08bbef8c1e124753445 Mon Sep 17 00:00:00 2001 From: Tomasz Lelek Date: Fri, 17 Feb 2017 12:19:57 +0100 Subject: [PATCH 10/68] BAEL-633 add mixing and bean creator test --- .../java/com/baeldung/cglib/mixin/Class1.java | 8 +++ .../java/com/baeldung/cglib/mixin/Class2.java | 8 +++ .../com/baeldung/cglib/mixin/Interface1.java | 5 ++ .../com/baeldung/cglib/mixin/Interface2.java | 5 ++ .../baeldung/cglib/mixin/MixinInterface.java | 4 ++ .../baeldung/cglib/proxy/PersonService.java | 11 ++++ .../com/baeldung/cglib/proxy/SampleClass.java | 7 --- .../cglib/proxy/BeanGeneratorTest.java | 28 +++++++++ .../com/baeldung/cglib/proxy/MixinTest.java | 24 ++++++++ .../cglib/proxy/PersonServiceProxyTest.java | 61 +++++++++++++++++++ .../baeldung/cglib/proxy/SampleClassTest.java | 20 ------ 11 files changed, 154 insertions(+), 27 deletions(-) create mode 100644 cglib/src/main/java/com/baeldung/cglib/mixin/Class1.java create mode 100644 cglib/src/main/java/com/baeldung/cglib/mixin/Class2.java create mode 100644 cglib/src/main/java/com/baeldung/cglib/mixin/Interface1.java create mode 100644 cglib/src/main/java/com/baeldung/cglib/mixin/Interface2.java create mode 100644 cglib/src/main/java/com/baeldung/cglib/mixin/MixinInterface.java create mode 100644 cglib/src/main/java/com/baeldung/cglib/proxy/PersonService.java delete mode 100644 cglib/src/main/java/com/baeldung/cglib/proxy/SampleClass.java create mode 100644 cglib/src/test/java/com/baeldung/cglib/proxy/BeanGeneratorTest.java create mode 100644 cglib/src/test/java/com/baeldung/cglib/proxy/MixinTest.java create mode 100644 cglib/src/test/java/com/baeldung/cglib/proxy/PersonServiceProxyTest.java delete mode 100644 cglib/src/test/java/com/baeldung/cglib/proxy/SampleClassTest.java diff --git a/cglib/src/main/java/com/baeldung/cglib/mixin/Class1.java b/cglib/src/main/java/com/baeldung/cglib/mixin/Class1.java new file mode 100644 index 0000000000..2921cda8a6 --- /dev/null +++ b/cglib/src/main/java/com/baeldung/cglib/mixin/Class1.java @@ -0,0 +1,8 @@ +package com.baeldung.cglib.mixin; + +public class Class1 implements Interface1 { + @Override + public String first() { + return "first"; + } +} \ No newline at end of file diff --git a/cglib/src/main/java/com/baeldung/cglib/mixin/Class2.java b/cglib/src/main/java/com/baeldung/cglib/mixin/Class2.java new file mode 100644 index 0000000000..0098c9debd --- /dev/null +++ b/cglib/src/main/java/com/baeldung/cglib/mixin/Class2.java @@ -0,0 +1,8 @@ +package com.baeldung.cglib.mixin; + +public class Class2 implements Interface2 { + @Override + public String second() { + return "second"; + } +} \ No newline at end of file diff --git a/cglib/src/main/java/com/baeldung/cglib/mixin/Interface1.java b/cglib/src/main/java/com/baeldung/cglib/mixin/Interface1.java new file mode 100644 index 0000000000..5aab155c1d --- /dev/null +++ b/cglib/src/main/java/com/baeldung/cglib/mixin/Interface1.java @@ -0,0 +1,5 @@ +package com.baeldung.cglib.mixin; + +public interface Interface1 { + String first(); +} \ No newline at end of file diff --git a/cglib/src/main/java/com/baeldung/cglib/mixin/Interface2.java b/cglib/src/main/java/com/baeldung/cglib/mixin/Interface2.java new file mode 100644 index 0000000000..7fd2e78608 --- /dev/null +++ b/cglib/src/main/java/com/baeldung/cglib/mixin/Interface2.java @@ -0,0 +1,5 @@ +package com.baeldung.cglib.mixin; + +public interface Interface2 { + String second(); +} \ No newline at end of file diff --git a/cglib/src/main/java/com/baeldung/cglib/mixin/MixinInterface.java b/cglib/src/main/java/com/baeldung/cglib/mixin/MixinInterface.java new file mode 100644 index 0000000000..e7ed362a25 --- /dev/null +++ b/cglib/src/main/java/com/baeldung/cglib/mixin/MixinInterface.java @@ -0,0 +1,4 @@ +package com.baeldung.cglib.mixin; + +public interface MixinInterface extends Interface1, Interface2 { +} \ No newline at end of file diff --git a/cglib/src/main/java/com/baeldung/cglib/proxy/PersonService.java b/cglib/src/main/java/com/baeldung/cglib/proxy/PersonService.java new file mode 100644 index 0000000000..9085e6c49a --- /dev/null +++ b/cglib/src/main/java/com/baeldung/cglib/proxy/PersonService.java @@ -0,0 +1,11 @@ +package com.baeldung.cglib.proxy; + +public class PersonService { + public String sayHello(String name) { + return "Hello " + name; + } + + public Integer lengthOfName(String name) { + return name.length(); + } +} \ No newline at end of file diff --git a/cglib/src/main/java/com/baeldung/cglib/proxy/SampleClass.java b/cglib/src/main/java/com/baeldung/cglib/proxy/SampleClass.java deleted file mode 100644 index 3e0d098dd1..0000000000 --- a/cglib/src/main/java/com/baeldung/cglib/proxy/SampleClass.java +++ /dev/null @@ -1,7 +0,0 @@ -package com.baeldung.cglib.proxy; - -public class SampleClass { - public String test(String input) { - return "Hello world!"; - } -} \ No newline at end of file diff --git a/cglib/src/test/java/com/baeldung/cglib/proxy/BeanGeneratorTest.java b/cglib/src/test/java/com/baeldung/cglib/proxy/BeanGeneratorTest.java new file mode 100644 index 0000000000..4b401ae690 --- /dev/null +++ b/cglib/src/test/java/com/baeldung/cglib/proxy/BeanGeneratorTest.java @@ -0,0 +1,28 @@ +package com.baeldung.cglib.proxy; + + +import net.sf.cglib.beans.BeanGenerator; +import org.junit.Test; + +import java.lang.reflect.Method; + +import static junit.framework.TestCase.assertEquals; + +public class BeanGeneratorTest { + + @Test + public void testBeanGenerator() throws Exception { + //given + BeanGenerator beanGenerator = new BeanGenerator(); + + //when + beanGenerator.addProperty("value", String.class); + Object myBean = beanGenerator.create(); + Method setter = myBean.getClass().getMethod("setValue", String.class); + setter.invoke(myBean, "some string value set by a cglib"); + + //then + Method getter = myBean.getClass().getMethod("getValue"); + assertEquals("some string value set by a cglib", getter.invoke(myBean)); + } +} diff --git a/cglib/src/test/java/com/baeldung/cglib/proxy/MixinTest.java b/cglib/src/test/java/com/baeldung/cglib/proxy/MixinTest.java new file mode 100644 index 0000000000..31e7b2204f --- /dev/null +++ b/cglib/src/test/java/com/baeldung/cglib/proxy/MixinTest.java @@ -0,0 +1,24 @@ +package com.baeldung.cglib.proxy; + +import com.baeldung.cglib.mixin.*; +import net.sf.cglib.proxy.Mixin; +import org.junit.Test; + +import static junit.framework.TestCase.assertEquals; + +public class MixinTest { + + @Test + public void testMixin() throws Exception { + //when + Mixin mixin = Mixin.create( + new Class[]{Interface1.class, Interface2.class, MixinInterface.class}, + new Object[]{new Class1(), new Class2()} + ); + MixinInterface mixinDelegate = (MixinInterface) mixin; + + //then + assertEquals("first", mixinDelegate.first()); + assertEquals("second", mixinDelegate.second()); + } +} diff --git a/cglib/src/test/java/com/baeldung/cglib/proxy/PersonServiceProxyTest.java b/cglib/src/test/java/com/baeldung/cglib/proxy/PersonServiceProxyTest.java new file mode 100644 index 0000000000..2ae6392a05 --- /dev/null +++ b/cglib/src/test/java/com/baeldung/cglib/proxy/PersonServiceProxyTest.java @@ -0,0 +1,61 @@ +package com.baeldung.cglib.proxy; + +import net.sf.cglib.proxy.Enhancer; +import net.sf.cglib.proxy.FixedValue; +import net.sf.cglib.proxy.MethodInterceptor; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class PersonServiceProxyTest { + @Test + public void testService() { + //given + PersonService personService = new PersonService(); + + //when + String res = personService.sayHello("Tom"); + + //then + assertEquals(res, "Hello Tom"); + } + + @Test + public void testFixedValue() throws Exception { + //given + Enhancer enhancer = new Enhancer(); + enhancer.setSuperclass(PersonService.class); + enhancer.setCallback((FixedValue) () -> "Hello cglib!"); + PersonService proxy = (PersonService) enhancer.create(); + + //when + String res = proxy.sayHello(null); + + //then + assertEquals("Hello cglib!", res); + } + + @Test + public void testMethodInterceptor() throws Exception { + //given + Enhancer enhancer = new Enhancer(); + enhancer.setSuperclass(PersonService.class); + enhancer.setCallback((MethodInterceptor) (obj, method, args, proxy) -> { + if (method.getDeclaringClass() != Object.class && method.getReturnType() == String.class) { + return "Hello cglib!"; + } else { + return proxy.invokeSuper(obj, args); + } + }); + + //when + PersonService proxy = (PersonService) enhancer.create(); + + //then + assertEquals("Hello cglib!", proxy.sayHello(null)); + + int lengthOfName = proxy.lengthOfName("Mary"); + assertEquals(4, lengthOfName); + } + +} \ No newline at end of file diff --git a/cglib/src/test/java/com/baeldung/cglib/proxy/SampleClassTest.java b/cglib/src/test/java/com/baeldung/cglib/proxy/SampleClassTest.java deleted file mode 100644 index 15037b3f1c..0000000000 --- a/cglib/src/test/java/com/baeldung/cglib/proxy/SampleClassTest.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.baeldung.cglib.proxy; - -import net.sf.cglib.proxy.Enhancer; -import net.sf.cglib.proxy.FixedValue; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; - -public class SampleClassTest { - @Test - public void testFixedValue() throws Exception { - Enhancer enhancer = new Enhancer(); - enhancer.setSuperclass(SampleClass.class); - enhancer.setCallback((FixedValue) () -> "Hello cglib!"); - SampleClass proxy = (SampleClass) enhancer.create(); - assertEquals("Hello cglib!", proxy.test(null)); - - } - -} \ No newline at end of file From 31cf702160ae3b6a2cfb84fb37340c953a35ff97 Mon Sep 17 00:00:00 2001 From: Tomasz Lelek Date: Fri, 17 Feb 2017 15:57:35 +0100 Subject: [PATCH 11/68] BAEL-633 more descriptive code --- cglib/src/main/java/com/baeldung/cglib/mixin/Class1.java | 2 +- cglib/src/main/java/com/baeldung/cglib/mixin/Class2.java | 2 +- .../java/com/baeldung/cglib/proxy/BeanGeneratorTest.java | 6 +++--- .../test/java/com/baeldung/cglib/proxy/MixinTest.java | 6 +++--- .../com/baeldung/cglib/proxy/PersonServiceProxyTest.java | 9 ++++----- 5 files changed, 12 insertions(+), 13 deletions(-) diff --git a/cglib/src/main/java/com/baeldung/cglib/mixin/Class1.java b/cglib/src/main/java/com/baeldung/cglib/mixin/Class1.java index 2921cda8a6..932951b4cd 100644 --- a/cglib/src/main/java/com/baeldung/cglib/mixin/Class1.java +++ b/cglib/src/main/java/com/baeldung/cglib/mixin/Class1.java @@ -3,6 +3,6 @@ package com.baeldung.cglib.mixin; public class Class1 implements Interface1 { @Override public String first() { - return "first"; + return "first behaviour"; } } \ No newline at end of file diff --git a/cglib/src/main/java/com/baeldung/cglib/mixin/Class2.java b/cglib/src/main/java/com/baeldung/cglib/mixin/Class2.java index 0098c9debd..b2b922a05d 100644 --- a/cglib/src/main/java/com/baeldung/cglib/mixin/Class2.java +++ b/cglib/src/main/java/com/baeldung/cglib/mixin/Class2.java @@ -3,6 +3,6 @@ package com.baeldung.cglib.mixin; public class Class2 implements Interface2 { @Override public String second() { - return "second"; + return "second behaviour"; } } \ No newline at end of file diff --git a/cglib/src/test/java/com/baeldung/cglib/proxy/BeanGeneratorTest.java b/cglib/src/test/java/com/baeldung/cglib/proxy/BeanGeneratorTest.java index 4b401ae690..32706409c5 100644 --- a/cglib/src/test/java/com/baeldung/cglib/proxy/BeanGeneratorTest.java +++ b/cglib/src/test/java/com/baeldung/cglib/proxy/BeanGeneratorTest.java @@ -16,13 +16,13 @@ public class BeanGeneratorTest { BeanGenerator beanGenerator = new BeanGenerator(); //when - beanGenerator.addProperty("value", String.class); + beanGenerator.addProperty("name", String.class); Object myBean = beanGenerator.create(); - Method setter = myBean.getClass().getMethod("setValue", String.class); + Method setter = myBean.getClass().getMethod("setName", String.class); setter.invoke(myBean, "some string value set by a cglib"); //then - Method getter = myBean.getClass().getMethod("getValue"); + Method getter = myBean.getClass().getMethod("getName"); assertEquals("some string value set by a cglib", getter.invoke(myBean)); } } diff --git a/cglib/src/test/java/com/baeldung/cglib/proxy/MixinTest.java b/cglib/src/test/java/com/baeldung/cglib/proxy/MixinTest.java index 31e7b2204f..ac4b50af4c 100644 --- a/cglib/src/test/java/com/baeldung/cglib/proxy/MixinTest.java +++ b/cglib/src/test/java/com/baeldung/cglib/proxy/MixinTest.java @@ -9,7 +9,7 @@ import static junit.framework.TestCase.assertEquals; public class MixinTest { @Test - public void testMixin() throws Exception { + public void testMixinBehaviour() throws Exception { //when Mixin mixin = Mixin.create( new Class[]{Interface1.class, Interface2.class, MixinInterface.class}, @@ -18,7 +18,7 @@ public class MixinTest { MixinInterface mixinDelegate = (MixinInterface) mixin; //then - assertEquals("first", mixinDelegate.first()); - assertEquals("second", mixinDelegate.second()); + assertEquals("first behaviour", mixinDelegate.first()); + assertEquals("second behaviour", mixinDelegate.second()); } } diff --git a/cglib/src/test/java/com/baeldung/cglib/proxy/PersonServiceProxyTest.java b/cglib/src/test/java/com/baeldung/cglib/proxy/PersonServiceProxyTest.java index 2ae6392a05..e0ad017538 100644 --- a/cglib/src/test/java/com/baeldung/cglib/proxy/PersonServiceProxyTest.java +++ b/cglib/src/test/java/com/baeldung/cglib/proxy/PersonServiceProxyTest.java @@ -25,14 +25,14 @@ public class PersonServiceProxyTest { //given Enhancer enhancer = new Enhancer(); enhancer.setSuperclass(PersonService.class); - enhancer.setCallback((FixedValue) () -> "Hello cglib!"); + enhancer.setCallback((FixedValue) () -> "Hello Tom!"); PersonService proxy = (PersonService) enhancer.create(); //when String res = proxy.sayHello(null); //then - assertEquals("Hello cglib!", res); + assertEquals("Hello Tom!", res); } @Test @@ -42,7 +42,7 @@ public class PersonServiceProxyTest { enhancer.setSuperclass(PersonService.class); enhancer.setCallback((MethodInterceptor) (obj, method, args, proxy) -> { if (method.getDeclaringClass() != Object.class && method.getReturnType() == String.class) { - return "Hello cglib!"; + return "Hello Tom!"; } else { return proxy.invokeSuper(obj, args); } @@ -52,8 +52,7 @@ public class PersonServiceProxyTest { PersonService proxy = (PersonService) enhancer.create(); //then - assertEquals("Hello cglib!", proxy.sayHello(null)); - + assertEquals("Hello Tom!", proxy.sayHello(null)); int lengthOfName = proxy.lengthOfName("Mary"); assertEquals(4, lengthOfName); } From 8fe5a2ac727031afc043b89839844ff4fb4d5a97 Mon Sep 17 00:00:00 2001 From: lor6 Date: Fri, 17 Feb 2017 17:06:34 +0200 Subject: [PATCH 12/68] Bael 682 (#1124) * upload, read excel files with mvc * formatting * add excel files * update excel files --- spring-mvc-java/persons.xls | Bin 0 -> 33792 bytes spring-mvc-java/persons.xlsx | Bin 0 -> 13514 bytes spring-mvc-java/pom.xml | 16 -- .../com/baeldung/excel/ExcelPOIHelper.java | 264 +++++++++++------- .../java/com/baeldung/excel/JExcelHelper.java | 82 ------ .../main/java/com/baeldung/excel/MyCell.java | 57 ++++ .../baeldung/spring/web/config/WebConfig.java | 5 - .../web/controller/ExcelController.java | 50 +--- .../src/main/webapp/WEB-INF/view/excel.jsp | 80 +++--- 9 files changed, 254 insertions(+), 300 deletions(-) create mode 100644 spring-mvc-java/persons.xls create mode 100644 spring-mvc-java/persons.xlsx delete mode 100644 spring-mvc-java/src/main/java/com/baeldung/excel/JExcelHelper.java create mode 100644 spring-mvc-java/src/main/java/com/baeldung/excel/MyCell.java diff --git a/spring-mvc-java/persons.xls b/spring-mvc-java/persons.xls new file mode 100644 index 0000000000000000000000000000000000000000..ea270f69cc8eb16b866db0414dde14c23ae9d365 GIT binary patch literal 33792 zcmeHw2|QF^*zlRLjeRFeO!l(xWKTlbLR3VSv1H4VRJKH=MWQ`v6;aY6ib`k^Er_yJ zXqUA4E782?%ovR^{Pn)?_kHj8eU9Imd+&3f?L6l>&spxd*W*XLFOOdk?F2N*7%+g} zL{`8|1;@a1Q_3|DfcJ?+bV)gyz;g(iPX8BKfE5QTl9>rO>pftpslWhc763W{78Ehw zg1-QC!R!uJ0}cRO14Dho)<)u&L=qzK|EtqHk`7)Nqytn77{N0P$bwK_AVSiNkMzq= z`aMs=D<}OL0X|3r)ABqSJ<eo$ z26~!52mnzd{)T}bIe8rcz&6TnyhI6L3a$6=JoF~yA;BmPm|!gfDH$ngC246j&)Ej< z3X}`?*%FMiU{4`F=DwRF!FR=2Bq_iGX8u7^Q5(w#UL#iWP*o{fEE;YjsJdVd&;Y;_ zsWJyo(QM?>U4aB}pKUM#kW!X{dV4CEdO|5GU^e`Nq||hC@YF>B>E5KOCSI}6M%jqo^&MtRNxVui30ylxit;{ zuh<9CVgR1Z0{voX?0I?Q7USWu_8*0xp8%dZ0sPPe@S_vJ^Cy6xod7nPmM@22EUNx(?fhY7|IlAV7*1X7~-=j;jKN)y26 z(1o)>o+SP>@r0cMAFW@qAHtC#8UG1z03dM&9Iuhe#W@BJgeY*XKX5!e9S^6G%T2)_ zwGY{U;JB3{cO^U(rSRvaz(@6@@#idFULkfNXlHV}L!(7fc_DwYohj{tl?{0ST~f(xT|5Tw9q@L|EpamEUspv0_5vK#DWY2XU@SeyWeMUFC>OhJ6j=k_?9 zGKd=kM+jKJ3Ht>Gc!q|ts8faA(Ht(=Q((svAe6r%AP*M*LIP6cuOu-3EdlBR|4IUi z8Gl898t-2bpi22G0@U39iU76Kzal^l_^$|1%l<0@)a3t)0L6m8B0#}lQUWuY_JE=U z!^G`@kBSSns4oAxbg_J2UL2fuP6BK8rzy!JG#7t1|nF15!t`jpsk!lJ| zkPA=D1ckLJFhOoTF%uL6|CLFd&E&O%0_GH$OkO)MTu+6`gRfoN+B4THCosrTZ$SseiW++ z(I*`j(2kRp{+2-bZwXZXmOyos0M$0Cz%hm$KP*v$XylF$5x`l;SOSWV-RYzDG1{>W zT#lj&BdAKpNJYMbfRl$2xCZy{Oo%3vGtrz16WZ>EwqCU$lRu6yt~le}SsN1kX=5fP z(e#Z-tUEZ)o&WpV7;UBKjGBUO!>2T8Vj^Z#sqyZiJBCRXVFLFc_GD2pp>6GYqvk=j z95U8 zHH@MZb-1A@MGYGtg5c6HmFJDK)OZ4eF$A=(iiyK5q3I3`ApzsWa~WiJ+NMg~d@0?* zcukB(H9_tMD`TuZ$Ges>M4Q5pd-BfOWRwXheBoD)l}d3fQ1p4!wRYlWgPo}N9!aRU z4AANvfC*~-^yyQg2z(Avjzv!^Afn1RWE6qmEkwa!Mt)k2DAF+`icBmY;DH7P8fvn5 zWxSb>4PA#NY^iEuCNKixH$1 zqcdKN#w23M(VJv6R)SwwUn_kvLP(6(Pc$)kqVyHc>&Ze!cjlLKWS}}AyO>q)2)SgynLws|Pz8FzjF?2jl zbrLbuo>pgb`OaJVV#H|0(D5|QNyJclTAj_EmzU{_5l3QlCigTv6r(c*Pt(vmT=|A9 z1_d;jMq}DcS}}AyO=c1?)SjlH`MoHaz8DEwF?2jlVG=Rao~EH$Q&U4dd6smvdy(0^!| z%F-~U_89=s+?LdNHHLYaf8s=Pc_@W723@L`3ySFq2TV6Ujau+R+L#<2{sLPT~z-kaqE*1#QFUU29Y z2*ThH3{4GC-;0={83089T0S8OUk+Xq*((u#zm0EzCK{xD1}NQ3#3q?pJ=3h zu@3J{h>6O;S&n6e)h6-)3lIZ$CZf6zCDnfzRAe2rxieHJ2KoaU=TmAQRrZK`vBI*! zd4OeD7<5#Fv3xP2L`Gl?Su(L~ zKpV~4tV36?fv&J-Y?WaJV0o;eWmiLAM3w%h`J+V}N~NKdo&RF<=Yazx7?*4ULxTvu zBkg|?jWRP}TtfTzLSgGCz$&MX01TtvI2wy6(Fd*MFhj#}09~j*up_LAitvKrg(fg# z#9?5zgN#v0BcM;Az(StUkm4y^%Au4u3$FK!I6QQzL;#zk=2%@Z0E)DOo*qF85Qx=? z^)c`iM(5G?qKDdxU04($1Lr-K2bK-C7sMk9Dut%jl*o$~geV~mE#~|QwB##FNF!fS zLK^uh6Uz?t;jA3kuZben_*xpFf{?X6$p+)=oI2#A2sx(C5#Wy|FRY?YoMhoN+)|K6wMeoXmW~(xIYTDiq|!P;mDiEZY+>0^ zFkA!uP6Jvo5JZza=nn?<4p<$q3#SA#vM(i}1tSh%3;8*LaL98$kx}I$1cFIhr}!J4(m`uWLYp7f4}eLK-DR z!$XH=ndbs~M_V)J5q!ZBd}V~sI}YE`#g}?~KHbf!pODQ*unxvB3E^}HuQhu@^AK1YG2}a;00ep~MaXzs100}Gt z;6@;*wnres7=Xw5pk+S_3bH-U2gWBF2wHm_!I=RE6?BX2WMOV^H-BsaW{ebIf~s@2 zwjEz}d?PdyRK+q2Gn%U4ClTDj;J__d=$lShFhm}dpg>Cswq79wSV}X00vvBH3=0Uw zJB0*B1;F0NEYu5+0Fhy#L^v45fj#P|Edbzy6M`9@j3l9vBNpt#nR4u-y0h5@3mg_P zFbY5qN8nlD{d4Y{uqgv^Xlj0VbTbbNje={Rp3du56Czir#DoN&-Cb)}rN(11U^TSq zwerHOi>5f)_epot?w*LPxOY%SsOH(l+$FyTY#N;B<>7^bm0C0hvJJW;u1m5MR~^X8 zU3mWMJVluvG0PP>FLdo&&bg#+6rEw{mBW4D`?VY83c6boty2#<=4^Pg+TZ4)QCfP0 zWcsOwK|O4(cC=jLa3a@(HH+J4^rY_A>#i|>`kLWN(afbrpI-kwAk&q8`I?E2-Pwia zxJ{QTGM;?Q`?;V-Xr9XbtMdISrxa6eay)`C7f4!ZNV6-yT>N*mG|*H)o!Qj>3M|wClZf~OMf;G!=j8Y zXwbTSLYrU#x513IgG2=oLI|oP&)%T|TwG3|^yzBau@*QZRu!uw)v zVc9>xJWJHSuC8}2oF@YmVAy?$`#9NwGzLr6x?}N z@Xh;}88w%8^1raY7r8i2x+y*7TyY1!xmdAjR-aOb$5okCM%yi2-!%@*?y~4nlw-r{ z(N}(ZQs~lBIE+YxTWU~ipi-M$1a+kL$7PSUw=EF3Av|b;>zGN*81hNgeRlCk$fd){ z&#Y_0ns9{cTi11skY;XtWK4>`{5#)d04y5!0@bN z{GY|@y>p!N1*PYEKTTcC7#e0H=bL=wc+~q(Dn|wsvhF0+ZsfIPZqU|I5{Qm<-iN#K z5@Va?Gv7_6PSa1`Jl3<6%fh`PWuKd4LTLCv1aGhJ0_WfhO&rRMN54)ggO>;g$)+k3KcoG<&CPUyI^9KKbSnON~G(e>>BN8u*5gY4c1 ze=eUPQo(R1V2f-saLR+ysz zyVh-Wp|tWlX=+!_eo~pWz@7V^f>)Mpa^!x!ne)%i63H+>neMV+gG^GnQZVDJrUk)V zpY&!f2r2l~<-^aa%XmWQtGUfiH4lr(jZ%keXKl-q_#7uQPpsyal*s#13E}kjvrpH( z->Lpq=dc*-S=U8_+C0J=TDjjzY3xqLTk0JBwN-s)3&GNC>xqM$2KewiACoRihZFa% z)+BSrSJWBI&o-BEDA(>-i_>X{yTa4XyrOCLIkx#82j%ylyV!^Nhj?P)LI845JeNgW?>z_L92@Vxy0TM5&S3e5YDH<3Y_PE(WydED` zw*SSARp*mMwcJAm8#}JGmRY)$eBz(?-c(89`P)?k504w|{H3PQ`@~1_q2z17S>pE% zMC8;~H!{u^llSW4m#Q?|>Q{7IwXC6LkAGoq`$2Usg9Pp1e&sJw5;Kd!2dkH>)aEui z8|v{De91VvtEzFcRekZ`huKPN&p)$kYW$j!csY)ruhmyBH9uv3oW8=QQ`rw@1qku2 zB7~(_G*+IN)uHNCd<&cRdfCR|Oop<;eLWjiVi$aNNKufeHOx8V;pZy*EyQoVo7zsc z#0S+Eu`UNg7Z$qhmv`O2uT@_oB>nOI2QtT{POhK9T|dLWX}0Af^KRS74)>fV$K=hA zzuDBVygpC&_s-iEk(|*zt)@OJW(R!Cy4=3JX-kI4@ny9~Dl$#P7ii|qR2jIw)vmL@ z#Y*NqX96$bpsafoFLz_2V!d?}UJ5JECA+wGZ|$Q-Q=gn!+uW{VlRiJp2ueCz9`p+T z-Y?^eZ<6Ia`!XFV=_A=mmAT8kuIJaOa_!p}%=KO(xtsrO`ol~&+`_u^`w8G~auxfr z2+;@X8+8v|O+WV`?3_B`V!=&EXYD;{(sS7tH!qG-x9?ndKXUE|rCl4N_ni~L=BLO6 zC2w(RS=oO~v_q>;boQ2WD+Yq4Mf7d8T#XtX1Y5Lw=lInX1v3O)C|{cSbg8mA$Dz|} ze>h)zDqC{($+hCRdCz6>FV_tztr!sJG&_0aP~9;(k+AzkPd)}e`8czdDch{q&~~2F zaBD;F^7gM1UPri?RiFI46?%P@SHQ(~*LK zcsQSA%k1SWwfMCCV{@Xke|Gx5U&mgRTtC*ulHyp^Cs>~EI!9|Ly7=)w!>g{o-M^^A z?)5A!A>IW9li`mITLuZ_Plp3!_E4SCQ(OxnEq*Pswn6eOGVNW)`qvotnKY9h;5rf^hJWXFm z9-WYSKdSC}WVB+-R;ebCJdm^&7Z%odjc2!9$JWQ^o-ic8(&p4Lky-q)m$Og_r?SQC z#=*d{{YPr}eKek{2jq+5=Dyz>e&|+6>BlrvMq? zsHh)^;F4dMne}*^^UuUWy_36KUP+xiZFaBhvDLtuxMN=qV`6X1T=(YL&;&&GIUFKh z8rBm{^;eY?TJd|aS!7EJlfsERbuXn4=i4yece{42`o(C`F<51|+hL;ND@3D8bOy5y zi^ejb9nbxtR)hAW8r{(n(vFY*5Q@No9Iait&Lv;&stV0TJFq`42WN=JfUd%N7{#8IzZ`6yA2SbzX?DctGBNqJ7(tpY>Sy z`ok4m2RQk43S3VBHiW1sLImmBk>C#>S;FT!YgPvnVnz;jVa{tpyn_kAkq{Xcyaqkg zggY|SVqo@N9XPqp=XJ~Ou#nXegvdyOukw7aC@)o2 zU}{0c%5wxQGGFw-opZHdlb~@Je|)70=5(p)*UC220FXJ5DOt>vA>iP>L)B)xaa1C>cM4 zqsRvoTlB~>nJh~G6^e1m*&3PnDgAVKB6{jxhbuwY~n4SCjN}OY0BXFO8VpALyRXJ=~|=`O79|C+mx_${^dABdjJX zD|>8X7`nInTxbn&h^bE8CAvEEOt;^@ces_QY~kI-VGWxkN^V5&D~$7e8E>q>5Xe&U zHtp^QmK_E1EBm#4Oo~kuJ#HRY9vu-myW{Jjn3S-zf7k+^ezaY$E!q37#3+}ua7UAI@0VmxuUMv|@<;p(_aDeMYv=BH5z)5z z#7~1eC5`c)0IOuF2d@?Nv)?mF3K`GYZ3rZ}tO$3@8ktfWz^yx^}Lm;=6*nYN`F zUu;hmEHPgHAWtf}>BbMW=3iFEH#7%@LtaVzdT+}v(s%NCiQ04X-V%*$(;q=Q1{#bO z#mx%{>i1W!dUW-B2G`89-$gdHOIr;wF{vmIh^#M~ceA9;pCk70^Jix^mHk|GOM7W? z&(4Bs={6Rr;Fmo=tH03 zEMdfxtXk!s{^@0)*5=cT4Z1xlXLj8Q{ITyn&z+K#B9$#{LnR!d>E=8=5#hz+n!{qr zJr}NXUDc^vgH=Dnx7~kFl|Z`0o2S>$J_eeFGG%3%U(N?e3}u817avVewpz1E=KB4o zE{(;yB4M&aLfyT|#5p3^8`VDqin;5&Eau$f4e7LADyUH?{n6iW)91_LJ}M6hn; z)s6K<3M7LbwcQO7KGVx|$LRZ~qnbD7^{Q(8R_$ciQR88KcLw*Yp>6+^cem^8XtjG4 zaH*3sw0xuJ3-2L|$}r7r>mM`kXeTF}j8m_4o$*Lw=iaPZpItgGbMC(uwrl>tcOzPP z#o>&gcX0>8_q0myD6zSfzwe9rv+Vs%V`wHAnqh&9TRNO~*Mq#2E4{S9qo#^xGs^oX6HLm$dZ_)BE=MPmbH1j{hOo z)ql$YB=DUt!!y?^EltsU$ac)6ouNZ-e)YL~)dj2sLH@7H3zxCq)ES71uhVwoV5wMz z>uL_3oz^{{|Ga8vncFduYrE_DO5K(g@m=<6toNRgp;$Yk#H+~q-iKZ0V4+PppQTE2 z-#piRS*#eFHejuh!6k8XUT<-V%mg3p){LdA3hDkq< z2JBVsZvMHRUv-7b-en>diI(a_&feV#Lf%+=+?`*v9vh#gtB zMCd@sEt~R(Zy4$>buKx=b3JgVZn4yNv3W=9Cuw@7YYr=G^|HFF3PViaCr^D?LQ44>tL^i0>mC2aPUOzx7_ z1GTp0XPYe>1~nQ(ug%#c?qAhyDv^F5ll9vLsfV&%yuy*&DhC*I*E$SH$K9_D*1WGV z_cZ%U)=tretg}T~yRNk-oi5IL>nHd63hqkfFTJ!=zjS5ZUB4yo-D=_*qm{<(?XlhX zu1mYU^WhlnPFzpWVk@_V80D{icE+23CSD2k+PYtxbY@lIOL*ToVzw%qmvTaZac1o=JY%FU%*64LHr8 z^In<9S5?<;v$SxZdvIXOjhzSe>OJN*v#3@*<@C#H(fh&m!s-{$);JC`<*k?*efPW^QqF3P=eraQ!9u7nEb zo^5L`=W~s?_xYNUU}QwQt6=zxM(S1=kff=S{obvzM^i6?spyQECcVa+cKx?mHI1w^JwMAvN5uj-)B|5oq5UWp@DO6 zIm5;E&v|Y2`y4EW3TDQI49arU+)vMb(Iq3ZO?my3l^25E&SZCln2lGHyj-sv_buHM z@%;F)2xm~H>}b*nnPQfl4H z4K`=Ix4raYY0CEgUmqK_+4e6`znh!7K2`5|>EJ#Km(n^xulVOW(d=UN>zlH>1ywfR zc>Lq@kGn-jSG-_l6Ylv^+~;|0wY6vVSyc-*;j%B6nx)%+E4nxIl`MFcpIDirI&3p^ z1#GLl{pnZB7bTa{Q!jR{vs+wxp#PMUUaj(K`R7eLhg}XSS+);5l+M3Bw|zTd+493( z@7aXEg%b*1#`fQc6i|8pk-hnV3;S&R6%jE@aQ6A)8NWX4_PqS-0^#t-*uY4GVXNrS z&Fyw>u}}dcUt16>rd-bJXz2{v(G2N*cv3HU{@b_kXBuZuOkhNW*KnnmLAcmeYnD}q|v(N%!cmYZ2$Dw-@0{f$?Gi; zv$FSl`N#G(YYlq$AK8Lww=>B+vPGcXj)m|xG{5bA;zz^zUD2&725Y}{*tz+>Jr4C{ zx%jP{>8ZWRkd%^{+v%Z2Vsk(28CtPF7XSMc+uqL1tEUAX-+zZqEH;1mX5dZve(B+! zgeOOS7uFt&^e;JQ@U>IF=6lreF6$**i3Lq3o8q5b;uz%fTzO>VkHRkoWniK%5A;(<2hX zqahqY*T5Ad@(O?ed`zhh*G^R6^K=#X>V!7jv!xHu5KIkT3&9;0>ZE%VxIV-{n0oO0 zW{lvN@PoV=BfLc5@o(psW9LcJSJM`lw!pLnrY$gSfoTg&TVUD(|0gYQ96m?Gz;Ps6 z(Z-OTy)DIRl2W4y{N7{V}cbRdZ=a(?SS1V8= zk4F&%hb2E`!-7-f;UtEe1Vi_*06zwXo`X@thDlFU(RYhcB41(P6(T}ch=&R3Hy&;r zN6%7CAjH@d-9x-gNWbVFdd`UM>6<=<_tN2+0vs!g17r9$`W4a>0aFO0r0ww8?)EWO zz`{@WasviZ5g{uqVvO;}2?KvBG^hX?)icZ|KMAIAn?C+ewZLyU17n6yqG=QIua_xD zzy7oj;3?RR1B&vOTHpgAPy{pDT8-|T!i>D!3TEU3D`Dn?IR<9*Pzm`M+B2R7Gul<0 z2Q!lN3KB`cBM;2fAC-rf{|dJa^oaL-ma$%-kBS0`~_%3q9>NVcMfj*?wEF=N(L}{X<1Qdq!L2DJ{ zxA+0t0gG6pr3Xaj8Ws^0iC*xJBymO9WIZWs2Y7gmpy+d>h|p~08l5AZ5d-hAuprAd zfxcsuR)9DC^mKH5y><1J^?eBhWi35zFJ*l%9bIKTf}fVRwwIo-mo9v9dpb>9VA=xH z7MQlcv<0RuFl~Wp3rt&J+5*!Sn6|*Q1^%~K0L^dFd=|}l(X)K?PBq#Lg67TWnLe7= zqj&nzgc;4n(YzP!Z$Wc>G@pmxRU zDcY2y1vC2mu@21Wz8*ZHxjXvL90o8O!i@ga1=>r4?hnI^j{iR=N4Sd~eM}M$zov`+ zeFgf0?%yOKz(m^dgKUWW2#-U*0925E!2)pzlzkPb8zVqYugYL0!n-ME&zk>52F%K>4T*q4qOX`RE@# gks1#?Fu`wEhQT))kPhUpO8lKZdjE;?>r%=856^P^mH+?% literal 0 HcmV?d00001 diff --git a/spring-mvc-java/persons.xlsx b/spring-mvc-java/persons.xlsx new file mode 100644 index 0000000000000000000000000000000000000000..1f58606532f81e6d03575f4e127b841c31a61304 GIT binary patch literal 13514 zcmeHuRd5{1677hYC5xGv87;P$naN@XihfAe*<8?Rh3+#)ns(!K3+J=!Z20q=i12fm3l5J?ka^ z(*b`3_y^5O%a6opxI)YXYa0k3agkE(8~SBbL|?_E#%RsaizWhhvwDKXQG7#`Jdzky zV6JL6eB)c{!IM4aEW@TR5MUcCsNMCiuNH>CYs9tNPBxavP@P*P&>H1Cn%)XZPGSTP zzi;zeu5tb>;5VZIqM%$XfJG5PjqG4VwWDa}htu}nO|uXWJIc_MPr&|&68d!Fu)@_r z|Cu*nI#xy{SAO39%u#og5c8eW{<}l>xT?Z%aw%NXll41nAHGPIZ;+fEGkci9jQ2!m z%(0j59l=yiAYl!qAa=SxsXW4UN&y~H-#d245uWD-_5&p>Q8Oj7R{(3}HH2{-t^1nL zpx9jS!?k9-$nHDU47?3)pCQ+hvKZrZoY;M6oxmA>K=3>QN%{5$22l75WzqD$nSuf- zy8&b*9FVg5j;7X5j10fM|BtHw#r*u!&@19)rNNkyL(T) ztFa)>2xb@Jem*|vddH5q4Tg5xOh(z?JWhR8pa)QZ)#nNdCe?(SueY9c6?=eXEq%xqP zsfj3^+HCi&MP&6SWKMuP;q)~&%m+RMcfj<)95L;Ex7bhUtM+FzS@#L6?)*piUA%H> zf--h~uMlUV&>;yzU+Q?L-Mq9m)$=W9;inPm&ebcr(VKI#o#Dax4d5^RpDttA=hZ&| zbQc6j000U21kh#v>?{?DHu9@X$b%d5D{f)WMk{2(1nI(u>A6#oily>aU}&gHyA-xR z;+E1*r}8W|yi_5L16-s6&)3KzVnWS{D_;(CgF@ z$X3b};V^>jFwGx#F!yx-rD=EDwf~_K5ZwN81cP^p00}y(J$Ui46IZaZ(34dfm)wycq0Z&xqeW1}BWuq%cTt2R} zkgBp=X3yM5%S>u2kd&@fc`t(zuImgg{f&ImCyYdp)*QAi`Nt9kOZ%Y8SkSu!b_OL3 zVN@JGJnJUwRR(!s1)s7o1C2O=kFacB3UB(iWlJwXBCZYG51I<^bE%}3+X=iJ=K5*5 zdugU&<628LIuwJq1GZJxg$#vS4wrQc-n!iG&Ezefd7{pza3oBpAVqaO)h6|6Hg89G zzU7(F<}}cyteo+hGH*4`g!b&8DxvPo0p(ASbDZDqMn4L zu981c7RfkbTa(*A;-Eo+VUZ_oND@-3ih{!`NpDKVKnp?SSq?R4zH%oqdU?COYR-Mp zYv3-B74w2@0TX4c#KC#B*G>1VL)eb^{LmW*wm(XlxvPI1HR6gg_S`}!w?99Sz5Iy> zh7pJN>C_eM?J*c*W=i_L|K@7C_~VH^7h>FU>1D#^AWaZop3E* z3SW-}IK3L8YSD`8YMGL#t0h^|VjxOcM(aV!Zt82T`}Tp^l;yG9e%}JG+~~s7L>J=9 zU^fw$)!S<)5-gi$lY4?RBO09wmAx}uB|GIYE8UiVDJ|@C&?ZiG)WFCS*x$K)b!K{g zBhb~KfiLubxV)3Op`)pZva_Rw?MJ8If}}}wH!N@wC<@Fh$Umz}Nl4;FMK3nHBOXD% z=w=Ajm#$SFY-pogZeEUO_%mlnSkB?|MyJt@6+uuHu&Bpb!lhjM)PeSImtM(IG+c;= zMB&E3*OwSdTN8&hSXH*br}D`=MX;q>oyDhsv}rl5eyQdnlCr#Mr%L8|kjx$Vo}i)O zcSODJ@!fkFJ9;9F_c2I3g7tAc$6EVMEEG>NODXX}ouYa@ne`=+V7(0!IEepbHm?nf zbT{yhFfhoI{$rTGG26-9)YRGOpLyZ;3+8`kg`k-3&pk}&Vs~J#BA+}H7J}hLlsQS7 zRrkS+9#?I(9RGM@NKam^6p=;m5MgLjNE&1)_^SLi$6*K!?w zM~9*3i#42O_?8YPCnps?7^{H+R~xKI_JY;x)3;n34O1E`u($jn+?{FoAp&=*qRi6f zN*}6$QLgcvZpT|akcVGM^YtX!n~={ozjH$JYBJ~pcdUUxo+S5>*y)v@(feb!pV?O4 zh~PywW24tUVg7CgA_8nR5`b|n8t1o7-|w-_+1%8|l=1iX--msoIT?Y=iT(}qQ4rqA z?b*uAn(-3S)B5A;2?g8fe9$M2_`G9 zc8E}gMaTC3@yvO2-8OFiEuI!nFLw-i^e9_cyiM8ZuMR*1@)3Y@Y$mfGf_nmQqwZ zEn+wrsv5otM=&$TE!=S0!)5Hs*XI^|tk4I+XWoW!yPw>|m^`hv9{w4@yU_>iHsx-V zHxkLpa?IJI250Wti}9f1gcg8t-0YK2coUe!4Vd;$B z+t-(nTm_>+s}}B2I;B*eXXC`mcO0g?s^DnPxns^}2$p**^#%5*C_Jtek}! z%+C`s=6JtdEu-!4)(=u5WoPrN7; z^P?sD?KV{2=i$ML7nwr))7C>ujIZb8{eybW>+2dRTzf(u7`^}*^WKE2*U5JS<}q3K zZj)%F8Q%qCEMlV@A(W-Bsb>*G06kqI%U+k-!**?I=tzW5?mmN?t%QY6@99YO!;Qe2 z2eD4;=8pVm9qGnh6h~ zTp}JwUS6ZX3F zA=wHRniKX3E8Q9xHD^$;X5|)Tp2zhYP{>Ct$srE;RwQFww=XsNq_&qy_;PF{jVvzu#g9<jttrl7rCj zfc*x^67=1&usxcyYOxAgm=}vYeQkSW+JgYY0k~ly3(-?Z`XCv%@``+T>zmwMeVMb? z94~e4H!a@c0xz^PL8?uDHu);!PahZHmUv%$iZ{;Q^g1~^M)#{%f)xToFw0#UU1K;I z)+mE!ylZnmrIgR}hF-A|2<5M+m2~V{(sD*@vTRUcHCudEA*3|jCMC&xfgU#q*H^`z zrk<3QZ(%jYQ+qAbv;6IL=L;oSL$?g73tad>9ZKGX8`<30M6lRsPagF+qex@q_6UZM4F-mWAa~7? zW!txuEby)(SVg{MhY>`G$cpXkT`3VN;M)k0U_rlVg z2|>vrC^$d(cyIF)t|MK_p-bP(w;DZgptIqObD~&LlN2}jQ?x2g^>N#_ynZujPd!HYLsp_FZ5F)w@$cR5hODTmkU++YE|Yd4X$|} zo2Wzif)Mh$mLq*^Qd1Nj_!>M*w#5DKLUiVMQgh4%5e;D`;OIo4%~RG zO#L;gXjQk%k@WkI5sg_Xs_5wbhxh*8`eBA zI)4PS3(~2k`*iNicSw~Ux*ZippD&hscWNEFSlxZ838SidT>h8>k|{AQ_IfGSSncf> zo+4q3DNe`0dh1_U5mt`GG{3Tk#DoYk-GVs>S?N)Zo2Yd=k-Kx-I<72iw*0*>I+;t0y0icoOT2ch6 zlsxg9Ce0aND;!d*W&C?j>QvMhi ztf+0XeiD50Vc7p@80iDMVa71$1%0^(_mH?QNG5GYi0X}Ya(G6wUt)0-x}{JOA8gVV zhL9CfMvSn)XJJ85kV@5T#J60i9>e}Y+ibP?Q37fO$gnhw_gXw!-;pOqvMuUE8X|KB zcZC?Sm@9QhH_bh!=lR4r+_fyrht;30y!l;mTR zXyM@8VGY$AFd<}O@i%gKr&NVPq93u^-fGwHc21zCM>*l+>$U2aHt6!*qvDKsh}+Al z{3k85`rg<5?69}#lD3)Bo|Ix@bAwRQb*|}q=>`-}kT%ef4WtOUafr8;HGzxz0?^3h0){q}Lmr=n z-D-|^|Tj3D#E7BoxGzCNATTEenGLcpi*5^>$KD_i~eh!+LNRd4OVitpi zVs2}Y1GsC`K||vhmJghb`PC6JvsjcRNz{_|>@IL&Rtu~|Jxc%-3wFmPus7L>%eVLa+O^9AJ0WaW*V zka5pgE^tfD`#S-*}Sc`Owuf>%=yqOVe|;5v|I6hiuw)Uvz8SwTTwB z8Q)fb@oYP%IeZ)!485Ju783E!#m?A-y)$Exr9CyWEMo_3Mf&UmvS&5vhSTSyeO^PE zg$W)#F*%fnZU)4rCPftbW(mQ8wL6^+LVY*32lIRlX>K&-&$8i-I>Fxq4t?=@vPiZj zs*NB1p1+4FW%As>{dF!l0D$!$tMEV8-;pY#w)srxYcXtZ2JZUsK{x(>WmzTZbmBOT z*BZQgplVEJ*89_)IabgX8P0JCb>kiTcsWkQZ5!pu*4-MVZJFZlC1IR}Q|ii{9-kIx z=jq88E~|Db&xnev4Gz68Z>7J^n%Uq2B#^(vUZhhq@bZup?4iQzsw#B}M??ioL=i!1ohfwMnAlOWku&%!f3M(tNt!B? z2urdq^u!mURwU6~$l~yt-|o0m?CB5Ux1QKcLKoN8Z2@d8QQbUGxSd23E5#+D+HKlU zLY8F;h#ndXYsMkGKF8J6pD*-X&cihj;U!|Bk*~idEm-L{_vIs_oGNUhwAeesZRFaZ zdg^UY$de%%{NlM^v5f#^MEJGR9`D}a^>!UV}o zuXT#vXfuK8uoy;m=Si`OPB*ZeD|IsAcr}BY+{idf%@>K$ix>9^$_DQ2Qyg{&Dc3t_ z6VJ0yIw@nR_E@Y*fw;I_gq|k8`|K}h5%k%%5lqI~@{T2+_^QLAG2qi;Zq~cpZgSRr z@@^YTqT@+zyo7mGKYgg@lqJNP{twk4WAPqC)W$jj(;6u*AeiFj3;c~A*g$SuJ7vS>b_JWexOqR(E3fp`@#wI7GtN)ZeJtOEdD{qdTz zQHofY1S+*Qcu;sw)Pb0M1xG88)z3uaf0 z5Bu}9!}PR-?5KgKK>G$B37FL_>%f4f$g}jz;xamuO5)3kws~SE2gYn=_gQ>?;6zep zk)@3+m^8;ik_B?27j+3P)610F0Qp>5z8F-p@W>SZ^%8^FPjh6COkrmV?jPZ`9?Pll z4rp=lKG26SRsdmgO{RCD_GMWEXXV*QpokN}9o^79QBHkP`H&a-jV$5o#M$!UP$6+{ zeVTV(KQffRZH+$9(4L!`=U+m-45Ae@d+w-Gxzm+t@8?`F*!DCMbIJW|FIi~(pm4<^ zmmr8Shk(S+-Z5Oq;RCm;c>Q&Hqx_*w^1=;8Px}JAzek7a5b?Lx~H#= zVT5%E7H9p>xY)6ep=E ze8JYpi}VWO7GZzb>U#MD<7He*it9)^nS~sLv%N+z>OKZ|Ik`?Z`nCsb`V&c z7u?IpT?^(5y+^vNB98wcZU(jup|E#<Shalq$1Sf})L`5MB#RexMCBmKO7?59{j#{7KIhnOu_ z3=Ps2Ply6=K!yA?#R)wy6>~>7h zJ5E{yDbW3+PzGl;Z|b+cub$D!Z*MFYlfDCQ-_%i4IapgO zd^P%EcRu)p}PSgseXXSH@jEz$c)e!PJlVYqu$`A z4^_h?a7AByjJ1ghdF;f`v{@spr-cLMim4ytJ6ZjX6EKv^sEO~x$ZGzkIh;vfZ3dW> zfMliRf&iU@p>ZsmC79q~5z*zF6J&jhCcgxW$GLiPS5AcVlkOb%2V_IOIq_qSeP3N3 z99<*~>^IUOYs3S?Gh`Cdee#paT%HA(P8oWBPpMBqJM4|FRu42unCqNJDs2NB#vKO5 zV7udz$wV~_fre^wSS4~Wi*KF7HT+F zygmz^GLh@r=raK=A^XAJhx@=bz$&tN^f)6^!oizM#FssKI!0;hn)!2%2JTG_)tDh( z%yalkU^JJkd3K>R9dc)@Pq${J@M-%?hRQLK@5yqUUFW8e7xV@;zd3%$R1d9`YdMa) z_Hi?L-@5rZ|BSePCUga}rBg{!)V)DSNxD-zRV`*q4eIG8mejQ<-5j&X8E@X!A*~i0+v*RmS=Hh)eA1fvqKArepo$zK-m|O@Kl01o zrxvyCw`Q1#sPC@uC(j03@hSH9w6?LaDaGUR5tFCeg^AdXeTk8Hy3Q;6S~cHYNwar` z&IXG#4Wmn#vuddmx3S6~awRC)gsA-SE$h7^ z)(P(h`5F4mGH8@fj^z_J+^q$kM;@cGlu2A798!kHGJ=e{>dg}U+9r)(zmf4uIUG;RrQ&kSRLc>yoA(|>j8ejF> z!nk-{EsY_q+sUCTkIT8&Ds`T5xZdcAE_$w=#&{7aM#qirt6am&?EK-G%!Ix@{&>;X z>A;Slm<3dZl|Zrb;L`$){L~CIDn_zOMHhi5m4Ohb+r$@$590{=mU^?&t}Pt+f+*AC zavwb&8AtZ2!Ms?Yyt)mi=c}w-_TgTxQa18bNt&{o1^pBszX~rIx+)=6U6@N84B_GF zn_DY|`-L|gv)Q+ZA~b(>dP1b4u7PQdZA^NYsZMn&^q9w1bCXkSN|8-1o?U`^f)#bk zuRi|7>N9%Y?^2_=DlyR(!RKhQTWeqDZ&bf{84yf4ThSI_zHHBod9rG3&n!D6An;TI z!$%?~Fw;+RditPWZT%2ZH?qk*u(jc#oemx`A#4!;bCueKF>Gb2QHCz%!vr^j2 zxG8$DLi(B!@7mL=W1gsgfKSC5l4em$XhU6pQ_pk*8Q61Y*oDRUQCxz*hDj%4;KtVh zcd2ws%Y^5w2$hy zywE-M_z-bv={yd_@7bllL-fE^Y7U1iYXV7Fmhj;s>NpRaCV4`ulis}4xtw0k-z0-VhroU7!HSXm$+34>S~fkIUK6*cb*F-N z!q3|*NQwAPMj5nyee9?!OuRK{O`Q&Zd=SmKW%qNSc}m z?_1OKhnkv7cF|mkBI-B~cfYbxD&B_-eEo!Pqg^P0+hlppC+xgRdpq5YBS7dTiz{SZ z=QYyf`K3L_OK-I6)^g5yj1M`7F|j<1$J>a`Y;+BtLdPq-V@WIIQLM(@GB#lV`!vd6 zq6s06|9SVs?4%is@w@u!>yWYI?vjfHTQ|4<kpY2c#GH6clhV7{vg+Mk_TAcozqdX4G>TCU7TVN#*2B3Q%QgZPuG;ql>WM z9pO|D62h}2PB*zN1%396Lf_FVD^(HO``C+z3v zrsHi6S1efM&$N$j+X~)^!vkMe9T04d4oGST7V36{uXY-xEoi(d_ou{`qbk`4-ub-R zE8Z9_J%zB9G?VjXRwt_K5GBceuLJ#Z04hO>l6R12<`?Q1{JRr2SxtI+LP`JtRj_3QIbMbw)eY}@#J8385phLQ5P6tfyx5oU`=$9R$$ z8C8^1LL#%-lyq38*XgNJ@<2?8MJ| zzfBikmdpFr6?tFtec$v7^D1_HQ-X;{C603-O|jN)vXQLJYe>Z2I?(UH^Qv>twz3bZ zX-LOt%S-(va=u6zNJ(PsjCpmX$b$_AvXkQRQm0VFZ}lwl7?zM!dNX|iu2y#fUmPK8 z?1z~Wauq3Ez#6qX^VS^w#3FRf|v)U7;W zeK*V3KXf`?^r|yxBuG-rwj(dUE^C8eYPA_B&Y)OrzOsk;?4||IJ0UoFt}}b8vuF(R zvHy12cn#o&Az)6z5j>K-Xg32>%XW{_x&$?&A7xsoW;4++QA4rFZ3O4O5;Z#UY)kA0rrx)Tc(77sD%Ca;a-?u(NM|oaF*7+% zR;ti(n>-$Io1phJV7HX3jXVa0-c7Fh=#c20OvaS>BCfK2_lD?OKad@`d(he$8=)t7!>MWBMqa@{ zD7AWN7iZDdO4t#dQ<&ylK4m)SuEAY1Hg%0W1~(o(Y<1y|z)>GI5%rok?%ZhO^_ugH zHMqS_Erz*c&^u1$CSFC&qclW~-YDC5Iv19sr%w)8wgF{29LsHxUQ+dUZl_AZ5O+Du z0;!T!V|(`(J|Q%Dp!3{~%&J)=?VOme)ZU#X^k~Q8USFPVglk>s4bH2wA5H@U)+_X$ zi&>J_NZ9-@w$?G<>T~Xj>7%}U=9EApQh~|^`1w) zAN32JU(7>*H3OnI%OglrogiO?0B{3=UH67;$Of;Mpj{wB-FK*58srE+;d055r~(!Y zWpHj<{FHOOQ=dqyptVAip#5*=#cN<%%L%B?NU|A&!_yX$S|ELcdyEr-9+5E4t@~^TAutXTkZ}+PRoWecQs$ksw96W;imlJ`2(gVd(fB$!i zKj!QoXMgkO3I*xE0{m6n{bzuBU^(zlQtw|4|0;3)v*8|4>GOXVy#9*wtM2g+By3=( z^%oW7ug1SB$o?>fhyUI9KlEh3BK#`O`UBzm9|-?1sn)N6ziOTS0K5gZje+C&Rr&NQ zz~C>9=05;hfFd~{z#nbrUrm4Qas6TH4s3b-b1wg>JNu*K^()F>TNi&|000L-{noFI zj9;z)S}*>ybuBOu{@eOr)#G2ye=Yd^0l-M~`w;$r%74Ft{x#VC0SN#=GyeHI{X6t3 WNJ9Xf__szs0^lQ%2eQn+UHu<^-Hi7D literal 0 HcmV?d00001 diff --git a/spring-mvc-java/pom.xml b/spring-mvc-java/pom.xml index 9513c81064..ef18cef3e0 100644 --- a/spring-mvc-java/pom.xml +++ b/spring-mvc-java/pom.xml @@ -161,26 +161,11 @@ - - org.apache.poi - poi - ${poi.version} - org.apache.poi poi-ooxml ${poi.version} - - org.apache.poi - poi-ooxml-schemas - ${poi.version} - - - org.jxls - jxls-jexcel - ${jexcel.version} - @@ -395,7 +380,6 @@ 3.16-beta1 - 1.0.6 diff --git a/spring-mvc-java/src/main/java/com/baeldung/excel/ExcelPOIHelper.java b/spring-mvc-java/src/main/java/com/baeldung/excel/ExcelPOIHelper.java index 5cf74aff63..e3658e8f89 100644 --- a/spring-mvc-java/src/main/java/com/baeldung/excel/ExcelPOIHelper.java +++ b/spring-mvc-java/src/main/java/com/baeldung/excel/ExcelPOIHelper.java @@ -1,140 +1,188 @@ package com.baeldung.excel; import org.apache.poi.ss.usermodel.Cell; -import org.apache.poi.ss.usermodel.CellType; -import org.apache.poi.ss.usermodel.CellStyle; -import org.apache.poi.ss.usermodel.IndexedColors; -import org.apache.poi.ss.usermodel.Row; -import org.apache.poi.ss.usermodel.Sheet; -import org.apache.poi.ss.usermodel.Workbook; -import org.apache.poi.xssf.usermodel.XSSFWorkbook; +import org.apache.poi.xssf.usermodel.XSSFCell; +import org.apache.poi.xssf.usermodel.XSSFCellStyle; +import org.apache.poi.xssf.usermodel.XSSFColor; import org.apache.poi.xssf.usermodel.XSSFFont; +import org.apache.poi.xssf.usermodel.XSSFRow; +import org.apache.poi.xssf.usermodel.XSSFSheet; +import org.apache.poi.xssf.usermodel.XSSFWorkbook; +import org.apache.poi.hssf.usermodel.HSSFCell; +import org.apache.poi.hssf.usermodel.HSSFCellStyle; +import org.apache.poi.hssf.usermodel.HSSFFont; +import org.apache.poi.hssf.usermodel.HSSFRow; +import org.apache.poi.hssf.usermodel.HSSFSheet; +import org.apache.poi.hssf.usermodel.HSSFWorkbook; +import org.apache.poi.hssf.util.HSSFColor; import org.apache.poi.ss.usermodel.DateUtil; -import org.apache.poi.ss.usermodel.FillPatternType; + import java.io.File; -import java.io.FileOutputStream; import java.io.FileInputStream; import java.io.IOException; -import java.io.InputStream; import java.util.Map; import java.util.HashMap; import java.util.ArrayList; -import org.springframework.stereotype.Service; +import java.util.List; -@Service public class ExcelPOIHelper { - public Map> readExcel(String fileLocation) throws IOException { + public Map> readExcel(String fileLocation) throws IOException { - Map> data = new HashMap>(); - FileInputStream file = new FileInputStream(new File(fileLocation)); - Workbook workbook = new XSSFWorkbook(file); - Sheet sheet = workbook.getSheetAt(0); - int i = 0; - for (Row row : sheet) { - data.put(i, new ArrayList()); - for (Cell cell : row) { - switch (cell.getCellTypeEnum()) { - case STRING: - data.get(i) - .add(cell.getRichStringCellValue() - .getString()); - break; - case NUMERIC: - if (DateUtil.isCellDateFormatted(cell)) { - data.get(i) - .add(cell.getDateCellValue() + ""); - } else { - data.get(i) - .add(cell.getNumericCellValue() + ""); - } - break; - case BOOLEAN: - data.get(i) - .add(cell.getBooleanCellValue() + ""); - break; - case FORMULA: - data.get(i) - .add(cell.getCellFormula() + ""); - break; - default: - data.get(i) - .add(" "); + Map> data = new HashMap<>(); + FileInputStream fis = new FileInputStream(new File(fileLocation)); + + if (fileLocation.endsWith(".xls")) { + data = readHSSFWorkbook(fis); + } else if (fileLocation.endsWith(".xlsx")) { + data = readXSSFWorkbook(fis); + } + + int maxNrCols = 0; + + for (List ls : data.values()) { + if (ls.size() > maxNrCols) { + maxNrCols = ls.size(); + } + } + + for (List ls : data.values()) { + if (ls.size() < maxNrCols) { + for (int i = ls.size(); i < maxNrCols; i++) { + ls.add(new MyCell("")); } } - i++; } - if (workbook != null){ - workbook.close(); + + return data; + } + + private String readCellContent(Cell cell) { + String content = ""; + switch (cell.getCellTypeEnum()) { + case STRING: + content = cell.getStringCellValue(); + break; + case NUMERIC: + if (DateUtil.isCellDateFormatted(cell)) { + content = cell.getDateCellValue() + ""; + } else { + content = cell.getNumericCellValue() + ""; + } + break; + case BOOLEAN: + content = cell.getBooleanCellValue() + ""; + break; + case FORMULA: + content = cell.getCellFormula() + ""; + break; + default: + content = ""; + } + return content; + } + + private Map> readHSSFWorkbook(FileInputStream fis) throws IOException { + Map> data = new HashMap<>(); + HSSFWorkbook workbook = null; + try { + workbook = new HSSFWorkbook(fis); + + HSSFSheet sheet = workbook.getSheetAt(0); + for (int i = sheet.getFirstRowNum(); i <= sheet.getLastRowNum(); i++) { + HSSFRow row = sheet.getRow(i); + data.put(i, new ArrayList()); + if (row != null) { + for (int j = 0; j < row.getLastCellNum(); j++) { + HSSFCell cell = row.getCell(j); + if (cell != null) { + HSSFCellStyle cellStyle = cell.getCellStyle(); + + MyCell myCell = new MyCell(); + + HSSFColor bgColor = cellStyle.getFillForegroundColorColor(); + if (bgColor != null) { + short[] rgbColor = bgColor.getTriplet(); + myCell.setBgColor("rgb(" + rgbColor[0] + "," + rgbColor[1] + "," + rgbColor[2] + ")"); + } + HSSFFont font = cell.getCellStyle() + .getFont(workbook); + myCell.setTextSize(font.getFontHeightInPoints() + ""); + if (font.getBold()) { + myCell.setTextWeight("bold"); + } + HSSFColor textColor = font.getHSSFColor(workbook); + if (textColor != null) { + short[] rgbColor = textColor.getTriplet(); + myCell.setTextColor("rgb(" + rgbColor[0] + "," + rgbColor[1] + "," + rgbColor[2] + ")"); + } + myCell.setContent(readCellContent(cell)); + data.get(i) + .add(myCell); + } else { + data.get(i) + .add(new MyCell("")); + } + } + } + } + } finally { + if (workbook != null) { + workbook.close(); + } } return data; } - public void writeExcel() throws IOException { - Workbook workbook = new XSSFWorkbook(); - + private Map> readXSSFWorkbook(FileInputStream fis) throws IOException { + XSSFWorkbook workbook = null; + Map> data = new HashMap<>(); try { - Sheet sheet = workbook.createSheet("Persons"); - sheet.setColumnWidth(0, 6000); - sheet.setColumnWidth(1, 4000); - Row header = sheet.createRow(0); + workbook = new XSSFWorkbook(fis); + XSSFSheet sheet = workbook.getSheetAt(0); - CellStyle headerStyle = workbook.createCellStyle(); + for (int i = sheet.getFirstRowNum(); i <= sheet.getLastRowNum(); i++) { + XSSFRow row = sheet.getRow(i); + data.put(i, new ArrayList()); + if (row != null) { + for (int j = 0; j < row.getLastCellNum(); j++) { + XSSFCell cell = row.getCell(j); + if (cell != null) { + XSSFCellStyle cellStyle = cell.getCellStyle(); - headerStyle.setFillForegroundColor(IndexedColors.LIGHT_BLUE.getIndex()); - headerStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); - - XSSFFont font = ((XSSFWorkbook) workbook).createFont(); - font.setFontName("Arial"); - font.setFontHeightInPoints((short) 16); - font.setBold(true); - headerStyle.setFont(font); - - Cell headerCell = header.createCell(0); - headerCell.setCellValue("Name"); - headerCell.setCellStyle(headerStyle); - - headerCell = header.createCell(1); - headerCell.setCellValue("Age"); - headerCell.setCellStyle(headerStyle); - - CellStyle style = workbook.createCellStyle(); - style.setWrapText(true); - - Row row = sheet.createRow(2); - Cell cell = row.createCell(0); - cell.setCellValue("John Smith"); - cell.setCellStyle(style); - - cell = row.createCell(1); - cell.setCellValue(20); - cell.setCellStyle(style); - - row = sheet.createRow(3); - cell = row.createCell(0); - cell.setCellValue("Ana Johnson"); - cell.setCellStyle(style); - - cell = row.createCell(1); - cell.setCellValue(30); - cell.setCellStyle(style); - - File currDir = new File("."); - String path = currDir.getAbsolutePath(); - String fileLocation = path.substring(0, path.length() - 1) + "temp.xlsx"; - - FileOutputStream outputStream = new FileOutputStream(fileLocation); - workbook.write(outputStream); - } finally { - if (workbook != null) { - try { - workbook.close(); - } catch (IOException e) { - e.printStackTrace(); + MyCell myCell = new MyCell(); + XSSFColor bgColor = cellStyle.getFillForegroundColorColor(); + if (bgColor != null) { + byte[] rgbColor = bgColor.getRGB(); + myCell.setBgColor("rgb(" + (rgbColor[0] < 0 ? (rgbColor[0] + 0xff) : rgbColor[0]) + "," + (rgbColor[1] < 0 ? (rgbColor[1] + 0xff) : rgbColor[1]) + "," + (rgbColor[2] < 0 ? (rgbColor[2] + 0xff) : rgbColor[2]) + ")"); + } + XSSFFont font = cellStyle.getFont(); + myCell.setTextSize(font.getFontHeightInPoints() + ""); + if (font.getBold()) { + myCell.setTextWeight("bold"); + } + XSSFColor textColor = font.getXSSFColor(); + if (textColor != null) { + byte[] rgbColor = textColor.getRGB(); + myCell.setTextColor("rgb(" + (rgbColor[0] < 0 ? (rgbColor[0] + 0xff) : rgbColor[0]) + "," + (rgbColor[1] < 0 ? (rgbColor[1] + 0xff) : rgbColor[1]) + "," + (rgbColor[2] < 0 ? (rgbColor[2] + 0xff) : rgbColor[2]) + ")"); + } + myCell.setContent(readCellContent(cell)); + data.get(i) + .add(myCell); + } else { + data.get(i) + .add(new MyCell("")); + } + } } } + } finally { + if (workbook != null) { + workbook.close(); + } } + return data; } } \ No newline at end of file diff --git a/spring-mvc-java/src/main/java/com/baeldung/excel/JExcelHelper.java b/spring-mvc-java/src/main/java/com/baeldung/excel/JExcelHelper.java deleted file mode 100644 index d0e33bf471..0000000000 --- a/spring-mvc-java/src/main/java/com/baeldung/excel/JExcelHelper.java +++ /dev/null @@ -1,82 +0,0 @@ -package com.baeldung.excel; - -import jxl.Sheet; -import jxl.Workbook; -import jxl.format.Colour; -import jxl.read.biff.BiffException; -import jxl.write.*; -import jxl.write.Number; -import org.springframework.stereotype.Service; - -import java.io.File; -import java.io.IOException; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.Map; - -@Service -public class JExcelHelper { - - public Map> readJExcel(String fileLocation) throws IOException, BiffException { - Map> data = new HashMap<>(); - - Workbook workbook = Workbook.getWorkbook(new File(fileLocation)); - Sheet sheet = workbook.getSheet(0); - int rows = sheet.getRows(); - int columns = sheet.getColumns(); - - for (int i = 0; i < rows; i++) { - data.put(i, new ArrayList<>()); - for (int j = 0; j < columns; j++) { - data.get(i).add(sheet.getCell(j, i).getContents()); - } - } - return data; - } - - public void writeJExcel() throws IOException, WriteException { - WritableWorkbook workbook = null; - try { - File currDir = new File("."); - String path = currDir.getAbsolutePath(); - String fileLocation = path.substring(0, path.length() - 1) + "temp.xls"; - - workbook = Workbook.createWorkbook(new File(fileLocation)); - - WritableSheet sheet = workbook.createSheet("Sheet 1", 0); - - WritableCellFormat headerFormat = new WritableCellFormat(); - WritableFont font = new WritableFont(WritableFont.ARIAL, 16, WritableFont.BOLD); - headerFormat.setFont(font); - headerFormat.setBackground(Colour.LIGHT_BLUE); - headerFormat.setWrap(true); - Label headerLabel = new Label(0, 0, "Name", headerFormat); - sheet.setColumnView(0, 60); - sheet.addCell(headerLabel); - - headerLabel = new Label(1, 0, "Age", headerFormat); - sheet.setColumnView(0, 40); - sheet.addCell(headerLabel); - - WritableCellFormat cellFormat = new WritableCellFormat(); - cellFormat.setWrap(true); - - Label cellLabel = new Label(0, 2, "John Smith", cellFormat); - sheet.addCell(cellLabel); - Number cellNumber = new Number(1, 2, 20, cellFormat); - sheet.addCell(cellNumber); - - cellLabel = new Label(0, 3, "Ana Johnson", cellFormat); - sheet.addCell(cellLabel); - cellNumber = new Number(1, 3, 30, cellFormat); - sheet.addCell(cellNumber); - - workbook.write(); - } finally { - if (workbook != null) { - workbook.close(); - } - } - - } -} \ No newline at end of file diff --git a/spring-mvc-java/src/main/java/com/baeldung/excel/MyCell.java b/spring-mvc-java/src/main/java/com/baeldung/excel/MyCell.java new file mode 100644 index 0000000000..409829bc35 --- /dev/null +++ b/spring-mvc-java/src/main/java/com/baeldung/excel/MyCell.java @@ -0,0 +1,57 @@ +package com.baeldung.excel; + +public class MyCell { + private String content; + private String textColor; + private String bgColor; + private String textSize; + private String textWeight; + + public MyCell() { + } + + public MyCell(String content) { + this.content = content; + } + + public String getContent() { + return content; + } + + public void setContent(String content) { + this.content = content; + } + + public String getTextColor() { + return textColor; + } + + public void setTextColor(String textColor) { + this.textColor = textColor; + } + + public String getBgColor() { + return bgColor; + } + + public void setBgColor(String bgColor) { + this.bgColor = bgColor; + } + + public String getTextSize() { + return textSize; + } + + public void setTextSize(String textSize) { + this.textSize = textSize; + } + + public String getTextWeight() { + return textWeight; + } + + public void setTextWeight(String textWeight) { + this.textWeight = textWeight; + } + +} \ No newline at end of file diff --git a/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/WebConfig.java b/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/WebConfig.java index 9578303554..11be08a79d 100644 --- a/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/WebConfig.java +++ b/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/WebConfig.java @@ -108,11 +108,6 @@ public class WebConfig extends WebMvcConfigurerAdapter { configurer.setUrlPathHelper(urlPathHelper); } - @Bean - public JExcelHelper jExcelHelper() { - return new JExcelHelper(); - } - @Bean public ExcelPOIHelper excelPOIHelper() { return new ExcelPOIHelper(); diff --git a/spring-mvc-java/src/main/java/com/baeldung/web/controller/ExcelController.java b/spring-mvc-java/src/main/java/com/baeldung/web/controller/ExcelController.java index 810282dd65..f76f7441a5 100644 --- a/spring-mvc-java/src/main/java/com/baeldung/web/controller/ExcelController.java +++ b/spring-mvc-java/src/main/java/com/baeldung/web/controller/ExcelController.java @@ -10,20 +10,15 @@ import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import com.baeldung.excel.*; -import jxl.read.biff.BiffException; import java.util.Map; -import java.util.ArrayList; +import java.util.List; import javax.annotation.Resource; -import jxl.write.WriteException; @Controller public class ExcelController { private String fileLocation; - @Resource(name = "jExcelHelper") - private JExcelHelper jExcelHelper; - @Resource(name = "excelPOIHelper") private ExcelPOIHelper excelPOIHelper; @@ -45,36 +40,19 @@ public class ExcelController { } f.flush(); f.close(); - System.out.println(fileLocation); model.addAttribute("message", "File: " + file.getOriginalFilename() + " has been uploaded successfully!"); return "excel"; } - @RequestMapping(method = RequestMethod.GET, value = "/readJExcel") - public String readJExcel(Model model) throws IOException, BiffException { - - if (fileLocation != null) { - if (fileLocation.endsWith(".xls")) { - Map> data = jExcelHelper.readJExcel(fileLocation); - model.addAttribute("data", data); - } else { - model.addAttribute("message", "Not a valid .xls file!"); - } - } else { - model.addAttribute("message", "File missing! Please upload an excel file."); - } - return "excel"; - } - @RequestMapping(method = RequestMethod.GET, value = "/readPOI") public String readPOI(Model model) throws IOException { if (fileLocation != null) { - if (fileLocation.endsWith(".xlsx")) { - Map> data = excelPOIHelper.readExcel(fileLocation); + if (fileLocation.endsWith(".xlsx") || fileLocation.endsWith(".xls")) { + Map> data = excelPOIHelper.readExcel(fileLocation); model.addAttribute("data", data); } else { - model.addAttribute("message", "Not a valid .xlsx file!"); + model.addAttribute("message", "Not a valid excel file!"); } } else { model.addAttribute("message", "File missing! Please upload an excel file."); @@ -82,24 +60,4 @@ public class ExcelController { return "excel"; } - @RequestMapping(method = RequestMethod.POST, value = "/writeJExcel") - public String writeJExcel(Model model) throws IOException, BiffException, WriteException { - - jExcelHelper.writeJExcel(); - - model.addAttribute("message", "Write successful!"); - - return "excel"; - } - - @RequestMapping(method = RequestMethod.POST, value = "/writePOI") - public String writePOI(Model model) throws IOException { - - excelPOIHelper.writeExcel(); - - model.addAttribute("message", "Write successful!"); - - return "excel"; - } - } \ No newline at end of file diff --git a/spring-mvc-java/src/main/webapp/WEB-INF/view/excel.jsp b/spring-mvc-java/src/main/webapp/WEB-INF/view/excel.jsp index b50687df15..bcb5a31371 100644 --- a/spring-mvc-java/src/main/webapp/WEB-INF/view/excel.jsp +++ b/spring-mvc-java/src/main/webapp/WEB-INF/view/excel.jsp @@ -1,6 +1,6 @@ <%@ page language="java" contentType="text/html; charset=ISO-8859-1" - pageEncoding="ISO-8859-1"%> -<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> + pageEncoding="ISO-8859-1"%> +<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> @@ -8,49 +8,43 @@ Excel Processing - - - - - - + + + -
- - -
-
-${message } -

-
- -
-

-Read file using JExcel     -Read file using Apache POI -

+
+ + +
+
+
+ +
+
${message } +
+
-File content: - - - - - - - - - -
${cell}
-
-

-
- -
-
-
- -
+
+ +
+
+
+ + + + + + + + + + +
+ ${cell.content} +
+
- \ No newline at end of file From 00d83f90709108e88c16ade58dbeb9899f7067dc Mon Sep 17 00:00:00 2001 From: Tomasz Lelek Date: Fri, 17 Feb 2017 18:26:23 +0100 Subject: [PATCH 13/68] BAEL-602 create JOOL module, one test case --- jooq/pom.xml | 46 +++++++++++++++++++ jooq/src/test/java/com/baeldung/JOOLTest.java | 29 ++++++++++++ pom.xml | 1 + 3 files changed, 76 insertions(+) create mode 100644 jooq/pom.xml create mode 100644 jooq/src/test/java/com/baeldung/JOOLTest.java diff --git a/jooq/pom.xml b/jooq/pom.xml new file mode 100644 index 0000000000..ef287d0292 --- /dev/null +++ b/jooq/pom.xml @@ -0,0 +1,46 @@ + + + + parent-modules + com.baeldung + 1.0.0-SNAPSHOT + + 4.0.0 + + jooq + + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.8 + 1.8 + + + + + + + + org.jooq + jool + ${jool.version} + + + junit + junit + ${junit.version} + test + + + + + 0.9.12 + 4.12 + + + + \ No newline at end of file diff --git a/jooq/src/test/java/com/baeldung/JOOLTest.java b/jooq/src/test/java/com/baeldung/JOOLTest.java new file mode 100644 index 0000000000..aa729a3edf --- /dev/null +++ b/jooq/src/test/java/com/baeldung/JOOLTest.java @@ -0,0 +1,29 @@ +package com.baeldung; + + +import org.jooq.lambda.Seq; +import org.junit.Test; + +import java.util.Arrays; +import java.util.List; + +import static junit.framework.Assert.assertTrue; +import static junit.framework.TestCase.assertEquals; + +public class JOOLTest { + @Test + public void givenSeq_whenCheckContains_shouldReturnTrue() { + List concat = Seq.of(1, 2, 3).concat(Seq.of(4, 5, 6)).toList(); + + assertEquals(concat, Arrays.asList(1, 2, 3, 4, 5, 6)); + + + assertTrue(Seq.of(1, 2, 3, 4).contains(2)); + + + assertTrue(Seq.of(1, 2, 3, 4).containsAll(2, 3)); + + + assertTrue(Seq.of(1, 2, 3, 4).containsAny(2, 5)); + } +} diff --git a/pom.xml b/pom.xml index ecb07e987b..42ded9de0d 100644 --- a/pom.xml +++ b/pom.xml @@ -193,6 +193,7 @@ struts2 apache-velocity + jooq From 6c282a742ce71e04513f1a2c6d560754a7acd854 Mon Sep 17 00:00:00 2001 From: slavisa-baeldung Date: Sat, 18 Feb 2017 07:31:10 +0100 Subject: [PATCH 14/68] BAEL-639: Fixing failing test --- .../DivisibilityTest.java | 4 +- .../MyParameterisedUnitTest.java | 28 +---- .../StringCaseTest.java | 2 +- .../SuiteTest.java | 2 +- .../SummationServiceTest.java | 9 +- .../src/test/resources/parameterised_test.xml | 10 -- core-java/src/test/resources/test_group.xml | 13 --- core-java/src/test/resources/test_suite.xml | 9 -- pom.xml | 1 + testng/pom.xml | 109 ++++++++++++++++++ .../java/baeldung/com}/DependentTests.java | 10 +- .../baeldung/com/ParametrizedTestNGTest.java | 36 ++---- .../java/baeldung/com}/RegistrationTest.java | 2 +- .../test/java/baeldung/com}/SignInTest.java | 2 +- .../baeldung/com/SummationServiceTest.java | 49 ++++---- .../src/test/java/baeldung/com/TestGroup.java | 44 +++++++ .../test/java/baeldung/com}/TimeOutTest.java | 6 +- testng/src/test/resources/logback.xml | 14 +++ .../src/test/resources/parameterised_test.xml | 17 +++ testng/src/test/resources/test_group.xml | 13 +++ testng/src/test/resources/test_suite.xml | 9 ++ 21 files changed, 265 insertions(+), 124 deletions(-) rename core-java/src/test/java/com/baeldung/{test/comparison => junit4vstestng}/DivisibilityTest.java (74%) rename core-java/src/test/java/com/baeldung/{test/comparison => junit4vstestng}/MyParameterisedUnitTest.java (59%) rename core-java/src/test/java/com/baeldung/{test/comparison => junit4vstestng}/StringCaseTest.java (90%) rename core-java/src/test/java/com/baeldung/{test/comparison => junit4vstestng}/SuiteTest.java (83%) rename core-java/src/test/java/com/baeldung/{test/comparison => junit4vstestng}/SummationServiceTest.java (94%) delete mode 100644 core-java/src/test/resources/parameterised_test.xml delete mode 100644 core-java/src/test/resources/test_group.xml delete mode 100644 core-java/src/test/resources/test_suite.xml create mode 100644 testng/pom.xml rename {core-java/src/test/java/com/baeldung/test/comparison => testng/src/test/java/baeldung/com}/DependentTests.java (82%) rename core-java/src/test/java/com/baeldung/test/comparison/MyParameterisedUnitTestNg.java => testng/src/test/java/baeldung/com/ParametrizedTestNGTest.java (51%) rename {core-java/src/test/java/com/baeldung/test/comparison => testng/src/test/java/baeldung/com}/RegistrationTest.java (78%) rename {core-java/src/test/java/com/baeldung/test/comparison => testng/src/test/java/baeldung/com}/SignInTest.java (78%) rename core-java/src/test/java/com/baeldung/test/comparison/SummationServiceTestTestNg.java => testng/src/test/java/baeldung/com/SummationServiceTest.java (79%) create mode 100644 testng/src/test/java/baeldung/com/TestGroup.java rename {core-java/src/test/java/com/baeldung/test/comparison => testng/src/test/java/baeldung/com}/TimeOutTest.java (70%) create mode 100644 testng/src/test/resources/logback.xml create mode 100644 testng/src/test/resources/parameterised_test.xml create mode 100644 testng/src/test/resources/test_group.xml create mode 100644 testng/src/test/resources/test_suite.xml diff --git a/core-java/src/test/java/com/baeldung/test/comparison/DivisibilityTest.java b/core-java/src/test/java/com/baeldung/junit4vstestng/DivisibilityTest.java similarity index 74% rename from core-java/src/test/java/com/baeldung/test/comparison/DivisibilityTest.java rename to core-java/src/test/java/com/baeldung/junit4vstestng/DivisibilityTest.java index 9ae13f5934..dec7dbe6aa 100644 --- a/core-java/src/test/java/com/baeldung/test/comparison/DivisibilityTest.java +++ b/core-java/src/test/java/com/baeldung/junit4vstestng/DivisibilityTest.java @@ -1,4 +1,4 @@ -package com.baeldung.test.comparison; +package com.baeldung.junit4vstestng; import static org.junit.Assert.assertEquals; @@ -15,7 +15,7 @@ public class DivisibilityTest { } @Test - public void givenNumber_whenDivisiblebyTwo_thenCorrect() { + public void givenNumber_whenDivisibleByTwo_thenCorrect() { assertEquals(number % 2, 0); } } diff --git a/core-java/src/test/java/com/baeldung/test/comparison/MyParameterisedUnitTest.java b/core-java/src/test/java/com/baeldung/junit4vstestng/MyParameterisedUnitTest.java similarity index 59% rename from core-java/src/test/java/com/baeldung/test/comparison/MyParameterisedUnitTest.java rename to core-java/src/test/java/com/baeldung/junit4vstestng/MyParameterisedUnitTest.java index 3372bbb577..95f9274af3 100644 --- a/core-java/src/test/java/com/baeldung/test/comparison/MyParameterisedUnitTest.java +++ b/core-java/src/test/java/com/baeldung/junit4vstestng/MyParameterisedUnitTest.java @@ -1,7 +1,4 @@ -package com.baeldung.test.comparison; - -import java.util.Arrays; -import java.util.Collection; +package com.baeldung.junit4vstestng; import org.junit.Assert; import org.junit.Before; @@ -10,16 +7,13 @@ import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import org.junit.runners.Parameterized.Parameters; +import java.util.Arrays; +import java.util.Collection; + @RunWith(value = Parameterized.class) public class MyParameterisedUnitTest { private String name; - private NameCheck nameCheck; - - @Before - public void initialSetup() { - nameCheck = new NameCheck(); - } public MyParameterisedUnitTest(String myName) { this.name = myName; @@ -27,23 +21,13 @@ public class MyParameterisedUnitTest { @Parameters public static Collection data() { - Object[][] data = new Object[][] { { "Peter" }, { "Sam" }, { "Tim" }, { "Lucy" } }; + Object[][] data = new Object[][]{{"Peter"}, {"Sam"}, {"Tim"}, {"Lucy"}}; return Arrays.asList(data); } @Test public void givenName_whenValidLength_thenTrue() { - boolean valid = nameCheck.nameCheck(name); + boolean valid = name.length() > 0; Assert.assertEquals(valid, true); } } - -class NameCheck { - - public boolean nameCheck(String name) { - if (name.length() > 0) - return true; - return false; - } - -} diff --git a/core-java/src/test/java/com/baeldung/test/comparison/StringCaseTest.java b/core-java/src/test/java/com/baeldung/junit4vstestng/StringCaseTest.java similarity index 90% rename from core-java/src/test/java/com/baeldung/test/comparison/StringCaseTest.java rename to core-java/src/test/java/com/baeldung/junit4vstestng/StringCaseTest.java index b4226b82e7..520017e4c1 100644 --- a/core-java/src/test/java/com/baeldung/test/comparison/StringCaseTest.java +++ b/core-java/src/test/java/com/baeldung/junit4vstestng/StringCaseTest.java @@ -1,4 +1,4 @@ -package com.baeldung.test.comparison; +package com.baeldung.junit4vstestng; import static org.junit.Assert.assertEquals; diff --git a/core-java/src/test/java/com/baeldung/test/comparison/SuiteTest.java b/core-java/src/test/java/com/baeldung/junit4vstestng/SuiteTest.java similarity index 83% rename from core-java/src/test/java/com/baeldung/test/comparison/SuiteTest.java rename to core-java/src/test/java/com/baeldung/junit4vstestng/SuiteTest.java index a30e5d312a..3d6d6cbbfb 100644 --- a/core-java/src/test/java/com/baeldung/test/comparison/SuiteTest.java +++ b/core-java/src/test/java/com/baeldung/junit4vstestng/SuiteTest.java @@ -1,4 +1,4 @@ -package com.baeldung.test.comparison; +package com.baeldung.junit4vstestng; import org.junit.runner.RunWith; import org.junit.runners.Suite; diff --git a/core-java/src/test/java/com/baeldung/test/comparison/SummationServiceTest.java b/core-java/src/test/java/com/baeldung/junit4vstestng/SummationServiceTest.java similarity index 94% rename from core-java/src/test/java/com/baeldung/test/comparison/SummationServiceTest.java rename to core-java/src/test/java/com/baeldung/junit4vstestng/SummationServiceTest.java index b76a87c0fe..7d1bf3b7af 100644 --- a/core-java/src/test/java/com/baeldung/test/comparison/SummationServiceTest.java +++ b/core-java/src/test/java/com/baeldung/junit4vstestng/SummationServiceTest.java @@ -1,8 +1,4 @@ -package com.baeldung.test.comparison; - -import java.security.Security; -import java.util.ArrayList; -import java.util.List; +package com.baeldung.junit4vstestng; import org.junit.After; import org.junit.AfterClass; @@ -12,6 +8,9 @@ import org.junit.BeforeClass; import org.junit.Ignore; import org.junit.Test; +import java.util.ArrayList; +import java.util.List; + public class SummationServiceTest { private static List numbers; diff --git a/core-java/src/test/resources/parameterised_test.xml b/core-java/src/test/resources/parameterised_test.xml deleted file mode 100644 index 69a2c60460..0000000000 --- a/core-java/src/test/resources/parameterised_test.xml +++ /dev/null @@ -1,10 +0,0 @@ - - - - - - - - - - \ No newline at end of file diff --git a/core-java/src/test/resources/test_group.xml b/core-java/src/test/resources/test_group.xml deleted file mode 100644 index 0c9a6c73df..0000000000 --- a/core-java/src/test/resources/test_group.xml +++ /dev/null @@ -1,13 +0,0 @@ - - - - - - - - - - - - - \ No newline at end of file diff --git a/core-java/src/test/resources/test_suite.xml b/core-java/src/test/resources/test_suite.xml deleted file mode 100644 index 36305aa5fc..0000000000 --- a/core-java/src/test/resources/test_suite.xml +++ /dev/null @@ -1,9 +0,0 @@ - - - - - - - - - \ No newline at end of file diff --git a/pom.xml b/pom.xml index ecb07e987b..eb89b9cbaa 100644 --- a/pom.xml +++ b/pom.xml @@ -194,6 +194,7 @@ struts2 apache-velocity + testng diff --git a/testng/pom.xml b/testng/pom.xml new file mode 100644 index 0000000000..02f51d4d99 --- /dev/null +++ b/testng/pom.xml @@ -0,0 +1,109 @@ + + + 4.0.0 + com.baeldung + testng + 0.1.0-SNAPSHOT + jar + testng + + + + + + + org.slf4j + slf4j-api + ${org.slf4j.version} + + + ch.qos.logback + logback-classic + ${logback.version} + + + + + + org.testng + testng + ${testng.version} + test + + + + + + testng + + + src/main/resources + true + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + ${maven-compiler-plugin.version} + + 1.8 + 1.8 + + + + + org.apache.maven.plugins + maven-surefire-plugin + ${maven-surefire-plugin.version} + + + **/*IntegrationTest.java + **/*LongRunningUnitTest.java + **/*ManualTest.java + + true + + + + + org.apache.maven.plugins + maven-dependency-plugin + + + copy-dependencies + prepare-package + + copy-dependencies + + + ${project.build.directory}/libs + + + + + + + + + + + + 1.7.21 + 1.1.7 + + + 6.10 + + + 3.6.0 + 2.19.1 + + + + + \ No newline at end of file diff --git a/core-java/src/test/java/com/baeldung/test/comparison/DependentTests.java b/testng/src/test/java/baeldung/com/DependentTests.java similarity index 82% rename from core-java/src/test/java/com/baeldung/test/comparison/DependentTests.java rename to testng/src/test/java/baeldung/com/DependentTests.java index 3ef4949067..ab593e0439 100644 --- a/core-java/src/test/java/com/baeldung/test/comparison/DependentTests.java +++ b/testng/src/test/java/baeldung/com/DependentTests.java @@ -1,4 +1,4 @@ -package com.baeldung.test.comparison; +package baeldung.com; import org.testng.Assert; import org.testng.annotations.BeforeClass; @@ -6,9 +6,9 @@ import org.testng.annotations.Test; public class DependentTests { + private String validEmail = "abc@qwe.com"; private EmailValidator emailValidator; private LoginValidator loginValidator; - private String validEmail = "abc@qwe.com"; @BeforeClass public void setup() { @@ -23,7 +23,7 @@ public class DependentTests { } @Test(dependsOnMethods = { "givenEmail_ifValid_thenTrue" }) - public void givenValidEmail_whenLoggedin_thenTrue() { + public void givenValidEmail_whenLoggedIn_thenTrue() { boolean valid = loginValidator.validate(); Assert.assertEquals(valid, true); } @@ -31,7 +31,7 @@ public class DependentTests { class EmailValidator { - public boolean validate(String validEmail) { + boolean validate(String validEmail) { return true; } @@ -39,7 +39,7 @@ class EmailValidator { class LoginValidator { - public boolean validate() { + boolean validate() { return true; } diff --git a/core-java/src/test/java/com/baeldung/test/comparison/MyParameterisedUnitTestNg.java b/testng/src/test/java/baeldung/com/ParametrizedTestNGTest.java similarity index 51% rename from core-java/src/test/java/com/baeldung/test/comparison/MyParameterisedUnitTestNg.java rename to testng/src/test/java/baeldung/com/ParametrizedTestNGTest.java index 4096c3fb6f..e4f3aacf5b 100644 --- a/core-java/src/test/java/com/baeldung/test/comparison/MyParameterisedUnitTestNg.java +++ b/testng/src/test/java/baeldung/com/ParametrizedTestNGTest.java @@ -1,4 +1,4 @@ -package com.baeldung.test.comparison; +package baeldung.com; import org.testng.Assert; import org.testng.annotations.BeforeClass; @@ -6,29 +6,22 @@ import org.testng.annotations.DataProvider; import org.testng.annotations.Parameters; import org.testng.annotations.Test; -public class MyParameterisedUnitTestNg { +public class ParametrizedTestNGTest { - private PrimeNumberCheck primeNumberChecker; - - @BeforeClass - public void intialSetup() { - primeNumberChecker = new PrimeNumberCheck(); - } - - @Test(enabled = false) - @Parameters({ "num", "expectedResult" }) - public void givenNumber_ifPrime_thenCorrect(int number, boolean expectedResult) { - Assert.assertEquals(expectedResult, primeNumberChecker.validate(number)); + @Test + @Parameters({ "name", "expectedResult" }) + public void givenNumber_ifPrime_thenCorrect(String name, boolean expectedResult) { + Assert.assertEquals(expectedResult, name.length() > 0); } @DataProvider(name = "test1") public static Object[][] primeNumbers() { - return new Object[][] { { 2, true }, { 6, false }, { 19, true }, { 22, false }, { 23, true } }; + return new Object[][] { { "Peter", true }, { "Sam", true }, { "Tim", true }, { "Lucy", true } }; } @Test(dataProvider = "test1") - public void givenNumber_whenPrime_thenCorrect(Integer inputNumber, Boolean expectedResult) { - Assert.assertEquals(expectedResult, primeNumberChecker.validate(inputNumber)); + public void givenNumber_whenPrime_thenCorrect(String name, boolean expectedResult) { + Assert.assertEquals(expectedResult, name.length() > 0); } @Test(dataProvider = "myDataProvider") @@ -47,17 +40,6 @@ public class MyParameterisedUnitTestNg { } -class PrimeNumberCheck { - - public Object validate(int number) { - for (int i = 2; i < number; i++) { - if (number % i == 0) - return false; - } - return true; - } - -} class User { private String name; diff --git a/core-java/src/test/java/com/baeldung/test/comparison/RegistrationTest.java b/testng/src/test/java/baeldung/com/RegistrationTest.java similarity index 78% rename from core-java/src/test/java/com/baeldung/test/comparison/RegistrationTest.java rename to testng/src/test/java/baeldung/com/RegistrationTest.java index 86836425a9..7cb17c9cb8 100644 --- a/core-java/src/test/java/com/baeldung/test/comparison/RegistrationTest.java +++ b/testng/src/test/java/baeldung/com/RegistrationTest.java @@ -1,4 +1,4 @@ -package com.baeldung.test.comparison; +package baeldung.com; import org.testng.annotations.Test; diff --git a/core-java/src/test/java/com/baeldung/test/comparison/SignInTest.java b/testng/src/test/java/baeldung/com/SignInTest.java similarity index 78% rename from core-java/src/test/java/com/baeldung/test/comparison/SignInTest.java rename to testng/src/test/java/baeldung/com/SignInTest.java index 9669f60b6b..4884154345 100644 --- a/core-java/src/test/java/com/baeldung/test/comparison/SignInTest.java +++ b/testng/src/test/java/baeldung/com/SignInTest.java @@ -1,4 +1,4 @@ -package com.baeldung.test.comparison; +package baeldung.com; import org.testng.annotations.Test; diff --git a/core-java/src/test/java/com/baeldung/test/comparison/SummationServiceTestTestNg.java b/testng/src/test/java/baeldung/com/SummationServiceTest.java similarity index 79% rename from core-java/src/test/java/com/baeldung/test/comparison/SummationServiceTestTestNg.java rename to testng/src/test/java/baeldung/com/SummationServiceTest.java index fb02f28d06..471d7a7c8a 100644 --- a/core-java/src/test/java/com/baeldung/test/comparison/SummationServiceTestTestNg.java +++ b/testng/src/test/java/baeldung/com/SummationServiceTest.java @@ -1,19 +1,13 @@ -package com.baeldung.test.comparison; +package baeldung.com; + +import org.testng.Assert; +import org.testng.TestNG; +import org.testng.annotations.*; import java.util.ArrayList; import java.util.List; -import org.testng.Assert; -import org.testng.TestNG; -import org.testng.annotations.AfterClass; -import org.testng.annotations.AfterGroups; -import org.testng.annotations.AfterMethod; -import org.testng.annotations.BeforeClass; -import org.testng.annotations.BeforeGroups; -import org.testng.annotations.BeforeMethod; -import org.testng.annotations.Test; - -public class SummationServiceTestTestNg extends TestNG { +public class SummationServiceTest extends TestNG { private List numbers; @@ -29,14 +23,16 @@ public class SummationServiceTestTestNg extends TestNG { numbers = null; } - @BeforeMethod - public void runBeforeEachTest() { - testCount++; + @BeforeSuite(groups = "regression") + public void runBeforeRegressionSuite() { + numbers = new ArrayList<>(); + numbers.add(-11); + numbers.add(2); } - @AfterMethod - public void runAfterEachTest() { - + @AfterSuite(groups = "regression") + public void runAfterRegressionSuite() { + numbers = null; } @BeforeGroups("negative_tests") @@ -62,6 +58,17 @@ public class SummationServiceTestTestNg extends TestNG { numbers.clear(); } + @BeforeMethod + public void runBeforeEachTest() { + testCount++; + } + + @AfterMethod + public void runAfterEachTest() { + + } + + @Test(groups = "positive_tests", enabled = false) public void givenNumbers_sumEquals_thenCorrect() { int sum = numbers.stream().reduce(0, Integer::sum); @@ -78,12 +85,6 @@ public class SummationServiceTestTestNg extends TestNG { public void givenNegativeNumber_sumLessthanZero_thenCorrect() { int sum = numbers.stream().reduce(0, Integer::sum); Assert.assertTrue(sum < 0); - ; - } - - @Test(groups = "sanity") - public void givenNumbers_doSum() { - } @Test(expectedExceptions = ArithmeticException.class) diff --git a/testng/src/test/java/baeldung/com/TestGroup.java b/testng/src/test/java/baeldung/com/TestGroup.java new file mode 100644 index 0000000000..08bb5c996e --- /dev/null +++ b/testng/src/test/java/baeldung/com/TestGroup.java @@ -0,0 +1,44 @@ +package baeldung.com; + +import org.testng.annotations.AfterGroups; +import org.testng.annotations.BeforeGroups; +import org.testng.annotations.Test; + +public class TestGroup { + + @BeforeGroups("database") + public void setupDB() { + System.out.println("setupDB()"); + } + + @AfterGroups("database") + public void cleanDB() { + System.out.println("cleanDB()"); + } + + @Test(groups= "selenium-test") + public void runSelenium() { + System.out.println("runSelenium()"); + } + + @Test(groups= "selenium-test") + public void runSelenium1() { + System.out.println("runSelenium()1"); + } + + @Test(groups = "database") + public void testConnectOracle() { + System.out.println("testConnectOracle()"); + } + + @Test(groups = "database") + public void testConnectMsSQL() { + System.out.println("testConnectMsSQL"); + } + + @Test(dependsOnGroups = {"database","selenium-test"}) + public void runFinal() { + System.out.println("runFinal"); + } + +} \ No newline at end of file diff --git a/core-java/src/test/java/com/baeldung/test/comparison/TimeOutTest.java b/testng/src/test/java/baeldung/com/TimeOutTest.java similarity index 70% rename from core-java/src/test/java/com/baeldung/test/comparison/TimeOutTest.java rename to testng/src/test/java/baeldung/com/TimeOutTest.java index 6e06132009..d54a914e08 100644 --- a/core-java/src/test/java/com/baeldung/test/comparison/TimeOutTest.java +++ b/testng/src/test/java/baeldung/com/TimeOutTest.java @@ -1,11 +1,11 @@ -package com.baeldung.test.comparison; +package baeldung.com; import org.testng.annotations.Test; public class TimeOutTest { + @Test(timeOut = 1000, enabled = false) public void givenExecution_takeMoreTime_thenFail() { - while (true) - ; + while (true) ; } } diff --git a/testng/src/test/resources/logback.xml b/testng/src/test/resources/logback.xml new file mode 100644 index 0000000000..e9ae1894a6 --- /dev/null +++ b/testng/src/test/resources/logback.xml @@ -0,0 +1,14 @@ + + + + + web - %date [%thread] %-5level %logger{36} - %message%n + + + + + + + + + \ No newline at end of file diff --git a/testng/src/test/resources/parameterised_test.xml b/testng/src/test/resources/parameterised_test.xml new file mode 100644 index 0000000000..2381ba67d0 --- /dev/null +++ b/testng/src/test/resources/parameterised_test.xml @@ -0,0 +1,17 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/testng/src/test/resources/test_group.xml b/testng/src/test/resources/test_group.xml new file mode 100644 index 0000000000..d636a4174d --- /dev/null +++ b/testng/src/test/resources/test_group.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/testng/src/test/resources/test_suite.xml b/testng/src/test/resources/test_suite.xml new file mode 100644 index 0000000000..201ed3f645 --- /dev/null +++ b/testng/src/test/resources/test_suite.xml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file From 2ee0a8a7a13145df12259429012f9c57b5509509 Mon Sep 17 00:00:00 2001 From: tschiman Date: Sat, 11 Feb 2017 19:52:16 +0100 Subject: [PATCH 15/68] BAEL-684 Adding spring JPA to resource servers --- .../gateway/GatewayApplicationLiveTest.java | 69 ------ .../bootstrap/gateway/IntegrationTest.java | 201 ++++++++++++++++++ .../spring-cloud-bootstrap/svc-book/pom.xml | 11 + .../svcbook/BookServiceApplication.java | 24 --- .../bootstrap/svcbook/SecurityConfig.java | 8 +- .../bootstrap/svcbook/{ => book}/Book.java | 19 +- .../svcbook/book/BookController.java | 40 ++++ .../svcbook/book/BookNotFoundException.java | 11 + .../svcbook/book/BookRepository.java | 6 + .../bootstrap/svcbook/book/BookService.java | 55 +++++ .../spring-cloud-bootstrap/svc-rating/pom.xml | 11 + .../svcrating/RatingServiceApplication.java | 28 --- .../bootstrap/svcrating/SecurityConfig.java | 8 +- .../svcrating/{ => rating}/Rating.java | 14 +- .../svcrating/rating/RatingController.java | 38 ++++ .../rating/RatingNotFoundException.java | 11 + .../svcrating/rating/RatingRepository.java | 9 + .../svcrating/rating/RatingService.java | 57 +++++ 18 files changed, 487 insertions(+), 133 deletions(-) delete mode 100644 spring-cloud/spring-cloud-bootstrap/gateway/src/test/java/com/baeldung/spring/cloud/bootstrap/gateway/GatewayApplicationLiveTest.java create mode 100644 spring-cloud/spring-cloud-bootstrap/gateway/src/test/java/com/baeldung/spring/cloud/bootstrap/gateway/IntegrationTest.java rename spring-cloud/spring-cloud-bootstrap/svc-book/src/main/java/com/baeldung/spring/cloud/bootstrap/svcbook/{ => book}/Book.java (57%) create mode 100644 spring-cloud/spring-cloud-bootstrap/svc-book/src/main/java/com/baeldung/spring/cloud/bootstrap/svcbook/book/BookController.java create mode 100644 spring-cloud/spring-cloud-bootstrap/svc-book/src/main/java/com/baeldung/spring/cloud/bootstrap/svcbook/book/BookNotFoundException.java create mode 100644 spring-cloud/spring-cloud-bootstrap/svc-book/src/main/java/com/baeldung/spring/cloud/bootstrap/svcbook/book/BookRepository.java create mode 100644 spring-cloud/spring-cloud-bootstrap/svc-book/src/main/java/com/baeldung/spring/cloud/bootstrap/svcbook/book/BookService.java rename spring-cloud/spring-cloud-bootstrap/svc-rating/src/main/java/com/baeldung/spring/cloud/bootstrap/svcrating/{ => rating}/Rating.java (62%) create mode 100644 spring-cloud/spring-cloud-bootstrap/svc-rating/src/main/java/com/baeldung/spring/cloud/bootstrap/svcrating/rating/RatingController.java create mode 100644 spring-cloud/spring-cloud-bootstrap/svc-rating/src/main/java/com/baeldung/spring/cloud/bootstrap/svcrating/rating/RatingNotFoundException.java create mode 100644 spring-cloud/spring-cloud-bootstrap/svc-rating/src/main/java/com/baeldung/spring/cloud/bootstrap/svcrating/rating/RatingRepository.java create mode 100644 spring-cloud/spring-cloud-bootstrap/svc-rating/src/main/java/com/baeldung/spring/cloud/bootstrap/svcrating/rating/RatingService.java diff --git a/spring-cloud/spring-cloud-bootstrap/gateway/src/test/java/com/baeldung/spring/cloud/bootstrap/gateway/GatewayApplicationLiveTest.java b/spring-cloud/spring-cloud-bootstrap/gateway/src/test/java/com/baeldung/spring/cloud/bootstrap/gateway/GatewayApplicationLiveTest.java deleted file mode 100644 index aa39232bb2..0000000000 --- a/spring-cloud/spring-cloud-bootstrap/gateway/src/test/java/com/baeldung/spring/cloud/bootstrap/gateway/GatewayApplicationLiveTest.java +++ /dev/null @@ -1,69 +0,0 @@ -package com.baeldung.spring.cloud.bootstrap.gateway; - -import org.junit.Assert; -import org.junit.Test; -import org.springframework.boot.test.web.client.TestRestTemplate; -import org.springframework.http.*; -import org.springframework.util.LinkedMultiValueMap; -import org.springframework.util.MultiValueMap; - -public class GatewayApplicationLiveTest { - - @Test - public void testAccess() throws Exception { - TestRestTemplate testRestTemplate = new TestRestTemplate(); - String testUrl = "http://localhost:8080"; - - ResponseEntity response = testRestTemplate.getForEntity(testUrl + "/book-service/books", String.class); - Assert.assertEquals(HttpStatus.OK, response.getStatusCode()); - Assert.assertNotNull(response.getBody()); - - //try the protected resource and confirm the redirect to login - response = testRestTemplate.getForEntity(testUrl + "/book-service/books/1", String.class); - Assert.assertEquals(HttpStatus.FOUND, response.getStatusCode()); - Assert.assertEquals("http://localhost:8080/login", response.getHeaders().get("Location").get(0)); - - //login as user/password - MultiValueMap form = new LinkedMultiValueMap<>(); - form.add("username", "user"); - form.add("password", "password"); - response = testRestTemplate.postForEntity(testUrl + "/login", form, String.class); - - //extract the session from the cookie and propagate it to the next request - String sessionCookie = response.getHeaders().get("Set-Cookie").get(0).split(";")[0]; - HttpHeaders headers = new HttpHeaders(); - headers.add("Cookie", sessionCookie); - HttpEntity httpEntity = new HttpEntity<>(headers); - - //request the protected resource - response = testRestTemplate.exchange(testUrl + "/book-service/books/1", HttpMethod.GET, httpEntity, String.class); - Assert.assertEquals(HttpStatus.OK, response.getStatusCode()); - Assert.assertNotNull(response.getBody()); - - //request the admin protected resource to determine it is still protected - response = testRestTemplate.exchange(testUrl + "/rating-service/ratings/all", HttpMethod.GET, httpEntity, String.class); - Assert.assertEquals(HttpStatus.FORBIDDEN, response.getStatusCode()); - - //login as the admin - form.clear(); - form.add("username", "admin"); - form.add("password", "admin"); - response = testRestTemplate.postForEntity(testUrl + "/login", form, String.class); - - //extract the session from the cookie and propagate it to the next request - sessionCookie = response.getHeaders().get("Set-Cookie").get(0).split(";")[0]; - headers = new HttpHeaders(); - headers.add("Cookie", sessionCookie); - httpEntity = new HttpEntity<>(headers); - - //request the protected resource - response = testRestTemplate.exchange(testUrl + "/rating-service/ratings/all", HttpMethod.GET, httpEntity, String.class); - Assert.assertEquals(HttpStatus.OK, response.getStatusCode()); - Assert.assertNotNull(response.getBody()); - - //request the discovery resources as the admin - response = testRestTemplate.exchange(testUrl + "/discovery", HttpMethod.GET, httpEntity, String.class); - Assert.assertEquals(HttpStatus.OK, response.getStatusCode()); - } - -} \ No newline at end of file diff --git a/spring-cloud/spring-cloud-bootstrap/gateway/src/test/java/com/baeldung/spring/cloud/bootstrap/gateway/IntegrationTest.java b/spring-cloud/spring-cloud-bootstrap/gateway/src/test/java/com/baeldung/spring/cloud/bootstrap/gateway/IntegrationTest.java new file mode 100644 index 0000000000..16057edc48 --- /dev/null +++ b/spring-cloud/spring-cloud-bootstrap/gateway/src/test/java/com/baeldung/spring/cloud/bootstrap/gateway/IntegrationTest.java @@ -0,0 +1,201 @@ +package com.baeldung.spring.cloud.bootstrap.gateway; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import org.apache.http.entity.ContentType; +import org.junit.Assert; +import org.junit.Test; +import org.springframework.boot.test.web.client.TestRestTemplate; +import org.springframework.http.*; +import org.springframework.util.LinkedMultiValueMap; +import org.springframework.util.MultiValueMap; + +public class IntegrationTest { + + private TestRestTemplate testRestTemplate = new TestRestTemplate(); + private String testUrl = "http://localhost:8080"; + + @Test + public void testAccess() throws Exception { + ResponseEntity response = testRestTemplate.getForEntity(testUrl + "/book-service/books", String.class); + Assert.assertEquals(HttpStatus.OK, response.getStatusCode()); + Assert.assertNotNull(response.getBody()); + + //try the protected resource and confirm the redirect to login + response = testRestTemplate.getForEntity(testUrl + "/book-service/books/1", String.class); + Assert.assertEquals(HttpStatus.FOUND, response.getStatusCode()); + Assert.assertEquals("http://localhost:8080/login", response.getHeaders().get("Location").get(0)); + + //login as user/password + MultiValueMap form = new LinkedMultiValueMap<>(); + form.add("username", "user"); + form.add("password", "password"); + response = testRestTemplate.postForEntity(testUrl + "/login", form, String.class); + + //extract the session from the cookie and propagate it to the next request + String sessionCookie = response.getHeaders().get("Set-Cookie").get(0).split(";")[0]; + HttpHeaders headers = new HttpHeaders(); + headers.add("Cookie", sessionCookie); + HttpEntity httpEntity = new HttpEntity<>(headers); + + addBook(); + + //request the protected resource + response = testRestTemplate.exchange(testUrl + "/book-service/books/1", HttpMethod.GET, httpEntity, String.class); + Assert.assertEquals(HttpStatus.OK, response.getStatusCode()); + Assert.assertNotNull(response.getBody()); + + addRatings(); + + //request the admin protected resource to determine it is still protected + response = testRestTemplate.exchange(testUrl + "/rating-service/ratings", HttpMethod.GET, httpEntity, String.class); + Assert.assertEquals(HttpStatus.FORBIDDEN, response.getStatusCode()); + + //login as the admin + form.clear(); + form.add("username", "admin"); + form.add("password", "admin"); + response = testRestTemplate.postForEntity(testUrl + "/login", form, String.class); + + //extract the session from the cookie and propagate it to the next request + sessionCookie = response.getHeaders().get("Set-Cookie").get(0).split(";")[0]; + headers = new HttpHeaders(); + headers.add("Cookie", sessionCookie); + httpEntity = new HttpEntity<>(headers); + + //request the protected resource + response = testRestTemplate.exchange(testUrl + "/rating-service/ratings", HttpMethod.GET, httpEntity, String.class); + Assert.assertEquals(HttpStatus.OK, response.getStatusCode()); + Assert.assertNotNull(response.getBody()); + + //request the discovery resources as the admin + response = testRestTemplate.exchange(testUrl + "/discovery", HttpMethod.GET, httpEntity, String.class); + Assert.assertEquals(HttpStatus.OK, response.getStatusCode()); + } + + private void addRatings() { + //login as user/password + MultiValueMap form = new LinkedMultiValueMap<>(); + form.add("username", "user"); + form.add("password", "password"); + ResponseEntity response = testRestTemplate.postForEntity(testUrl + "/login", form, String.class); + + //extract the session from the cookie and propagate it to the next request + String sessionCookie = response.getHeaders().get("Set-Cookie").get(0).split(";")[0]; + HttpHeaders headers = new HttpHeaders(); + headers.add("Cookie", sessionCookie); + headers.add("ContentType", ContentType.APPLICATION_JSON.getMimeType()); + Rating rating = new Rating(1L, 4); + + HttpEntity httpEntity = new HttpEntity<>(rating, headers); + + //request the protected resource + ResponseEntity bookResponse = testRestTemplate.postForEntity(testUrl + "/rating-service/ratings", httpEntity, Rating.class); + Assert.assertEquals(HttpStatus.OK, bookResponse.getStatusCode()); + Assert.assertEquals(rating.getBookId(), bookResponse.getBody().getBookId()); + Assert.assertEquals(rating.getStars(), bookResponse.getBody().getStars()); + } + + private void addBook(){ + //login as user/password + MultiValueMap form = new LinkedMultiValueMap<>(); + form.add("username", "admin"); + form.add("password", "admin"); + ResponseEntity response = testRestTemplate.postForEntity(testUrl + "/login", form, String.class); + + //extract the session from the cookie and propagate it to the next request + String sessionCookie = response.getHeaders().get("Set-Cookie").get(0).split(";")[0]; + HttpHeaders headers = new HttpHeaders(); + headers.add("Cookie", sessionCookie); + headers.add("ContentType", ContentType.APPLICATION_JSON.getMimeType()); + Book book = new Book("Baeldung", "How to spring cloud"); + + HttpEntity httpEntity = new HttpEntity<>(book, headers); + + //request the protected resource + ResponseEntity bookResponse = testRestTemplate.postForEntity(testUrl + "/book-service/books", httpEntity, Book.class); + Assert.assertEquals(HttpStatus.OK, bookResponse.getStatusCode()); + Assert.assertEquals(book.getAuthor(), bookResponse.getBody().getAuthor()); + Assert.assertEquals(book.getTitle(), bookResponse.getBody().getTitle()); + } + + @JsonIgnoreProperties(ignoreUnknown = true) + public static class Book { + + private Long id; + private String author; + private String title; + + public Book() { + } + + public Book(String author, String title) { + this.author = author; + this.title = title; + } + + public String getAuthor() { + return author; + } + + public void setAuthor(String author) { + this.author = author; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + } + + @JsonIgnoreProperties(ignoreUnknown = true) + public static class Rating { + private Long id; + private Long bookId; + private int stars; + + public Rating() { + } + + public Rating(Long bookId, int stars) { + this.bookId = bookId; + this.stars = stars; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public Long getBookId() { + return bookId; + } + + public void setBookId(Long bookId) { + this.bookId = bookId; + } + + public int getStars() { + return stars; + } + + public void setStars(int stars) { + this.stars = stars; + } + } + + +} \ No newline at end of file diff --git a/spring-cloud/spring-cloud-bootstrap/svc-book/pom.xml b/spring-cloud/spring-cloud-bootstrap/svc-book/pom.xml index 9a99054ed5..c351c444f6 100644 --- a/spring-cloud/spring-cloud-bootstrap/svc-book/pom.xml +++ b/spring-cloud/spring-cloud-bootstrap/svc-book/pom.xml @@ -42,6 +42,17 @@ spring-boot-starter-data-redis + + org.springframework.boot + spring-boot-starter-data-jpa + + + + com.h2database + h2 + runtime + + org.springframework.boot spring-boot-starter-test diff --git a/spring-cloud/spring-cloud-bootstrap/svc-book/src/main/java/com/baeldung/spring/cloud/bootstrap/svcbook/BookServiceApplication.java b/spring-cloud/spring-cloud-bootstrap/svc-book/src/main/java/com/baeldung/spring/cloud/bootstrap/svcbook/BookServiceApplication.java index 25ad2a83b2..c5499cd924 100644 --- a/spring-cloud/spring-cloud-bootstrap/svc-book/src/main/java/com/baeldung/spring/cloud/bootstrap/svcbook/BookServiceApplication.java +++ b/spring-cloud/spring-cloud-bootstrap/svc-book/src/main/java/com/baeldung/spring/cloud/bootstrap/svcbook/BookServiceApplication.java @@ -3,35 +3,11 @@ package com.baeldung.spring.cloud.bootstrap.svcbook; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; - -import java.util.Arrays; -import java.util.List; @SpringBootApplication @EnableEurekaClient -@RestController -@RequestMapping("/books") public class BookServiceApplication { public static void main(String[] args) { SpringApplication.run(BookServiceApplication.class, args); } - - private List bookList = Arrays.asList( - new Book(1L, "Baeldung goes to the market", "Tim Schimandle"), - new Book(2L, "Baeldung goes to the park", "Slavisa") - ); - - @GetMapping("") - public List findAllBooks() { - return bookList; - } - - @GetMapping("/{bookId}") - public Book findBook(@PathVariable Long bookId) { - return bookList.stream().filter(b -> b.getId().equals(bookId)).findFirst().orElse(null); - } } diff --git a/spring-cloud/spring-cloud-bootstrap/svc-book/src/main/java/com/baeldung/spring/cloud/bootstrap/svcbook/SecurityConfig.java b/spring-cloud/spring-cloud-bootstrap/svc-book/src/main/java/com/baeldung/spring/cloud/bootstrap/svcbook/SecurityConfig.java index 300b4d7c5a..6aa996c575 100644 --- a/spring-cloud/spring-cloud-bootstrap/svc-book/src/main/java/com/baeldung/spring/cloud/bootstrap/svcbook/SecurityConfig.java +++ b/spring-cloud/spring-cloud-bootstrap/svc-book/src/main/java/com/baeldung/spring/cloud/bootstrap/svcbook/SecurityConfig.java @@ -2,6 +2,7 @@ package com.baeldung.spring.cloud.bootstrap.svcbook; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; +import org.springframework.http.HttpMethod; import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; @@ -22,8 +23,11 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter { http.httpBasic() .disable() .authorizeRequests() - .antMatchers("/books").permitAll() - .antMatchers("/books/*").hasAnyRole("USER", "ADMIN") + .antMatchers(HttpMethod.GET, "/books").permitAll() + .antMatchers(HttpMethod.GET, "/books/*").permitAll() + .antMatchers(HttpMethod.POST, "/books").hasRole("ADMIN") + .antMatchers(HttpMethod.PATCH, "/books/*").hasRole("ADMIN") + .antMatchers(HttpMethod.DELETE, "/books/*").hasRole("ADMIN") .anyRequest().authenticated() .and() .csrf() diff --git a/spring-cloud/spring-cloud-bootstrap/svc-book/src/main/java/com/baeldung/spring/cloud/bootstrap/svcbook/Book.java b/spring-cloud/spring-cloud-bootstrap/svc-book/src/main/java/com/baeldung/spring/cloud/bootstrap/svcbook/book/Book.java similarity index 57% rename from spring-cloud/spring-cloud-bootstrap/svc-book/src/main/java/com/baeldung/spring/cloud/bootstrap/svcbook/Book.java rename to spring-cloud/spring-cloud-bootstrap/svc-book/src/main/java/com/baeldung/spring/cloud/bootstrap/svcbook/book/Book.java index e652437454..33ea8dcb81 100644 --- a/spring-cloud/spring-cloud-bootstrap/svc-book/src/main/java/com/baeldung/spring/cloud/bootstrap/svcbook/Book.java +++ b/spring-cloud/spring-cloud-bootstrap/svc-book/src/main/java/com/baeldung/spring/cloud/bootstrap/svcbook/book/Book.java @@ -1,16 +1,21 @@ -package com.baeldung.spring.cloud.bootstrap.svcbook; +package com.baeldung.spring.cloud.bootstrap.svcbook.book; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; + +@Entity +@JsonIgnoreProperties(ignoreUnknown = true) public class Book { + @Id + @GeneratedValue(strategy = GenerationType.AUTO) private Long id; private String author; private String title; - public Book(Long id, String title, String author) { - this.id = id; - this.author = author; - this.title = title; - } - public Book() { } diff --git a/spring-cloud/spring-cloud-bootstrap/svc-book/src/main/java/com/baeldung/spring/cloud/bootstrap/svcbook/book/BookController.java b/spring-cloud/spring-cloud-bootstrap/svc-book/src/main/java/com/baeldung/spring/cloud/bootstrap/svcbook/book/BookController.java new file mode 100644 index 0000000000..d00f114b8c --- /dev/null +++ b/spring-cloud/spring-cloud-bootstrap/svc-book/src/main/java/com/baeldung/spring/cloud/bootstrap/svcbook/book/BookController.java @@ -0,0 +1,40 @@ +package com.baeldung.spring.cloud.bootstrap.svcbook.book; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.*; + +import java.util.List; +import java.util.Map; + +@RestController +@RequestMapping("/books") +public class BookController { + + @Autowired + private BookService bookService; + + @GetMapping("") + public List findAllBooks() { + return bookService.findAllBooks(); + } + + @GetMapping("/{bookId}") + public Book findBook(@PathVariable Long bookId) { + return bookService.findBookById(bookId); + } + + @PostMapping("") + public Book createBook(@RequestBody Book book) { + return bookService.createBook(book); + } + + @DeleteMapping("/{bookId}") + public void deleteBook(@PathVariable Long bookId) { + bookService.deleteBook(bookId); + } + + @PatchMapping("/{bookId") + public Book updateBook(@RequestBody Map updates, @PathVariable Long bookId) { + return bookService.updateBook(updates, bookId); + } +} diff --git a/spring-cloud/spring-cloud-bootstrap/svc-book/src/main/java/com/baeldung/spring/cloud/bootstrap/svcbook/book/BookNotFoundException.java b/spring-cloud/spring-cloud-bootstrap/svc-book/src/main/java/com/baeldung/spring/cloud/bootstrap/svcbook/book/BookNotFoundException.java new file mode 100644 index 0000000000..f0a4797387 --- /dev/null +++ b/spring-cloud/spring-cloud-bootstrap/svc-book/src/main/java/com/baeldung/spring/cloud/bootstrap/svcbook/book/BookNotFoundException.java @@ -0,0 +1,11 @@ +package com.baeldung.spring.cloud.bootstrap.svcbook.book; + +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.ResponseStatus; + +@ResponseStatus(HttpStatus.NOT_FOUND) +class BookNotFoundException extends RuntimeException { + BookNotFoundException(String message) { + super(message); + } +} diff --git a/spring-cloud/spring-cloud-bootstrap/svc-book/src/main/java/com/baeldung/spring/cloud/bootstrap/svcbook/book/BookRepository.java b/spring-cloud/spring-cloud-bootstrap/svc-book/src/main/java/com/baeldung/spring/cloud/bootstrap/svcbook/book/BookRepository.java new file mode 100644 index 0000000000..66fd3880c5 --- /dev/null +++ b/spring-cloud/spring-cloud-bootstrap/svc-book/src/main/java/com/baeldung/spring/cloud/bootstrap/svcbook/book/BookRepository.java @@ -0,0 +1,6 @@ +package com.baeldung.spring.cloud.bootstrap.svcbook.book; + +import org.springframework.data.jpa.repository.JpaRepository; + +interface BookRepository extends JpaRepository{ +} diff --git a/spring-cloud/spring-cloud-bootstrap/svc-book/src/main/java/com/baeldung/spring/cloud/bootstrap/svcbook/book/BookService.java b/spring-cloud/spring-cloud-bootstrap/svc-book/src/main/java/com/baeldung/spring/cloud/bootstrap/svcbook/book/BookService.java new file mode 100644 index 0000000000..cfcbf15757 --- /dev/null +++ b/spring-cloud/spring-cloud-bootstrap/svc-book/src/main/java/com/baeldung/spring/cloud/bootstrap/svcbook/book/BookService.java @@ -0,0 +1,55 @@ +package com.baeldung.spring.cloud.bootstrap.svcbook.book; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Propagation; +import org.springframework.transaction.annotation.Transactional; + +import java.util.List; +import java.util.Map; +import java.util.Optional; + +@Service +@Transactional(readOnly = true) +public class BookService { + + @Autowired + private BookRepository bookRepository; + + public List findAllBooks() { + return bookRepository.findAll(); + } + + public Book findBookById(Long bookId) { + return Optional.ofNullable(bookRepository.findOne(bookId)) + .orElseThrow(() -> new BookNotFoundException("Book not found. ID: " + bookId)); + } + + @Transactional(propagation = Propagation.REQUIRED) + public Book createBook(Book book) { + Book newBook = new Book(); + newBook.setTitle(book.getTitle()); + newBook.setAuthor(book.getAuthor()); + return bookRepository.save(newBook); + } + + @Transactional(propagation = Propagation.REQUIRED) + public void deleteBook(Long bookId) { + bookRepository.delete(bookId); + } + + @Transactional(propagation = Propagation.REQUIRED) + public Book updateBook(Map updates, Long bookId) { + Book book = findBookById(bookId); + updates.keySet().forEach(key -> { + switch (key) { + case "author": + book.setAuthor(updates.get(key)); + break; + case "title": + book.setTitle(updates.get(key)); + } + }); + return bookRepository.save(book); + } +} diff --git a/spring-cloud/spring-cloud-bootstrap/svc-rating/pom.xml b/spring-cloud/spring-cloud-bootstrap/svc-rating/pom.xml index 35da8beba8..2285286812 100644 --- a/spring-cloud/spring-cloud-bootstrap/svc-rating/pom.xml +++ b/spring-cloud/spring-cloud-bootstrap/svc-rating/pom.xml @@ -42,6 +42,17 @@ spring-boot-starter-data-redis + + org.springframework.boot + spring-boot-starter-data-jpa + + + + com.h2database + h2 + runtime + + org.springframework.boot spring-boot-starter-test diff --git a/spring-cloud/spring-cloud-bootstrap/svc-rating/src/main/java/com/baeldung/spring/cloud/bootstrap/svcrating/RatingServiceApplication.java b/spring-cloud/spring-cloud-bootstrap/svc-rating/src/main/java/com/baeldung/spring/cloud/bootstrap/svcrating/RatingServiceApplication.java index 11fb5f06b6..61074e0bcc 100644 --- a/spring-cloud/spring-cloud-bootstrap/svc-rating/src/main/java/com/baeldung/spring/cloud/bootstrap/svcrating/RatingServiceApplication.java +++ b/spring-cloud/spring-cloud-bootstrap/svc-rating/src/main/java/com/baeldung/spring/cloud/bootstrap/svcrating/RatingServiceApplication.java @@ -3,39 +3,11 @@ package com.baeldung.spring.cloud.bootstrap.svcrating; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.bind.annotation.RestController; - -import java.util.Arrays; -import java.util.Collections; -import java.util.List; -import java.util.stream.Collectors; @SpringBootApplication @EnableEurekaClient -@RestController -@RequestMapping("/ratings") public class RatingServiceApplication { public static void main(String[] args) { SpringApplication.run(RatingServiceApplication.class, args); } - - private List ratingList = Arrays.asList( - new Rating(1L, 1L, 2), - new Rating(2L, 1L, 3), - new Rating(3L, 2L, 4), - new Rating(4L, 2L, 5) - ); - - @GetMapping("") - public List findRatingsByBookId(@RequestParam Long bookId) { - return bookId == null || bookId.equals(0L) ? Collections.EMPTY_LIST : ratingList.stream().filter(r -> r.getBookId().equals(bookId)).collect(Collectors.toList()); - } - - @GetMapping("/all") - public List findAllRatings() { - return ratingList; - } } \ No newline at end of file diff --git a/spring-cloud/spring-cloud-bootstrap/svc-rating/src/main/java/com/baeldung/spring/cloud/bootstrap/svcrating/SecurityConfig.java b/spring-cloud/spring-cloud-bootstrap/svc-rating/src/main/java/com/baeldung/spring/cloud/bootstrap/svcrating/SecurityConfig.java index 371dc810d5..171fbba7af 100644 --- a/spring-cloud/spring-cloud-bootstrap/svc-rating/src/main/java/com/baeldung/spring/cloud/bootstrap/svcrating/SecurityConfig.java +++ b/spring-cloud/spring-cloud-bootstrap/svc-rating/src/main/java/com/baeldung/spring/cloud/bootstrap/svcrating/SecurityConfig.java @@ -2,6 +2,7 @@ package com.baeldung.spring.cloud.bootstrap.svcrating; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; +import org.springframework.http.HttpMethod; import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; @@ -22,8 +23,11 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter { http.httpBasic() .disable() .authorizeRequests() - .antMatchers("/ratings").hasRole("USER") - .antMatchers("/ratings/all").hasRole("ADMIN") + .regexMatchers("^/ratings\\?bookId.*$").authenticated() + .antMatchers(HttpMethod.POST,"/ratings").authenticated() + .antMatchers(HttpMethod.PATCH,"/ratings/*").hasRole("ADMIN") + .antMatchers(HttpMethod.DELETE,"/ratings/*").hasRole("ADMIN") + .antMatchers(HttpMethod.GET,"/ratings").hasRole("ADMIN") .anyRequest().authenticated() .and() .csrf() diff --git a/spring-cloud/spring-cloud-bootstrap/svc-rating/src/main/java/com/baeldung/spring/cloud/bootstrap/svcrating/Rating.java b/spring-cloud/spring-cloud-bootstrap/svc-rating/src/main/java/com/baeldung/spring/cloud/bootstrap/svcrating/rating/Rating.java similarity index 62% rename from spring-cloud/spring-cloud-bootstrap/svc-rating/src/main/java/com/baeldung/spring/cloud/bootstrap/svcrating/Rating.java rename to spring-cloud/spring-cloud-bootstrap/svc-rating/src/main/java/com/baeldung/spring/cloud/bootstrap/svcrating/rating/Rating.java index 5dd3572098..ae44f9ae2e 100644 --- a/spring-cloud/spring-cloud-bootstrap/svc-rating/src/main/java/com/baeldung/spring/cloud/bootstrap/svcrating/Rating.java +++ b/spring-cloud/spring-cloud-bootstrap/svc-rating/src/main/java/com/baeldung/spring/cloud/bootstrap/svcrating/rating/Rating.java @@ -1,6 +1,18 @@ -package com.baeldung.spring.cloud.bootstrap.svcrating; +package com.baeldung.spring.cloud.bootstrap.svcrating.rating; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; + +@Entity +@JsonIgnoreProperties(ignoreUnknown = true) public class Rating { + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) private Long id; private Long bookId; private int stars; diff --git a/spring-cloud/spring-cloud-bootstrap/svc-rating/src/main/java/com/baeldung/spring/cloud/bootstrap/svcrating/rating/RatingController.java b/spring-cloud/spring-cloud-bootstrap/svc-rating/src/main/java/com/baeldung/spring/cloud/bootstrap/svcrating/rating/RatingController.java new file mode 100644 index 0000000000..83452ad747 --- /dev/null +++ b/spring-cloud/spring-cloud-bootstrap/svc-rating/src/main/java/com/baeldung/spring/cloud/bootstrap/svcrating/rating/RatingController.java @@ -0,0 +1,38 @@ +package com.baeldung.spring.cloud.bootstrap.svcrating.rating; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.*; + +import java.util.List; +import java.util.Map; + +@RestController +@RequestMapping("/ratings") +public class RatingController { + + @Autowired + private RatingService ratingService; + + @GetMapping("") + public List findRatingsByBookId(@RequestParam(required = false, defaultValue = "0") Long bookId) { + if (bookId.equals(0L)) { + return ratingService.findAllRatings(); + } + return ratingService.findRatingsByBookId(bookId); + } + + @PostMapping("") + public Rating createRating(@RequestBody Rating rating) { + return ratingService.createRating(rating); + } + + @DeleteMapping("/{ratingId}") + public void deleteRating(@PathVariable Long ratingId) { + ratingService.deleteRating(ratingId); + } + + @PatchMapping("/{ratingId") + public Rating updateRating(@RequestBody Map updates, @PathVariable Long ratingId) { + return ratingService.updateRating(updates, ratingId); + } +} diff --git a/spring-cloud/spring-cloud-bootstrap/svc-rating/src/main/java/com/baeldung/spring/cloud/bootstrap/svcrating/rating/RatingNotFoundException.java b/spring-cloud/spring-cloud-bootstrap/svc-rating/src/main/java/com/baeldung/spring/cloud/bootstrap/svcrating/rating/RatingNotFoundException.java new file mode 100644 index 0000000000..473d636a71 --- /dev/null +++ b/spring-cloud/spring-cloud-bootstrap/svc-rating/src/main/java/com/baeldung/spring/cloud/bootstrap/svcrating/rating/RatingNotFoundException.java @@ -0,0 +1,11 @@ +package com.baeldung.spring.cloud.bootstrap.svcrating.rating; + +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.ResponseStatus; + +@ResponseStatus(HttpStatus.NOT_FOUND) +class RatingNotFoundException extends RuntimeException { + RatingNotFoundException(String message) { + super(message); + } +} diff --git a/spring-cloud/spring-cloud-bootstrap/svc-rating/src/main/java/com/baeldung/spring/cloud/bootstrap/svcrating/rating/RatingRepository.java b/spring-cloud/spring-cloud-bootstrap/svc-rating/src/main/java/com/baeldung/spring/cloud/bootstrap/svcrating/rating/RatingRepository.java new file mode 100644 index 0000000000..08d781b5a3 --- /dev/null +++ b/spring-cloud/spring-cloud-bootstrap/svc-rating/src/main/java/com/baeldung/spring/cloud/bootstrap/svcrating/rating/RatingRepository.java @@ -0,0 +1,9 @@ +package com.baeldung.spring.cloud.bootstrap.svcrating.rating; + +import org.springframework.data.jpa.repository.JpaRepository; + +import java.util.List; + +interface RatingRepository extends JpaRepository{ + List findRatingsByBookId(Long bookId); +} diff --git a/spring-cloud/spring-cloud-bootstrap/svc-rating/src/main/java/com/baeldung/spring/cloud/bootstrap/svcrating/rating/RatingService.java b/spring-cloud/spring-cloud-bootstrap/svc-rating/src/main/java/com/baeldung/spring/cloud/bootstrap/svcrating/rating/RatingService.java new file mode 100644 index 0000000000..a2360b7be5 --- /dev/null +++ b/spring-cloud/spring-cloud-bootstrap/svc-rating/src/main/java/com/baeldung/spring/cloud/bootstrap/svcrating/rating/RatingService.java @@ -0,0 +1,57 @@ +package com.baeldung.spring.cloud.bootstrap.svcrating.rating; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Propagation; +import org.springframework.transaction.annotation.Transactional; + +import java.util.List; +import java.util.Map; +import java.util.Optional; + +@Service +@Transactional(readOnly = true) +public class RatingService { + + @Autowired + private RatingRepository ratingRepository; + + public Rating findRatingById(Long ratingId) { + return Optional.ofNullable(ratingRepository.findOne(ratingId)) + .orElseThrow(() -> new RatingNotFoundException("Rating not found. ID: " + ratingId)); + } + + public List findRatingsByBookId(Long bookId) { + return ratingRepository.findRatingsByBookId(bookId); + } + + public List findAllRatings() { + return ratingRepository.findAll(); + } + + @Transactional(propagation = Propagation.REQUIRED) + public Rating createRating(Rating rating) { + Rating newRating = new Rating(); + newRating.setBookId(rating.getBookId()); + newRating.setStars(rating.getStars()); + return ratingRepository.save(newRating); + } + + @Transactional(propagation = Propagation.REQUIRED) + public void deleteRating(Long ratingId) { + ratingRepository.delete(ratingId); + } + + @Transactional(propagation = Propagation.REQUIRED) + public Rating updateRating(Map updates, Long ratingId) { + Rating rating = findRatingById(ratingId); + updates.keySet().forEach(key -> { + switch (key) { + case "stars": + rating.setStars(Integer.parseInt(updates.get(key))); + break; + } + }); + return ratingRepository.save(rating); + } +} From f800e9dc4f32f118117c06362a61332e274cdf1e Mon Sep 17 00:00:00 2001 From: slavisa-baeldung Date: Sat, 18 Feb 2017 07:47:36 +0100 Subject: [PATCH 16/68] BAEL-684 - suffix testname to match surefire configuration --- .../gateway/{IntegrationTest.java => IntegrationLiveTest.java} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename spring-cloud/spring-cloud-bootstrap/gateway/src/test/java/com/baeldung/spring/cloud/bootstrap/gateway/{IntegrationTest.java => IntegrationLiveTest.java} (99%) diff --git a/spring-cloud/spring-cloud-bootstrap/gateway/src/test/java/com/baeldung/spring/cloud/bootstrap/gateway/IntegrationTest.java b/spring-cloud/spring-cloud-bootstrap/gateway/src/test/java/com/baeldung/spring/cloud/bootstrap/gateway/IntegrationLiveTest.java similarity index 99% rename from spring-cloud/spring-cloud-bootstrap/gateway/src/test/java/com/baeldung/spring/cloud/bootstrap/gateway/IntegrationTest.java rename to spring-cloud/spring-cloud-bootstrap/gateway/src/test/java/com/baeldung/spring/cloud/bootstrap/gateway/IntegrationLiveTest.java index 16057edc48..47a4b744eb 100644 --- a/spring-cloud/spring-cloud-bootstrap/gateway/src/test/java/com/baeldung/spring/cloud/bootstrap/gateway/IntegrationTest.java +++ b/spring-cloud/spring-cloud-bootstrap/gateway/src/test/java/com/baeldung/spring/cloud/bootstrap/gateway/IntegrationLiveTest.java @@ -9,7 +9,7 @@ import org.springframework.http.*; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; -public class IntegrationTest { +public class IntegrationLiveTest { private TestRestTemplate testRestTemplate = new TestRestTemplate(); private String testUrl = "http://localhost:8080"; From 2bf21cbff37fb78efad08fea90a9d26ecbe141ca Mon Sep 17 00:00:00 2001 From: pivovarit Date: Sat, 18 Feb 2017 08:31:42 +0100 Subject: [PATCH 17/68] Refactor ExcelPOIHelper to Stream API --- .../com/baeldung/excel/ExcelPOIHelper.java | 31 +++++++++---------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/spring-mvc-java/src/main/java/com/baeldung/excel/ExcelPOIHelper.java b/spring-mvc-java/src/main/java/com/baeldung/excel/ExcelPOIHelper.java index e3658e8f89..0519a8f3c7 100644 --- a/spring-mvc-java/src/main/java/com/baeldung/excel/ExcelPOIHelper.java +++ b/spring-mvc-java/src/main/java/com/baeldung/excel/ExcelPOIHelper.java @@ -24,6 +24,7 @@ import java.util.Map; import java.util.HashMap; import java.util.ArrayList; import java.util.List; +import java.util.stream.IntStream; public class ExcelPOIHelper { @@ -38,27 +39,23 @@ public class ExcelPOIHelper { data = readXSSFWorkbook(fis); } - int maxNrCols = 0; + int maxNrCols = data.values().stream() + .mapToInt(List::size) + .max() + .orElse(0); - for (List ls : data.values()) { - if (ls.size() > maxNrCols) { - maxNrCols = ls.size(); - } - } - - for (List ls : data.values()) { - if (ls.size() < maxNrCols) { - for (int i = ls.size(); i < maxNrCols; i++) { - ls.add(new MyCell("")); - } - } - } + data.values().stream() + .filter(ls -> ls.size() < maxNrCols) + .forEach(ls -> { + IntStream.range(ls.size(), maxNrCols) + .forEach(i -> ls.add(new MyCell(""))); + }); return data; } private String readCellContent(Cell cell) { - String content = ""; + String content; switch (cell.getCellTypeEnum()) { case STRING: content = cell.getStringCellValue(); @@ -91,7 +88,7 @@ public class ExcelPOIHelper { HSSFSheet sheet = workbook.getSheetAt(0); for (int i = sheet.getFirstRowNum(); i <= sheet.getLastRowNum(); i++) { HSSFRow row = sheet.getRow(i); - data.put(i, new ArrayList()); + data.put(i, new ArrayList<>()); if (row != null) { for (int j = 0; j < row.getLastCellNum(); j++) { HSSFCell cell = row.getCell(j); @@ -144,7 +141,7 @@ public class ExcelPOIHelper { for (int i = sheet.getFirstRowNum(); i <= sheet.getLastRowNum(); i++) { XSSFRow row = sheet.getRow(i); - data.put(i, new ArrayList()); + data.put(i, new ArrayList<>()); if (row != null) { for (int j = 0; j < row.getLastCellNum(); j++) { XSSFCell cell = row.getCell(j); From 8d4b69671a164f224c1bb7593973a9eca1b2e19c Mon Sep 17 00:00:00 2001 From: Tomasz Lelek Date: Sat, 18 Feb 2017 13:59:08 +0100 Subject: [PATCH 18/68] BAEL-602 more examples of JOOL --- jooq/src/test/java/com/baeldung/JOOLTest.java | 122 ++++++++++++++++++ 1 file changed, 122 insertions(+) diff --git a/jooq/src/test/java/com/baeldung/JOOLTest.java b/jooq/src/test/java/com/baeldung/JOOLTest.java index aa729a3edf..4f5cb3741a 100644 --- a/jooq/src/test/java/com/baeldung/JOOLTest.java +++ b/jooq/src/test/java/com/baeldung/JOOLTest.java @@ -5,10 +5,13 @@ import org.jooq.lambda.Seq; import org.junit.Test; import java.util.Arrays; +import java.util.HashMap; import java.util.List; +import java.util.Map; import static junit.framework.Assert.assertTrue; import static junit.framework.TestCase.assertEquals; +import static org.jooq.lambda.tuple.Tuple.tuple; public class JOOLTest { @Test @@ -26,4 +29,123 @@ public class JOOLTest { assertTrue(Seq.of(1, 2, 3, 4).containsAny(2, 5)); } + + @Test + public void givenSeq_whenJoin_shouldHaveElementsFromBothSeq() { + assertEquals( + Seq.of(1, 2).crossJoin(Seq.of("A", "B")).toList(), + Arrays.asList(tuple(1, "A"), tuple(1, "B"), tuple(2, "A"), tuple(2, "B")) + ); + + + assertEquals( + Seq.of(1, 2, 4).innerJoin(Seq.of(1, 2, 3), (a, b) -> a == b).toList(), + Arrays.asList(tuple(1, 1), tuple(2, 2)) + ); + + + assertEquals( + Seq.of(1, 2, 4).leftOuterJoin(Seq.of(1, 2, 3), (a, b) -> a == b).toList(), + Arrays.asList(tuple(1, 1), tuple(2, 2), tuple(4, null)) + ); + + assertEquals( + Seq.of(1, 2, 4).rightOuterJoin(Seq.of(1, 2, 3), (a, b) -> a == b).toList(), + Arrays.asList(tuple(1, 1), tuple(2, 2), tuple(null, 3)) + ); + } + + @Test + public void givenSeq_whenManipulateSeq_seqShouldHaveNewElementsInIt() { + assertEquals( + Seq.of(1, 2, 3).cycle().limit(9).toList(), + Arrays.asList(1, 2, 3, 1, 2, 3, 1, 2, 3) + ); + + assertEquals( + Seq.of(1, 2, 3).duplicate().map((first, second) -> tuple(first.toList(), second.toList())), + tuple(Arrays.asList(1, 2, 3), Arrays.asList(1, 2, 3)) + ); + + assertEquals( + Seq.of(1, 2, 3, 4).intersperse(0).toList(), + Arrays.asList(1, 0, 2, 0, 3, 0, 4) + ); + + assertEquals( + Seq.of(1, 2, 3, 4, 5).shuffle().toList().size(), + 5 + ); + + assertEquals( + Seq.of(1, 2, 3, 4).partition(i -> i % 2 != 0).map((first, second) -> tuple(first.toList(), second.toList())), + tuple(Arrays.asList(1, 3), Arrays.asList(2, 4)) + + ); + + assertEquals( + Seq.of(1, 2, 3, 4).reverse().toList(), + Arrays.asList(4, 3, 2, 1) + ); + } + + @Test + public void givenSeq_whenGroupByAndFold_shouldReturnProperSeq() { + + Map> expectedAfterGroupBy = new HashMap<>(); + expectedAfterGroupBy.put(1, Arrays.asList(1, 3)); + expectedAfterGroupBy.put(0, Arrays.asList(2, 4)); + + assertEquals( + Seq.of(1, 2, 3, 4).groupBy(i -> i % 2), + expectedAfterGroupBy + ); + + + assertEquals( + Seq.of("a", "b", "c").foldLeft("!", (u, t) -> u + t), + "!abc" + ); + + + assertEquals( + Seq.of("a", "b", "c").foldRight("!", (t, u) -> t + u), + "abc!" + ); + } + + @Test + public void givenSeq_whenUsingSeqWhile_shouldBehaveAsWhileLoop() { + + assertEquals( + Seq.of(1, 2, 3, 4, 5).skipWhile(i -> i < 3).toList(), + Arrays.asList(3, 4, 5) + ); + + assertEquals( + Seq.of(1, 2, 3, 4, 5).skipUntil(i -> i == 3).toList(), + Arrays.asList(3, 4, 5) + ); + } + + @Test + public void givenSeq_whenZip_shouldHaveZippedSeq() { + + assertEquals( + Seq.of(1, 2, 3).zip(Seq.of("a", "b", "c")).toList(), + Arrays.asList(tuple(1, "a"), tuple(2, "b"), tuple(3, "c")) + ); + + // ("1:a", "2:b", "3:c") + assertEquals( + Seq.of(1, 2, 3).zip(Seq.of("a", "b", "c"), (x, y) -> x + ":" + y).toList(), + Arrays.asList("1:a", "2:b", "3:c") + ); + + + assertEquals( + Seq.of("a", "b", "c").zipWithIndex().toList(), + Arrays.asList(tuple("a", 0L), tuple("b", 1L), tuple("c", 2L)) + ); + } } From 306eb4ee96036d762c6611899953fa9afc02322c Mon Sep 17 00:00:00 2001 From: Tomasz Lelek Date: Sat, 18 Feb 2017 14:06:08 +0100 Subject: [PATCH 19/68] BAEL-602 example of using Unchecked --- jooq/src/test/java/com/baeldung/JOOLTest.java | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/jooq/src/test/java/com/baeldung/JOOLTest.java b/jooq/src/test/java/com/baeldung/JOOLTest.java index 4f5cb3741a..487af9bddb 100644 --- a/jooq/src/test/java/com/baeldung/JOOLTest.java +++ b/jooq/src/test/java/com/baeldung/JOOLTest.java @@ -2,12 +2,14 @@ package com.baeldung; import org.jooq.lambda.Seq; +import org.jooq.lambda.Unchecked; import org.junit.Test; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.stream.Collectors; import static junit.framework.Assert.assertTrue; import static junit.framework.TestCase.assertEquals; @@ -148,4 +150,44 @@ public class JOOLTest { Arrays.asList(tuple("a", 0L), tuple("b", 1L), tuple("c", 2L)) ); } + + + public Integer methodThatThrowsChecked(String arg) throws Exception { + return arg.length(); + } + + @Test + public void givenOperationThatThrowsCheckedException_whenExecuteAndNeedToWrapCheckedIntoUnchecked_shouldPass() { + //when + List collect = Arrays.asList("a", "b", "c").stream().map(elem -> { + try { + return methodThatThrowsChecked(elem); + } catch (Exception e) { + e.printStackTrace(); + throw new RuntimeException(e); + } + }).collect(Collectors.toList()); + + //then + assertEquals( + collect, + Arrays.asList(1, 1, 1) + ); + } + + + @Test + public void givenOperationThatThrowsCheckedException_whenExecuteUsingUncheckedFuction_shouldPass() { + //when + List collect = Arrays.asList("a", "b", "c").stream() + .map(Unchecked.function(elem -> methodThatThrowsChecked(elem))) + .collect(Collectors.toList()); + + //then + assertEquals( + collect, + Arrays.asList(1, 1, 1) + ); + } + } From 51756367ce8b8d9db7e2898559bbcf642f870f7d Mon Sep 17 00:00:00 2001 From: Abhinab Kanrar Date: Sat, 18 Feb 2017 21:24:41 +0530 Subject: [PATCH 20/68] adding testcase for WebUtils and ServletRequestUtils (#1193) * rest with spark java * 4 * Update Application.java * indentation changes * spring @requestmapping shortcuts * removing spring requestmapping and pushing spring-mvc-java * Joining/Splitting Strings with Java and Stream API * adding more join/split functionality * changing package name * testcase change * adding webutils * adding testcase for WebUtils and ServletRequestUtils * adding testcase --- .../baeldung/utils/UtilsControllerTest.java | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 spring-boot/src/test/java/com/baeldung/utils/UtilsControllerTest.java diff --git a/spring-boot/src/test/java/com/baeldung/utils/UtilsControllerTest.java b/spring-boot/src/test/java/com/baeldung/utils/UtilsControllerTest.java new file mode 100644 index 0000000000..7aed45dbb0 --- /dev/null +++ b/spring-boot/src/test/java/com/baeldung/utils/UtilsControllerTest.java @@ -0,0 +1,41 @@ +package com.baeldung.utils; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.InjectMocks; +import org.mockito.MockitoAnnotations; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; + +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + + +import com.baeldung.utils.controller.UtilsController; + +public class UtilsControllerTest { + + @InjectMocks + private UtilsController utilsController; + + private MockMvc mockMvc; + + @Before + public void setup() { + MockitoAnnotations.initMocks(this); + this.mockMvc = MockMvcBuilders.standaloneSetup(utilsController) + .build(); + + } + + @Test + public void givenParameter_setRequestParam_andSetSessionAttribute() throws Exception { + String param = "testparam"; + this.mockMvc.perform( + post("/setParam") + .param("param", param) + .sessionAttr("parameter", param)) + .andExpect(status().isOk()); + } + +} From e35c452ad1ac8ce4fd6bbf5f182cdf952e6bf02e Mon Sep 17 00:00:00 2001 From: Tomasz Lelek Date: Sat, 18 Feb 2017 22:40:15 +0100 Subject: [PATCH 21/68] BAEL-602 add example of join using only Stream API --- jooq/src/test/java/com/baeldung/JOOLTest.java | 26 ++++++++++++++----- 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/jooq/src/test/java/com/baeldung/JOOLTest.java b/jooq/src/test/java/com/baeldung/JOOLTest.java index 487af9bddb..64ad3b316d 100644 --- a/jooq/src/test/java/com/baeldung/JOOLTest.java +++ b/jooq/src/test/java/com/baeldung/JOOLTest.java @@ -10,6 +10,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.stream.Collectors; +import java.util.stream.Stream; import static junit.framework.Assert.assertTrue; import static junit.framework.TestCase.assertEquals; @@ -32,14 +33,22 @@ public class JOOLTest { assertTrue(Seq.of(1, 2, 3, 4).containsAny(2, 5)); } + @Test + public void givenStreams_whenJoin_shouldHaveElementsFromTwoStreams() { + //given + Stream left = Arrays.asList(1, 2, 4).stream(); + Stream right = Arrays.asList(1, 2, 3).stream(); + + //when + List rightCollected = right.collect(Collectors.toList()); + List collect = left.filter(rightCollected::contains).collect(Collectors.toList()); + + //then + assertEquals(collect, Arrays.asList(1, 2)); + } + @Test public void givenSeq_whenJoin_shouldHaveElementsFromBothSeq() { - assertEquals( - Seq.of(1, 2).crossJoin(Seq.of("A", "B")).toList(), - Arrays.asList(tuple(1, "A"), tuple(1, "B"), tuple(2, "A"), tuple(2, "B")) - ); - - assertEquals( Seq.of(1, 2, 4).innerJoin(Seq.of(1, 2, 3), (a, b) -> a == b).toList(), Arrays.asList(tuple(1, 1), tuple(2, 2)) @@ -55,6 +64,11 @@ public class JOOLTest { Seq.of(1, 2, 4).rightOuterJoin(Seq.of(1, 2, 3), (a, b) -> a == b).toList(), Arrays.asList(tuple(1, 1), tuple(2, 2), tuple(null, 3)) ); + + assertEquals( + Seq.of(1, 2).crossJoin(Seq.of("A", "B")).toList(), + Arrays.asList(tuple(1, "A"), tuple(1, "B"), tuple(2, "A"), tuple(2, "B")) + ); } @Test From 333953b204e473c06b7c86c72a164538e51f60e6 Mon Sep 17 00:00:00 2001 From: slavisa-baeldung Date: Sat, 18 Feb 2017 22:56:41 +0100 Subject: [PATCH 22/68] BAEL-183 - refactoring and moving testng to dedicated module --- ...edUnitTest.java => ParametrizedTests.java} | 17 ++-- .../junit4vstestng/RegistrationTest.java | 15 ++++ .../baeldung/junit4vstestng/SignInTest.java | 14 ++++ .../baeldung/junit4vstestng/SuiteTest.java | 2 +- pom.xml | 3 +- testng/pom.xml | 6 ++ .../java/baeldung/com/DependentTests.java | 35 ++------- .../baeldung/com/ParametrizedTestNGTest.java | 63 --------------- .../java/baeldung/com/ParametrizedTests.java | 77 +++++++++++++++++++ .../java/baeldung/com/RegistrationTest.java | 7 +- .../test/java/baeldung/com/SignInTest.java | 8 +- .../baeldung/com/SummationServiceTest.java | 3 + .../src/test/resources/parameterised_test.xml | 17 ---- .../src/test/resources/parametrized_test.xml | 10 +++ testng/src/test/resources/test_group.xml | 2 +- testng/src/test/resources/test_setup.xml | 17 ++++ testng/src/test/resources/test_suite.xml | 2 +- 17 files changed, 172 insertions(+), 126 deletions(-) rename core-java/src/test/java/com/baeldung/junit4vstestng/{MyParameterisedUnitTest.java => ParametrizedTests.java} (53%) create mode 100644 core-java/src/test/java/com/baeldung/junit4vstestng/RegistrationTest.java create mode 100644 core-java/src/test/java/com/baeldung/junit4vstestng/SignInTest.java delete mode 100644 testng/src/test/java/baeldung/com/ParametrizedTestNGTest.java create mode 100644 testng/src/test/java/baeldung/com/ParametrizedTests.java delete mode 100644 testng/src/test/resources/parameterised_test.xml create mode 100644 testng/src/test/resources/parametrized_test.xml create mode 100644 testng/src/test/resources/test_setup.xml diff --git a/core-java/src/test/java/com/baeldung/junit4vstestng/MyParameterisedUnitTest.java b/core-java/src/test/java/com/baeldung/junit4vstestng/ParametrizedTests.java similarity index 53% rename from core-java/src/test/java/com/baeldung/junit4vstestng/MyParameterisedUnitTest.java rename to core-java/src/test/java/com/baeldung/junit4vstestng/ParametrizedTests.java index 95f9274af3..e9a9c6a07a 100644 --- a/core-java/src/test/java/com/baeldung/junit4vstestng/MyParameterisedUnitTest.java +++ b/core-java/src/test/java/com/baeldung/junit4vstestng/ParametrizedTests.java @@ -11,23 +11,24 @@ import java.util.Arrays; import java.util.Collection; @RunWith(value = Parameterized.class) -public class MyParameterisedUnitTest { +public class ParametrizedTests { - private String name; + private int value; + private boolean isEven; - public MyParameterisedUnitTest(String myName) { - this.name = myName; + public ParametrizedTests(int value, boolean isEven) { + this.value = value; + this.isEven = isEven; } @Parameters public static Collection data() { - Object[][] data = new Object[][]{{"Peter"}, {"Sam"}, {"Tim"}, {"Lucy"}}; + Object[][] data = new Object[][]{{1, false}, {2, true}, {4, true}}; return Arrays.asList(data); } @Test - public void givenName_whenValidLength_thenTrue() { - boolean valid = name.length() > 0; - Assert.assertEquals(valid, true); + public void givenParametrizedNumber_ifEvenCheckOK_thenCorrect() { + Assert.assertEquals(isEven, value % 2 == 0); } } diff --git a/core-java/src/test/java/com/baeldung/junit4vstestng/RegistrationTest.java b/core-java/src/test/java/com/baeldung/junit4vstestng/RegistrationTest.java new file mode 100644 index 0000000000..4aa1fa1a17 --- /dev/null +++ b/core-java/src/test/java/com/baeldung/junit4vstestng/RegistrationTest.java @@ -0,0 +1,15 @@ +package com.baeldung.junit4vstestng; + +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +public class RegistrationTest { + private static final Logger LOGGER = LoggerFactory.getLogger(RegistrationTest.class); + + @Test + public void whenCalledFromSuite_thanOK() { + LOGGER.info("Registration successful"); + } +} diff --git a/core-java/src/test/java/com/baeldung/junit4vstestng/SignInTest.java b/core-java/src/test/java/com/baeldung/junit4vstestng/SignInTest.java new file mode 100644 index 0000000000..bb908ff37e --- /dev/null +++ b/core-java/src/test/java/com/baeldung/junit4vstestng/SignInTest.java @@ -0,0 +1,14 @@ +package com.baeldung.junit4vstestng; + +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class SignInTest { + private static final Logger LOGGER = LoggerFactory.getLogger(SignInTest.class); + + @Test + public void whenCalledFromSuite_thanOK() { + LOGGER.info("SignIn successful"); + } +} diff --git a/core-java/src/test/java/com/baeldung/junit4vstestng/SuiteTest.java b/core-java/src/test/java/com/baeldung/junit4vstestng/SuiteTest.java index 3d6d6cbbfb..effd7fa10a 100644 --- a/core-java/src/test/java/com/baeldung/junit4vstestng/SuiteTest.java +++ b/core-java/src/test/java/com/baeldung/junit4vstestng/SuiteTest.java @@ -4,7 +4,7 @@ import org.junit.runner.RunWith; import org.junit.runners.Suite; @RunWith(Suite.class) -@Suite.SuiteClasses({ StringCaseTest.class, DivisibilityTest.class }) +@Suite.SuiteClasses({ RegistrationTest.class, SignInTest.class }) public class SuiteTest { } diff --git a/pom.xml b/pom.xml index eb89b9cbaa..82df776044 100644 --- a/pom.xml +++ b/pom.xml @@ -182,6 +182,7 @@ spring-reactor testing + testng video-tutorials @@ -194,8 +195,6 @@ struts2 apache-velocity - testng - diff --git a/testng/pom.xml b/testng/pom.xml index 02f51d4d99..272b1d316d 100644 --- a/testng/pom.xml +++ b/testng/pom.xml @@ -43,6 +43,12 @@ true + + + src/main/resources + true + + diff --git a/testng/src/test/java/baeldung/com/DependentTests.java b/testng/src/test/java/baeldung/com/DependentTests.java index ab593e0439..b60f8a6c0d 100644 --- a/testng/src/test/java/baeldung/com/DependentTests.java +++ b/testng/src/test/java/baeldung/com/DependentTests.java @@ -1,46 +1,25 @@ package baeldung.com; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.testng.Assert; -import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; public class DependentTests { - private String validEmail = "abc@qwe.com"; - private EmailValidator emailValidator; - private LoginValidator loginValidator; + private static final Logger LOGGER = LoggerFactory.getLogger(DependentTests.class); - @BeforeClass - public void setup() { - emailValidator = new EmailValidator(); - loginValidator = new LoginValidator(); - } + private String email = "abc@qwe.com"; @Test public void givenEmail_ifValid_thenTrue() { - boolean valid = emailValidator.validate(validEmail); + boolean valid = email.contains("@"); Assert.assertEquals(valid, true); } - @Test(dependsOnMethods = { "givenEmail_ifValid_thenTrue" }) + @Test(dependsOnMethods = {"givenEmail_ifValid_thenTrue"}) public void givenValidEmail_whenLoggedIn_thenTrue() { - boolean valid = loginValidator.validate(); - Assert.assertEquals(valid, true); + LOGGER.info("Email {} valid >> logging in", email); } } -class EmailValidator { - - boolean validate(String validEmail) { - return true; - } - -} - -class LoginValidator { - - boolean validate() { - return true; - } - -} diff --git a/testng/src/test/java/baeldung/com/ParametrizedTestNGTest.java b/testng/src/test/java/baeldung/com/ParametrizedTestNGTest.java deleted file mode 100644 index e4f3aacf5b..0000000000 --- a/testng/src/test/java/baeldung/com/ParametrizedTestNGTest.java +++ /dev/null @@ -1,63 +0,0 @@ -package baeldung.com; - -import org.testng.Assert; -import org.testng.annotations.BeforeClass; -import org.testng.annotations.DataProvider; -import org.testng.annotations.Parameters; -import org.testng.annotations.Test; - -public class ParametrizedTestNGTest { - - @Test - @Parameters({ "name", "expectedResult" }) - public void givenNumber_ifPrime_thenCorrect(String name, boolean expectedResult) { - Assert.assertEquals(expectedResult, name.length() > 0); - } - - @DataProvider(name = "test1") - public static Object[][] primeNumbers() { - return new Object[][] { { "Peter", true }, { "Sam", true }, { "Tim", true }, { "Lucy", true } }; - } - - @Test(dataProvider = "test1") - public void givenNumber_whenPrime_thenCorrect(String name, boolean expectedResult) { - Assert.assertEquals(expectedResult, name.length() > 0); - } - - @Test(dataProvider = "myDataProvider") - public void parameterCheckTest(User user) { - Assert.assertEquals("sam", user.getName()); - Assert.assertEquals(12, user.getAge()); - } - - @DataProvider(name = "myDataProvider") - public Object[][] parameterProvider() { - User usr = new User(); - usr.setName("sam"); - usr.setAge(12); - return new Object[][] { { usr } }; - } - -} - - -class User { - private String name; - private int age; - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public int getAge() { - return age; - } - - public void setAge(int age) { - this.age = age; - } -} diff --git a/testng/src/test/java/baeldung/com/ParametrizedTests.java b/testng/src/test/java/baeldung/com/ParametrizedTests.java new file mode 100644 index 0000000000..d3813f5382 --- /dev/null +++ b/testng/src/test/java/baeldung/com/ParametrizedTests.java @@ -0,0 +1,77 @@ +package baeldung.com; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.testng.Assert; +import org.testng.annotations.DataProvider; +import org.testng.annotations.Parameters; +import org.testng.annotations.Test; + +public class ParametrizedTests { + + private static final Logger LOGGER = LoggerFactory.getLogger(ParametrizedTests.class); + + @Test + @Parameters({"value", "isEven"}) + public void givenNumberFromXML_ifEvenCheckOK_thenCorrect(int value, boolean isEven) { + Assert.assertEquals(isEven, value % 2 == 0); + } + + @DataProvider(name = "numbers") + public static Object[][] evenNumbers() { + return new Object[][]{{1, false}, {2, true}, {4, true}}; + } + + @Test(dataProvider = "numbers") + public void givenNumberFromDataProvider_ifEvenCheckOK_thenCorrect(Integer number, boolean expected) { + Assert.assertEquals(expected, number % 2 == 0); + } + + @Test(dataProvider = "numbersObject") + public void givenNumberObjectFromDataProvider_ifEvenCheckOK_thenCorrect(EvenNumber number) { + Assert.assertEquals(number.isEven(), number.getValue() % 2 == 0); + } + + @DataProvider(name = "numbersObject") + public Object[][] parameterProvider() { + return new Object[][]{{new EvenNumber(1, false)}, {new EvenNumber(2, true)}, {new EvenNumber(4, true),}}; + } + +} + + +class EvenNumber { + private int value; + private boolean isEven; + + public EvenNumber(int number, boolean isEven) { + this.value = number; + this.isEven = isEven; + } + + public int getValue() { + return value; + } + + public void setValue(int value) { + this.value = value; + } + + public boolean isEven() { + return isEven; + } + + public void setEven(boolean even) { + isEven = even; + } + + @Override + public String toString() { + return "EvenNumber{" + + "value=" + value + + ", isEven=" + isEven + + '}'; + } +} + + diff --git a/testng/src/test/java/baeldung/com/RegistrationTest.java b/testng/src/test/java/baeldung/com/RegistrationTest.java index 7cb17c9cb8..ec551d9c27 100644 --- a/testng/src/test/java/baeldung/com/RegistrationTest.java +++ b/testng/src/test/java/baeldung/com/RegistrationTest.java @@ -1,11 +1,14 @@ package baeldung.com; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.testng.annotations.Test; public class RegistrationTest { + private static final Logger LOGGER = LoggerFactory.getLogger(RegistrationTest.class); @Test - public void givenEmail_ifValid_thenCorrect() { - + public void whenCalledFromSuite_thanOK() { + LOGGER.info("Registration successful"); } } diff --git a/testng/src/test/java/baeldung/com/SignInTest.java b/testng/src/test/java/baeldung/com/SignInTest.java index 4884154345..f0547374d1 100644 --- a/testng/src/test/java/baeldung/com/SignInTest.java +++ b/testng/src/test/java/baeldung/com/SignInTest.java @@ -1,12 +1,14 @@ package baeldung.com; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.testng.annotations.Test; public class SignInTest { + private static final Logger LOGGER = LoggerFactory.getLogger(SignInTest.class); @Test - public void givenUsername_ifValid_thenCorrect() { - + public void whenCalledFromSuite_thanOK() { + LOGGER.info("SignIn successful"); } - } diff --git a/testng/src/test/java/baeldung/com/SummationServiceTest.java b/testng/src/test/java/baeldung/com/SummationServiceTest.java index 471d7a7c8a..f377a009df 100644 --- a/testng/src/test/java/baeldung/com/SummationServiceTest.java +++ b/testng/src/test/java/baeldung/com/SummationServiceTest.java @@ -1,5 +1,7 @@ package baeldung.com; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.testng.Assert; import org.testng.TestNG; import org.testng.annotations.*; @@ -8,6 +10,7 @@ import java.util.ArrayList; import java.util.List; public class SummationServiceTest extends TestNG { + private static final Logger LOGGER = LoggerFactory.getLogger(DependentTests.class); private List numbers; diff --git a/testng/src/test/resources/parameterised_test.xml b/testng/src/test/resources/parameterised_test.xml deleted file mode 100644 index 2381ba67d0..0000000000 --- a/testng/src/test/resources/parameterised_test.xml +++ /dev/null @@ -1,17 +0,0 @@ - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/testng/src/test/resources/parametrized_test.xml b/testng/src/test/resources/parametrized_test.xml new file mode 100644 index 0000000000..932af30e4e --- /dev/null +++ b/testng/src/test/resources/parametrized_test.xml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/testng/src/test/resources/test_group.xml b/testng/src/test/resources/test_group.xml index d636a4174d..26868375f2 100644 --- a/testng/src/test/resources/test_group.xml +++ b/testng/src/test/resources/test_group.xml @@ -1,6 +1,6 @@ - + diff --git a/testng/src/test/resources/test_setup.xml b/testng/src/test/resources/test_setup.xml new file mode 100644 index 0000000000..7d9708193e --- /dev/null +++ b/testng/src/test/resources/test_setup.xml @@ -0,0 +1,17 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/testng/src/test/resources/test_suite.xml b/testng/src/test/resources/test_suite.xml index 201ed3f645..0fe5d1cc40 100644 --- a/testng/src/test/resources/test_suite.xml +++ b/testng/src/test/resources/test_suite.xml @@ -1,6 +1,6 @@ - + From 46f854c03be775b12477f238eec62b66a6e7b9e7 Mon Sep 17 00:00:00 2001 From: lor6 Date: Sun, 19 Feb 2017 05:01:49 +0200 Subject: [PATCH 23/68] apache bval project (#1178) * apache bval project * update tests * remove extra lines --- apache-bval/pom.xml | 51 ++++++++ .../main/java/com/baeldung/model/User.java | 120 ++++++++++++++++++ .../com/baeldung/validation/Password.java | 25 ++++ .../validation/PasswordValidator.java | 35 +++++ .../baeldung/validation/ValidationTest.java | 97 ++++++++++++++ 5 files changed, 328 insertions(+) create mode 100644 apache-bval/pom.xml create mode 100644 apache-bval/src/main/java/com/baeldung/model/User.java create mode 100644 apache-bval/src/main/java/com/baeldung/validation/Password.java create mode 100644 apache-bval/src/main/java/com/baeldung/validation/PasswordValidator.java create mode 100644 apache-bval/src/test/java/com/baeldung/validation/ValidationTest.java diff --git a/apache-bval/pom.xml b/apache-bval/pom.xml new file mode 100644 index 0000000000..5d556af56f --- /dev/null +++ b/apache-bval/pom.xml @@ -0,0 +1,51 @@ + + 4.0.0 + apache-bval + apache-bval + 0.0.1-SNAPSHOT + + + + org.apache.bval + bval-jsr + ${bval.version} + + + javax.validation + validation-api + 1.1.0.Final + + + org.apache.bval + bval-extras + ${bval.version} + + + + junit + junit + ${junit.version} + test + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + ${maven-compiler-plugin.version} + + 1.8 + 1.8 + + + + + + 3.6.0 + 4.12 + 1.1.2 + + \ No newline at end of file diff --git a/apache-bval/src/main/java/com/baeldung/model/User.java b/apache-bval/src/main/java/com/baeldung/model/User.java new file mode 100644 index 0000000000..477136ddb4 --- /dev/null +++ b/apache-bval/src/main/java/com/baeldung/model/User.java @@ -0,0 +1,120 @@ +package com.baeldung.model; + +import java.io.File; + +import javax.validation.constraints.Min; +import javax.validation.constraints.NotNull; +import javax.validation.constraints.Size; + +import org.apache.bval.constraints.Email; +import org.apache.bval.constraints.NotEmpty; +import org.apache.bval.extras.constraints.checkdigit.IBAN; +import org.apache.bval.extras.constraints.creditcard.Visa; +import org.apache.bval.extras.constraints.file.Directory; +import org.apache.bval.extras.constraints.net.InetAddress; + +import com.baeldung.validation.Password; + +public class User { + @NotNull + @Email + private String email; + + @NotEmpty + @Password + private String password; + + @Size(min = 1, max = 20) + private String name; + + @Min(18) + private int age; + + @Visa + private String cardNumber = ""; + + @IBAN + private String iban = ""; + + @InetAddress + private String website = ""; + + @Directory + private File mainDirectory=new File("."); + + public User() { + } + + public User(String email, String password, String name, int age) { + super(); + this.email = email; + this.password = password; + this.name = name; + this.age = age; + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public int getAge() { + return age; + } + + public void setAge(int age) { + this.age = age; + } + + public String getCardNumber() { + return cardNumber; + } + + public void setCardNumber(String cardNumber) { + this.cardNumber = cardNumber; + } + + public String getIban() { + return iban; + } + + public void setIban(String iban) { + this.iban = iban; + } + + public String getWebsite() { + return website; + } + + public void setWebsite(String website) { + this.website = website; + } + + public File getMainDirectory() { + return mainDirectory; + } + + public void setMainDirectory(File mainDirectory) { + this.mainDirectory = mainDirectory; + } + +} diff --git a/apache-bval/src/main/java/com/baeldung/validation/Password.java b/apache-bval/src/main/java/com/baeldung/validation/Password.java new file mode 100644 index 0000000000..4ae06b2fb0 --- /dev/null +++ b/apache-bval/src/main/java/com/baeldung/validation/Password.java @@ -0,0 +1,25 @@ +package com.baeldung.validation; + +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import javax.validation.Constraint; +import javax.validation.Payload; + +import static java.lang.annotation.ElementType.*; + +@Constraint(validatedBy = { PasswordValidator.class }) +@Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER }) +@Retention(RetentionPolicy.RUNTIME) +public @interface Password { + String message() default "Invalid password"; + + Class[] groups() default {}; + + Class[] payload() default {}; + + int length() default 6; + + int nonAlpha() default 1; +} diff --git a/apache-bval/src/main/java/com/baeldung/validation/PasswordValidator.java b/apache-bval/src/main/java/com/baeldung/validation/PasswordValidator.java new file mode 100644 index 0000000000..19038d04d5 --- /dev/null +++ b/apache-bval/src/main/java/com/baeldung/validation/PasswordValidator.java @@ -0,0 +1,35 @@ +package com.baeldung.validation; + +import javax.validation.ConstraintValidator; +import javax.validation.ConstraintValidatorContext; + +public class PasswordValidator implements ConstraintValidator { + + private int length; + private int nonAlpha; + + @Override + public void initialize(Password password) { + this.length = password.length(); + this.nonAlpha = password.nonAlpha(); + + } + + @Override + public boolean isValid(String value, ConstraintValidatorContext context) { + if (value.length() < length) { + return false; + } + int nonAlphaNr = 0; + for (int i = 0; i < value.length(); i++) { + if (!Character.isLetterOrDigit(value.charAt(i))) { + nonAlphaNr++; + } + } + if (nonAlphaNr < nonAlpha) { + return false; + } + return true; + } + +} diff --git a/apache-bval/src/test/java/com/baeldung/validation/ValidationTest.java b/apache-bval/src/test/java/com/baeldung/validation/ValidationTest.java new file mode 100644 index 0000000000..cd58d4460a --- /dev/null +++ b/apache-bval/src/test/java/com/baeldung/validation/ValidationTest.java @@ -0,0 +1,97 @@ +package com.baeldung.validation; + +import java.io.File; +import java.util.Set; + +import javax.validation.ConstraintViolation; +import javax.validation.Validation; +import javax.validation.Validator; +import javax.validation.ValidatorFactory; + +import org.apache.bval.jsr.ApacheValidationProvider; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; + +import static org.junit.Assert.*; + +import com.baeldung.model.User; + +public class ValidationTest { + private static ValidatorFactory validatorFactory; + private static Validator validator; + + @BeforeClass + public static void setup() { + validatorFactory = Validation.byProvider(ApacheValidationProvider.class) + .configure() + .buildValidatorFactory(); + validator = validatorFactory.getValidator(); + } + + @Test + public void givenUser_whenValidate_thenValidationViolations() { + User user = new User("ana@yahoo.com", "pass", "nameTooLong_______________", 15); + + Set> violations = validator.validate(user); + assertTrue("no violations", violations.size() > 0); + } + + @Test + public void givenInvalidAge_whenValidateProperty_thenConstraintViolation() { + User user = new User("ana@yahoo.com", "pass", "Ana", 12); + + Set> propertyViolations = validator.validateProperty(user, "age"); + assertEquals("size is not 1", 1, propertyViolations.size()); + } + + @Test + public void givenValidAge_whenValidateValue_thenNoConstraintViolation() { + User user = new User("ana@yahoo.com", "pass", "Ana", 18); + + Set> valueViolations = validator.validateValue(User.class, "age", 20); + assertEquals("size is not 0", 0, valueViolations.size()); + } + + @Test + public void whenValidateNonJSR_thenCorrect() { + User user = new User("ana@yahoo.com", "pass", "Ana", 20); + user.setCardNumber("1234"); + user.setIban("1234"); + user.setWebsite("10.0.2.50"); + user.setMainDirectory(new File(".")); + + Set> violations = validator.validateProperty(user, "iban"); + assertEquals("size is not 1", 1, violations.size()); + + violations = validator.validateProperty(user, "website"); + assertEquals("size is not 0", 0, violations.size()); + + violations = validator.validateProperty(user, "mainDirectory"); + assertEquals("size is not 0", 0, violations.size()); + } + + @Test + public void givenInvalidPassword_whenValidatePassword_thenConstraintViolation() { + User user = new User("ana@yahoo.com", "password", "Ana", 20); + Set> violations = validator.validateProperty(user, "password"); + assertEquals("message incorrect", "Invalid password", violations.iterator() + .next() + .getMessage()); + } + + @Test + public void givenValidPassword_whenValidatePassword_thenNoConstraintViolation() { + User user = new User("ana@yahoo.com", "password#", "Ana", 20); + + Set> violations = validator.validateProperty(user, "password"); + assertEquals("size is not 0", 0, violations.size()); + } + + @AfterClass + public static void close() { + if (validatorFactory != null) { + validatorFactory.close(); + } + } +} From e5ecd86f27adedff215716f6916e72caccdfda83 Mon Sep 17 00:00:00 2001 From: mariiakulik Date: Sun, 19 Feb 2017 22:22:43 +0100 Subject: [PATCH 24/68] README files update (#1195) * Update README.md * Update README.md * Update README.md * Update README.md * Update README.md * Update README.md * Update README.md * Update README.md * Update README.md * Update README.md * Update README.md * Update README.md * Update README.md * Update README.md * Update README.md * Update README.md * Update README.md * Update README.md * Update README.md * Update README.MD * Update README.md * Update README.md * Update README.md * Update README.md * Update README.md * Create README.md * Update README.md * Update README.md * Create README.md * Update README.md * Update README.md * Update README.md * Create README.md * Update README.md * Update README.md * Update README.md * Create README.md * Update README.md * Update README.md * Create README.md * Update README.md * Update README.md * Create README.md * Update README.md * Update README.MD * Create README.md * Update README.md * Create README.md * Update README.md * Update README.md * Create README.md * Update README.md * Update README.md * Update README.md * Update README.md * Create README.md * Update README.md * Update README.md * Update README.md * Update README.md * Update README.md * Update README.md * Update README.md * Update README.md * Update README.md * Update README.md * Create README.md * Update README.md * Update README.md * Update README.md * Create README.md * Update README.md * Update README.md * Update README.md * Update README.MD * Create README.md * Update README.md * Update README.md * Update README.md * Update README.md * Update README.md * Create README.md * Update README.md * Update README.md * Create README.md * Update README.md * Update README.md * Update README.md * Update README.md * Delete README.md --- JGit/README.md | 3 +++ algorithms/README.md | 3 +++ apache-thrift/README.md | 3 +++ apache-velocity/README.md | 3 +++ core-java-9/README.md | 2 ++ core-java/README.md | 21 +++++++++++++++++++ .../test/java/com/baeldung/java/map/README.md | 2 -- couchbase-sdk/README.md | 1 + disruptor/README.md | 3 +++ ejb/README.md | 3 +++ guava/README.md | 8 +++++++ httpclient/README.md | 1 + jackson/README.md | 1 + java-mongodb/README.md | 3 +++ javaslang/README.md | 2 ++ jee7/README.md | 1 + kotlin/README.md | 3 +++ metrics/README.md | 3 +++ pdf/README.md | 1 + rxjava/README.md | 4 ++++ spring-all/README.md | 2 ++ spring-amqp/README.md | 3 +++ spring-boot/README.MD | 3 +++ spring-data-mongodb/README.md | 1 + spring-data-neo4j/README.md | 1 + spring-hibernate4/README.md | 2 ++ spring-jersey/README.md | 2 ++ spring-mobile/README.md | 4 ++++ spring-mvc-email/README.md | 6 +++++- spring-mvc-java/README.md | 2 ++ spring-mvc-simple/README.md | 3 +++ spring-reactor/README.md | 3 +++ spring-security-rest/README.md | 1 + spring-sleuth/README.md | 3 +++ static-analysis/README.md | 3 +++ 35 files changed, 107 insertions(+), 3 deletions(-) create mode 100644 JGit/README.md create mode 100644 algorithms/README.md create mode 100644 apache-thrift/README.md create mode 100644 apache-velocity/README.md delete mode 100644 core-java/src/test/java/com/baeldung/java/map/README.md create mode 100644 ejb/README.md create mode 100644 java-mongodb/README.md create mode 100644 kotlin/README.md create mode 100644 metrics/README.md create mode 100644 rxjava/README.md create mode 100644 spring-amqp/README.md create mode 100644 spring-mobile/README.md create mode 100644 spring-mvc-simple/README.md create mode 100644 spring-reactor/README.md create mode 100644 spring-sleuth/README.md create mode 100644 static-analysis/README.md diff --git a/JGit/README.md b/JGit/README.md new file mode 100644 index 0000000000..5c65f1101b --- /dev/null +++ b/JGit/README.md @@ -0,0 +1,3 @@ +## Relevant articles: + +- [A Guide to JGit](http://www.baeldung.com/jgit) diff --git a/algorithms/README.md b/algorithms/README.md new file mode 100644 index 0000000000..4789768fad --- /dev/null +++ b/algorithms/README.md @@ -0,0 +1,3 @@ +## Relevant articles: + +- [Dijkstra Algorithm in Java](http://www.baeldung.com/java-dijkstra) diff --git a/apache-thrift/README.md b/apache-thrift/README.md new file mode 100644 index 0000000000..d8b9195dcc --- /dev/null +++ b/apache-thrift/README.md @@ -0,0 +1,3 @@ +## Relevant articles: + +- [Working with Apache Thrift](http://www.baeldung.com/apache-thrift) diff --git a/apache-velocity/README.md b/apache-velocity/README.md new file mode 100644 index 0000000000..53c67f847e --- /dev/null +++ b/apache-velocity/README.md @@ -0,0 +1,3 @@ +## Relevant articles: + +- [Introduction to Apache Velocity](http://www.baeldung.com/apache-velocity) diff --git a/core-java-9/README.md b/core-java-9/README.md index 1b44239e40..53ad79e59c 100644 --- a/core-java-9/README.md +++ b/core-java-9/README.md @@ -6,3 +6,5 @@ ### Relevant Articles: - [Java 9 Stream API Improvements](http://www.baeldung.com/java-9-stream-api) +- [Java 9 Convenience Factory Methods for Collections](http://www.baeldung.com/java-9-collections-factory-methods) +- [New Stream Collectors in Java 9](http://www.baeldung.com/java9-stream-collectors) diff --git a/core-java/README.md b/core-java/README.md index 341dbdf910..a34908d8ae 100644 --- a/core-java/README.md +++ b/core-java/README.md @@ -58,3 +58,24 @@ - [Guide to java.util.concurrent.BlockingQueue](http://www.baeldung.com/java-blocking-queue) - [Guide to CountDownLatch in Java](http://www.baeldung.com/java-countdown-latch) - [How to Design a Genetic Algorithm in Java](http://www.baeldung.com/java-genetic-algorithm) +- [A Guide to ConcurrentMap](http://www.baeldung.com/java-concurrent-map) +- [Guide to PriorityBlockingQueue in Java](http://www.baeldung.com/java-priority-blocking-queue) +- [Guide to Java 8 groupingBy Collector](http://www.baeldung.com/java-groupingby-collector) +- [Avoiding the ConcurrentModificationException in Java](http://www.baeldung.com/java-concurrentmodificationexception) +- [Guide to WeakHashMap in Java](http://www.baeldung.com/java-weakhashmap) +- [Strategy Design Pattern in Java 8](http://www.baeldung.com/java-strategy-pattern) +- [Java 8 and Infinite Streams](http://www.baeldung.com/java-inifinite-streams) +- [Custom Thread Pools In Java 8 Parallel Streams](http://www.baeldung.com/java-8-parallel-streams-custom-threadpool) +- [String Operations with Java Streams](http://www.baeldung.com/java-stream-operations-on-strings) +- [Spring Security – Cache Control Headers](http://www.baeldung.com/spring-security-cache-control-headers) +- [Basic Introduction to JMX](http://www.baeldung.com/java-management-extensions) +- [AWS Lambda With Java](http://www.baeldung.com/java-aws-lambda) +- [Introduction to Nashorn](http://www.baeldung.com/java-nashorn) +- [Exceptions in Java 8 Lambda Expressions](http://www.baeldung.com/java-lambda-exceptions) +- [Guide to the Guava BiMap](http://www.baeldung.com/guava-bimap) +- [Iterable to Stream in Java](http://www.baeldung.com/java-iterable-to-stream) +- [Java 8 Stream findFirst() vs. findAny()](http://www.baeldung.com/java-stream-findfirst-vs-findany) +- [Chained Exceptions in Java](http://www.baeldung.com/java-chained-exceptions) +- [The Java HashMap Under the Hood](http://www.baeldung.com/java-hashmap) +- [A Guide to LinkedHashMap in Java](http://www.baeldung.com/java-linked-hashmap) +- [A Guide to TreeMap in Java](http://www.baeldung.com/java-treemap) diff --git a/core-java/src/test/java/com/baeldung/java/map/README.md b/core-java/src/test/java/com/baeldung/java/map/README.md deleted file mode 100644 index 0bba153763..0000000000 --- a/core-java/src/test/java/com/baeldung/java/map/README.md +++ /dev/null @@ -1,2 +0,0 @@ -### Relevant Articles: -- [The Java HashMap Under the Hood](http://www.baeldung.com/java-hashmap) diff --git a/couchbase-sdk/README.md b/couchbase-sdk/README.md index 9cdcdea012..f124a0192c 100644 --- a/couchbase-sdk/README.md +++ b/couchbase-sdk/README.md @@ -4,6 +4,7 @@ - [Introduction to Couchbase SDK for Java](http://www.baeldung.com/java-couchbase-sdk) - [Using Couchbase in a Spring Application](http://www.baeldung.com/couchbase-sdk-spring) - [Asynchronous Batch Opereations in Couchbase](http://www.baeldung.com/async-batch-operations-in-couchbase) +- [Querying Couchbase with MapReduce Views](http://www.baeldung.com/couchbase-query-mapreduce-view) ### Overview This Maven project contains the Java code for the Couchbase entities and Spring services diff --git a/disruptor/README.md b/disruptor/README.md index e69de29bb2..779b1e89c4 100644 --- a/disruptor/README.md +++ b/disruptor/README.md @@ -0,0 +1,3 @@ +## Relevant articles: + +- [Concurrency with LMAX Disruptor – An Introduction](http://www.baeldung.com/lmax-disruptor-concurrency) diff --git a/ejb/README.md b/ejb/README.md new file mode 100644 index 0000000000..08392bc80d --- /dev/null +++ b/ejb/README.md @@ -0,0 +1,3 @@ +## Relevant articles: + +- [Guide to EJB Set-up](http://www.baeldung.com/ejb-intro) diff --git a/guava/README.md b/guava/README.md index 40e7f19f41..ee224bae5f 100644 --- a/guava/README.md +++ b/guava/README.md @@ -16,3 +16,11 @@ - [Guava – Sets](http://www.baeldung.com/guava-sets) - [Guava – Maps](http://www.baeldung.com/guava-maps) - [Guava Set + Function = Map](http://www.baeldung.com/guava-set-function-map-tutorial) +- [Guide to Guava’s Ordering](http://www.baeldung.com/guava-ordering) +- [Guide to Guava’s PreConditions](http://www.baeldung.com/guava-preconditions) +- [Introduction to Guava CacheLoader](http://www.baeldung.com/guava-cacheloader) +- [Guide to Guava’s EventBus](http://www.baeldung.com/guava-eventbus) +- [Guide to Guava Multimap](http://www.baeldung.com/guava-multimap) +- [Guide to Guava RangeSet](http://www.baeldung.com/guava-rangeset) +- [Guide to Guava RangeMap](http://www.baeldung.com/guava-rangemap) +- [Guide to Guava Table](http://www.baeldung.com/guava-table) diff --git a/httpclient/README.md b/httpclient/README.md index a848edfea6..2a98c2feac 100644 --- a/httpclient/README.md +++ b/httpclient/README.md @@ -19,3 +19,4 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring - [Multipart Upload with HttpClient 4](http://www.baeldung.com/httpclient-multipart-upload) - [HttpAsyncClient Tutorial](http://www.baeldung.com/httpasyncclient-tutorial) - [HttpClient 4 Tutorial](http://www.baeldung.com/httpclient-guide) +- [Advanced HttpClient Configuration](http://www.baeldung.com/httpclient-advanced-config) diff --git a/jackson/README.md b/jackson/README.md index 67a03589a8..d9faa377f1 100644 --- a/jackson/README.md +++ b/jackson/README.md @@ -25,3 +25,4 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring - [More Jackson Annotations](http://www.baeldung.com/jackson-advanced-annotations) - [Inheritance with Jackson](http://www.baeldung.com/jackson-inheritance) - [Guide to @JsonFormat in Jackson](http://www.baeldung.com/jackson-jsonformat) +- [A Guide to Optional with Jackson](http://www.baeldung.com/jackson-optional) diff --git a/java-mongodb/README.md b/java-mongodb/README.md new file mode 100644 index 0000000000..01245ac6cf --- /dev/null +++ b/java-mongodb/README.md @@ -0,0 +1,3 @@ +## Relevant articles: + +- [A Guide to MongoDB with Java](http://www.baeldung.com/java-mongodb) diff --git a/javaslang/README.md b/javaslang/README.md index 334ac02f60..e451883516 100644 --- a/javaslang/README.md +++ b/javaslang/README.md @@ -1,2 +1,4 @@ ### Relevant Articles: - [Introduction to Javaslang](http://www.baeldung.com/javaslang) +- [Guide to Try in Javaslang](http://www.baeldung.com/javaslang-try) +- [Guide to Pattern Matching in Javaslang](http://www.baeldung.com/javaslang-pattern-matching) diff --git a/jee7/README.md b/jee7/README.md index 44ca9c2f6e..bc242c3340 100644 --- a/jee7/README.md +++ b/jee7/README.md @@ -1,2 +1,3 @@ ### Relevant Articles: - [Scheduling in Java EE](http://www.baeldung.com/scheduling-in-java-enterprise-edition) +- [JSON Processing in Java EE 7](http://www.baeldung.com/jee7-json) diff --git a/kotlin/README.md b/kotlin/README.md new file mode 100644 index 0000000000..6447a26f5c --- /dev/null +++ b/kotlin/README.md @@ -0,0 +1,3 @@ +## Relevant articles: + +- [Introduction to the Kotlin Language](http://www.baeldung.com/kotlin) diff --git a/metrics/README.md b/metrics/README.md new file mode 100644 index 0000000000..c98024c479 --- /dev/null +++ b/metrics/README.md @@ -0,0 +1,3 @@ +## Relevant articles: + +- [Intro to Dropwizard Metrics](http://www.baeldung.com/dropwizard-metrics) diff --git a/pdf/README.md b/pdf/README.md index 7160df4081..5454d2b2de 100644 --- a/pdf/README.md +++ b/pdf/README.md @@ -1,2 +1,3 @@ ### Relevant Articles: - [PDF Conversions in Java](http://www.baeldung.com/pdf-conversions-java) +- [Creating PDF Files in Java](http://www.baeldung.com/java-pdf-creation) diff --git a/rxjava/README.md b/rxjava/README.md new file mode 100644 index 0000000000..7670dd4ed3 --- /dev/null +++ b/rxjava/README.md @@ -0,0 +1,4 @@ +## Relevant articles: + +- [Dealing with Backpressure with RxJava](http://www.baeldung.com/rxjava-backpressure) +- [How to Test RxJava?](http://www.baeldung.com/rxjava-testing) diff --git a/spring-all/README.md b/spring-all/README.md index 90ae69300a..a8ea7c58c7 100644 --- a/spring-all/README.md +++ b/spring-all/README.md @@ -16,3 +16,5 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring - [Quick Guide to Spring Controllers](http://www.baeldung.com/spring-controllers) - [Quick Guide to Spring Bean Scopes](http://www.baeldung.com/spring-bean-scopes) - [Introduction To Ehcache](http://www.baeldung.com/ehcache) +- [A Guide to the Spring Task Scheduler](http://www.baeldung.com/spring-task-scheduler) +- [Guide to Spring Retry](http://www.baeldung.com/spring-retry) diff --git a/spring-amqp/README.md b/spring-amqp/README.md new file mode 100644 index 0000000000..b0d16c9305 --- /dev/null +++ b/spring-amqp/README.md @@ -0,0 +1,3 @@ +## Relevant articles: + +- [Messaging With Spring AMQP](http://www.baeldung.com/spring-amqp) diff --git a/spring-boot/README.MD b/spring-boot/README.MD index 05173ef318..d0a02c69fc 100644 --- a/spring-boot/README.MD +++ b/spring-boot/README.MD @@ -7,3 +7,6 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring - [A Guide to Spring in Eclipse STS](http://www.baeldung.com/eclipse-sts-spring) - [Introduction to WebJars](http://www.baeldung.com/maven-webjars) - [Create a Fat Jar App with Spring Boot](http://www.baeldung.com/deployable-fat-jar-spring-boot) +- [The @ServletComponentScan Annotation in Spring Boot](http://www.baeldung.com/spring-servletcomponentscan) +- [A Custom Data Binder in Spring MVC](http://www.baeldung.com/spring-mvc-custom-data-binder) +- [Intro to Building an Application with Spring Boot](http://www.baeldung.com/intro-to-spring-boot) diff --git a/spring-data-mongodb/README.md b/spring-data-mongodb/README.md index d656bc897c..c2a1f703b5 100644 --- a/spring-data-mongodb/README.md +++ b/spring-data-mongodb/README.md @@ -9,3 +9,4 @@ - [Custom Cascading in Spring Data MongoDB](http://www.baeldung.com/cascading-with-dbref-and-lifecycle-events-in-spring-data-mongodb) - [GridFS in Spring Data MongoDB](http://www.baeldung.com/spring-data-mongodb-gridfs) - [Introduction to Spring Data MongoDB](http://www.baeldung.com/spring-data-mongodb-tutorial) +- [Spring Data MongoDB: Projections and Aggregations](http://www.baeldung.com/spring-data-mongodb-projections-aggregations) diff --git a/spring-data-neo4j/README.md b/spring-data-neo4j/README.md index 0f13d9dbc9..03c9ed333d 100644 --- a/spring-data-neo4j/README.md +++ b/spring-data-neo4j/README.md @@ -2,6 +2,7 @@ ### Relevant Articles: - [Introduction to Spring Data Neo4j](http://www.baeldung.com/spring-data-neo4j-intro) +- [A Guide to Neo4J with Java](http://www.baeldung.com/java-neo4j) ### Build the Project with Tests Running ``` diff --git a/spring-hibernate4/README.md b/spring-hibernate4/README.md index 7888e8b4ee..02888c4ad0 100644 --- a/spring-hibernate4/README.md +++ b/spring-hibernate4/README.md @@ -12,6 +12,7 @@ - [Hibernate: save, persist, update, merge, saveOrUpdate](http://www.baeldung.com/hibernate-save-persist-update-merge-saveorupdate/) - [Eager/Lazy Loading In Hibernate](http://www.baeldung.com/hibernate-lazy-eager-loading) - [Hibernate Criteria Queries](http://www.baeldung.com/hibernate-criteria-queries) +- [Hibernate One to Many Annotation Tutorial](http://www.baeldung.com/hibernate-one-to-many) ### Quick Start @@ -22,3 +23,4 @@ mvn install mvn cargo:run ``` - **note**: starts on port `8082` + diff --git a/spring-jersey/README.md b/spring-jersey/README.md index 2767ceb9a7..8b2eecc0e1 100644 --- a/spring-jersey/README.md +++ b/spring-jersey/README.md @@ -1,3 +1,5 @@ ========= ## REST API with Jersey & Spring Example Project +- [REST API with Jersey and Spring](http://www.baeldung.com/jersey-rest-api-with-spring) +- [JAX-RS Client with Jersey](http://www.baeldung.com/jersey-jax-rs-client) diff --git a/spring-mobile/README.md b/spring-mobile/README.md new file mode 100644 index 0000000000..e3d23bcda6 --- /dev/null +++ b/spring-mobile/README.md @@ -0,0 +1,4 @@ +## Relevant articles: + +- [A Guide to Spring Mobile](http://www.baeldung.com/spring-mobile) + diff --git a/spring-mvc-email/README.md b/spring-mvc-email/README.md index 0de6532393..aa880188d7 100644 --- a/spring-mvc-email/README.md +++ b/spring-mvc-email/README.md @@ -1,3 +1,7 @@ +## Relevant articles: + +- [Guide to Spring Email](http://www.baeldung.com/spring-email) + ## Spring MVC Email Example Spring MVC project to send email from web form. @@ -10,4 +14,4 @@ Type http://localhost:8080 in your browser to open the application. ### Sending test emails -Follow UI links to send simple email, email using template or email with attachment. \ No newline at end of file +Follow UI links to send simple email, email using template or email with attachment. diff --git a/spring-mvc-java/README.md b/spring-mvc-java/README.md index 0f267c5ec9..575e4f583e 100644 --- a/spring-mvc-java/README.md +++ b/spring-mvc-java/README.md @@ -18,3 +18,5 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring - [Spring MVC Content Negotiation](http://www.baeldung.com/spring-mvc-content-negotiation-json-xml) - [Circular Dependencies in Spring](http://www.baeldung.com/circular-dependencies-in-spring) - [Introduction to HtmlUnit](http://www.baeldung.com/htmlunit) +- [Spring @RequestMapping New Shortcut Annotations](http://www.baeldung.com/spring-new-requestmapping-shortcuts) +- [Guide to Spring Handler Mappings](http://www.baeldung.com/spring-handler-mappings) diff --git a/spring-mvc-simple/README.md b/spring-mvc-simple/README.md new file mode 100644 index 0000000000..ffb02c846a --- /dev/null +++ b/spring-mvc-simple/README.md @@ -0,0 +1,3 @@ +## Relevant articles: + +- [HandlerAdapters in Spring MVC](http://www.baeldung.com/spring-mvc-handler-adapters) diff --git a/spring-reactor/README.md b/spring-reactor/README.md new file mode 100644 index 0000000000..0da2d6be51 --- /dev/null +++ b/spring-reactor/README.md @@ -0,0 +1,3 @@ +## Relevant articles: + +- [Introduction to Spring Reactor](http://www.baeldung.com/spring-reactor) diff --git a/spring-security-rest/README.md b/spring-security-rest/README.md index a1dfa32c6d..92b759a66a 100644 --- a/spring-security-rest/README.md +++ b/spring-security-rest/README.md @@ -13,3 +13,4 @@ The "Learn Spring Security" Classes: http://github.learnspringsecurity.com - [Custom Error Message Handling for REST API](http://www.baeldung.com/global-error-handler-in-a-spring-rest-api) - [An Intro to Spring HATEOAS](http://www.baeldung.com/spring-hateoas-tutorial) - [Spring Security Context Propagation with @Async](http://www.baeldung.com/spring-security-async-principal-propagation) +- [Servlet 3 Async Support with Spring MVC and Spring Security](http://www.baeldung.com/spring-mvc-async-security) diff --git a/spring-sleuth/README.md b/spring-sleuth/README.md new file mode 100644 index 0000000000..47aa126939 --- /dev/null +++ b/spring-sleuth/README.md @@ -0,0 +1,3 @@ +## Relevant articles: + +- [Spring Cloud Sleuth in a Monolith Application](http://www.baeldung.com/spring-cloud-sleuth-single-application) diff --git a/static-analysis/README.md b/static-analysis/README.md new file mode 100644 index 0000000000..74cc64b90d --- /dev/null +++ b/static-analysis/README.md @@ -0,0 +1,3 @@ +## Relevant articles: + +- [Introduction to PMD](http://www.baeldung.com/pmd) From 7c337eb6c08ec8500f22e328d715b60d682744c6 Mon Sep 17 00:00:00 2001 From: buddhini81 Date: Mon, 20 Feb 2017 05:24:42 +0530 Subject: [PATCH 25/68] Adding Code for BAEL-394 (#1163) * Code for evaluation article Article : Field vs. Constructor Injection * Correct typo in attribute name * Delete EbookRepositiry.java * Add corrected class * Update LibraryUtils.java * Update Member.java * Update Reservation.java * Adding new file AccountServlet * Adding new files for BAEL-394 * Add new files for BAEL-394 * Add new file for BAEL-394 * Indentation of annotations fixed * Indentation of annotations fixed * Indentation of annotations fixed * Removing this class since it is not relevant * New example added for @WebListener --- .../javaeeannotations/AccountServlet.java | 57 +++++++++++++++++++ .../BankAppServletContextListener.java | 17 ++++++ .../javaeeannotations/LoggingFilter.java | 36 ++++++++++++ .../UploadCustomerDocumentsServlet.java | 29 ++++++++++ jee7/src/main/webapp/WEB-INF/web.xml | 11 ++++ jee7/src/main/webapp/index.jsp | 16 ++++++ jee7/src/main/webapp/login.jsp | 12 ++++ jee7/src/main/webapp/upload.jsp | 16 ++++++ .../src/main/java/com/baeldung/Ebook.java | 20 +++++++ .../java/com/baeldung/EbookRepository.java | 5 ++ .../main/java/com/baeldung/LibraryUtils.java | 12 ++++ .../src/main/java/com/baeldung/Member.java | 20 +++++++ .../main/java/com/baeldung/Reservation.java | 14 +++++ 13 files changed, 265 insertions(+) create mode 100644 jee7/src/main/java/com/baeldung/javaeeannotations/AccountServlet.java create mode 100644 jee7/src/main/java/com/baeldung/javaeeannotations/BankAppServletContextListener.java create mode 100644 jee7/src/main/java/com/baeldung/javaeeannotations/LoggingFilter.java create mode 100644 jee7/src/main/java/com/baeldung/javaeeannotations/UploadCustomerDocumentsServlet.java create mode 100644 jee7/src/main/webapp/WEB-INF/web.xml create mode 100644 jee7/src/main/webapp/index.jsp create mode 100644 jee7/src/main/webapp/login.jsp create mode 100644 jee7/src/main/webapp/upload.jsp create mode 100644 spring-core/src/main/java/com/baeldung/Ebook.java create mode 100644 spring-core/src/main/java/com/baeldung/EbookRepository.java create mode 100644 spring-core/src/main/java/com/baeldung/LibraryUtils.java create mode 100644 spring-core/src/main/java/com/baeldung/Member.java create mode 100644 spring-core/src/main/java/com/baeldung/Reservation.java diff --git a/jee7/src/main/java/com/baeldung/javaeeannotations/AccountServlet.java b/jee7/src/main/java/com/baeldung/javaeeannotations/AccountServlet.java new file mode 100644 index 0000000000..e3f1667595 --- /dev/null +++ b/jee7/src/main/java/com/baeldung/javaeeannotations/AccountServlet.java @@ -0,0 +1,57 @@ +package com.baeldung.javaeeannotations; + +import java.io.IOException; +import java.io.PrintWriter; + +import javax.servlet.ServletConfig; +import javax.servlet.ServletException; +import javax.servlet.annotation.HttpConstraint; +import javax.servlet.annotation.HttpMethodConstraint; +import javax.servlet.annotation.ServletSecurity; +import javax.servlet.annotation.WebInitParam; +import javax.servlet.annotation.WebServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +@WebServlet( + name = "BankAccountServlet", + description = "Represents a Bank Account and it's transactions", + urlPatterns = {"/account", "/bankAccount" }, + initParams = { @WebInitParam(name = "type", value = "savings") } + ) +@ServletSecurity( + value = @HttpConstraint(rolesAllowed = {"admin"}), + httpMethodConstraints = {@HttpMethodConstraint(value = "POST", rolesAllowed = {"admin"})} + ) +public class AccountServlet extends javax.servlet.http.HttpServlet { + + String accountType = null; + + @Override + public void init(ServletConfig config) throws ServletException { + accountType = config.getInitParameter("type"); + } + + public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { + PrintWriter writer = response.getWriter(); + writer.println("Hello, I am an AccountServlet!"); + writer.flush(); + } + + public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException { + double accountBalance = 1000d; + double interestRate = Double.parseDouble(request.getAttribute("interest").toString()); + + String paramDepositAmt = request.getParameter("dep"); + double depositAmt = Double.parseDouble(paramDepositAmt); + + accountBalance = accountBalance + depositAmt; + + PrintWriter writer = response.getWriter(); + writer.println(" Balance of " + accountType + " account is: " + + accountBalance + "
This account bares an interest rate of " + interestRate + + " % "); + writer.flush(); + + } +} diff --git a/jee7/src/main/java/com/baeldung/javaeeannotations/BankAppServletContextListener.java b/jee7/src/main/java/com/baeldung/javaeeannotations/BankAppServletContextListener.java new file mode 100644 index 0000000000..6b43dd8a84 --- /dev/null +++ b/jee7/src/main/java/com/baeldung/javaeeannotations/BankAppServletContextListener.java @@ -0,0 +1,17 @@ +package com.baeldung.javaeeannotations; + +import javax.servlet.ServletContextEvent; +import javax.servlet.ServletContextListener; +import javax.servlet.annotation.WebListener; + +@WebListener +public class BankAppServletContextListener implements ServletContextListener { + + public void contextInitialized(ServletContextEvent sce) { + sce.getServletContext().setAttribute("ATTR_DEFAULT_LANGUAGE", "english"); + } + + public void contextDestroyed(ServletContextEvent sce) { + System.out.println("CONTEXT DESTROYED"); + } +} diff --git a/jee7/src/main/java/com/baeldung/javaeeannotations/LoggingFilter.java b/jee7/src/main/java/com/baeldung/javaeeannotations/LoggingFilter.java new file mode 100644 index 0000000000..97de5ec0de --- /dev/null +++ b/jee7/src/main/java/com/baeldung/javaeeannotations/LoggingFilter.java @@ -0,0 +1,36 @@ +package com.baeldung.javaeeannotations; + +import java.io.IOException; + +import javax.servlet.FilterChain; +import javax.servlet.FilterConfig; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; +import javax.servlet.annotation.WebFilter; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +@WebFilter( + urlPatterns = "/bankAccount/*", + filterName = "LoggingFilter", + description = "Filter all account transaction URLs" + ) +public class LoggingFilter implements javax.servlet.Filter { + @Override + public void init(FilterConfig filterConfig) throws ServletException { + } + + public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { + HttpServletRequest req = (HttpServletRequest) request; + HttpServletResponse res = (HttpServletResponse) response; + + res.sendRedirect(req.getContextPath() + "/login.jsp"); + chain.doFilter(request, response); + } + + @Override + public void destroy() { + } + +} diff --git a/jee7/src/main/java/com/baeldung/javaeeannotations/UploadCustomerDocumentsServlet.java b/jee7/src/main/java/com/baeldung/javaeeannotations/UploadCustomerDocumentsServlet.java new file mode 100644 index 0000000000..8a6c709b81 --- /dev/null +++ b/jee7/src/main/java/com/baeldung/javaeeannotations/UploadCustomerDocumentsServlet.java @@ -0,0 +1,29 @@ +package com.baeldung.javaeeannotations; + +import java.io.IOException; + +import javax.servlet.ServletException; +import javax.servlet.annotation.MultipartConfig; +import javax.servlet.annotation.WebServlet; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.Part; + +@WebServlet(urlPatterns = { "/uploadCustDocs" }) +@MultipartConfig( + fileSizeThreshold = 1024 * 1024 * 20, + maxFileSize = 1024 * 1024 * 20, + maxRequestSize = 1024 * 1024 * 25, + location = "D:/custDocs" + ) +public class UploadCustomerDocumentsServlet extends HttpServlet { + + protected void doPost( + HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + for (Part part : request.getParts()) { + part.write("myFile"); + } + } + +} diff --git a/jee7/src/main/webapp/WEB-INF/web.xml b/jee7/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000000..0a3d84d2d4 --- /dev/null +++ b/jee7/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,11 @@ + + + + BASIC + default + + + \ No newline at end of file diff --git a/jee7/src/main/webapp/index.jsp b/jee7/src/main/webapp/index.jsp new file mode 100644 index 0000000000..0c389ef5b1 --- /dev/null +++ b/jee7/src/main/webapp/index.jsp @@ -0,0 +1,16 @@ +<%@ page language="java" contentType="text/html; charset=ISO-8859-1" + pageEncoding="ISO-8859-1"%> + + + + +My Account + + +
+ Width: +    + +
+ + \ No newline at end of file diff --git a/jee7/src/main/webapp/login.jsp b/jee7/src/main/webapp/login.jsp new file mode 100644 index 0000000000..885df0c3d9 --- /dev/null +++ b/jee7/src/main/webapp/login.jsp @@ -0,0 +1,12 @@ +<%@ page language="java" contentType="text/html; charset=ISO-8859-1" + pageEncoding="ISO-8859-1"%> + + + + +Login + + +Login Here... + + \ No newline at end of file diff --git a/jee7/src/main/webapp/upload.jsp b/jee7/src/main/webapp/upload.jsp new file mode 100644 index 0000000000..020483b99f --- /dev/null +++ b/jee7/src/main/webapp/upload.jsp @@ -0,0 +1,16 @@ +<%@ page language="java" contentType="text/html; charset=ISO-8859-1" + pageEncoding="ISO-8859-1"%> + + + + +Insert title here + + +
+ +
+ +
+ + \ No newline at end of file diff --git a/spring-core/src/main/java/com/baeldung/Ebook.java b/spring-core/src/main/java/com/baeldung/Ebook.java new file mode 100644 index 0000000000..fc29ddfcf5 --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/Ebook.java @@ -0,0 +1,20 @@ +package com.baeldung; + +public class Ebook { + + private int bookId; + private String bookTitle; + + public int getBookId() { + return bookId; + } + public void setBookId(int bookId) { + this.bookId = bookId; + } + public String getBookTitle() { + return bookTitle; + } + public void setBookTitle(String bookTitle) { + this.bookTitle = bookTitle; + } +} diff --git a/spring-core/src/main/java/com/baeldung/EbookRepository.java b/spring-core/src/main/java/com/baeldung/EbookRepository.java new file mode 100644 index 0000000000..661283c355 --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/EbookRepository.java @@ -0,0 +1,5 @@ +package com.baeldung; + +public interface EbookRepository { + String titleById(int id); +} diff --git a/spring-core/src/main/java/com/baeldung/LibraryUtils.java b/spring-core/src/main/java/com/baeldung/LibraryUtils.java new file mode 100644 index 0000000000..49af60c89d --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/LibraryUtils.java @@ -0,0 +1,12 @@ +package com.baeldung; + +import org.springframework.beans.factory.annotation.Autowired; + +public class LibraryUtils { + @Autowired + private EbookRepository eBookRepository; + + public String findBook(int id) { + return eBookRepository.titleById(id); + } +} diff --git a/spring-core/src/main/java/com/baeldung/Member.java b/spring-core/src/main/java/com/baeldung/Member.java new file mode 100644 index 0000000000..ceebb32017 --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/Member.java @@ -0,0 +1,20 @@ +package com.baeldung; + +public class Member { + + private int memberId; + private String memberName; + + public int getMemberId() { + return memberId; + } + public void setMemberId(int memberId) { + this.memberId = memberId; + } + public String getMemberName() { + return memberName; + } + public void setMemberName(String memberName) { + this.memberName = memberName; + } +} diff --git a/spring-core/src/main/java/com/baeldung/Reservation.java b/spring-core/src/main/java/com/baeldung/Reservation.java new file mode 100644 index 0000000000..ed33bb6464 --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/Reservation.java @@ -0,0 +1,14 @@ +package com.baeldung; + +import org.springframework.beans.factory.annotation.Autowired; + +public class Reservation { + private Member member; + private Ebook eBook; + + @Autowired + public Reservation(Member member, Ebook eBook) { + this.member = member; + this.eBook = eBook; + } +} From 689219c7187577afbd460d733e5e5153f4b0f990 Mon Sep 17 00:00:00 2001 From: KevinGilmore Date: Sun, 19 Feb 2017 22:02:57 -0600 Subject: [PATCH 26/68] Create README.md --- apache-bval/README.md | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 apache-bval/README.md diff --git a/apache-bval/README.md b/apache-bval/README.md new file mode 100644 index 0000000000..80ea149993 --- /dev/null +++ b/apache-bval/README.md @@ -0,0 +1,2 @@ +### Relevant Articles: +- [Intro to Apache BVal](http://www.baeldung.com/apache-bval) From 99c8ee52282b30d97cbab7fd717a43356d80b4a8 Mon Sep 17 00:00:00 2001 From: dhruba619 Date: Mon, 20 Feb 2017 12:52:19 +0530 Subject: [PATCH 27/68] BAEL-41 Intro to Log4J2 code simplification and refactoring --- log4j2/pom.xml | 179 +++++++++--------- .../AsyncFileAppenderUsingJsonLayoutTest.java | 32 ---- ...ConsoleAppenderUsingDefaultLayoutTest.java | 21 -- ...enderUsingPatternLayoutWithColorsTest.java | 51 ----- .../log4j2/tests/CustomLoggingTest.java | 121 ++++++++++++ .../FailoverSyslogConsoleAppenderTest.java | 27 --- .../log4j2/tests/JDBCAppenderTest.java | 51 ----- ...RollingFileAppenderUsingXMLLayoutTest.java | 34 ---- .../log4j2/tests/jdbc/ConnectionFactory.java | 1 - ...log4j2-async-file-appender_json-layout.xml | 17 -- ...log4j2-console-appender_pattern-layout.xml | 27 --- ...syslog-console-appender_pattern-layout.xml | 20 -- ...onsole-appender_pattern-layout_colored.xml | 3 +- .../test/resources/log4j2-jdbc-appender.xml | 19 -- ...og4j2-rolling-file-appender_xml-layout.xml | 18 -- log4j2/src/test/resources/log4j2.xml | 66 ++++++- 16 files changed, 273 insertions(+), 414 deletions(-) delete mode 100644 log4j2/src/test/java/com/baeldung/logging/log4j2/tests/AsyncFileAppenderUsingJsonLayoutTest.java delete mode 100644 log4j2/src/test/java/com/baeldung/logging/log4j2/tests/ConsoleAppenderUsingDefaultLayoutTest.java delete mode 100644 log4j2/src/test/java/com/baeldung/logging/log4j2/tests/ConsoleAppenderUsingPatternLayoutWithColorsTest.java create mode 100644 log4j2/src/test/java/com/baeldung/logging/log4j2/tests/CustomLoggingTest.java delete mode 100644 log4j2/src/test/java/com/baeldung/logging/log4j2/tests/FailoverSyslogConsoleAppenderTest.java delete mode 100644 log4j2/src/test/java/com/baeldung/logging/log4j2/tests/JDBCAppenderTest.java delete mode 100644 log4j2/src/test/java/com/baeldung/logging/log4j2/tests/RollingFileAppenderUsingXMLLayoutTest.java delete mode 100644 log4j2/src/test/resources/log4j2-async-file-appender_json-layout.xml delete mode 100644 log4j2/src/test/resources/log4j2-console-appender_pattern-layout.xml delete mode 100644 log4j2/src/test/resources/log4j2-failover-syslog-console-appender_pattern-layout.xml delete mode 100644 log4j2/src/test/resources/log4j2-jdbc-appender.xml delete mode 100644 log4j2/src/test/resources/log4j2-rolling-file-appender_xml-layout.xml diff --git a/log4j2/pom.xml b/log4j2/pom.xml index 893c79be72..a4c8f19f69 100644 --- a/log4j2/pom.xml +++ b/log4j2/pom.xml @@ -1,105 +1,104 @@ - 4.0.0 + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + 4.0.0 - log4j2 + log4j2 - - com.baeldung - parent-modules - 1.0.0-SNAPSHOT - .. - + + com.baeldung + parent-modules + 1.0.0-SNAPSHOT + .. + - - - - org.apache.logging.log4j - log4j-core - ${log4j-core.version} - + + + + org.apache.logging.log4j + log4j-core + ${log4j-core.version} + - - - com.fasterxml.jackson.core - jackson-databind - ${jackson.version} - + + + com.fasterxml.jackson.core + jackson-databind + ${jackson.version} + - - - com.fasterxml.jackson.dataformat - jackson-dataformat-xml - ${jackson.version} - + + + com.fasterxml.jackson.dataformat + jackson-dataformat-xml + ${jackson.version} + - - - com.h2database - h2 - ${h2.version} - - - org.apache.commons - commons-dbcp2 - ${commons-dbcp2.version} - + + + com.h2database + h2 + ${h2.version} + + + org.apache.commons + commons-dbcp2 + ${commons-dbcp2.version} + - - - org.apache.logging.log4j - log4j-core - ${log4j-core.version} - test-jar - test - - - junit - junit - ${junit.version} - test - - + + + org.apache.logging.log4j + log4j-core + ${log4j-core.version} + test-jar + test + + + junit + junit + ${junit.version} + test + + - - + + - - org.apache.maven.plugins - maven-compiler-plugin - ${maven-compiler-plugin.version} - - 1.8 - 1.8 - - - - org.apache.maven.plugins - maven-surefire-plugin - ${maven-surefire-plugin.version} - - - **/*IntegrationTest.java - **/*LongRunningUnitTest.java - **/*ManualTest.java - - true - - + + org.apache.maven.plugins + maven-compiler-plugin + ${maven-compiler-plugin.version} + + 1.8 + 1.8 + + + + org.apache.maven.plugins + maven-surefire-plugin + ${maven-surefire-plugin.version} + + + **/*IntegrationTest.java + **/*LongRunningUnitTest.java + **/*ManualTest.java + + true + + - - + + - - 2.8.5 - 1.4.193 - 2.1.1 - 2.7 - 4.12 - - 3.6.0 - 2.19.1 - + + 2.8.5 + 1.4.193 + 2.1.1 + 2.7 + 4.12 + 3.6.0 + 2.19.1 + diff --git a/log4j2/src/test/java/com/baeldung/logging/log4j2/tests/AsyncFileAppenderUsingJsonLayoutTest.java b/log4j2/src/test/java/com/baeldung/logging/log4j2/tests/AsyncFileAppenderUsingJsonLayoutTest.java deleted file mode 100644 index 0472c2219e..0000000000 --- a/log4j2/src/test/java/com/baeldung/logging/log4j2/tests/AsyncFileAppenderUsingJsonLayoutTest.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.baeldung.logging.log4j2.tests; - -import org.apache.logging.log4j.Logger; -import org.apache.logging.log4j.junit.LoggerContextRule; -import org.junit.Rule; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; - -import java.nio.file.Files; -import java.nio.file.Paths; - -import static org.junit.Assert.assertTrue; - -@RunWith(JUnit4.class) -public class AsyncFileAppenderUsingJsonLayoutTest { - @Rule - public LoggerContextRule contextRule = - new LoggerContextRule("log4j2-async-file-appender_json-layout.xml"); - - @Test - public void givenLoggerWithAsyncConfig_shouldLogToJsonFile() - throws Exception { - Logger logger = contextRule.getLogger(getClass().getSimpleName()); - final int count = 88; - for (int i = 0; i < count; i++) { - logger.info("This is async JSON message #{} at INFO level.", count); - } - long logEventsCount = Files.lines(Paths.get("target/logfile.json")).count(); - assertTrue(logEventsCount > 0 && logEventsCount <= count); - } -} diff --git a/log4j2/src/test/java/com/baeldung/logging/log4j2/tests/ConsoleAppenderUsingDefaultLayoutTest.java b/log4j2/src/test/java/com/baeldung/logging/log4j2/tests/ConsoleAppenderUsingDefaultLayoutTest.java deleted file mode 100644 index 9831030d02..0000000000 --- a/log4j2/src/test/java/com/baeldung/logging/log4j2/tests/ConsoleAppenderUsingDefaultLayoutTest.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.baeldung.logging.log4j2.tests; - -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; - -@RunWith(JUnit4.class) -public class ConsoleAppenderUsingDefaultLayoutTest { - @Test - public void givenLoggerWithDefaultConfig_shouldLogToConsole() - throws Exception { - Logger logger = LogManager.getLogger(getClass()); - Exception e = new RuntimeException("This is only a test!"); - logger.info("This is a simple message at INFO level. " + - "It will be hidden."); - logger.error("This is a simple message at ERROR level. " + - "This is the minimum visible level.", e); - } -} diff --git a/log4j2/src/test/java/com/baeldung/logging/log4j2/tests/ConsoleAppenderUsingPatternLayoutWithColorsTest.java b/log4j2/src/test/java/com/baeldung/logging/log4j2/tests/ConsoleAppenderUsingPatternLayoutWithColorsTest.java deleted file mode 100644 index 86b005538f..0000000000 --- a/log4j2/src/test/java/com/baeldung/logging/log4j2/tests/ConsoleAppenderUsingPatternLayoutWithColorsTest.java +++ /dev/null @@ -1,51 +0,0 @@ -package com.baeldung.logging.log4j2.tests; - -import org.apache.logging.log4j.Logger; -import org.apache.logging.log4j.Marker; -import org.apache.logging.log4j.MarkerManager; -import org.apache.logging.log4j.ThreadContext; -import org.apache.logging.log4j.junit.LoggerContextRule; -import org.junit.Rule; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; - -@RunWith(JUnit4.class) -public class ConsoleAppenderUsingPatternLayoutWithColorsTest { - @Rule - public LoggerContextRule contextRule = - new LoggerContextRule("log4j2-console-appender_pattern-layout.xml"); - - @Test - public void givenLoggerWithConsoleConfig_shouldLogToConsoleInColors() - throws Exception { - Logger logger = contextRule.getLogger(getClass().getSimpleName()); - logger.trace("This is a colored message at TRACE level."); - logger.debug("This is a colored message at DEBUG level. " + - "This is the minimum visible level."); - logger.info("This is a colored message at INFO level."); - logger.warn("This is a colored message at WARN level."); - Exception e = new RuntimeException("This is only a test!"); - logger.error("This is a colored message at ERROR level.", e); - logger.fatal("This is a colored message at FATAL level."); - } - - @Test - public void givenLoggerWithConsoleConfig_shouldFilterByMarker() throws Exception { - Logger logger = contextRule.getLogger("ConnTrace"); - Marker appError = MarkerManager.getMarker("APP_ERROR"); - logger.error(appError, "This marker message at ERROR level should be hidden."); - Marker connectionTrace = MarkerManager.getMarker("CONN_TRACE"); - logger.trace(connectionTrace, "This is a marker message at TRACE level."); - } - - @Test - public void givenLoggerWithConsoleConfig_shouldFilterByThreadContext() throws Exception { - Logger logger = contextRule.getLogger("UserAudit"); - ThreadContext.put("userId", "1000"); - logger.info("This is a log-visible user login. Maybe from an admin account?"); - ThreadContext.put("userId", "1001"); - logger.info("This is a log-invisible user login."); - boolean b = true; - } -} diff --git a/log4j2/src/test/java/com/baeldung/logging/log4j2/tests/CustomLoggingTest.java b/log4j2/src/test/java/com/baeldung/logging/log4j2/tests/CustomLoggingTest.java new file mode 100644 index 0000000000..d88a967562 --- /dev/null +++ b/log4j2/src/test/java/com/baeldung/logging/log4j2/tests/CustomLoggingTest.java @@ -0,0 +1,121 @@ +package com.baeldung.logging.log4j2.tests; + +import static org.junit.Assert.assertTrue; + +import java.nio.file.Files; +import java.nio.file.Paths; +import java.sql.Connection; +import java.sql.ResultSet; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.apache.logging.log4j.Marker; +import org.apache.logging.log4j.MarkerManager; +import org.apache.logging.log4j.ThreadContext; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +import com.baeldung.logging.log4j2.tests.jdbc.ConnectionFactory; + +@RunWith(JUnit4.class) +public class CustomLoggingTest { + + @BeforeClass + public static void setup() throws Exception { + Connection connection = ConnectionFactory.getConnection(); + connection.createStatement() + .execute("CREATE TABLE logs(" + "when TIMESTAMP," + "logger VARCHAR(255)," + "level VARCHAR(255)," + "message VARCHAR(4096)," + "throwable TEXT)"); + connection.commit(); + } + + @Test + public void givenLoggerWithDefaultConfig_shouldLogToConsole() throws Exception { + Logger logger = LogManager.getLogger(getClass()); + Exception e = new RuntimeException("This is only a test!"); + logger.info("This is a simple message at INFO level. " + "It will be hidden."); + logger.error("This is a simple message at ERROR level. " + "This is the minimum visible level.", e); + } + + @Test + public void givenLoggerWithConsoleConfig_shouldLogToConsoleInColors() throws Exception { + Logger logger = LogManager.getLogger("CONSOLE_PATTERN_APPENDER_MARKER"); + logger.trace("This is a colored message at TRACE level."); + logger.debug("This is a colored message at DEBUG level. " + "This is the minimum visible level."); + logger.info("This is a colored message at INFO level."); + logger.warn("This is a colored message at WARN level."); + Exception e = new RuntimeException("This is only a test!"); + logger.error("This is a colored message at ERROR level.", e); + logger.fatal("This is a colored message at FATAL level."); + } + + @Test + public void givenLoggerWithConsoleConfig_shouldFilterByMarker() throws Exception { + Logger logger = LogManager.getLogger("CONSOLE_PATTERN_APPENDER_MARKER"); + Marker appError = MarkerManager.getMarker("APP_ERROR"); + logger.error(appError, "This marker message at ERROR level should be hidden."); + Marker connectionTrace = MarkerManager.getMarker("CONN_TRACE"); + logger.trace(connectionTrace, "This is a marker message at TRACE level."); + } + + @Test + public void givenLoggerWithConsoleConfig_shouldFilterByThreadContext() throws Exception { + Logger logger = LogManager.getLogger("CONSOLE_PATTERN_APPENDER_THREAD_CONTEXT"); + ThreadContext.put("userId", "1000"); + logger.info("This is a log-visible user login. Maybe from an admin account?"); + ThreadContext.put("userId", "1001"); + logger.info("This is a log-invisible user login."); + + } + + @Test + public void givenLoggerWithAsyncConfig_shouldLogToJsonFile() throws Exception { + Logger logger = LogManager.getLogger("ASYNC_JSON_FILE_APPENDER"); + final int count = 88; + for (int i = 0; i < count; i++) { + logger.info("This is async JSON message #{} at INFO level.", count); + } + long logEventsCount = Files.lines(Paths.get("target/logfile.json")) + .count(); + assertTrue(logEventsCount > 0 && logEventsCount <= count); + } + + @Test + public void givenLoggerWithFailoverConfig_shouldLog() throws Exception { + Logger logger = LogManager.getLogger("FAIL_OVER_SYSLOG_APPENDER"); + logger.trace("This is a syslog message at TRACE level."); + logger.debug("This is a syslog message at DEBUG level."); + logger.info("This is a syslog message at INFO level. This is the minimum visible level."); + logger.warn("This is a syslog message at WARN level."); + Exception e = new RuntimeException("This is only a test!"); + logger.error("This is a syslog message at ERROR level.", e); + logger.fatal("This is a syslog message at FATAL level."); + } + + @Test + public void givenLoggerWithJdbcConfig_shouldLogToDataSource() throws Exception { + Logger logger = LogManager.getLogger("JDBC_APPENDER"); + final int count = 88; + for (int i = 0; i < count; i++) { + logger.info("This is JDBC message #{} at INFO level.", count); + } + Connection connection = ConnectionFactory.getConnection(); + ResultSet resultSet = connection.createStatement() + .executeQuery("SELECT COUNT(*) AS ROW_COUNT FROM logs"); + int logCount = 0; + if (resultSet.next()) { + logCount = resultSet.getInt("ROW_COUNT"); + } + assertTrue(logCount == count); + } + + @Test + public void givenLoggerWithRollingFileConfig_shouldLogToXMLFile() throws Exception { + Logger logger = LogManager.getLogger("XML_ROLLING_FILE_APPENDER"); + final int count = 88; + for (int i = 0; i < count; i++) { + logger.info("This is rolling file XML message #{} at INFO level.", i); + } + } + +} diff --git a/log4j2/src/test/java/com/baeldung/logging/log4j2/tests/FailoverSyslogConsoleAppenderTest.java b/log4j2/src/test/java/com/baeldung/logging/log4j2/tests/FailoverSyslogConsoleAppenderTest.java deleted file mode 100644 index 0653394e5a..0000000000 --- a/log4j2/src/test/java/com/baeldung/logging/log4j2/tests/FailoverSyslogConsoleAppenderTest.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.baeldung.logging.log4j2.tests; - -import org.apache.logging.log4j.Logger; -import org.apache.logging.log4j.junit.LoggerContextRule; -import org.junit.Rule; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; - -@RunWith(JUnit4.class) -public class FailoverSyslogConsoleAppenderTest { - @Rule - public LoggerContextRule contextRule = - new LoggerContextRule("log4j2-failover-syslog-console-appender_pattern-layout.xml"); - - @Test - public void givenLoggerWithFailoverConfig_shouldLog() throws Exception { - Logger logger = contextRule.getLogger(getClass().getSimpleName()); - logger.trace("This is a syslog message at TRACE level."); - logger.debug("This is a syslog message at DEBUG level."); - logger.info("This is a syslog message at INFO level. This is the minimum visible level."); - logger.warn("This is a syslog message at WARN level."); - Exception e = new RuntimeException("This is only a test!"); - logger.error("This is a syslog message at ERROR level.", e); - logger.fatal("This is a syslog message at FATAL level."); - } -} diff --git a/log4j2/src/test/java/com/baeldung/logging/log4j2/tests/JDBCAppenderTest.java b/log4j2/src/test/java/com/baeldung/logging/log4j2/tests/JDBCAppenderTest.java deleted file mode 100644 index 1b8d33e2bf..0000000000 --- a/log4j2/src/test/java/com/baeldung/logging/log4j2/tests/JDBCAppenderTest.java +++ /dev/null @@ -1,51 +0,0 @@ -package com.baeldung.logging.log4j2.tests; - -import com.baeldung.logging.log4j2.tests.jdbc.ConnectionFactory; -import org.apache.logging.log4j.Logger; -import org.apache.logging.log4j.junit.LoggerContextRule; -import org.junit.BeforeClass; -import org.junit.Rule; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; - -import java.sql.Connection; -import java.sql.ResultSet; - -import static org.junit.Assert.assertTrue; - -@RunWith(JUnit4.class) -public class JDBCAppenderTest { - @Rule - public LoggerContextRule contextRule = new LoggerContextRule("log4j2-jdbc-appender.xml"); - - @BeforeClass - public static void setup() throws Exception { - Connection connection = ConnectionFactory.getConnection(); - connection.createStatement() - .execute("CREATE TABLE logs(" + - "when TIMESTAMP," + - "logger VARCHAR(255)," + - "level VARCHAR(255)," + - "message VARCHAR(4096)," + - "throwable TEXT)"); - //connection.commit(); - } - - @Test - public void givenLoggerWithJdbcConfig_shouldLogToDataSource() throws Exception { - Logger logger = contextRule.getLogger(getClass().getSimpleName()); - final int count = 88; - for (int i = 0; i < count; i++) { - logger.info("This is JDBC message #{} at INFO level.", count); - } - Connection connection = ConnectionFactory.getConnection(); - ResultSet resultSet = connection.createStatement() - .executeQuery("SELECT COUNT(*) AS ROW_COUNT FROM logs"); - int logCount = 0; - if (resultSet.next()) { - logCount = resultSet.getInt("ROW_COUNT"); - } - assertTrue(logCount == count); - } -} diff --git a/log4j2/src/test/java/com/baeldung/logging/log4j2/tests/RollingFileAppenderUsingXMLLayoutTest.java b/log4j2/src/test/java/com/baeldung/logging/log4j2/tests/RollingFileAppenderUsingXMLLayoutTest.java deleted file mode 100644 index 3ab69d263c..0000000000 --- a/log4j2/src/test/java/com/baeldung/logging/log4j2/tests/RollingFileAppenderUsingXMLLayoutTest.java +++ /dev/null @@ -1,34 +0,0 @@ -package com.baeldung.logging.log4j2.tests; - -import org.apache.logging.log4j.Logger; -import org.apache.logging.log4j.junit.LoggerContextRule; -import org.junit.Rule; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.junit.runners.JUnit4; - -import java.nio.file.Files; -import java.nio.file.Paths; -import java.util.stream.Collectors; - -import static org.junit.Assert.assertTrue; - -@RunWith(JUnit4.class) -public class RollingFileAppenderUsingXMLLayoutTest { - @Rule - public LoggerContextRule contextRule = - new LoggerContextRule("log4j2-rolling-file-appender_xml-layout.xml"); - - @Test - public void givenLoggerWithRollingFileConfig_shouldLogToXMLFile() throws Exception { - Logger logger = contextRule.getLogger(getClass().getSimpleName()); - final int count = 88; - for (int i = 0; i < count; i++) { - logger.info("This is rolling file XML message #{} at INFO level.", i); - } - String[] logEvents = Files.readAllLines(Paths.get("target/logfile.xml")).stream() - .collect(Collectors.joining(System.lineSeparator())) - .split("\\n\\n+"); - assertTrue(logEvents.length == 39); - } -} diff --git a/log4j2/src/test/java/com/baeldung/logging/log4j2/tests/jdbc/ConnectionFactory.java b/log4j2/src/test/java/com/baeldung/logging/log4j2/tests/jdbc/ConnectionFactory.java index 73b323f335..cc594f293c 100644 --- a/log4j2/src/test/java/com/baeldung/logging/log4j2/tests/jdbc/ConnectionFactory.java +++ b/log4j2/src/test/java/com/baeldung/logging/log4j2/tests/jdbc/ConnectionFactory.java @@ -2,7 +2,6 @@ package com.baeldung.logging.log4j2.tests.jdbc; import org.apache.commons.dbcp2.BasicDataSource; import org.h2.Driver; - import java.sql.Connection; import java.sql.SQLException; diff --git a/log4j2/src/test/resources/log4j2-async-file-appender_json-layout.xml b/log4j2/src/test/resources/log4j2-async-file-appender_json-layout.xml deleted file mode 100644 index c291eacd59..0000000000 --- a/log4j2/src/test/resources/log4j2-async-file-appender_json-layout.xml +++ /dev/null @@ -1,17 +0,0 @@ - - - - - - - - - - - - - - - - - diff --git a/log4j2/src/test/resources/log4j2-console-appender_pattern-layout.xml b/log4j2/src/test/resources/log4j2-console-appender_pattern-layout.xml deleted file mode 100644 index d6621f9166..0000000000 --- a/log4j2/src/test/resources/log4j2-console-appender_pattern-layout.xml +++ /dev/null @@ -1,27 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/log4j2/src/test/resources/log4j2-failover-syslog-console-appender_pattern-layout.xml b/log4j2/src/test/resources/log4j2-failover-syslog-console-appender_pattern-layout.xml deleted file mode 100644 index 62ba37f28c..0000000000 --- a/log4j2/src/test/resources/log4j2-failover-syslog-console-appender_pattern-layout.xml +++ /dev/null @@ -1,20 +0,0 @@ - - - - - - - - - - - - - - - - - - - - diff --git a/log4j2/src/test/resources/log4j2-includes/console-appender_pattern-layout_colored.xml b/log4j2/src/test/resources/log4j2-includes/console-appender_pattern-layout_colored.xml index c2b9c65430..fd61e4581f 100644 --- a/log4j2/src/test/resources/log4j2-includes/console-appender_pattern-layout_colored.xml +++ b/log4j2/src/test/resources/log4j2-includes/console-appender_pattern-layout_colored.xml @@ -1,4 +1,5 @@ - + diff --git a/log4j2/src/test/resources/log4j2-jdbc-appender.xml b/log4j2/src/test/resources/log4j2-jdbc-appender.xml deleted file mode 100644 index 6b50f7d5a4..0000000000 --- a/log4j2/src/test/resources/log4j2-jdbc-appender.xml +++ /dev/null @@ -1,19 +0,0 @@ - - - - - - - - - - - - - - - - - - diff --git a/log4j2/src/test/resources/log4j2-rolling-file-appender_xml-layout.xml b/log4j2/src/test/resources/log4j2-rolling-file-appender_xml-layout.xml deleted file mode 100644 index 9de1a29186..0000000000 --- a/log4j2/src/test/resources/log4j2-rolling-file-appender_xml-layout.xml +++ /dev/null @@ -1,18 +0,0 @@ - - - - - - - - - - - - - - - - diff --git a/log4j2/src/test/resources/log4j2.xml b/log4j2/src/test/resources/log4j2.xml index 8f7608aa78..83c1184f1f 100644 --- a/log4j2/src/test/resources/log4j2.xml +++ b/log4j2/src/test/resources/log4j2.xml @@ -1,13 +1,69 @@ - + + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - + + + + + + + + + + + + + + + + + + + + + + + - + \ No newline at end of file From 08ced4054783518d34038059f831fe902ca32e20 Mon Sep 17 00:00:00 2001 From: Tomasz Lelek Date: Mon, 20 Feb 2017 10:13:25 +0100 Subject: [PATCH 28/68] BAEL-602 remove unnecessary comment --- jooq/src/test/java/com/baeldung/JOOLTest.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/jooq/src/test/java/com/baeldung/JOOLTest.java b/jooq/src/test/java/com/baeldung/JOOLTest.java index 64ad3b316d..5560e7ea14 100644 --- a/jooq/src/test/java/com/baeldung/JOOLTest.java +++ b/jooq/src/test/java/com/baeldung/JOOLTest.java @@ -94,8 +94,8 @@ public class JOOLTest { ); assertEquals( - Seq.of(1, 2, 3, 4).partition(i -> i % 2 != 0).map((first, second) -> tuple(first.toList(), second.toList())), - tuple(Arrays.asList(1, 3), Arrays.asList(2, 4)) + Seq.of(1, 2, 3, 4).partition(i -> i > 2).map((first, second) -> tuple(first.toList(), second.toList())), + tuple(Arrays.asList(3, 4), Arrays.asList(1, 2)) ); @@ -152,7 +152,6 @@ public class JOOLTest { Arrays.asList(tuple(1, "a"), tuple(2, "b"), tuple(3, "c")) ); - // ("1:a", "2:b", "3:c") assertEquals( Seq.of(1, 2, 3).zip(Seq.of("a", "b", "c"), (x, y) -> x + ":" + y).toList(), Arrays.asList("1:a", "2:b", "3:c") From fae4d381c1b4662f86918f45423698533bbc7461 Mon Sep 17 00:00:00 2001 From: KevinGilmore Date: Mon, 20 Feb 2017 08:51:46 -0600 Subject: [PATCH 29/68] Update README.md --- spring-mvc-java/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/spring-mvc-java/README.md b/spring-mvc-java/README.md index 575e4f583e..4d3e58558b 100644 --- a/spring-mvc-java/README.md +++ b/spring-mvc-java/README.md @@ -20,3 +20,4 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring - [Introduction to HtmlUnit](http://www.baeldung.com/htmlunit) - [Spring @RequestMapping New Shortcut Annotations](http://www.baeldung.com/spring-new-requestmapping-shortcuts) - [Guide to Spring Handler Mappings](http://www.baeldung.com/spring-handler-mappings) +- [Uploading and Displaying Excel Files with Spring MVC](http://www.baeldung.com/spring-mvc-excel-files) From 99dbc3efd11ba5567b6f3e8ab30922506febbf09 Mon Sep 17 00:00:00 2001 From: slavisa-baeldung Date: Mon, 20 Feb 2017 20:03:32 +0100 Subject: [PATCH 30/68] BAEL-41 - Log4j 2 appenders, layout, filters --- .../baeldung/logging/log4j2/tests/CustomLoggingTest.java | 7 ++++--- .../logging/log4j2/tests/jdbc/ConnectionFactory.java | 1 + 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/log4j2/src/test/java/com/baeldung/logging/log4j2/tests/CustomLoggingTest.java b/log4j2/src/test/java/com/baeldung/logging/log4j2/tests/CustomLoggingTest.java index d88a967562..1562b67068 100644 --- a/log4j2/src/test/java/com/baeldung/logging/log4j2/tests/CustomLoggingTest.java +++ b/log4j2/src/test/java/com/baeldung/logging/log4j2/tests/CustomLoggingTest.java @@ -6,6 +6,7 @@ import java.nio.file.Files; import java.nio.file.Paths; import java.sql.Connection; import java.sql.ResultSet; + import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Marker; @@ -25,7 +26,7 @@ public class CustomLoggingTest { public static void setup() throws Exception { Connection connection = ConnectionFactory.getConnection(); connection.createStatement() - .execute("CREATE TABLE logs(" + "when TIMESTAMP," + "logger VARCHAR(255)," + "level VARCHAR(255)," + "message VARCHAR(4096)," + "throwable TEXT)"); + .execute("CREATE TABLE logs(" + "when TIMESTAMP," + "logger VARCHAR(255)," + "level VARCHAR(255)," + "message VARCHAR(4096)," + "throwable TEXT)"); connection.commit(); } @@ -76,7 +77,7 @@ public class CustomLoggingTest { logger.info("This is async JSON message #{} at INFO level.", count); } long logEventsCount = Files.lines(Paths.get("target/logfile.json")) - .count(); + .count(); assertTrue(logEventsCount > 0 && logEventsCount <= count); } @@ -101,7 +102,7 @@ public class CustomLoggingTest { } Connection connection = ConnectionFactory.getConnection(); ResultSet resultSet = connection.createStatement() - .executeQuery("SELECT COUNT(*) AS ROW_COUNT FROM logs"); + .executeQuery("SELECT COUNT(*) AS ROW_COUNT FROM logs"); int logCount = 0; if (resultSet.next()) { logCount = resultSet.getInt("ROW_COUNT"); diff --git a/log4j2/src/test/java/com/baeldung/logging/log4j2/tests/jdbc/ConnectionFactory.java b/log4j2/src/test/java/com/baeldung/logging/log4j2/tests/jdbc/ConnectionFactory.java index cc594f293c..73b323f335 100644 --- a/log4j2/src/test/java/com/baeldung/logging/log4j2/tests/jdbc/ConnectionFactory.java +++ b/log4j2/src/test/java/com/baeldung/logging/log4j2/tests/jdbc/ConnectionFactory.java @@ -2,6 +2,7 @@ package com.baeldung.logging.log4j2.tests.jdbc; import org.apache.commons.dbcp2.BasicDataSource; import org.h2.Driver; + import java.sql.Connection; import java.sql.SQLException; From 494d6d57e1ee74b2a6699da15c3fcdb67fe08984 Mon Sep 17 00:00:00 2001 From: GuenHamza Date: Mon, 20 Feb 2017 22:17:08 +0000 Subject: [PATCH 31/68] Add Cobertura Plugin configuration (#1185) --- algorithms/pom.xml | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/algorithms/pom.xml b/algorithms/pom.xml index 0c85a19534..f72457650a 100644 --- a/algorithms/pom.xml +++ b/algorithms/pom.xml @@ -41,4 +41,23 @@
- \ No newline at end of file + + + + org.codehaus.mojo + cobertura-maven-plugin + 2.7 + + + + com/baeldung/algorithms/dijkstra/* + + + com/baeldung/algorithms/dijkstra/* + + + + + + + From fda362f79d114473afba5e82698299fce3c111e1 Mon Sep 17 00:00:00 2001 From: Andrew Evans Date: Mon, 20 Feb 2017 15:17:48 -0700 Subject: [PATCH 32/68] Spring Groovy Config with fixed pom (#1200) * 'bean_injection' * 'bean_injection' * 'changes' * code * git ignore * pom fix * pom fix --- .../com/baeldung/annotationinjection/App.java | 48 +++++++++++++ .../annotationinjection/AppConfig.java | 23 +++++++ .../baeldung/annotationinjection/AppTest.java | 52 ++++++++++++++ .../annotationinjection/AutowireObject.java | 6 ++ .../annotationinjection/ImportConfig.java | 20 ++++++ .../annotationinjection/InjectedClass.java | 39 +++++++++++ spring-groovy-config/.gitignore | 1 + .../org.eclipse.core.resources.prefs | 4 ++ .../.settings/org.eclipse.jdt.core.prefs | 5 ++ .../org.eclipse.jdt.groovy.core.prefs | 2 + .../.settings/org.eclipse.m2e.core.prefs | 4 ++ .../groovyContextWithConstructor.groovy | 9 +++ .../config/groovyPropConfig.groovy | 5 ++ .../config/groovyPropConfigWithClosure.groovy | 8 +++ .../config/groovyTestWithRefBean.groovy | 10 +++ spring-groovy-config/pom.xml | 67 +++++++++++++++++++ .../spring_groovy_config/ClassWithRef.java | 18 +++++ .../spring_groovy_config/GroovyClass.groovy | 9 +++ .../spring_groovy_config/MainApp.java | 14 ++++ .../spring_groovy_config/TestClass.java | 22 ++++++ .../spring_groovy_config/TestClassB.java | 26 +++++++ .../spring_groovy_config/AppTest.java | 57 ++++++++++++++++ 22 files changed, 449 insertions(+) create mode 100644 spring-core/src/main/java/com/baeldung/annotationinjection/App.java create mode 100644 spring-core/src/main/java/com/baeldung/annotationinjection/AppConfig.java create mode 100644 spring-core/src/main/java/com/baeldung/annotationinjection/AppTest.java create mode 100644 spring-core/src/main/java/com/baeldung/annotationinjection/AutowireObject.java create mode 100644 spring-core/src/main/java/com/baeldung/annotationinjection/ImportConfig.java create mode 100644 spring-core/src/main/java/com/baeldung/annotationinjection/InjectedClass.java create mode 100644 spring-groovy-config/.gitignore create mode 100644 spring-groovy-config/.settings/org.eclipse.core.resources.prefs create mode 100644 spring-groovy-config/.settings/org.eclipse.jdt.core.prefs create mode 100644 spring-groovy-config/.settings/org.eclipse.jdt.groovy.core.prefs create mode 100644 spring-groovy-config/.settings/org.eclipse.m2e.core.prefs create mode 100644 spring-groovy-config/config/groovyContextWithConstructor.groovy create mode 100644 spring-groovy-config/config/groovyPropConfig.groovy create mode 100644 spring-groovy-config/config/groovyPropConfigWithClosure.groovy create mode 100644 spring-groovy-config/config/groovyTestWithRefBean.groovy create mode 100644 spring-groovy-config/pom.xml create mode 100644 spring-groovy-config/src/main/java/com/baeldung/spring_groovy_config/ClassWithRef.java create mode 100644 spring-groovy-config/src/main/java/com/baeldung/spring_groovy_config/GroovyClass.groovy create mode 100644 spring-groovy-config/src/main/java/com/baeldung/spring_groovy_config/MainApp.java create mode 100644 spring-groovy-config/src/main/java/com/baeldung/spring_groovy_config/TestClass.java create mode 100644 spring-groovy-config/src/main/java/com/baeldung/spring_groovy_config/TestClassB.java create mode 100644 spring-groovy-config/src/test/java/com/baeldung/spring_groovy_config/AppTest.java diff --git a/spring-core/src/main/java/com/baeldung/annotationinjection/App.java b/spring-core/src/main/java/com/baeldung/annotationinjection/App.java new file mode 100644 index 0000000000..dc9560286d --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/annotationinjection/App.java @@ -0,0 +1,48 @@ +package com.baeldung.java_bean_injection; + +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +import com.baeldung.java_bean_injection.*; + +import org.springframework.context.ApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + + +import junit.framework.TestResult; +import junit.framework.TestFailure; + +import java.util.Enumeration; + + +/** + * Bean Injection Test Application + * + */ +public class App +{ + public static void main( String[] args ) + { + + TestResult result = new TestResult(); + AppTest.suite().run(result); + System.out.println(String.format("Tests: %d",result.runCount())); + System.out.println(String.format("Errors: %d",result.errorCount())); + System.out.println(String.format("Failures: %d",result.failureCount())); + if(result.failureCount() > 0){ + Enumeration failures = result.failures(); + int failNum = 0; + TestFailure failure = null; + while(failures.hasMoreElements()){ + failure = failures.nextElement(); + + System.out.println(failure.exceptionMessage()); + System.out.println(String.format("Test Failure %d\n",failNum)); + System.out.println(failure.trace()); + System.out.print("\n"); + } + } + + } +} diff --git a/spring-core/src/main/java/com/baeldung/annotationinjection/AppConfig.java b/spring-core/src/main/java/com/baeldung/annotationinjection/AppConfig.java new file mode 100644 index 0000000000..138bda6b1b --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/annotationinjection/AppConfig.java @@ -0,0 +1,23 @@ +package com.baeldung.java_bean_injection; + +import org.springframework.beans.factory.annotation.*; +import org.springframework.context.annotation.*; +import org.springframework.stereotype.*; + +import com.baeldung.java_bean_injection.*; + +@Configuration +@PropertySource(value="classpath:inject.properties") +@Import(ImportConfig.class) +public class AppConfig { + @Value("${contructor.arg1}") String constructorArg; + + @Bean + public InjectedClass injectedClass(){ + InjectedClass ic = new InjectedClass(constructorArg); + ic.setMyInt(10); + ic.setTestString("test"); + return ic; + } + +} diff --git a/spring-core/src/main/java/com/baeldung/annotationinjection/AppTest.java b/spring-core/src/main/java/com/baeldung/annotationinjection/AppTest.java new file mode 100644 index 0000000000..fa7b46f06b --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/annotationinjection/AppTest.java @@ -0,0 +1,52 @@ +package com.baeldung.java_bean_injection; + +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +import com.baeldung.java_bean_injection.*; + +import java.io.File; +import java.util.Scanner; + +/** + * 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() + { + AnnotationConfigApplicationContext context= new AnnotationConfigApplicationContext(); + context.register(AppConfig.class); + context.refresh(); + InjectedClass injectedClass = (InjectedClass) context.getBean(InjectedClass.class); + assertTrue(injectedClass.getMyInt() == 10); + assertTrue(injectedClass.getTestString().equals("test")); + assertNotNull(injectedClass.obj); + assertTrue(injectedClass.myConstructorArg.equals("test")); + } +} diff --git a/spring-core/src/main/java/com/baeldung/annotationinjection/AutowireObject.java b/spring-core/src/main/java/com/baeldung/annotationinjection/AutowireObject.java new file mode 100644 index 0000000000..df5859f0af --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/annotationinjection/AutowireObject.java @@ -0,0 +1,6 @@ +package com.baeldung.java_bean_injection; + + +public class AutowireObject { + +} diff --git a/spring-core/src/main/java/com/baeldung/annotationinjection/ImportConfig.java b/spring-core/src/main/java/com/baeldung/annotationinjection/ImportConfig.java new file mode 100644 index 0000000000..e37d94931b --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/annotationinjection/ImportConfig.java @@ -0,0 +1,20 @@ +package com.baeldung.java_bean_injection; + +import org.springframework.context.annotation.*; + +import com.baeldung.java_bean_injection.*; + +@Configuration +public class ImportConfig { + + @Bean(name="autobean") + public AutowireObject autowireObject(){ + return new AutowireObject(); + } + + + @Bean(name="autobean2") + public AutowireObject autowireObject2(){ + return new AutowireObject(); + } +} diff --git a/spring-core/src/main/java/com/baeldung/annotationinjection/InjectedClass.java b/spring-core/src/main/java/com/baeldung/annotationinjection/InjectedClass.java new file mode 100644 index 0000000000..55fe733db5 --- /dev/null +++ b/spring-core/src/main/java/com/baeldung/annotationinjection/InjectedClass.java @@ -0,0 +1,39 @@ +package com.baeldung.java_bean_injection; + +import org.springframework.beans.factory.annotation.*; +import org.springframework.context.annotation.*; + +import com.baeldung.java_bean_injection.*; + + +public class InjectedClass { + private int myInt; + private String testString; + String myConstructorArg; + + + @Autowired + @Qualifier("autobean") + AutowireObject obj; + + public InjectedClass(String constArg){ + this.myConstructorArg = constArg; + } + + public void setMyInt(int myInt){ + this.myInt = myInt; + } + + public void setTestString(String testString){ + this.testString = testString; + } + + public int getMyInt(){ + return this.myInt; + } + + public String getTestString(){ + return this.testString; + } + +} diff --git a/spring-groovy-config/.gitignore b/spring-groovy-config/.gitignore new file mode 100644 index 0000000000..b83d22266a --- /dev/null +++ b/spring-groovy-config/.gitignore @@ -0,0 +1 @@ +/target/ diff --git a/spring-groovy-config/.settings/org.eclipse.core.resources.prefs b/spring-groovy-config/.settings/org.eclipse.core.resources.prefs new file mode 100644 index 0000000000..f9fe34593f --- /dev/null +++ b/spring-groovy-config/.settings/org.eclipse.core.resources.prefs @@ -0,0 +1,4 @@ +eclipse.preferences.version=1 +encoding//src/main/java=UTF-8 +encoding//src/test/java=UTF-8 +encoding/=UTF-8 diff --git a/spring-groovy-config/.settings/org.eclipse.jdt.core.prefs b/spring-groovy-config/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000000..abec6ca389 --- /dev/null +++ b/spring-groovy-config/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,5 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5 +org.eclipse.jdt.core.compiler.compliance=1.5 +org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning +org.eclipse.jdt.core.compiler.source=1.5 diff --git a/spring-groovy-config/.settings/org.eclipse.jdt.groovy.core.prefs b/spring-groovy-config/.settings/org.eclipse.jdt.groovy.core.prefs new file mode 100644 index 0000000000..ae98feaa79 --- /dev/null +++ b/spring-groovy-config/.settings/org.eclipse.jdt.groovy.core.prefs @@ -0,0 +1,2 @@ +eclipse.preferences.version=1 +groovy.compiler.level=24 diff --git a/spring-groovy-config/.settings/org.eclipse.m2e.core.prefs b/spring-groovy-config/.settings/org.eclipse.m2e.core.prefs new file mode 100644 index 0000000000..f897a7f1cb --- /dev/null +++ b/spring-groovy-config/.settings/org.eclipse.m2e.core.prefs @@ -0,0 +1,4 @@ +activeProfiles= +eclipse.preferences.version=1 +resolveWorkspaceProjects=true +version=1 diff --git a/spring-groovy-config/config/groovyContextWithConstructor.groovy b/spring-groovy-config/config/groovyContextWithConstructor.groovy new file mode 100644 index 0000000000..dbde5f3822 --- /dev/null +++ b/spring-groovy-config/config/groovyContextWithConstructor.groovy @@ -0,0 +1,9 @@ +import com.baeldung.spring_groovy_config.TestClass + +beans{ + testString String, 'Test String' + testClass(TestClass){ + beanDefinition -> + beanDefinition.constructorArgs = ["Test String",10.2] + } +} \ No newline at end of file diff --git a/spring-groovy-config/config/groovyPropConfig.groovy b/spring-groovy-config/config/groovyPropConfig.groovy new file mode 100644 index 0000000000..08d3acf354 --- /dev/null +++ b/spring-groovy-config/config/groovyPropConfig.groovy @@ -0,0 +1,5 @@ +import com.baeldung.spring_groovy_config.TestClassB + +beans{ + testClassB(TestClassB,testStringB:"Test String",testIntB:10.2) +} \ No newline at end of file diff --git a/spring-groovy-config/config/groovyPropConfigWithClosure.groovy b/spring-groovy-config/config/groovyPropConfigWithClosure.groovy new file mode 100644 index 0000000000..3ca306bdd7 --- /dev/null +++ b/spring-groovy-config/config/groovyPropConfigWithClosure.groovy @@ -0,0 +1,8 @@ +import com.baeldung.spring_groovy_config.TestClassB + +beans{ + testClassB(TestClassB){ + testStringB = "Test String" + testIntB = 10 + } +} \ No newline at end of file diff --git a/spring-groovy-config/config/groovyTestWithRefBean.groovy b/spring-groovy-config/config/groovyTestWithRefBean.groovy new file mode 100644 index 0000000000..d079480d46 --- /dev/null +++ b/spring-groovy-config/config/groovyTestWithRefBean.groovy @@ -0,0 +1,10 @@ +import com.baeldung.spring_groovy_config.GroovyClass +import com.baeldung.spring_groovy_config.ClassWithRef + +beans{ + groovyClass(GroovyClass, groovyInt:5) + classWithRef(ClassWithRef){ + myClass = groovyClass + } + +} \ No newline at end of file diff --git a/spring-groovy-config/pom.xml b/spring-groovy-config/pom.xml new file mode 100644 index 0000000000..f6c63ae232 --- /dev/null +++ b/spring-groovy-config/pom.xml @@ -0,0 +1,67 @@ + + + 4.0.0 + com.baeldung + spring-groovy-config + 0.0.1-SNAPSHOT + jar + spring-groovy-config + http://maven.apache.org + + UTF-8 + + + + + maven-compiler-plugin + + 3.1 + + groovy-eclipse-compiler + + + + org.codehaus.groovy + groovy-eclipse-compiler + 2.9.1-01 + + + + org.codehaus.groovy + groovy-eclipse-batch + 2.3.7-01 + + + + + + + + org.codehaus.groovy + groovy-all + 2.4.8 + + + junit + junit + 4.12 + test + + + org.springframework + spring-context + 4.3.6.RELEASE + + + org.springframework + spring-core + 4.3.6.RELEASE + + + org.springframework + spring-test + 4.3.6.RELEASE + + + + diff --git a/spring-groovy-config/src/main/java/com/baeldung/spring_groovy_config/ClassWithRef.java b/spring-groovy-config/src/main/java/com/baeldung/spring_groovy_config/ClassWithRef.java new file mode 100644 index 0000000000..ec8987f387 --- /dev/null +++ b/spring-groovy-config/src/main/java/com/baeldung/spring_groovy_config/ClassWithRef.java @@ -0,0 +1,18 @@ +package com.baeldung.spring_groovy_config; + +import org.springframework.stereotype.Component; + +import com.baeldung.spring_groovy_config.GroovyClass; + +@Component +public class ClassWithRef { + private GroovyClass myClass = null; + + public void setMyClass(GroovyClass myClass){ + this.myClass = myClass; + } + + public GroovyClass getMyClass(){ + return this.myClass; + } +} diff --git a/spring-groovy-config/src/main/java/com/baeldung/spring_groovy_config/GroovyClass.groovy b/spring-groovy-config/src/main/java/com/baeldung/spring_groovy_config/GroovyClass.groovy new file mode 100644 index 0000000000..2a1ef6a2c5 --- /dev/null +++ b/spring-groovy-config/src/main/java/com/baeldung/spring_groovy_config/GroovyClass.groovy @@ -0,0 +1,9 @@ +package com.baeldung.spring_groovy_config + +class GroovyClass { + int groovyInt = 0 + + def getGroovyInt(){ + return groovyInt + } +} diff --git a/spring-groovy-config/src/main/java/com/baeldung/spring_groovy_config/MainApp.java b/spring-groovy-config/src/main/java/com/baeldung/spring_groovy_config/MainApp.java new file mode 100644 index 0000000000..ac85963cf7 --- /dev/null +++ b/spring-groovy-config/src/main/java/com/baeldung/spring_groovy_config/MainApp.java @@ -0,0 +1,14 @@ +package com.baeldung.spring_groovy_config; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import org.springframework.context.ApplicationContext; +import org.springframework.context.support.GenericGroovyApplicationContext; + +public class MainApp { + public static void main(String[] args){ + ApplicationContext context = new GenericGroovyApplicationContext("file:config/groovyTestWithRefBean.groovy"); + ClassWithRef test = (ClassWithRef) context.getBean("classWithRef"); + } +} diff --git a/spring-groovy-config/src/main/java/com/baeldung/spring_groovy_config/TestClass.java b/spring-groovy-config/src/main/java/com/baeldung/spring_groovy_config/TestClass.java new file mode 100644 index 0000000000..75b6b5fc5b --- /dev/null +++ b/spring-groovy-config/src/main/java/com/baeldung/spring_groovy_config/TestClass.java @@ -0,0 +1,22 @@ +package com.baeldung.spring_groovy_config; + +import org.springframework.stereotype.Component; + +@Component +public class TestClass { + private String testString; + private double testDouble; + + public TestClass(String testString, double testDouble){ + this.testString = testString; + this.testDouble = testDouble; + } + + public String getTestString(){ + return this.testString; + } + + public double getTestDouble(){ + return this.testDouble; + } +} diff --git a/spring-groovy-config/src/main/java/com/baeldung/spring_groovy_config/TestClassB.java b/spring-groovy-config/src/main/java/com/baeldung/spring_groovy_config/TestClassB.java new file mode 100644 index 0000000000..d2387c87e5 --- /dev/null +++ b/spring-groovy-config/src/main/java/com/baeldung/spring_groovy_config/TestClassB.java @@ -0,0 +1,26 @@ +package com.baeldung.spring_groovy_config; + +import org.springframework.stereotype.Component; + +@Component +public class TestClassB { + private String testStringB; + private int testIntB; + + public void setTestStringB(String testStringB){ + this.testStringB = testStringB; + } + + public String getTestStringB(){ + return this.testStringB; + } + + + public void setTestIntB(int testIntB){ + this.testIntB = testIntB; + } + + public int getTestIntB(){ + return this.testIntB; + } +} diff --git a/spring-groovy-config/src/test/java/com/baeldung/spring_groovy_config/AppTest.java b/spring-groovy-config/src/test/java/com/baeldung/spring_groovy_config/AppTest.java new file mode 100644 index 0000000000..93de401e0c --- /dev/null +++ b/spring-groovy-config/src/test/java/com/baeldung/spring_groovy_config/AppTest.java @@ -0,0 +1,57 @@ +package com.baeldung.spring_groovy_config; + +import com.baeldung.spring_groovy_config.*; +import groovy.lang.Binding; +import junit.framework.TestCase; +import static org.junit.Assert.*; +import org.junit.Test; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.context.support.FileSystemXmlApplicationContext; +import org.springframework.context.support.GenericApplicationContext; +import org.springframework.context.support.GenericGroovyApplicationContext; +import org.springframework.core.env.ConfigurableEnvironment; +/** + * Spring Framework Tests for Groovy. + */ +public class AppTest{ + + @Test + public void testSimple(){ + ApplicationContext context = new GenericGroovyApplicationContext("file:config/groovyContextWithConstructor.groovy"); + TestClass test = (TestClass) context.getBean("testClass"); + assertNotNull(test.getTestString()); + assertEquals(test.getTestString(),"Test String"); + assertTrue(test.getTestDouble() == 10.2); + + String testString = context.getBean("testString",String.class); + assertEquals(testString,"Test String"); + } + + + @Test + public void testProperties(){ + ApplicationContext context = new GenericGroovyApplicationContext("file:config/groovyPropConfig.groovy"); + TestClassB test = (TestClassB) context.getBean("testClassB"); + assertNotNull(test.getTestStringB()); + assertEquals(test.getTestStringB(),"Test String"); + assertTrue(test.getTestIntB() == 10); + } + + @Test + public void testPropertiesWithClosure(){ + ApplicationContext context = new GenericGroovyApplicationContext("file:config/groovyPropConfigWithClosure.groovy"); + TestClassB test = (TestClassB) context.getBean("testClassB"); + assertNotNull(test.getTestStringB()); + assertEquals(test.getTestStringB(),"Test String"); + assertTrue(test.getTestIntB() == 10); + } + + + @Test + public void testWithRef(){ + ApplicationContext context = new GenericGroovyApplicationContext("file:config/groovyTestWithRefBean.groovy"); + ClassWithRef test = (ClassWithRef) context.getBean("classWithRef"); + assertEquals(test.getMyClass().getGroovyInt(),5); + } +} From 3371c9047a3d7d704ff0583dd4551a40be89183e Mon Sep 17 00:00:00 2001 From: Nancy Bosecker Date: Mon, 20 Feb 2017 17:28:16 -0800 Subject: [PATCH 33/68] Moved code to apache-solrj from spring-data-solr, updated code to 6.4.0 solrj, fixed dependencies in both modules (#1196) * Solr w Apache SolrJ * Solr w Apache SolrJ * updated test names and moved add to @before method * create apache-solrj module, moved code from spring-data-solr --- apache-solrj/pom.xml | 50 +++++++++++++++++++ .../solrjava/SolrJavaIntegration.java | 43 ++++++++++++++++ .../solrjava/SolrJavaIntegrationTest.java | 24 +++------ pom.xml | 1 + spring-data-solr/pom.xml | 6 --- 5 files changed, 100 insertions(+), 24 deletions(-) create mode 100644 apache-solrj/pom.xml create mode 100644 apache-solrj/src/main/java/com/baeldung/solrjava/SolrJavaIntegration.java rename {spring-data-solr => apache-solrj}/src/test/java/com/baeldung/solrjava/SolrJavaIntegrationTest.java (64%) diff --git a/apache-solrj/pom.xml b/apache-solrj/pom.xml new file mode 100644 index 0000000000..74daeae55c --- /dev/null +++ b/apache-solrj/pom.xml @@ -0,0 +1,50 @@ + + 4.0.0 + com.baeldung + apache-solrj + 0.0.1-SNAPSHOT + jar + apache-solrj + + + 4.12 + 2.19.1 + + + + + org.apache.solr + solr-solrj + 6.4.0 + + + junit + junit + ${junit.version} + test + + + + + + + maven-compiler-plugin + 2.3.2 + + + org.apache.maven.plugins + maven-surefire-plugin + ${maven-surefire-plugin.version} + + 1.8 + 1.8 + + **/*IntegrationTest.java + **/*LiveTest.java + + + + + + \ No newline at end of file diff --git a/apache-solrj/src/main/java/com/baeldung/solrjava/SolrJavaIntegration.java b/apache-solrj/src/main/java/com/baeldung/solrjava/SolrJavaIntegration.java new file mode 100644 index 0000000000..f2d21f0993 --- /dev/null +++ b/apache-solrj/src/main/java/com/baeldung/solrjava/SolrJavaIntegration.java @@ -0,0 +1,43 @@ +package com.baeldung.solrjava; + +import java.io.IOException; + +import org.apache.solr.client.solrj.SolrServerException; +import org.apache.solr.client.solrj.impl.HttpSolrClient; +import org.apache.solr.client.solrj.impl.XMLResponseParser; +import org.apache.solr.common.SolrInputDocument; + +public class SolrJavaIntegration { + + private HttpSolrClient solrClient; + + public SolrJavaIntegration(String clientUrl) { + + solrClient = new HttpSolrClient.Builder(clientUrl).build(); + solrClient.setParser(new XMLResponseParser()); + } + + public void addSolrDocument(String documentId, String itemName, String itemPrice) throws SolrServerException, IOException { + + SolrInputDocument document = new SolrInputDocument(); + document.addField("id", documentId); + document.addField("name", itemName); + document.addField("price", itemPrice); + solrClient.add(document); + solrClient.commit(); + } + + public void deleteSolrDocument(String documentId) throws SolrServerException, IOException { + + solrClient.deleteById(documentId); + solrClient.commit(); + } + + protected HttpSolrClient getSolrClient() { + return solrClient; + } + + protected void setSolrClient(HttpSolrClient solrClient) { + this.solrClient = solrClient; + } +} diff --git a/spring-data-solr/src/test/java/com/baeldung/solrjava/SolrJavaIntegrationTest.java b/apache-solrj/src/test/java/com/baeldung/solrjava/SolrJavaIntegrationTest.java similarity index 64% rename from spring-data-solr/src/test/java/com/baeldung/solrjava/SolrJavaIntegrationTest.java rename to apache-solrj/src/test/java/com/baeldung/solrjava/SolrJavaIntegrationTest.java index ce90ccaf16..22f9eae8ee 100644 --- a/spring-data-solr/src/test/java/com/baeldung/solrjava/SolrJavaIntegrationTest.java +++ b/apache-solrj/src/test/java/com/baeldung/solrjava/SolrJavaIntegrationTest.java @@ -6,32 +6,21 @@ import java.io.IOException; import org.apache.solr.client.solrj.SolrQuery; import org.apache.solr.client.solrj.SolrServerException; -import org.apache.solr.client.solrj.impl.HttpSolrClient; -import org.apache.solr.client.solrj.impl.XMLResponseParser; import org.apache.solr.client.solrj.response.QueryResponse; import org.apache.solr.common.SolrDocument; import org.apache.solr.common.SolrDocumentList; -import org.apache.solr.common.SolrInputDocument; import org.junit.Before; import org.junit.Test; public class SolrJavaIntegrationTest { - private HttpSolrClient solr; - private SolrInputDocument document; + private SolrJavaIntegration solrJavaIntegration; @Before public void setUp() throws Exception { - solr = new HttpSolrClient("http://localhost:8983/solr/bigboxstore"); - solr.setParser(new XMLResponseParser()); - - document = new SolrInputDocument(); - document.addField("id", "123456"); - document.addField("name", "Kenmore Dishwasher"); - document.addField("price", "599.99"); - solr.add(document); - solr.commit(); + solrJavaIntegration = new SolrJavaIntegration("http://localhost:8983/solr/bigboxstore"); + solrJavaIntegration.addSolrDocument("123456", "Kenmore Dishwasher", "599.99"); } @Test @@ -41,7 +30,7 @@ public class SolrJavaIntegrationTest { query.set("q", "id:123456"); QueryResponse response = null; - response = solr.query(query); + response = solrJavaIntegration.getSolrClient().query(query); SolrDocumentList docList = response.getResults(); assertEquals(docList.getNumFound(), 1); @@ -55,14 +44,13 @@ public class SolrJavaIntegrationTest { @Test public void whenDelete_thenVerifyDeleted() throws SolrServerException, IOException { - solr.deleteById("123456"); - solr.commit(); + solrJavaIntegration.deleteSolrDocument("123456"); SolrQuery query = new SolrQuery(); query.set("q", "id:123456"); QueryResponse response = null; - response = solr.query(query); + response = solrJavaIntegration.getSolrClient().query(query); SolrDocumentList docList = response.getResults(); assertEquals(docList.getNumFound(), 0); diff --git a/pom.xml b/pom.xml index 82df776044..aa58b1d2e9 100644 --- a/pom.xml +++ b/pom.xml @@ -194,6 +194,7 @@ struts2 apache-velocity + apache-solrj diff --git a/spring-data-solr/pom.xml b/spring-data-solr/pom.xml index 2aa9f86a96..e43b3ff774 100644 --- a/spring-data-solr/pom.xml +++ b/spring-data-solr/pom.xml @@ -51,12 +51,6 @@ ${spring.version} test
- From a75ed7e70ac5f7ec46b372661d015200da20a9c6 Mon Sep 17 00:00:00 2001 From: Daniele Demichelis Date: Tue, 21 Feb 2017 04:38:52 +0100 Subject: [PATCH 34/68] BAEL-554 Spring Remoting with Hessian and Burlap (#1166) * Burlap & Hessian server added * Burlap & Hessian client work * Fixed main * Fixed formatting --- spring-remoting/pom.xml | 12 +++--- .../remoting-hessian-burlap/client/pom.xml | 35 ++++++++++++++++++ .../com/baeldung/client/BurlapClient.java | 28 ++++++++++++++ .../com/baeldung/client/HessianClient.java | 28 ++++++++++++++ .../remoting-hessian-burlap/pom.xml | 20 ++++++++++ .../remoting-hessian-burlap/server/pom.xml | 30 +++++++++++++++ .../main/java/com/baeldung/server/Server.java | 37 +++++++++++++++++++ 7 files changed, 185 insertions(+), 5 deletions(-) create mode 100644 spring-remoting/remoting-hessian-burlap/client/pom.xml create mode 100644 spring-remoting/remoting-hessian-burlap/client/src/main/java/com/baeldung/client/BurlapClient.java create mode 100644 spring-remoting/remoting-hessian-burlap/client/src/main/java/com/baeldung/client/HessianClient.java create mode 100644 spring-remoting/remoting-hessian-burlap/pom.xml create mode 100644 spring-remoting/remoting-hessian-burlap/server/pom.xml create mode 100644 spring-remoting/remoting-hessian-burlap/server/src/main/java/com/baeldung/server/Server.java diff --git a/spring-remoting/pom.xml b/spring-remoting/pom.xml index a43dd52a3e..52d670a726 100644 --- a/spring-remoting/pom.xml +++ b/spring-remoting/pom.xml @@ -3,16 +3,17 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 + com.baeldung + spring-remoting + pom + 1.0-SNAPSHOT + spring-remoting + Parent for all projects related to Spring Remoting. org.springframework.boot spring-boot-starter-parent 1.4.3.RELEASE - com.baeldung - spring-remoting - 1.0-SNAPSHOT - Parent for all projects related to Spring Remoting. - pom 1.8 @@ -34,6 +35,7 @@ remoting-http + remoting-hessian-burlap \ No newline at end of file diff --git a/spring-remoting/remoting-hessian-burlap/client/pom.xml b/spring-remoting/remoting-hessian-burlap/client/pom.xml new file mode 100644 index 0000000000..11250e63d2 --- /dev/null +++ b/spring-remoting/remoting-hessian-burlap/client/pom.xml @@ -0,0 +1,35 @@ + + + + remoting-hessian-burlap + com.baeldung + 1.0-SNAPSHOT + + 4.0.0 + + spring-remoting-hessian-burlap-client + + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-tomcat + + + + + ${project.groupId} + api + + + com.caucho + hessian + 4.0.38 + + + \ No newline at end of file diff --git a/spring-remoting/remoting-hessian-burlap/client/src/main/java/com/baeldung/client/BurlapClient.java b/spring-remoting/remoting-hessian-burlap/client/src/main/java/com/baeldung/client/BurlapClient.java new file mode 100644 index 0000000000..477c29eb26 --- /dev/null +++ b/spring-remoting/remoting-hessian-burlap/client/src/main/java/com/baeldung/client/BurlapClient.java @@ -0,0 +1,28 @@ +package com.baeldung.client; + +import com.baeldung.api.BookingException; +import com.baeldung.api.CabBookingService; +import org.springframework.boot.SpringApplication; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.remoting.caucho.BurlapProxyFactoryBean; + +import static java.lang.System.out; + +@Configuration +public class BurlapClient { + + @Bean + public BurlapProxyFactoryBean burlapInvoker() { + BurlapProxyFactoryBean invoker = new BurlapProxyFactoryBean(); + invoker.setServiceUrl("http://localhost:8080/b_booking"); + invoker.setServiceInterface(CabBookingService.class); + return invoker; + } + + public static void main(String[] args) throws BookingException { + CabBookingService service = SpringApplication.run(BurlapClient.class, args).getBean(CabBookingService.class); + out.println(service.bookRide("13 Seagate Blvd, Key Largo, FL 33037")); + } + +} diff --git a/spring-remoting/remoting-hessian-burlap/client/src/main/java/com/baeldung/client/HessianClient.java b/spring-remoting/remoting-hessian-burlap/client/src/main/java/com/baeldung/client/HessianClient.java new file mode 100644 index 0000000000..b5f366094e --- /dev/null +++ b/spring-remoting/remoting-hessian-burlap/client/src/main/java/com/baeldung/client/HessianClient.java @@ -0,0 +1,28 @@ +package com.baeldung.client; + +import com.baeldung.api.BookingException; +import com.baeldung.api.CabBookingService; +import org.springframework.boot.SpringApplication; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.remoting.caucho.HessianProxyFactoryBean; + +import static java.lang.System.out; + +@Configuration +public class HessianClient { + + @Bean + public HessianProxyFactoryBean hessianInvoker() { + HessianProxyFactoryBean invoker = new HessianProxyFactoryBean(); + invoker.setServiceUrl("http://localhost:8080/booking"); + invoker.setServiceInterface(CabBookingService.class); + return invoker; + } + + public static void main(String[] args) throws BookingException { + CabBookingService service = SpringApplication.run(HessianClient.class, args).getBean(CabBookingService.class); + out.println(service.bookRide("13 Seagate Blvd, Key Largo, FL 33037")); + } + +} diff --git a/spring-remoting/remoting-hessian-burlap/pom.xml b/spring-remoting/remoting-hessian-burlap/pom.xml new file mode 100644 index 0000000000..682e460880 --- /dev/null +++ b/spring-remoting/remoting-hessian-burlap/pom.xml @@ -0,0 +1,20 @@ + + + + spring-remoting + com.baeldung + 1.0-SNAPSHOT + + pom + + server + client + + 4.0.0 + + remoting-hessian-burlap + + + \ No newline at end of file diff --git a/spring-remoting/remoting-hessian-burlap/server/pom.xml b/spring-remoting/remoting-hessian-burlap/server/pom.xml new file mode 100644 index 0000000000..c97092b247 --- /dev/null +++ b/spring-remoting/remoting-hessian-burlap/server/pom.xml @@ -0,0 +1,30 @@ + + + + remoting-hessian-burlap + com.baeldung + 1.0-SNAPSHOT + + 4.0.0 + + remoting-hessian-burlap-server + + + + com.baeldung + spring-remoting-http-server + 1.0-SNAPSHOT + + + org.springframework.boot + spring-boot-starter-web + + + com.caucho + hessian + 4.0.38 + + + \ No newline at end of file diff --git a/spring-remoting/remoting-hessian-burlap/server/src/main/java/com/baeldung/server/Server.java b/spring-remoting/remoting-hessian-burlap/server/src/main/java/com/baeldung/server/Server.java new file mode 100644 index 0000000000..9b7e463871 --- /dev/null +++ b/spring-remoting/remoting-hessian-burlap/server/src/main/java/com/baeldung/server/Server.java @@ -0,0 +1,37 @@ +package com.baeldung.server; + +import com.baeldung.api.CabBookingService; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.remoting.caucho.BurlapServiceExporter; +import org.springframework.remoting.caucho.HessianServiceExporter; +import org.springframework.remoting.support.RemoteExporter; + +@Configuration @ComponentScan @EnableAutoConfiguration public class Server { + + @Bean CabBookingService bookingService() { + return new CabBookingServiceImpl(); + } + + @Bean(name = "/booking") RemoteExporter hessianService(CabBookingService service) { + HessianServiceExporter exporter = new HessianServiceExporter(); + exporter.setService(bookingService()); + exporter.setServiceInterface(CabBookingService.class); + return exporter; + } + + @Bean(name = "/b_booking") RemoteExporter burlapService(CabBookingService service) { + BurlapServiceExporter exporter = new BurlapServiceExporter(); + exporter.setService(bookingService()); + exporter.setServiceInterface(CabBookingService.class); + return exporter; + } + + public static void main(String[] args) { + SpringApplication.run(Server.class, args); + } + +} \ No newline at end of file From d3d11a18f3d05e37ddd284cb6b4b2c06283c7a0f Mon Sep 17 00:00:00 2001 From: Grzegorz Piwowarek Date: Tue, 21 Feb 2017 07:35:07 +0100 Subject: [PATCH 35/68] Revert "Spring Groovy Config with fixed pom" (#1209) --- .../com/baeldung/annotationinjection/App.java | 48 ------------- .../annotationinjection/AppConfig.java | 23 ------- .../baeldung/annotationinjection/AppTest.java | 52 -------------- .../annotationinjection/AutowireObject.java | 6 -- .../annotationinjection/ImportConfig.java | 20 ------ .../annotationinjection/InjectedClass.java | 39 ----------- spring-groovy-config/.gitignore | 1 - .../org.eclipse.core.resources.prefs | 4 -- .../.settings/org.eclipse.jdt.core.prefs | 5 -- .../org.eclipse.jdt.groovy.core.prefs | 2 - .../.settings/org.eclipse.m2e.core.prefs | 4 -- .../groovyContextWithConstructor.groovy | 9 --- .../config/groovyPropConfig.groovy | 5 -- .../config/groovyPropConfigWithClosure.groovy | 8 --- .../config/groovyTestWithRefBean.groovy | 10 --- spring-groovy-config/pom.xml | 67 ------------------- .../spring_groovy_config/ClassWithRef.java | 18 ----- .../spring_groovy_config/GroovyClass.groovy | 9 --- .../spring_groovy_config/MainApp.java | 14 ---- .../spring_groovy_config/TestClass.java | 22 ------ .../spring_groovy_config/TestClassB.java | 26 ------- .../spring_groovy_config/AppTest.java | 57 ---------------- 22 files changed, 449 deletions(-) delete mode 100644 spring-core/src/main/java/com/baeldung/annotationinjection/App.java delete mode 100644 spring-core/src/main/java/com/baeldung/annotationinjection/AppConfig.java delete mode 100644 spring-core/src/main/java/com/baeldung/annotationinjection/AppTest.java delete mode 100644 spring-core/src/main/java/com/baeldung/annotationinjection/AutowireObject.java delete mode 100644 spring-core/src/main/java/com/baeldung/annotationinjection/ImportConfig.java delete mode 100644 spring-core/src/main/java/com/baeldung/annotationinjection/InjectedClass.java delete mode 100644 spring-groovy-config/.gitignore delete mode 100644 spring-groovy-config/.settings/org.eclipse.core.resources.prefs delete mode 100644 spring-groovy-config/.settings/org.eclipse.jdt.core.prefs delete mode 100644 spring-groovy-config/.settings/org.eclipse.jdt.groovy.core.prefs delete mode 100644 spring-groovy-config/.settings/org.eclipse.m2e.core.prefs delete mode 100644 spring-groovy-config/config/groovyContextWithConstructor.groovy delete mode 100644 spring-groovy-config/config/groovyPropConfig.groovy delete mode 100644 spring-groovy-config/config/groovyPropConfigWithClosure.groovy delete mode 100644 spring-groovy-config/config/groovyTestWithRefBean.groovy delete mode 100644 spring-groovy-config/pom.xml delete mode 100644 spring-groovy-config/src/main/java/com/baeldung/spring_groovy_config/ClassWithRef.java delete mode 100644 spring-groovy-config/src/main/java/com/baeldung/spring_groovy_config/GroovyClass.groovy delete mode 100644 spring-groovy-config/src/main/java/com/baeldung/spring_groovy_config/MainApp.java delete mode 100644 spring-groovy-config/src/main/java/com/baeldung/spring_groovy_config/TestClass.java delete mode 100644 spring-groovy-config/src/main/java/com/baeldung/spring_groovy_config/TestClassB.java delete mode 100644 spring-groovy-config/src/test/java/com/baeldung/spring_groovy_config/AppTest.java diff --git a/spring-core/src/main/java/com/baeldung/annotationinjection/App.java b/spring-core/src/main/java/com/baeldung/annotationinjection/App.java deleted file mode 100644 index dc9560286d..0000000000 --- a/spring-core/src/main/java/com/baeldung/annotationinjection/App.java +++ /dev/null @@ -1,48 +0,0 @@ -package com.baeldung.java_bean_injection; - -import org.springframework.context.ApplicationContext; -import org.springframework.context.annotation.AnnotationConfigApplicationContext; -import org.springframework.context.support.ClassPathXmlApplicationContext; - -import com.baeldung.java_bean_injection.*; - -import org.springframework.context.ApplicationContext; -import org.springframework.context.support.ClassPathXmlApplicationContext; - - -import junit.framework.TestResult; -import junit.framework.TestFailure; - -import java.util.Enumeration; - - -/** - * Bean Injection Test Application - * - */ -public class App -{ - public static void main( String[] args ) - { - - TestResult result = new TestResult(); - AppTest.suite().run(result); - System.out.println(String.format("Tests: %d",result.runCount())); - System.out.println(String.format("Errors: %d",result.errorCount())); - System.out.println(String.format("Failures: %d",result.failureCount())); - if(result.failureCount() > 0){ - Enumeration failures = result.failures(); - int failNum = 0; - TestFailure failure = null; - while(failures.hasMoreElements()){ - failure = failures.nextElement(); - - System.out.println(failure.exceptionMessage()); - System.out.println(String.format("Test Failure %d\n",failNum)); - System.out.println(failure.trace()); - System.out.print("\n"); - } - } - - } -} diff --git a/spring-core/src/main/java/com/baeldung/annotationinjection/AppConfig.java b/spring-core/src/main/java/com/baeldung/annotationinjection/AppConfig.java deleted file mode 100644 index 138bda6b1b..0000000000 --- a/spring-core/src/main/java/com/baeldung/annotationinjection/AppConfig.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.baeldung.java_bean_injection; - -import org.springframework.beans.factory.annotation.*; -import org.springframework.context.annotation.*; -import org.springframework.stereotype.*; - -import com.baeldung.java_bean_injection.*; - -@Configuration -@PropertySource(value="classpath:inject.properties") -@Import(ImportConfig.class) -public class AppConfig { - @Value("${contructor.arg1}") String constructorArg; - - @Bean - public InjectedClass injectedClass(){ - InjectedClass ic = new InjectedClass(constructorArg); - ic.setMyInt(10); - ic.setTestString("test"); - return ic; - } - -} diff --git a/spring-core/src/main/java/com/baeldung/annotationinjection/AppTest.java b/spring-core/src/main/java/com/baeldung/annotationinjection/AppTest.java deleted file mode 100644 index fa7b46f06b..0000000000 --- a/spring-core/src/main/java/com/baeldung/annotationinjection/AppTest.java +++ /dev/null @@ -1,52 +0,0 @@ -package com.baeldung.java_bean_injection; - -import junit.framework.Test; -import junit.framework.TestCase; -import junit.framework.TestSuite; - -import org.springframework.context.ApplicationContext; -import org.springframework.context.annotation.AnnotationConfigApplicationContext; -import org.springframework.context.support.ClassPathXmlApplicationContext; - -import com.baeldung.java_bean_injection.*; - -import java.io.File; -import java.util.Scanner; - -/** - * 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() - { - AnnotationConfigApplicationContext context= new AnnotationConfigApplicationContext(); - context.register(AppConfig.class); - context.refresh(); - InjectedClass injectedClass = (InjectedClass) context.getBean(InjectedClass.class); - assertTrue(injectedClass.getMyInt() == 10); - assertTrue(injectedClass.getTestString().equals("test")); - assertNotNull(injectedClass.obj); - assertTrue(injectedClass.myConstructorArg.equals("test")); - } -} diff --git a/spring-core/src/main/java/com/baeldung/annotationinjection/AutowireObject.java b/spring-core/src/main/java/com/baeldung/annotationinjection/AutowireObject.java deleted file mode 100644 index df5859f0af..0000000000 --- a/spring-core/src/main/java/com/baeldung/annotationinjection/AutowireObject.java +++ /dev/null @@ -1,6 +0,0 @@ -package com.baeldung.java_bean_injection; - - -public class AutowireObject { - -} diff --git a/spring-core/src/main/java/com/baeldung/annotationinjection/ImportConfig.java b/spring-core/src/main/java/com/baeldung/annotationinjection/ImportConfig.java deleted file mode 100644 index e37d94931b..0000000000 --- a/spring-core/src/main/java/com/baeldung/annotationinjection/ImportConfig.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.baeldung.java_bean_injection; - -import org.springframework.context.annotation.*; - -import com.baeldung.java_bean_injection.*; - -@Configuration -public class ImportConfig { - - @Bean(name="autobean") - public AutowireObject autowireObject(){ - return new AutowireObject(); - } - - - @Bean(name="autobean2") - public AutowireObject autowireObject2(){ - return new AutowireObject(); - } -} diff --git a/spring-core/src/main/java/com/baeldung/annotationinjection/InjectedClass.java b/spring-core/src/main/java/com/baeldung/annotationinjection/InjectedClass.java deleted file mode 100644 index 55fe733db5..0000000000 --- a/spring-core/src/main/java/com/baeldung/annotationinjection/InjectedClass.java +++ /dev/null @@ -1,39 +0,0 @@ -package com.baeldung.java_bean_injection; - -import org.springframework.beans.factory.annotation.*; -import org.springframework.context.annotation.*; - -import com.baeldung.java_bean_injection.*; - - -public class InjectedClass { - private int myInt; - private String testString; - String myConstructorArg; - - - @Autowired - @Qualifier("autobean") - AutowireObject obj; - - public InjectedClass(String constArg){ - this.myConstructorArg = constArg; - } - - public void setMyInt(int myInt){ - this.myInt = myInt; - } - - public void setTestString(String testString){ - this.testString = testString; - } - - public int getMyInt(){ - return this.myInt; - } - - public String getTestString(){ - return this.testString; - } - -} diff --git a/spring-groovy-config/.gitignore b/spring-groovy-config/.gitignore deleted file mode 100644 index b83d22266a..0000000000 --- a/spring-groovy-config/.gitignore +++ /dev/null @@ -1 +0,0 @@ -/target/ diff --git a/spring-groovy-config/.settings/org.eclipse.core.resources.prefs b/spring-groovy-config/.settings/org.eclipse.core.resources.prefs deleted file mode 100644 index f9fe34593f..0000000000 --- a/spring-groovy-config/.settings/org.eclipse.core.resources.prefs +++ /dev/null @@ -1,4 +0,0 @@ -eclipse.preferences.version=1 -encoding//src/main/java=UTF-8 -encoding//src/test/java=UTF-8 -encoding/=UTF-8 diff --git a/spring-groovy-config/.settings/org.eclipse.jdt.core.prefs b/spring-groovy-config/.settings/org.eclipse.jdt.core.prefs deleted file mode 100644 index abec6ca389..0000000000 --- a/spring-groovy-config/.settings/org.eclipse.jdt.core.prefs +++ /dev/null @@ -1,5 +0,0 @@ -eclipse.preferences.version=1 -org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5 -org.eclipse.jdt.core.compiler.compliance=1.5 -org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning -org.eclipse.jdt.core.compiler.source=1.5 diff --git a/spring-groovy-config/.settings/org.eclipse.jdt.groovy.core.prefs b/spring-groovy-config/.settings/org.eclipse.jdt.groovy.core.prefs deleted file mode 100644 index ae98feaa79..0000000000 --- a/spring-groovy-config/.settings/org.eclipse.jdt.groovy.core.prefs +++ /dev/null @@ -1,2 +0,0 @@ -eclipse.preferences.version=1 -groovy.compiler.level=24 diff --git a/spring-groovy-config/.settings/org.eclipse.m2e.core.prefs b/spring-groovy-config/.settings/org.eclipse.m2e.core.prefs deleted file mode 100644 index f897a7f1cb..0000000000 --- a/spring-groovy-config/.settings/org.eclipse.m2e.core.prefs +++ /dev/null @@ -1,4 +0,0 @@ -activeProfiles= -eclipse.preferences.version=1 -resolveWorkspaceProjects=true -version=1 diff --git a/spring-groovy-config/config/groovyContextWithConstructor.groovy b/spring-groovy-config/config/groovyContextWithConstructor.groovy deleted file mode 100644 index dbde5f3822..0000000000 --- a/spring-groovy-config/config/groovyContextWithConstructor.groovy +++ /dev/null @@ -1,9 +0,0 @@ -import com.baeldung.spring_groovy_config.TestClass - -beans{ - testString String, 'Test String' - testClass(TestClass){ - beanDefinition -> - beanDefinition.constructorArgs = ["Test String",10.2] - } -} \ No newline at end of file diff --git a/spring-groovy-config/config/groovyPropConfig.groovy b/spring-groovy-config/config/groovyPropConfig.groovy deleted file mode 100644 index 08d3acf354..0000000000 --- a/spring-groovy-config/config/groovyPropConfig.groovy +++ /dev/null @@ -1,5 +0,0 @@ -import com.baeldung.spring_groovy_config.TestClassB - -beans{ - testClassB(TestClassB,testStringB:"Test String",testIntB:10.2) -} \ No newline at end of file diff --git a/spring-groovy-config/config/groovyPropConfigWithClosure.groovy b/spring-groovy-config/config/groovyPropConfigWithClosure.groovy deleted file mode 100644 index 3ca306bdd7..0000000000 --- a/spring-groovy-config/config/groovyPropConfigWithClosure.groovy +++ /dev/null @@ -1,8 +0,0 @@ -import com.baeldung.spring_groovy_config.TestClassB - -beans{ - testClassB(TestClassB){ - testStringB = "Test String" - testIntB = 10 - } -} \ No newline at end of file diff --git a/spring-groovy-config/config/groovyTestWithRefBean.groovy b/spring-groovy-config/config/groovyTestWithRefBean.groovy deleted file mode 100644 index d079480d46..0000000000 --- a/spring-groovy-config/config/groovyTestWithRefBean.groovy +++ /dev/null @@ -1,10 +0,0 @@ -import com.baeldung.spring_groovy_config.GroovyClass -import com.baeldung.spring_groovy_config.ClassWithRef - -beans{ - groovyClass(GroovyClass, groovyInt:5) - classWithRef(ClassWithRef){ - myClass = groovyClass - } - -} \ No newline at end of file diff --git a/spring-groovy-config/pom.xml b/spring-groovy-config/pom.xml deleted file mode 100644 index f6c63ae232..0000000000 --- a/spring-groovy-config/pom.xml +++ /dev/null @@ -1,67 +0,0 @@ - - - 4.0.0 - com.baeldung - spring-groovy-config - 0.0.1-SNAPSHOT - jar - spring-groovy-config - http://maven.apache.org - - UTF-8 - - - - - maven-compiler-plugin - - 3.1 - - groovy-eclipse-compiler - - - - org.codehaus.groovy - groovy-eclipse-compiler - 2.9.1-01 - - - - org.codehaus.groovy - groovy-eclipse-batch - 2.3.7-01 - - - - - - - - org.codehaus.groovy - groovy-all - 2.4.8 - - - junit - junit - 4.12 - test - - - org.springframework - spring-context - 4.3.6.RELEASE - - - org.springframework - spring-core - 4.3.6.RELEASE - - - org.springframework - spring-test - 4.3.6.RELEASE - - - - diff --git a/spring-groovy-config/src/main/java/com/baeldung/spring_groovy_config/ClassWithRef.java b/spring-groovy-config/src/main/java/com/baeldung/spring_groovy_config/ClassWithRef.java deleted file mode 100644 index ec8987f387..0000000000 --- a/spring-groovy-config/src/main/java/com/baeldung/spring_groovy_config/ClassWithRef.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.baeldung.spring_groovy_config; - -import org.springframework.stereotype.Component; - -import com.baeldung.spring_groovy_config.GroovyClass; - -@Component -public class ClassWithRef { - private GroovyClass myClass = null; - - public void setMyClass(GroovyClass myClass){ - this.myClass = myClass; - } - - public GroovyClass getMyClass(){ - return this.myClass; - } -} diff --git a/spring-groovy-config/src/main/java/com/baeldung/spring_groovy_config/GroovyClass.groovy b/spring-groovy-config/src/main/java/com/baeldung/spring_groovy_config/GroovyClass.groovy deleted file mode 100644 index 2a1ef6a2c5..0000000000 --- a/spring-groovy-config/src/main/java/com/baeldung/spring_groovy_config/GroovyClass.groovy +++ /dev/null @@ -1,9 +0,0 @@ -package com.baeldung.spring_groovy_config - -class GroovyClass { - int groovyInt = 0 - - def getGroovyInt(){ - return groovyInt - } -} diff --git a/spring-groovy-config/src/main/java/com/baeldung/spring_groovy_config/MainApp.java b/spring-groovy-config/src/main/java/com/baeldung/spring_groovy_config/MainApp.java deleted file mode 100644 index ac85963cf7..0000000000 --- a/spring-groovy-config/src/main/java/com/baeldung/spring_groovy_config/MainApp.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.baeldung.spring_groovy_config; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; - -import org.springframework.context.ApplicationContext; -import org.springframework.context.support.GenericGroovyApplicationContext; - -public class MainApp { - public static void main(String[] args){ - ApplicationContext context = new GenericGroovyApplicationContext("file:config/groovyTestWithRefBean.groovy"); - ClassWithRef test = (ClassWithRef) context.getBean("classWithRef"); - } -} diff --git a/spring-groovy-config/src/main/java/com/baeldung/spring_groovy_config/TestClass.java b/spring-groovy-config/src/main/java/com/baeldung/spring_groovy_config/TestClass.java deleted file mode 100644 index 75b6b5fc5b..0000000000 --- a/spring-groovy-config/src/main/java/com/baeldung/spring_groovy_config/TestClass.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.baeldung.spring_groovy_config; - -import org.springframework.stereotype.Component; - -@Component -public class TestClass { - private String testString; - private double testDouble; - - public TestClass(String testString, double testDouble){ - this.testString = testString; - this.testDouble = testDouble; - } - - public String getTestString(){ - return this.testString; - } - - public double getTestDouble(){ - return this.testDouble; - } -} diff --git a/spring-groovy-config/src/main/java/com/baeldung/spring_groovy_config/TestClassB.java b/spring-groovy-config/src/main/java/com/baeldung/spring_groovy_config/TestClassB.java deleted file mode 100644 index d2387c87e5..0000000000 --- a/spring-groovy-config/src/main/java/com/baeldung/spring_groovy_config/TestClassB.java +++ /dev/null @@ -1,26 +0,0 @@ -package com.baeldung.spring_groovy_config; - -import org.springframework.stereotype.Component; - -@Component -public class TestClassB { - private String testStringB; - private int testIntB; - - public void setTestStringB(String testStringB){ - this.testStringB = testStringB; - } - - public String getTestStringB(){ - return this.testStringB; - } - - - public void setTestIntB(int testIntB){ - this.testIntB = testIntB; - } - - public int getTestIntB(){ - return this.testIntB; - } -} diff --git a/spring-groovy-config/src/test/java/com/baeldung/spring_groovy_config/AppTest.java b/spring-groovy-config/src/test/java/com/baeldung/spring_groovy_config/AppTest.java deleted file mode 100644 index 93de401e0c..0000000000 --- a/spring-groovy-config/src/test/java/com/baeldung/spring_groovy_config/AppTest.java +++ /dev/null @@ -1,57 +0,0 @@ -package com.baeldung.spring_groovy_config; - -import com.baeldung.spring_groovy_config.*; -import groovy.lang.Binding; -import junit.framework.TestCase; -import static org.junit.Assert.*; -import org.junit.Test; -import org.springframework.context.ApplicationContext; -import org.springframework.context.ConfigurableApplicationContext; -import org.springframework.context.support.FileSystemXmlApplicationContext; -import org.springframework.context.support.GenericApplicationContext; -import org.springframework.context.support.GenericGroovyApplicationContext; -import org.springframework.core.env.ConfigurableEnvironment; -/** - * Spring Framework Tests for Groovy. - */ -public class AppTest{ - - @Test - public void testSimple(){ - ApplicationContext context = new GenericGroovyApplicationContext("file:config/groovyContextWithConstructor.groovy"); - TestClass test = (TestClass) context.getBean("testClass"); - assertNotNull(test.getTestString()); - assertEquals(test.getTestString(),"Test String"); - assertTrue(test.getTestDouble() == 10.2); - - String testString = context.getBean("testString",String.class); - assertEquals(testString,"Test String"); - } - - - @Test - public void testProperties(){ - ApplicationContext context = new GenericGroovyApplicationContext("file:config/groovyPropConfig.groovy"); - TestClassB test = (TestClassB) context.getBean("testClassB"); - assertNotNull(test.getTestStringB()); - assertEquals(test.getTestStringB(),"Test String"); - assertTrue(test.getTestIntB() == 10); - } - - @Test - public void testPropertiesWithClosure(){ - ApplicationContext context = new GenericGroovyApplicationContext("file:config/groovyPropConfigWithClosure.groovy"); - TestClassB test = (TestClassB) context.getBean("testClassB"); - assertNotNull(test.getTestStringB()); - assertEquals(test.getTestStringB(),"Test String"); - assertTrue(test.getTestIntB() == 10); - } - - - @Test - public void testWithRef(){ - ApplicationContext context = new GenericGroovyApplicationContext("file:config/groovyTestWithRefBean.groovy"); - ClassWithRef test = (ClassWithRef) context.getBean("classWithRef"); - assertEquals(test.getMyClass().getGroovyInt(),5); - } -} From 12193e96fa8b39a8444ea97586ffa13ee8730e30 Mon Sep 17 00:00:00 2001 From: Tomasz Lelek Date: Tue, 21 Feb 2017 17:29:29 +0100 Subject: [PATCH 36/68] BAEL-633 proper formatting and test naming --- .../src/main/java/com/baeldung/cglib/mixin/Class1.java | 8 ++++---- .../src/main/java/com/baeldung/cglib/mixin/Class2.java | 8 ++++---- .../main/java/com/baeldung/cglib/mixin/Interface1.java | 2 +- .../main/java/com/baeldung/cglib/mixin/Interface2.java | 2 +- .../com/baeldung/cglib/proxy/BeanGeneratorTest.java | 10 +++++++--- .../test/java/com/baeldung/cglib/proxy/MixinTest.java | 2 +- .../baeldung/cglib/proxy/PersonServiceProxyTest.java | 6 +++--- pom.xml | 2 +- 8 files changed, 22 insertions(+), 18 deletions(-) diff --git a/cglib/src/main/java/com/baeldung/cglib/mixin/Class1.java b/cglib/src/main/java/com/baeldung/cglib/mixin/Class1.java index 932951b4cd..fa0cba5b78 100644 --- a/cglib/src/main/java/com/baeldung/cglib/mixin/Class1.java +++ b/cglib/src/main/java/com/baeldung/cglib/mixin/Class1.java @@ -1,8 +1,8 @@ package com.baeldung.cglib.mixin; public class Class1 implements Interface1 { - @Override - public String first() { - return "first behaviour"; - } + @Override + public String first() { + return "first behaviour"; + } } \ No newline at end of file diff --git a/cglib/src/main/java/com/baeldung/cglib/mixin/Class2.java b/cglib/src/main/java/com/baeldung/cglib/mixin/Class2.java index b2b922a05d..0db0620ab8 100644 --- a/cglib/src/main/java/com/baeldung/cglib/mixin/Class2.java +++ b/cglib/src/main/java/com/baeldung/cglib/mixin/Class2.java @@ -1,8 +1,8 @@ package com.baeldung.cglib.mixin; public class Class2 implements Interface2 { - @Override - public String second() { - return "second behaviour"; - } + @Override + public String second() { + return "second behaviour"; + } } \ No newline at end of file diff --git a/cglib/src/main/java/com/baeldung/cglib/mixin/Interface1.java b/cglib/src/main/java/com/baeldung/cglib/mixin/Interface1.java index 5aab155c1d..56ad679cad 100644 --- a/cglib/src/main/java/com/baeldung/cglib/mixin/Interface1.java +++ b/cglib/src/main/java/com/baeldung/cglib/mixin/Interface1.java @@ -1,5 +1,5 @@ package com.baeldung.cglib.mixin; public interface Interface1 { - String first(); + String first(); } \ No newline at end of file diff --git a/cglib/src/main/java/com/baeldung/cglib/mixin/Interface2.java b/cglib/src/main/java/com/baeldung/cglib/mixin/Interface2.java index 7fd2e78608..0dfb8737ab 100644 --- a/cglib/src/main/java/com/baeldung/cglib/mixin/Interface2.java +++ b/cglib/src/main/java/com/baeldung/cglib/mixin/Interface2.java @@ -1,5 +1,5 @@ package com.baeldung.cglib.mixin; public interface Interface2 { - String second(); + String second(); } \ No newline at end of file diff --git a/cglib/src/test/java/com/baeldung/cglib/proxy/BeanGeneratorTest.java b/cglib/src/test/java/com/baeldung/cglib/proxy/BeanGeneratorTest.java index 32706409c5..67214cd32d 100644 --- a/cglib/src/test/java/com/baeldung/cglib/proxy/BeanGeneratorTest.java +++ b/cglib/src/test/java/com/baeldung/cglib/proxy/BeanGeneratorTest.java @@ -11,18 +11,22 @@ import static junit.framework.TestCase.assertEquals; public class BeanGeneratorTest { @Test - public void testBeanGenerator() throws Exception { + public void givenBeanCreator_whenAddPropery_classShouldHaveFieldValue() throws Exception { //given BeanGenerator beanGenerator = new BeanGenerator(); //when beanGenerator.addProperty("name", String.class); Object myBean = beanGenerator.create(); - Method setter = myBean.getClass().getMethod("setName", String.class); + Method setter = myBean + .getClass() + .getMethod("setName", String.class); setter.invoke(myBean, "some string value set by a cglib"); //then - Method getter = myBean.getClass().getMethod("getName"); + Method getter = myBean + .getClass() + .getMethod("getName"); assertEquals("some string value set by a cglib", getter.invoke(myBean)); } } diff --git a/cglib/src/test/java/com/baeldung/cglib/proxy/MixinTest.java b/cglib/src/test/java/com/baeldung/cglib/proxy/MixinTest.java index ac4b50af4c..186c66aa42 100644 --- a/cglib/src/test/java/com/baeldung/cglib/proxy/MixinTest.java +++ b/cglib/src/test/java/com/baeldung/cglib/proxy/MixinTest.java @@ -9,7 +9,7 @@ import static junit.framework.TestCase.assertEquals; public class MixinTest { @Test - public void testMixinBehaviour() throws Exception { + public void givenTwoClasses_whenMixtThemIntoOne_mixinShouldHaveMethodsFromBothClasses() throws Exception { //when Mixin mixin = Mixin.create( new Class[]{Interface1.class, Interface2.class, MixinInterface.class}, diff --git a/cglib/src/test/java/com/baeldung/cglib/proxy/PersonServiceProxyTest.java b/cglib/src/test/java/com/baeldung/cglib/proxy/PersonServiceProxyTest.java index e0ad017538..6208ed12b8 100644 --- a/cglib/src/test/java/com/baeldung/cglib/proxy/PersonServiceProxyTest.java +++ b/cglib/src/test/java/com/baeldung/cglib/proxy/PersonServiceProxyTest.java @@ -9,7 +9,7 @@ import static org.junit.Assert.assertEquals; public class PersonServiceProxyTest { @Test - public void testService() { + public void givenPersonService_whenSayHello_shouldReturnResult() { //given PersonService personService = new PersonService(); @@ -21,7 +21,7 @@ public class PersonServiceProxyTest { } @Test - public void testFixedValue() throws Exception { + public void givenEnhancerProxy_whenExtendPersonService_shouldInterceptMethod() throws Exception { //given Enhancer enhancer = new Enhancer(); enhancer.setSuperclass(PersonService.class); @@ -36,7 +36,7 @@ public class PersonServiceProxyTest { } @Test - public void testMethodInterceptor() throws Exception { + public void givenEnhancer_whenExecuteMethodOnProxy_shouldInterceptOnlyStringReturnTypeMethod() throws Exception { //given Enhancer enhancer = new Enhancer(); enhancer.setSuperclass(PersonService.class); diff --git a/pom.xml b/pom.xml index a32a204304..52e00edbb4 100644 --- a/pom.xml +++ b/pom.xml @@ -28,6 +28,7 @@ autovalue cdi + cglib core-java couchbase-sdk @@ -192,7 +193,6 @@ xstream struts2 - cglib apache-velocity From 72d5bfca82ffe4a4f98612e3e7da39a11558438e Mon Sep 17 00:00:00 2001 From: Adam InTae Gerard Date: Tue, 21 Feb 2017 18:15:35 -0600 Subject: [PATCH 37/68] BAEL-9 #3 (#1207) * BAEL-9 #3 * pom.xml fix --- pom.xml | 1 + spring-boot-servlet/.gitignore | 4 ++ spring-boot-servlet/README.md | 2 + spring-boot-servlet/pom.xml | 55 +++++++++++++++++++ .../src/main/java/META-INF/MANIFEST.MF | 2 + .../java/com/baeldung/ApplicationMain.java | 19 +++++++ .../configuration/WebAppInitializer.java | 32 +++++++++++ .../configuration/WebMvcConfigure.java | 40 ++++++++++++++ .../java/com/baeldung/props/Constants.java | 20 +++++++ .../com/baeldung/props/PropertyLoader.java | 27 +++++++++ .../baeldung/props/PropertySourcesLoader.java | 23 ++++++++ .../servlets/GenericCustomServlet.java | 18 ++++++ .../servlets/javaee/AnnotationServlet.java | 20 +++++++ .../servlets/javaee/EEWebXmlServlet.java | 20 +++++++ .../SpringRegistrationBeanServlet.java | 19 +++++++ .../embedded/EmbeddedTomcatExample.java | 16 ++++++ .../src/main/resources/application.properties | 10 ++++ .../src/main/resources/custom.properties | 4 ++ .../src/main/webapp/WEB-INF/context.xml | 12 ++++ .../src/main/webapp/WEB-INF/dispatcher.xml | 16 ++++++ .../src/main/webapp/WEB-INF/web.xml | 40 ++++++++++++++ .../src/main/webapp/annotationservlet.jsp | 1 + spring-boot-servlet/src/main/webapp/index.jsp | 1 + 23 files changed, 402 insertions(+) create mode 100644 spring-boot-servlet/.gitignore create mode 100644 spring-boot-servlet/README.md create mode 100644 spring-boot-servlet/pom.xml create mode 100644 spring-boot-servlet/src/main/java/META-INF/MANIFEST.MF create mode 100644 spring-boot-servlet/src/main/java/com/baeldung/ApplicationMain.java create mode 100644 spring-boot-servlet/src/main/java/com/baeldung/configuration/WebAppInitializer.java create mode 100644 spring-boot-servlet/src/main/java/com/baeldung/configuration/WebMvcConfigure.java create mode 100644 spring-boot-servlet/src/main/java/com/baeldung/props/Constants.java create mode 100644 spring-boot-servlet/src/main/java/com/baeldung/props/PropertyLoader.java create mode 100644 spring-boot-servlet/src/main/java/com/baeldung/props/PropertySourcesLoader.java create mode 100644 spring-boot-servlet/src/main/java/com/baeldung/servlets/GenericCustomServlet.java create mode 100644 spring-boot-servlet/src/main/java/com/baeldung/servlets/javaee/AnnotationServlet.java create mode 100644 spring-boot-servlet/src/main/java/com/baeldung/servlets/javaee/EEWebXmlServlet.java create mode 100644 spring-boot-servlet/src/main/java/com/baeldung/servlets/springboot/SpringRegistrationBeanServlet.java create mode 100644 spring-boot-servlet/src/main/java/com/baeldung/servlets/springboot/embedded/EmbeddedTomcatExample.java create mode 100644 spring-boot-servlet/src/main/resources/application.properties create mode 100644 spring-boot-servlet/src/main/resources/custom.properties create mode 100644 spring-boot-servlet/src/main/webapp/WEB-INF/context.xml create mode 100644 spring-boot-servlet/src/main/webapp/WEB-INF/dispatcher.xml create mode 100644 spring-boot-servlet/src/main/webapp/WEB-INF/web.xml create mode 100644 spring-boot-servlet/src/main/webapp/annotationservlet.jsp create mode 100644 spring-boot-servlet/src/main/webapp/index.jsp diff --git a/pom.xml b/pom.xml index aa58b1d2e9..924bd96ade 100644 --- a/pom.xml +++ b/pom.xml @@ -109,6 +109,7 @@ spring-autowire spring-batch spring-boot + spring-boot-servlet spring-cloud-data-flow spring-cloud spring-core diff --git a/spring-boot-servlet/.gitignore b/spring-boot-servlet/.gitignore new file mode 100644 index 0000000000..60be5b80aa --- /dev/null +++ b/spring-boot-servlet/.gitignore @@ -0,0 +1,4 @@ +/target/ +.settings/ +.classpath +.project diff --git a/spring-boot-servlet/README.md b/spring-boot-servlet/README.md new file mode 100644 index 0000000000..262a11fc36 --- /dev/null +++ b/spring-boot-servlet/README.md @@ -0,0 +1,2 @@ +###Relevant Articles: +- [How to Register a Servlet in a Java Web Application](http://www.baeldung.com/how-to-register-a-servlet-in-a-java-web-application/) \ No newline at end of file diff --git a/spring-boot-servlet/pom.xml b/spring-boot-servlet/pom.xml new file mode 100644 index 0000000000..3818e3468f --- /dev/null +++ b/spring-boot-servlet/pom.xml @@ -0,0 +1,55 @@ + + 4.0.0 + com.baeldung + spring-boot-servlet + 0.0.1-SNAPSHOT + war + spring-boot-servlet + + + org.springframework.boot + spring-boot-dependencies + 1.5.1.RELEASE + + + + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-tomcat + provided + + + + org.apache.tomcat.embed + tomcat-embed-core + ${tomcat.version} + + + org.apache.tomcat.embed + tomcat-embed-jasper + ${tomcat.version} + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + + UTF-8 + 1.8 + 8.5.11 + + + diff --git a/spring-boot-servlet/src/main/java/META-INF/MANIFEST.MF b/spring-boot-servlet/src/main/java/META-INF/MANIFEST.MF new file mode 100644 index 0000000000..69ebae1751 --- /dev/null +++ b/spring-boot-servlet/src/main/java/META-INF/MANIFEST.MF @@ -0,0 +1,2 @@ +Manifest-Version: 1.0 +Main-Class: com.baeldung.ApplicationMain diff --git a/spring-boot-servlet/src/main/java/com/baeldung/ApplicationMain.java b/spring-boot-servlet/src/main/java/com/baeldung/ApplicationMain.java new file mode 100644 index 0000000000..66f2e85999 --- /dev/null +++ b/spring-boot-servlet/src/main/java/com/baeldung/ApplicationMain.java @@ -0,0 +1,19 @@ +package com.baeldung; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.builder.SpringApplicationBuilder; +import org.springframework.boot.web.support.SpringBootServletInitializer; + +@SpringBootApplication +public class ApplicationMain extends SpringBootServletInitializer { + + public static void main(String[] args) { + SpringApplication.run(ApplicationMain.class, args); + } + + @Override + protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { + return application.sources(ApplicationMain.class); + } +} \ No newline at end of file diff --git a/spring-boot-servlet/src/main/java/com/baeldung/configuration/WebAppInitializer.java b/spring-boot-servlet/src/main/java/com/baeldung/configuration/WebAppInitializer.java new file mode 100644 index 0000000000..b7e22500f4 --- /dev/null +++ b/spring-boot-servlet/src/main/java/com/baeldung/configuration/WebAppInitializer.java @@ -0,0 +1,32 @@ +package com.baeldung.configuration; + +import org.springframework.web.WebApplicationInitializer; +import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; +import org.springframework.web.context.support.XmlWebApplicationContext; +import org.springframework.web.servlet.DispatcherServlet; +import javax.servlet.ServletContext; +import javax.servlet.ServletException; +import javax.servlet.ServletRegistration; + +public class WebAppInitializer implements WebApplicationInitializer { + + public void onStartup(ServletContext container) throws ServletException { + + AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext(); + ctx.register(WebMvcConfigure.class); + ctx.setServletContext(container); + + ServletRegistration.Dynamic servletOne = container.addServlet("SpringProgrammaticDispatcherServlet", new DispatcherServlet(ctx)); + servletOne.setLoadOnStartup(1); + servletOne.addMapping("/"); + + XmlWebApplicationContext xctx = new XmlWebApplicationContext(); + xctx.setConfigLocation("/WEB-INF/context.xml"); + xctx.setServletContext(container); + + ServletRegistration.Dynamic servletTwo = container.addServlet("SpringProgrammaticXMLDispatcherServlet", new DispatcherServlet(xctx)); + servletTwo.setLoadOnStartup(1); + servletTwo.addMapping("/"); + } + +} \ No newline at end of file diff --git a/spring-boot-servlet/src/main/java/com/baeldung/configuration/WebMvcConfigure.java b/spring-boot-servlet/src/main/java/com/baeldung/configuration/WebMvcConfigure.java new file mode 100644 index 0000000000..de9067de6e --- /dev/null +++ b/spring-boot-servlet/src/main/java/com/baeldung/configuration/WebMvcConfigure.java @@ -0,0 +1,40 @@ +package com.baeldung.configuration; + +import org.springframework.boot.web.support.ErrorPageFilter; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.ViewResolver; +import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer; +import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; +import org.springframework.web.servlet.resource.PathResourceResolver; +import org.springframework.web.servlet.view.InternalResourceViewResolver; + +@Configuration +public class WebMvcConfigure extends WebMvcConfigurerAdapter { + + @Bean + public ViewResolver getViewResolver() { + InternalResourceViewResolver resolver = new InternalResourceViewResolver(); + resolver.setPrefix("/WEB-INF/"); + resolver.setSuffix(".jsp"); + return resolver; + } + + @Override + public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { + configurer.enable(); + } + + + @Override + public void addResourceHandlers(ResourceHandlerRegistry registry) { + registry.addResourceHandler("/resources/**").addResourceLocations("/resources/").setCachePeriod(3600).resourceChain(true).addResolver(new PathResourceResolver()); + } + + @Bean + public ErrorPageFilter errorPageFilter() { + return new ErrorPageFilter(); + } +} + diff --git a/spring-boot-servlet/src/main/java/com/baeldung/props/Constants.java b/spring-boot-servlet/src/main/java/com/baeldung/props/Constants.java new file mode 100644 index 0000000000..421401eec7 --- /dev/null +++ b/spring-boot-servlet/src/main/java/com/baeldung/props/Constants.java @@ -0,0 +1,20 @@ +package com.baeldung.props; + +import org.springframework.beans.factory.annotation.Autowired; + +import java.util.Properties; + +public final class Constants { + + @Autowired + PropertySourcesLoader psl; + + public static final String breakLine = System.getProperty("line.separator"); + private static final PropertyLoader pl = new PropertyLoader(); + private static final Properties mainProps = pl.getProperties("custom.properties"); + public static final String DISPATCHER_SERVLET_NAME = mainProps.getProperty("dispatcher.servlet.name"); + public static final String DISPATCHER_SERVLET_MAPPING = mainProps.getProperty("dispatcher.servlet.mapping"); + private final String EXAMPLE_SERVLET_NAME = psl.getProperty("example.servlet.name"); + private final String EXAMPLE_SERVLET_MAPPING = psl.getProperty("example.servlet.mapping"); + +} diff --git a/spring-boot-servlet/src/main/java/com/baeldung/props/PropertyLoader.java b/spring-boot-servlet/src/main/java/com/baeldung/props/PropertyLoader.java new file mode 100644 index 0000000000..5d890d96fa --- /dev/null +++ b/spring-boot-servlet/src/main/java/com/baeldung/props/PropertyLoader.java @@ -0,0 +1,27 @@ +package com.baeldung.props; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.io.InputStream; +import java.util.Properties; + +public class PropertyLoader { + private static final Logger log = LoggerFactory.getLogger(PropertyLoader.class); + + public Properties getProperties(String file) { + Properties prop = new Properties(); + InputStream input = null; + try { + input = getClass().getResourceAsStream(file); + prop.load(input); + if (input != null) { + input.close(); + } + } catch (IOException ex) { + log.error("IOException: " + ex); + } + return prop; + } +} diff --git a/spring-boot-servlet/src/main/java/com/baeldung/props/PropertySourcesLoader.java b/spring-boot-servlet/src/main/java/com/baeldung/props/PropertySourcesLoader.java new file mode 100644 index 0000000000..8c7b3a4af5 --- /dev/null +++ b/spring-boot-servlet/src/main/java/com/baeldung/props/PropertySourcesLoader.java @@ -0,0 +1,23 @@ +package com.baeldung.props; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.PropertySource; +import org.springframework.core.env.ConfigurableEnvironment; + +@Configuration +@ComponentScan(basePackages = { "com.baeldung.*" }) +@PropertySource("classpath:custom.properties") public class PropertySourcesLoader { + + private static final Logger log = LoggerFactory.getLogger(PropertySourcesLoader.class); + + @Autowired + ConfigurableEnvironment env; + + public String getProperty(String key) { + return env.getProperty(key); + } +} diff --git a/spring-boot-servlet/src/main/java/com/baeldung/servlets/GenericCustomServlet.java b/spring-boot-servlet/src/main/java/com/baeldung/servlets/GenericCustomServlet.java new file mode 100644 index 0000000000..c6543c9eef --- /dev/null +++ b/spring-boot-servlet/src/main/java/com/baeldung/servlets/GenericCustomServlet.java @@ -0,0 +1,18 @@ +package com.baeldung.servlets; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.io.PrintWriter; + +public class GenericCustomServlet extends HttpServlet { + + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + response.setContentType("text/html"); + PrintWriter out = response.getWriter(); + out.println("

Hello World

"); + } +} diff --git a/spring-boot-servlet/src/main/java/com/baeldung/servlets/javaee/AnnotationServlet.java b/spring-boot-servlet/src/main/java/com/baeldung/servlets/javaee/AnnotationServlet.java new file mode 100644 index 0000000000..d971e68cfa --- /dev/null +++ b/spring-boot-servlet/src/main/java/com/baeldung/servlets/javaee/AnnotationServlet.java @@ -0,0 +1,20 @@ +package com.baeldung.servlets.javaee; + +import javax.servlet.ServletException; +import javax.servlet.annotation.WebServlet; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; + +@WebServlet(name = "AnnotationServlet", + description = "Example Servlet Using Annotations", + urlPatterns = { "/annotationservlet" }) +public class AnnotationServlet extends HttpServlet { + private static final long serialVersionUID = 1L; + + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + request.getRequestDispatcher("/annotationservlet.jsp").forward(request, response); + } +} diff --git a/spring-boot-servlet/src/main/java/com/baeldung/servlets/javaee/EEWebXmlServlet.java b/spring-boot-servlet/src/main/java/com/baeldung/servlets/javaee/EEWebXmlServlet.java new file mode 100644 index 0000000000..4209e815cd --- /dev/null +++ b/spring-boot-servlet/src/main/java/com/baeldung/servlets/javaee/EEWebXmlServlet.java @@ -0,0 +1,20 @@ +package com.baeldung.servlets.javaee; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.io.PrintWriter; + +public class EEWebXmlServlet extends HttpServlet { + + private static final long serialVersionUID = 1L; + + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + response.setContentType("text/html"); + PrintWriter out = response.getWriter(); + out.println("

Hello World

"); + } +} \ No newline at end of file diff --git a/spring-boot-servlet/src/main/java/com/baeldung/servlets/springboot/SpringRegistrationBeanServlet.java b/spring-boot-servlet/src/main/java/com/baeldung/servlets/springboot/SpringRegistrationBeanServlet.java new file mode 100644 index 0000000000..4a34465894 --- /dev/null +++ b/spring-boot-servlet/src/main/java/com/baeldung/servlets/springboot/SpringRegistrationBeanServlet.java @@ -0,0 +1,19 @@ +package com.baeldung.servlets.springboot; + +import com.baeldung.servlets.GenericCustomServlet; +import org.springframework.boot.web.servlet.ServletRegistrationBean; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class SpringRegistrationBeanServlet { + + @Bean + public ServletRegistrationBean genericCustomServlet() { + ServletRegistrationBean bean = new ServletRegistrationBean(new GenericCustomServlet(), "/springregistrationbeanservlet/*"); + bean.setLoadOnStartup(1); + return bean; + } +} + + diff --git a/spring-boot-servlet/src/main/java/com/baeldung/servlets/springboot/embedded/EmbeddedTomcatExample.java b/spring-boot-servlet/src/main/java/com/baeldung/servlets/springboot/embedded/EmbeddedTomcatExample.java new file mode 100644 index 0000000000..b2458f33c7 --- /dev/null +++ b/spring-boot-servlet/src/main/java/com/baeldung/servlets/springboot/embedded/EmbeddedTomcatExample.java @@ -0,0 +1,16 @@ +package com.baeldung.servlets.springboot.embedded; + +import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory; +import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class EmbeddedTomcatExample { + + @Bean + public EmbeddedServletContainerFactory servletContainer() { + TomcatEmbeddedServletContainerFactory tomcat = new TomcatEmbeddedServletContainerFactory(); + return tomcat; + } +} diff --git a/spring-boot-servlet/src/main/resources/application.properties b/spring-boot-servlet/src/main/resources/application.properties new file mode 100644 index 0000000000..4e9e2b4cf1 --- /dev/null +++ b/spring-boot-servlet/src/main/resources/application.properties @@ -0,0 +1,10 @@ +#Server Configuration +#server.port=8080 +#server.context-path=/javabootdata +#Resource Handling +#spring.resources.static-locations=classpath:/WEB-INF/resources +#spring.mvc.view.prefix=/WEB-INF/ +#spring.mvc.view.suffix=.jsp +#spring.resources.cache-period=3600 +servlet.name=dispatcherExample +servlet.mapping=/dispatcherExampleURL \ No newline at end of file diff --git a/spring-boot-servlet/src/main/resources/custom.properties b/spring-boot-servlet/src/main/resources/custom.properties new file mode 100644 index 0000000000..34f31bcd50 --- /dev/null +++ b/spring-boot-servlet/src/main/resources/custom.properties @@ -0,0 +1,4 @@ +dispatcher.servlet.name=dispatcherExample +dispatcher.servlet.mapping=/dispatcherExampleURL +example.servlet.name=dispatcherExample +example.servlet.mapping=/dispatcherExampleURL \ No newline at end of file diff --git a/spring-boot-servlet/src/main/webapp/WEB-INF/context.xml b/spring-boot-servlet/src/main/webapp/WEB-INF/context.xml new file mode 100644 index 0000000000..263bed4430 --- /dev/null +++ b/spring-boot-servlet/src/main/webapp/WEB-INF/context.xml @@ -0,0 +1,12 @@ + + + + + + \ No newline at end of file diff --git a/spring-boot-servlet/src/main/webapp/WEB-INF/dispatcher.xml b/spring-boot-servlet/src/main/webapp/WEB-INF/dispatcher.xml new file mode 100644 index 0000000000..ade8e66777 --- /dev/null +++ b/spring-boot-servlet/src/main/webapp/WEB-INF/dispatcher.xml @@ -0,0 +1,16 @@ + + + + + + + + + \ No newline at end of file diff --git a/spring-boot-servlet/src/main/webapp/WEB-INF/web.xml b/spring-boot-servlet/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000000..60a4b079de --- /dev/null +++ b/spring-boot-servlet/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,40 @@ + + + JSP + + index.html + index.htm + index.jsp + + + + + EEWebXmlServlet + com.baeldung.servlets.javaee.EEWebXmlServlet + + + + EEWebXmlServlet + /eewebxmlservlet + + + + + SpringBootWebXmlServlet + org.springframework.web.servlet.DispatcherServlet + + contextConfigLocation + /WEB-INF/dispatcher.xml + + 1 + + + + SpringBootWebXmlServlet + / + + + + diff --git a/spring-boot-servlet/src/main/webapp/annotationservlet.jsp b/spring-boot-servlet/src/main/webapp/annotationservlet.jsp new file mode 100644 index 0000000000..f21748df50 --- /dev/null +++ b/spring-boot-servlet/src/main/webapp/annotationservlet.jsp @@ -0,0 +1 @@ +

Annotation Servlet!

\ No newline at end of file diff --git a/spring-boot-servlet/src/main/webapp/index.jsp b/spring-boot-servlet/src/main/webapp/index.jsp new file mode 100644 index 0000000000..e534282777 --- /dev/null +++ b/spring-boot-servlet/src/main/webapp/index.jsp @@ -0,0 +1 @@ +

Hello!

\ No newline at end of file From dbc52e86545b5631cdf39a17620d31c3f86d6a9e Mon Sep 17 00:00:00 2001 From: Pedja Date: Wed, 22 Feb 2017 12:58:04 +0100 Subject: [PATCH 38/68] BAEL-633 Small refactoring --- .../java/com/baeldung/cglib/proxy/BeanGeneratorTest.java | 2 +- .../src/test/java/com/baeldung/cglib/proxy/MixinTest.java | 2 +- .../proxy/{PersonServiceProxyTest.java => ProxyTest.java} | 8 ++++---- 3 files changed, 6 insertions(+), 6 deletions(-) rename cglib/src/test/java/com/baeldung/cglib/proxy/{PersonServiceProxyTest.java => ProxyTest.java} (82%) diff --git a/cglib/src/test/java/com/baeldung/cglib/proxy/BeanGeneratorTest.java b/cglib/src/test/java/com/baeldung/cglib/proxy/BeanGeneratorTest.java index 67214cd32d..1e49536065 100644 --- a/cglib/src/test/java/com/baeldung/cglib/proxy/BeanGeneratorTest.java +++ b/cglib/src/test/java/com/baeldung/cglib/proxy/BeanGeneratorTest.java @@ -11,7 +11,7 @@ import static junit.framework.TestCase.assertEquals; public class BeanGeneratorTest { @Test - public void givenBeanCreator_whenAddPropery_classShouldHaveFieldValue() throws Exception { + public void givenBeanCreator_whenAddProperty_thenClassShouldHaveFieldValue() throws Exception { //given BeanGenerator beanGenerator = new BeanGenerator(); diff --git a/cglib/src/test/java/com/baeldung/cglib/proxy/MixinTest.java b/cglib/src/test/java/com/baeldung/cglib/proxy/MixinTest.java index 186c66aa42..db8453e6c1 100644 --- a/cglib/src/test/java/com/baeldung/cglib/proxy/MixinTest.java +++ b/cglib/src/test/java/com/baeldung/cglib/proxy/MixinTest.java @@ -9,7 +9,7 @@ import static junit.framework.TestCase.assertEquals; public class MixinTest { @Test - public void givenTwoClasses_whenMixtThemIntoOne_mixinShouldHaveMethodsFromBothClasses() throws Exception { + public void givenTwoClasses_whenMixedIntoOne_thenMixinShouldHaveMethodsFromBothClasses() throws Exception { //when Mixin mixin = Mixin.create( new Class[]{Interface1.class, Interface2.class, MixinInterface.class}, diff --git a/cglib/src/test/java/com/baeldung/cglib/proxy/PersonServiceProxyTest.java b/cglib/src/test/java/com/baeldung/cglib/proxy/ProxyTest.java similarity index 82% rename from cglib/src/test/java/com/baeldung/cglib/proxy/PersonServiceProxyTest.java rename to cglib/src/test/java/com/baeldung/cglib/proxy/ProxyTest.java index 6208ed12b8..195c4b903d 100644 --- a/cglib/src/test/java/com/baeldung/cglib/proxy/PersonServiceProxyTest.java +++ b/cglib/src/test/java/com/baeldung/cglib/proxy/ProxyTest.java @@ -7,9 +7,9 @@ import org.junit.Test; import static org.junit.Assert.assertEquals; -public class PersonServiceProxyTest { +public class ProxyTest { @Test - public void givenPersonService_whenSayHello_shouldReturnResult() { + public void givenPersonService_whenSayHello_thenReturnResult() { //given PersonService personService = new PersonService(); @@ -21,7 +21,7 @@ public class PersonServiceProxyTest { } @Test - public void givenEnhancerProxy_whenExtendPersonService_shouldInterceptMethod() throws Exception { + public void givenEnhancerProxy_whenExtendPersonService_thenInterceptMethod() throws Exception { //given Enhancer enhancer = new Enhancer(); enhancer.setSuperclass(PersonService.class); @@ -36,7 +36,7 @@ public class PersonServiceProxyTest { } @Test - public void givenEnhancer_whenExecuteMethodOnProxy_shouldInterceptOnlyStringReturnTypeMethod() throws Exception { + public void givenEnhancer_whenExecuteMethodOnProxy_thenInterceptOnlyStringReturnTypeMethod() throws Exception { //given Enhancer enhancer = new Enhancer(); enhancer.setSuperclass(PersonService.class); From ed4ccd92f77f6514590456df9a6c5731384f63a7 Mon Sep 17 00:00:00 2001 From: Pedja Date: Wed, 22 Feb 2017 15:23:26 +0100 Subject: [PATCH 39/68] BAEL-633 Excluded jackson-databind transient dependency --- spring-mvc-xml/pom.xml | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/spring-mvc-xml/pom.xml b/spring-mvc-xml/pom.xml index f0e4bbff55..86eb17da64 100644 --- a/spring-mvc-xml/pom.xml +++ b/spring-mvc-xml/pom.xml @@ -116,7 +116,13 @@ com.maxmind.geoip2 geoip2 ${geoip2.version} - + + + com.fasterxml.jackson.core + jackson-databind + + + From d267d393210fc2c06272dd491a79f43633e9e70c Mon Sep 17 00:00:00 2001 From: Sunil Gulabani Date: Thu, 23 Feb 2017 11:19:30 +0530 Subject: [PATCH 40/68] BAEL-650: Review comment changes --- aws/pom.xml | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/aws/pom.xml b/aws/pom.xml index 881ba6522d..681b76cfd4 100644 --- a/aws/pom.xml +++ b/aws/pom.xml @@ -7,29 +7,36 @@ jar aws + + 2.5 + 1.3.0 + 1.1.0 + 2.8.0 + + com.amazonaws aws-lambda-java-core - 1.1.0 + ${aws-lambda-java-core.version} com.amazonaws aws-lambda-java-events - 1.3.0 + ${aws-lambda-java-events.version} commons-io commons-io - 2.5 + ${commons-io.version} com.google.code.gson gson - 2.8.0 + ${gson.version} From 0fcae56196e02170ff873b4aaec672c519442639 Mon Sep 17 00:00:00 2001 From: Tomasz Lelek Date: Thu, 23 Feb 2017 16:03:49 +0100 Subject: [PATCH 41/68] BAEL-602 use Stream.of --- jooq/src/test/java/com/baeldung/JOOLTest.java | 8 ++++---- pom.xml | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/jooq/src/test/java/com/baeldung/JOOLTest.java b/jooq/src/test/java/com/baeldung/JOOLTest.java index 5560e7ea14..18fca1f67a 100644 --- a/jooq/src/test/java/com/baeldung/JOOLTest.java +++ b/jooq/src/test/java/com/baeldung/JOOLTest.java @@ -36,8 +36,8 @@ public class JOOLTest { @Test public void givenStreams_whenJoin_shouldHaveElementsFromTwoStreams() { //given - Stream left = Arrays.asList(1, 2, 4).stream(); - Stream right = Arrays.asList(1, 2, 3).stream(); + Stream left = Stream.of(1, 2, 4); + Stream right = Stream.of(1, 2, 3); //when List rightCollected = right.collect(Collectors.toList()); @@ -172,7 +172,7 @@ public class JOOLTest { @Test public void givenOperationThatThrowsCheckedException_whenExecuteAndNeedToWrapCheckedIntoUnchecked_shouldPass() { //when - List collect = Arrays.asList("a", "b", "c").stream().map(elem -> { + List collect = Stream.of("a", "b", "c").map(elem -> { try { return methodThatThrowsChecked(elem); } catch (Exception e) { @@ -192,7 +192,7 @@ public class JOOLTest { @Test public void givenOperationThatThrowsCheckedException_whenExecuteUsingUncheckedFuction_shouldPass() { //when - List collect = Arrays.asList("a", "b", "c").stream() + List collect = Stream.of("a", "b", "c") .map(Unchecked.function(elem -> methodThatThrowsChecked(elem))) .collect(Collectors.toList()); diff --git a/pom.xml b/pom.xml index 42ded9de0d..7694f29d3a 100644 --- a/pom.xml +++ b/pom.xml @@ -65,6 +65,7 @@ jaxb jee7 jjwt + jooq jpa-storedprocedure jsf json-path @@ -193,7 +194,6 @@ struts2 apache-velocity - jooq From 9f108905017774816697c9e763b0c1a879bd321b Mon Sep 17 00:00:00 2001 From: eugenp Date: Fri, 24 Feb 2017 13:50:46 +0200 Subject: [PATCH 42/68] initial commit of spring 5 project --- spring-5/.gitignore | 13 + spring-5/README.md | 6 + spring-5/pom.xml | 345 ++++++++++ .../java/org/baeldung/config/Application.java | 16 + .../java/org/baeldung/config/WebConfig.java | 57 ++ .../converter/KryoHttpMessageConverter.java | 57 ++ .../BarMappingExamplesController.java | 47 ++ .../web/controller/CompanyController.java | 16 + .../web/controller/FooController.java | 45 ++ .../FooMappingExamplesController.java | 101 +++ .../web/controller/ItemController.java | 39 ++ .../web/controller/MyFooController.java | 76 +++ .../web/controller/SimplePostController.java | 73 +++ .../advice/JsonpControllerAdvice.java | 13 + .../mediatypes/CustomMediaTypeController.java | 20 + .../redirect/RedirectController.java | 52 ++ .../controller/status/ExampleController.java | 24 + .../controller/status/ForbiddenException.java | 10 + .../org/baeldung/web/dto/BaeldungItem.java | 13 + .../org/baeldung/web/dto/BaeldungItemV2.java | 14 + .../java/org/baeldung/web/dto/Company.java | 38 ++ .../main/java/org/baeldung/web/dto/Foo.java | 45 ++ .../java/org/baeldung/web/dto/FooProtos.java | 597 ++++++++++++++++++ .../main/java/org/baeldung/web/dto/Item.java | 36 ++ .../org/baeldung/web/dto/ItemManager.java | 9 + .../main/java/org/baeldung/web/dto/Views.java | 9 + .../exception/ResourceNotFoundException.java | 8 + .../src/main/resources/application.properties | 2 + spring-5/src/main/resources/logback.xml | 20 + .../src/main/webapp/WEB-INF/api-servlet.xml | 56 ++ spring-5/src/main/webapp/WEB-INF/company.html | 44 ++ .../src/main/webapp/WEB-INF/spring-views.xml | 10 + spring-5/src/main/webapp/WEB-INF/web.xml | 41 ++ .../test/java/org/baeldung/client/Consts.java | 5 + .../client/RestTemplateBasicLiveTest.java | 216 +++++++ .../okhttp/DefaultContentTypeInterceptor.java | 24 + .../okhttp/OkHttpFileUploadingLiveTest.java | 65 ++ .../baeldung/okhttp/OkHttpGetLiveTest.java | 77 +++ .../baeldung/okhttp/OkHttpHeaderLiveTest.java | 45 ++ .../baeldung/okhttp/OkHttpMiscLiveTest.java | 99 +++ .../okhttp/OkHttpPostingLiveTest.java | 85 +++ .../okhttp/OkHttpRedirectLiveTest.java | 29 + .../okhttp/ProgressRequestWrapper.java | 73 +++ .../uribuilder/SpringUriBuilderTest.java | 49 ++ .../CustomMediaTypeControllerLiveTest.java | 37 ++ .../CustomMediaTypeControllerTest.java | 42 ++ .../web/controller/mediatypes/TestConfig.java | 11 + .../RedirectControllerIntegrationTest.java | 66 ++ .../ExampleControllerIntegrationTest.java | 42 ++ .../web/test/RequestMappingLiveTest.java | 63 ++ .../SpringHttpMessageConvertersLiveTest.java | 123 ++++ spring-5/src/test/resources/.gitignore | 13 + spring-5/src/test/resources/test.txt | 1 + 53 files changed, 3117 insertions(+) create mode 100644 spring-5/.gitignore create mode 100644 spring-5/README.md create mode 100644 spring-5/pom.xml create mode 100644 spring-5/src/main/java/org/baeldung/config/Application.java create mode 100644 spring-5/src/main/java/org/baeldung/config/WebConfig.java create mode 100644 spring-5/src/main/java/org/baeldung/config/converter/KryoHttpMessageConverter.java create mode 100644 spring-5/src/main/java/org/baeldung/web/controller/BarMappingExamplesController.java create mode 100644 spring-5/src/main/java/org/baeldung/web/controller/CompanyController.java create mode 100644 spring-5/src/main/java/org/baeldung/web/controller/FooController.java create mode 100644 spring-5/src/main/java/org/baeldung/web/controller/FooMappingExamplesController.java create mode 100644 spring-5/src/main/java/org/baeldung/web/controller/ItemController.java create mode 100644 spring-5/src/main/java/org/baeldung/web/controller/MyFooController.java create mode 100644 spring-5/src/main/java/org/baeldung/web/controller/SimplePostController.java create mode 100644 spring-5/src/main/java/org/baeldung/web/controller/advice/JsonpControllerAdvice.java create mode 100644 spring-5/src/main/java/org/baeldung/web/controller/mediatypes/CustomMediaTypeController.java create mode 100644 spring-5/src/main/java/org/baeldung/web/controller/redirect/RedirectController.java create mode 100644 spring-5/src/main/java/org/baeldung/web/controller/status/ExampleController.java create mode 100644 spring-5/src/main/java/org/baeldung/web/controller/status/ForbiddenException.java create mode 100644 spring-5/src/main/java/org/baeldung/web/dto/BaeldungItem.java create mode 100644 spring-5/src/main/java/org/baeldung/web/dto/BaeldungItemV2.java create mode 100644 spring-5/src/main/java/org/baeldung/web/dto/Company.java create mode 100644 spring-5/src/main/java/org/baeldung/web/dto/Foo.java create mode 100644 spring-5/src/main/java/org/baeldung/web/dto/FooProtos.java create mode 100644 spring-5/src/main/java/org/baeldung/web/dto/Item.java create mode 100644 spring-5/src/main/java/org/baeldung/web/dto/ItemManager.java create mode 100644 spring-5/src/main/java/org/baeldung/web/dto/Views.java create mode 100644 spring-5/src/main/java/org/baeldung/web/exception/ResourceNotFoundException.java create mode 100644 spring-5/src/main/resources/application.properties create mode 100644 spring-5/src/main/resources/logback.xml create mode 100644 spring-5/src/main/webapp/WEB-INF/api-servlet.xml create mode 100644 spring-5/src/main/webapp/WEB-INF/company.html create mode 100644 spring-5/src/main/webapp/WEB-INF/spring-views.xml create mode 100644 spring-5/src/main/webapp/WEB-INF/web.xml create mode 100644 spring-5/src/test/java/org/baeldung/client/Consts.java create mode 100644 spring-5/src/test/java/org/baeldung/client/RestTemplateBasicLiveTest.java create mode 100644 spring-5/src/test/java/org/baeldung/okhttp/DefaultContentTypeInterceptor.java create mode 100644 spring-5/src/test/java/org/baeldung/okhttp/OkHttpFileUploadingLiveTest.java create mode 100644 spring-5/src/test/java/org/baeldung/okhttp/OkHttpGetLiveTest.java create mode 100644 spring-5/src/test/java/org/baeldung/okhttp/OkHttpHeaderLiveTest.java create mode 100644 spring-5/src/test/java/org/baeldung/okhttp/OkHttpMiscLiveTest.java create mode 100644 spring-5/src/test/java/org/baeldung/okhttp/OkHttpPostingLiveTest.java create mode 100644 spring-5/src/test/java/org/baeldung/okhttp/OkHttpRedirectLiveTest.java create mode 100644 spring-5/src/test/java/org/baeldung/okhttp/ProgressRequestWrapper.java create mode 100644 spring-5/src/test/java/org/baeldung/uribuilder/SpringUriBuilderTest.java create mode 100644 spring-5/src/test/java/org/baeldung/web/controller/mediatypes/CustomMediaTypeControllerLiveTest.java create mode 100644 spring-5/src/test/java/org/baeldung/web/controller/mediatypes/CustomMediaTypeControllerTest.java create mode 100644 spring-5/src/test/java/org/baeldung/web/controller/mediatypes/TestConfig.java create mode 100644 spring-5/src/test/java/org/baeldung/web/controller/redirect/RedirectControllerIntegrationTest.java create mode 100644 spring-5/src/test/java/org/baeldung/web/controller/status/ExampleControllerIntegrationTest.java create mode 100644 spring-5/src/test/java/org/baeldung/web/test/RequestMappingLiveTest.java create mode 100644 spring-5/src/test/java/org/baeldung/web/test/SpringHttpMessageConvertersLiveTest.java create mode 100644 spring-5/src/test/resources/.gitignore create mode 100644 spring-5/src/test/resources/test.txt diff --git a/spring-5/.gitignore b/spring-5/.gitignore new file mode 100644 index 0000000000..83c05e60c8 --- /dev/null +++ b/spring-5/.gitignore @@ -0,0 +1,13 @@ +*.class + +#folders# +/target +/neoDb* +/data +/src/main/webapp/WEB-INF/classes +*/META-INF/* + +# Packaged files # +*.jar +*.war +*.ear \ No newline at end of file diff --git a/spring-5/README.md b/spring-5/README.md new file mode 100644 index 0000000000..0914388b49 --- /dev/null +++ b/spring-5/README.md @@ -0,0 +1,6 @@ +## Spring REST Example Project + +###The Course +The "REST With Spring" Classes: http://bit.ly/restwithspring + +### Relevant Articles: diff --git a/spring-5/pom.xml b/spring-5/pom.xml new file mode 100644 index 0000000000..c2545d920f --- /dev/null +++ b/spring-5/pom.xml @@ -0,0 +1,345 @@ + + 4.0.0 + com.baeldung + spring-5 + 0.1-SNAPSHOT + spring-5 + war + + + org.springframework.boot + spring-boot-starter-parent + + 2.0.0.BUILD-SNAPSHOT + + + + + + + + org.springframework.boot + spring-boot-starter-thymeleaf + + + org.springframework.boot + spring-boot-starter-actuator + + + org.springframework.boot + spring-boot-devtools + + + + + + org.springframework + spring-web + + + commons-logging + commons-logging + + + + + org.springframework + spring-webmvc + + + org.springframework + spring-oxm + + + + commons-fileupload + commons-fileupload + ${commons-fileupload.version} + + + + + javax.servlet + javax.servlet-api + provided + + + + javax.servlet + jstl + runtime + + + + + + com.fasterxml.jackson.core + jackson-databind + + + + com.fasterxml.jackson.dataformat + jackson-dataformat-xml + + + + com.thoughtworks.xstream + xstream + ${xstream.version} + + + + + + com.google.guava + guava + ${guava.version} + + + + org.apache.commons + commons-lang3 + ${commons-lang3.version} + + + + + + org.slf4j + slf4j-api + + + ch.qos.logback + logback-classic + + + + org.slf4j + jcl-over-slf4j + + + + org.slf4j + log4j-over-slf4j + + + + + + com.squareup.okhttp3 + okhttp + ${com.squareup.okhttp3.version} + + + + + + junit + junit + test + + + + org.hamcrest + hamcrest-core + test + + + org.hamcrest + hamcrest-library + test + + + + org.mockito + mockito-core + test + + + + org.springframework + spring-test + + + + com.jayway.restassured + rest-assured + ${rest-assured.version} + + + + + com.google.protobuf + protobuf-java + ${protobuf-java.version} + + + com.googlecode.protobuf-java-format + protobuf-java-format + ${protobuf-java-format.version} + + + + com.esotericsoftware + kryo + ${kryo.version} + + + + + spring-5 + + + src/main/resources + true + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.8 + 1.8 + + + + + org.apache.maven.plugins + maven-war-plugin + + + + org.apache.maven.plugins + maven-surefire-plugin + + + **/*IntegrationTest.java + **/*LiveTest.java + + + + + + + + + org.codehaus.cargo + cargo-maven2-plugin + ${cargo-maven2-plugin.version} + + true + + tomcat8x + embedded + + + + + + + 8082 + + + + + + + + + + + + + integration + + + + org.apache.maven.plugins + maven-surefire-plugin + + + integration-test + + test + + + + none + + + **/*IntegrationTest.java + + + + + + + + + + + + + + 1.3.2 + 4.0.0 + 1.4 + 3.1.0 + 3.5 + 1.4.9 + + + 20.0 + 2.9.0 + + + 1.6.0 + 3.0.4 + + + 3.4.1 + + + + + + spring-snapshots + Spring Snapshots + https://repo.spring.io/snapshot + + true + + + + spring-milestones + Spring Milestones + https://repo.spring.io/milestone + + false + + + + + + spring-snapshots + Spring Snapshots + https://repo.spring.io/snapshot + + true + + + + spring-milestones + Spring Milestones + https://repo.spring.io/milestone + + false + + + + + diff --git a/spring-5/src/main/java/org/baeldung/config/Application.java b/spring-5/src/main/java/org/baeldung/config/Application.java new file mode 100644 index 0000000000..077213b04d --- /dev/null +++ b/spring-5/src/main/java/org/baeldung/config/Application.java @@ -0,0 +1,16 @@ +package org.baeldung.config; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; + +@EnableAutoConfiguration +@ComponentScan("org.baeldung") +public class Application extends WebMvcConfigurerAdapter { + + public static void main(final String[] args) { + SpringApplication.run(Application.class, args); + } + +} \ No newline at end of file diff --git a/spring-5/src/main/java/org/baeldung/config/WebConfig.java b/spring-5/src/main/java/org/baeldung/config/WebConfig.java new file mode 100644 index 0000000000..f40c9477d4 --- /dev/null +++ b/spring-5/src/main/java/org/baeldung/config/WebConfig.java @@ -0,0 +1,57 @@ +package org.baeldung.config; + +import org.baeldung.config.converter.KryoHttpMessageConverter; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.http.converter.HttpMessageConverter; +import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder; +import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; +import org.springframework.http.converter.protobuf.ProtobufHttpMessageConverter; +import org.springframework.http.converter.xml.MarshallingHttpMessageConverter; +import org.springframework.oxm.xstream.XStreamMarshaller; +import org.springframework.web.servlet.config.annotation.EnableWebMvc; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; + +import java.text.SimpleDateFormat; +import java.util.List; + +/* + * Please note that main web configuration is in src/main/webapp/WEB-INF/api-servlet.xml + */ +@Configuration +@EnableWebMvc +@ComponentScan({ "org.baeldung.web" }) +public class WebConfig extends WebMvcConfigurerAdapter { + + public WebConfig() { + super(); + } + + // + + @Override + public void configureMessageConverters(final List> messageConverters) { + final Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder(); + builder.indentOutput(true).dateFormat(new SimpleDateFormat("dd-MM-yyyy hh:mm")); + messageConverters.add(new MappingJackson2HttpMessageConverter(builder.build())); + // messageConverters.add(new MappingJackson2XmlHttpMessageConverter(builder.createXmlMapper(true).build())); + + // messageConverters.add(createXmlHttpMessageConverter()); + // messageConverters.add(new MappingJackson2HttpMessageConverter()); + + messageConverters.add(new ProtobufHttpMessageConverter()); + messageConverters.add(new KryoHttpMessageConverter()); + super.configureMessageConverters(messageConverters); + } + + private HttpMessageConverter createXmlHttpMessageConverter() { + final MarshallingHttpMessageConverter xmlConverter = new MarshallingHttpMessageConverter(); + + final XStreamMarshaller xstreamMarshaller = new XStreamMarshaller(); + xmlConverter.setMarshaller(xstreamMarshaller); + xmlConverter.setUnmarshaller(xstreamMarshaller); + + return xmlConverter; + } + +} diff --git a/spring-5/src/main/java/org/baeldung/config/converter/KryoHttpMessageConverter.java b/spring-5/src/main/java/org/baeldung/config/converter/KryoHttpMessageConverter.java new file mode 100644 index 0000000000..7e63a3ba9e --- /dev/null +++ b/spring-5/src/main/java/org/baeldung/config/converter/KryoHttpMessageConverter.java @@ -0,0 +1,57 @@ +package org.baeldung.config.converter; + +import java.io.IOException; + +import org.baeldung.web.dto.Foo; +import org.springframework.http.HttpInputMessage; +import org.springframework.http.HttpOutputMessage; +import org.springframework.http.MediaType; +import org.springframework.http.converter.AbstractHttpMessageConverter; + +import com.esotericsoftware.kryo.Kryo; +import com.esotericsoftware.kryo.io.Input; +import com.esotericsoftware.kryo.io.Output; + +/** + * An {@code HttpMessageConverter} that can read and write Kryo messages. + */ +public class KryoHttpMessageConverter extends AbstractHttpMessageConverter { + + public static final MediaType KRYO = new MediaType("application", "x-kryo"); + + private static final ThreadLocal kryoThreadLocal = new ThreadLocal() { + @Override + protected Kryo initialValue() { + final Kryo kryo = new Kryo(); + kryo.register(Foo.class, 1); + return kryo; + } + }; + + public KryoHttpMessageConverter() { + super(KRYO); + } + + @Override + protected boolean supports(final Class clazz) { + return Object.class.isAssignableFrom(clazz); + } + + @Override + protected Object readInternal(final Class clazz, final HttpInputMessage inputMessage) throws IOException { + final Input input = new Input(inputMessage.getBody()); + return kryoThreadLocal.get().readClassAndObject(input); + } + + @Override + protected void writeInternal(final Object object, final HttpOutputMessage outputMessage) throws IOException { + final Output output = new Output(outputMessage.getBody()); + kryoThreadLocal.get().writeClassAndObject(output, object); + output.flush(); + } + + @Override + protected MediaType getDefaultContentType(final Object object) { + return KRYO; + } +} diff --git a/spring-5/src/main/java/org/baeldung/web/controller/BarMappingExamplesController.java b/spring-5/src/main/java/org/baeldung/web/controller/BarMappingExamplesController.java new file mode 100644 index 0000000000..1c3a1086ca --- /dev/null +++ b/spring-5/src/main/java/org/baeldung/web/controller/BarMappingExamplesController.java @@ -0,0 +1,47 @@ +package org.baeldung.web.controller; + +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.ResponseBody; + +@Controller +@RequestMapping(value = "/ex") +public class BarMappingExamplesController { + + public BarMappingExamplesController() { + super(); + } + + // API + + // with @RequestParam + + @RequestMapping(value = "/bars") + @ResponseBody + public String getBarBySimplePathWithRequestParam(@RequestParam("id") final long id) { + return "Get a specific Bar with id=" + id; + } + + @RequestMapping(value = "/bars", params = "id") + @ResponseBody + public String getBarBySimplePathWithExplicitRequestParam(@RequestParam("id") final long id) { + return "Get a specific Bar with id=" + id; + } + + @RequestMapping(value = "/bars", params = { "id", "second" }) + @ResponseBody + public String getBarBySimplePathWithExplicitRequestParams(@RequestParam("id") final long id) { + return "Get a specific Bar with id=" + id; + } + + // with @PathVariable + + @RequestMapping(value = "/bars/{numericId:[\\d]+}") + @ResponseBody + public String getBarsBySimplePathWithPathVariable(@PathVariable final long numericId) { + return "Get a specific Bar with id=" + numericId; + } + +} diff --git a/spring-5/src/main/java/org/baeldung/web/controller/CompanyController.java b/spring-5/src/main/java/org/baeldung/web/controller/CompanyController.java new file mode 100644 index 0000000000..aa694c08ed --- /dev/null +++ b/spring-5/src/main/java/org/baeldung/web/controller/CompanyController.java @@ -0,0 +1,16 @@ +package org.baeldung.web.controller; + +import org.baeldung.web.dto.Company; +import org.springframework.http.MediaType; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class CompanyController { + + @RequestMapping(value = "/companyRest", produces = MediaType.APPLICATION_JSON_VALUE) + public Company getCompanyRest() { + final Company company = new Company(1, "Xpto"); + return company; + } +} diff --git a/spring-5/src/main/java/org/baeldung/web/controller/FooController.java b/spring-5/src/main/java/org/baeldung/web/controller/FooController.java new file mode 100644 index 0000000000..21ba3c6d13 --- /dev/null +++ b/spring-5/src/main/java/org/baeldung/web/controller/FooController.java @@ -0,0 +1,45 @@ +package org.baeldung.web.controller; + +import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic; + +import org.baeldung.web.dto.Foo; +import org.baeldung.web.dto.FooProtos; +import org.springframework.http.HttpStatus; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.bind.annotation.ResponseStatus; + +@Controller +public class FooController { + + public FooController() { + super(); + } + + // API - read + + @RequestMapping(method = RequestMethod.GET, value = "/foos/{id}") + @ResponseBody + public Foo findById(@PathVariable final long id) { + return new Foo(id, randomAlphabetic(4)); + } + + // API - write + + @RequestMapping(method = RequestMethod.PUT, value = "/foos/{id}") + @ResponseStatus(HttpStatus.OK) + @ResponseBody + public Foo updateFoo(@PathVariable("id") final String id, @RequestBody final Foo foo) { + return foo; + } + + @RequestMapping(method = RequestMethod.GET, value = "/foos/{id}", produces = { "application/x-protobuf" }) + @ResponseBody + public FooProtos.Foo findProtoById(@PathVariable final long id) { + return FooProtos.Foo.newBuilder().setId(1).setName("Foo Name").build(); + } +} diff --git a/spring-5/src/main/java/org/baeldung/web/controller/FooMappingExamplesController.java b/spring-5/src/main/java/org/baeldung/web/controller/FooMappingExamplesController.java new file mode 100644 index 0000000000..5fb92d6d87 --- /dev/null +++ b/spring-5/src/main/java/org/baeldung/web/controller/FooMappingExamplesController.java @@ -0,0 +1,101 @@ +package org.baeldung.web.controller; + +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.ResponseBody; + +@Controller +@RequestMapping(value = "/ex") +public class FooMappingExamplesController { + + public FooMappingExamplesController() { + super(); + } + + // API + + // mapping examples + + @RequestMapping(value = "/foos") + @ResponseBody + public String getFoosBySimplePath() { + return "Simple Get some Foos"; + } + + // with @PathVariable + + @RequestMapping(value = "/foos/{id}") + @ResponseBody + public String getFoosBySimplePathWithPathVariable(@PathVariable final long id) { + return "Get a specific Foo with id=" + id; + } + + @RequestMapping(value = "/foos/{fooid}/bar/{barid}") + @ResponseBody + public String getFoosBySimplePathWithPathVariables(@PathVariable final long fooid, @PathVariable final long barid) { + return "Get a specific Bar with id=" + barid + " from a Foo with id=" + fooid; + } + + // other HTTP verbs + + @RequestMapping(value = "/foos", method = RequestMethod.POST) + @ResponseBody + public String postFoos() { + return "Post some Foos"; + } + + // with headers + + @RequestMapping(value = "/foos", headers = "key=val") + @ResponseBody + public String getFoosWithHeader() { + return "Get some Foos with Header"; + } + + @RequestMapping(value = "/foos", headers = { "key1=val1", "key2=val2" }) + @ResponseBody + public String getFoosWithHeaders() { + return "Get some Foos with Header"; + } + + // @RequestMapping(value = "/foos", method = RequestMethod.GET, headers = "Accept=application/json") + // @ResponseBody + // public String getFoosAsJsonFromBrowser() { + // return "Get some Foos with Header Old"; + // } + + @RequestMapping(value = "/foos", produces = { "application/json", "application/xml" }) + @ResponseBody + public String getFoosAsJsonFromREST() { + return "Get some Foos with Header New"; + } + + // advanced - multiple mappings + + @RequestMapping(value = { "/advanced/bars", "/advanced/foos" }) + @ResponseBody + public String getFoosOrBarsByPath() { + return "Advanced - Get some Foos or Bars"; + } + + @RequestMapping(value = "*") + @ResponseBody + public String getFallback() { + return "Fallback for GET Requests"; + } + + @RequestMapping(value = "*", method = { RequestMethod.GET, RequestMethod.POST }) + @ResponseBody + public String allFallback() { + return "Fallback for All Requests"; + } + + @RequestMapping(value = "/foos/multiple", method = { RequestMethod.PUT, RequestMethod.POST }) + @ResponseBody + public String putAndPostFoos() { + return "Advanced - PUT and POST within single method"; + } + +} diff --git a/spring-5/src/main/java/org/baeldung/web/controller/ItemController.java b/spring-5/src/main/java/org/baeldung/web/controller/ItemController.java new file mode 100644 index 0000000000..1cc3eae432 --- /dev/null +++ b/spring-5/src/main/java/org/baeldung/web/controller/ItemController.java @@ -0,0 +1,39 @@ +package org.baeldung.web.controller; + +import java.util.Date; + +import org.baeldung.web.dto.Item; +import org.baeldung.web.dto.ItemManager; +import org.baeldung.web.dto.Views; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import com.fasterxml.jackson.annotation.JsonView; + +@RestController +public class ItemController { + + @JsonView(Views.Public.class) + @RequestMapping("/items/{id}") + public Item getItemPublic(@PathVariable final int id) { + return ItemManager.getById(id); + } + + @JsonView(Views.Internal.class) + @RequestMapping("/items/internal/{id}") + public Item getItemInternal(@PathVariable final int id) { + return ItemManager.getById(id); + } + + @RequestMapping("/date") + public Date getCurrentDate() throws Exception { + return new Date(); + } + + @RequestMapping("/delay/{seconds}") + public void getCurrentTime(@PathVariable final int seconds) throws Exception { + + Thread.sleep(seconds * 1000); + } +} \ No newline at end of file diff --git a/spring-5/src/main/java/org/baeldung/web/controller/MyFooController.java b/spring-5/src/main/java/org/baeldung/web/controller/MyFooController.java new file mode 100644 index 0000000000..f19ddca435 --- /dev/null +++ b/spring-5/src/main/java/org/baeldung/web/controller/MyFooController.java @@ -0,0 +1,76 @@ +package org.baeldung.web.controller; + +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +import javax.servlet.http.HttpServletResponse; + +import org.baeldung.web.dto.Foo; +import org.baeldung.web.exception.ResourceNotFoundException; +import org.springframework.http.HttpStatus; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.bind.annotation.ResponseStatus; +import org.springframework.web.servlet.support.ServletUriComponentsBuilder; + +@Controller +@RequestMapping(value = "/myfoos") +public class MyFooController { + + private final Map myfoos; + + public MyFooController() { + super(); + myfoos = new HashMap(); + myfoos.put(1L, new Foo(1L, "sample foo")); + } + + // API - read + + @RequestMapping(method = RequestMethod.GET) + @ResponseBody + public Collection findAll() { + return myfoos.values(); + } + + @RequestMapping(method = RequestMethod.GET, value = "/{id}", produces = { "application/json" }) + @ResponseBody + public Foo findById(@PathVariable final long id) { + final Foo foo = myfoos.get(id); + if (foo == null) { + throw new ResourceNotFoundException(); + } + return foo; + } + + // API - write + + @RequestMapping(method = RequestMethod.PUT, value = "/{id}") + @ResponseStatus(HttpStatus.OK) + @ResponseBody + public Foo updateFoo(@PathVariable("id") final long id, @RequestBody final Foo foo) { + myfoos.put(id, foo); + return foo; + } + + @RequestMapping(method = RequestMethod.POST) + @ResponseStatus(HttpStatus.CREATED) + @ResponseBody + public Foo createFoo(@RequestBody final Foo foo, HttpServletResponse response) { + myfoos.put(foo.getId(), foo); + response.setHeader("Location", ServletUriComponentsBuilder.fromCurrentRequest().path("/" + foo.getId()).toUriString()); + return foo; + } + + @RequestMapping(method = RequestMethod.DELETE, value = "/{id}") + @ResponseStatus(HttpStatus.OK) + public void deleteById(@PathVariable final long id) { + myfoos.remove(id); + } + +} diff --git a/spring-5/src/main/java/org/baeldung/web/controller/SimplePostController.java b/spring-5/src/main/java/org/baeldung/web/controller/SimplePostController.java new file mode 100644 index 0000000000..f8407acb47 --- /dev/null +++ b/spring-5/src/main/java/org/baeldung/web/controller/SimplePostController.java @@ -0,0 +1,73 @@ +package org.baeldung.web.controller; + +import java.io.BufferedOutputStream; +import java.io.File; +import java.io.FileOutputStream; +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.Date; + +import org.baeldung.web.dto.Foo; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.multipart.MultipartFile; + +// used to test HttpClientPostingTest +@RestController +public class SimplePostController { + + @RequestMapping(value = "/users", method = RequestMethod.POST) + public String postUser(@RequestParam final String username, @RequestParam final String password) { + return "Success" + username; + } + + @RequestMapping(value = "/users/detail", method = RequestMethod.POST) + public String postUserDetail(@RequestBody final Foo entity) { + return "Success" + entity.getId(); + } + + @RequestMapping(value = "/users/multipart", method = RequestMethod.POST) + public String uploadFile(@RequestParam final String username, @RequestParam final String password, @RequestParam("file") final MultipartFile file) { + if (!file.isEmpty()) { + try { + final DateFormat dateFormat = new SimpleDateFormat("yyyy_MM_dd_HH.mm.ss"); + final String fileName = dateFormat.format(new Date()); + final File fileServer = new File(fileName); + fileServer.createNewFile(); + final byte[] bytes = file.getBytes(); + final BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(fileServer)); + stream.write(bytes); + stream.close(); + return "You successfully uploaded " + username; + } catch (final Exception e) { + return "You failed to upload " + e.getMessage(); + } + } else { + return "You failed to upload because the file was empty."; + } + } + + @RequestMapping(value = "/users/upload", method = RequestMethod.POST) + public String postMultipart(@RequestParam("file") final MultipartFile file) { + if (!file.isEmpty()) { + try { + final DateFormat dateFormat = new SimpleDateFormat("yyyy_MM_dd_HH.mm.ss"); + final String fileName = dateFormat.format(new Date()); + final File fileServer = new File(fileName); + fileServer.createNewFile(); + final byte[] bytes = file.getBytes(); + final BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(fileServer)); + stream.write(bytes); + stream.close(); + return "You successfully uploaded "; + } catch (final Exception e) { + return "You failed to upload " + e.getMessage(); + } + } else { + return "You failed to upload because the file was empty."; + } + } +} diff --git a/spring-5/src/main/java/org/baeldung/web/controller/advice/JsonpControllerAdvice.java b/spring-5/src/main/java/org/baeldung/web/controller/advice/JsonpControllerAdvice.java new file mode 100644 index 0000000000..996f229128 --- /dev/null +++ b/spring-5/src/main/java/org/baeldung/web/controller/advice/JsonpControllerAdvice.java @@ -0,0 +1,13 @@ +package org.baeldung.web.controller.advice; + +import org.springframework.web.bind.annotation.ControllerAdvice; +import org.springframework.web.servlet.mvc.method.annotation.AbstractJsonpResponseBodyAdvice; + +@ControllerAdvice +public class JsonpControllerAdvice extends AbstractJsonpResponseBodyAdvice { + + public JsonpControllerAdvice() { + super("callback"); + } + +} diff --git a/spring-5/src/main/java/org/baeldung/web/controller/mediatypes/CustomMediaTypeController.java b/spring-5/src/main/java/org/baeldung/web/controller/mediatypes/CustomMediaTypeController.java new file mode 100644 index 0000000000..410c72d34f --- /dev/null +++ b/spring-5/src/main/java/org/baeldung/web/controller/mediatypes/CustomMediaTypeController.java @@ -0,0 +1,20 @@ +package org.baeldung.web.controller.mediatypes; + +import org.baeldung.web.dto.BaeldungItem; +import org.baeldung.web.dto.BaeldungItemV2; +import org.springframework.web.bind.annotation.*; + +@RestController +@RequestMapping(value = "/", produces = "application/vnd.baeldung.api.v1+json") +public class CustomMediaTypeController { + + @RequestMapping(method = RequestMethod.GET, value = "/public/api/items/{id}", produces = "application/vnd.baeldung.api.v1+json") + public @ResponseBody BaeldungItem getItem(@PathVariable("id") String id) { + return new BaeldungItem("itemId1"); + } + + @RequestMapping(method = RequestMethod.GET, value = "/public/api/items/{id}", produces = "application/vnd.baeldung.api.v2+json") + public @ResponseBody BaeldungItemV2 getItemSecondAPIVersion(@PathVariable("id") String id) { + return new BaeldungItemV2("itemName"); + } +} diff --git a/spring-5/src/main/java/org/baeldung/web/controller/redirect/RedirectController.java b/spring-5/src/main/java/org/baeldung/web/controller/redirect/RedirectController.java new file mode 100644 index 0000000000..472c0c8bf5 --- /dev/null +++ b/spring-5/src/main/java/org/baeldung/web/controller/redirect/RedirectController.java @@ -0,0 +1,52 @@ +package org.baeldung.web.controller.redirect; + +import org.springframework.stereotype.Controller; +import org.springframework.ui.ModelMap; +import org.springframework.web.bind.annotation.ModelAttribute; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.servlet.ModelAndView; +import org.springframework.web.servlet.mvc.support.RedirectAttributes; +import org.springframework.web.servlet.view.RedirectView; + +@Controller +@RequestMapping("/") +public class RedirectController { + + @RequestMapping(value = "/redirectWithXMLConfig", method = RequestMethod.GET) + public ModelAndView redirectWithUsingXMLConfig(final ModelMap model) { + model.addAttribute("attribute", "redirectWithXMLConfig"); + return new ModelAndView("RedirectedUrl", model); + } + + @RequestMapping(value = "/redirectWithRedirectPrefix", method = RequestMethod.GET) + public ModelAndView redirectWithUsingRedirectPrefix(final ModelMap model) { + model.addAttribute("attribute", "redirectWithRedirectPrefix"); + return new ModelAndView("redirect:/redirectedUrl", model); + } + + @RequestMapping(value = "/redirectWithRedirectAttributes", method = RequestMethod.GET) + public RedirectView redirectWithRedirectAttributes(final RedirectAttributes redirectAttributes) { + redirectAttributes.addFlashAttribute("flashAttribute", "redirectWithRedirectAttributes"); + redirectAttributes.addAttribute("attribute", "redirectWithRedirectAttributes"); + return new RedirectView("redirectedUrl"); + } + + @RequestMapping(value = "/redirectWithRedirectView", method = RequestMethod.GET) + public RedirectView redirectWithUsingRedirectView(final ModelMap model) { + model.addAttribute("attribute", "redirectWithRedirectView"); + return new RedirectView("redirectedUrl"); + } + + @RequestMapping(value = "/forwardWithForwardPrefix", method = RequestMethod.GET) + public ModelAndView forwardWithUsingForwardPrefix(final ModelMap model) { + model.addAttribute("attribute", "redirectWithForwardPrefix"); + return new ModelAndView("forward:/redirectedUrl", model); + } + + @RequestMapping(value = "/redirectedUrl", method = RequestMethod.GET) + public ModelAndView redirection(final ModelMap model, @ModelAttribute("flashAttribute") final Object flashAttribute) { + model.addAttribute("redirectionAttribute", flashAttribute); + return new ModelAndView("redirection", model); + } +} \ No newline at end of file diff --git a/spring-5/src/main/java/org/baeldung/web/controller/status/ExampleController.java b/spring-5/src/main/java/org/baeldung/web/controller/status/ExampleController.java new file mode 100644 index 0000000000..ceda138768 --- /dev/null +++ b/spring-5/src/main/java/org/baeldung/web/controller/status/ExampleController.java @@ -0,0 +1,24 @@ +package org.baeldung.web.controller.status; + +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.ResponseBody; + +@Controller +public class ExampleController { + + @RequestMapping(value = "/controller", method = RequestMethod.GET) + @ResponseBody + public ResponseEntity sendViaResponseEntity() { + return new ResponseEntity(HttpStatus.NOT_ACCEPTABLE); + } + + @RequestMapping(value = "/exception", method = RequestMethod.GET) + @ResponseBody + public ResponseEntity sendViaException() { + throw new ForbiddenException(); + } +} diff --git a/spring-5/src/main/java/org/baeldung/web/controller/status/ForbiddenException.java b/spring-5/src/main/java/org/baeldung/web/controller/status/ForbiddenException.java new file mode 100644 index 0000000000..458bdaf170 --- /dev/null +++ b/spring-5/src/main/java/org/baeldung/web/controller/status/ForbiddenException.java @@ -0,0 +1,10 @@ +package org.baeldung.web.controller.status; + +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.ResponseStatus; + +@ResponseStatus(value = HttpStatus.FORBIDDEN, reason = "To show an example of a custom message") +public class ForbiddenException extends RuntimeException { + private static final long serialVersionUID = 6826605655586311552L; + +} diff --git a/spring-5/src/main/java/org/baeldung/web/dto/BaeldungItem.java b/spring-5/src/main/java/org/baeldung/web/dto/BaeldungItem.java new file mode 100644 index 0000000000..9b3ecd33b9 --- /dev/null +++ b/spring-5/src/main/java/org/baeldung/web/dto/BaeldungItem.java @@ -0,0 +1,13 @@ +package org.baeldung.web.dto; + +public class BaeldungItem { + private final String itemId; + + public BaeldungItem(String itemId) { + this.itemId = itemId; + } + + public String getItemId() { + return itemId; + } +} diff --git a/spring-5/src/main/java/org/baeldung/web/dto/BaeldungItemV2.java b/spring-5/src/main/java/org/baeldung/web/dto/BaeldungItemV2.java new file mode 100644 index 0000000000..64df20a14e --- /dev/null +++ b/spring-5/src/main/java/org/baeldung/web/dto/BaeldungItemV2.java @@ -0,0 +1,14 @@ +package org.baeldung.web.dto; + + +public class BaeldungItemV2 { + private final String itemName; + + public BaeldungItemV2(String itemName) { + this.itemName = itemName; + } + + public String getItemName() { + return itemName; + } +} diff --git a/spring-5/src/main/java/org/baeldung/web/dto/Company.java b/spring-5/src/main/java/org/baeldung/web/dto/Company.java new file mode 100644 index 0000000000..3164d604ad --- /dev/null +++ b/spring-5/src/main/java/org/baeldung/web/dto/Company.java @@ -0,0 +1,38 @@ +package org.baeldung.web.dto; + +public class Company { + + private long id; + private String name; + + public Company() { + super(); + } + + public Company(final long id, final String name) { + this.id = id; + this.name = name; + } + + public String getName() { + return name; + } + + public void setName(final String name) { + this.name = name; + } + + public long getId() { + return id; + } + + public void setId(final long id) { + this.id = id; + } + + @Override + public String toString() { + return "Company [id=" + id + ", name=" + name + "]"; + } + +} diff --git a/spring-5/src/main/java/org/baeldung/web/dto/Foo.java b/spring-5/src/main/java/org/baeldung/web/dto/Foo.java new file mode 100644 index 0000000000..240b368b50 --- /dev/null +++ b/spring-5/src/main/java/org/baeldung/web/dto/Foo.java @@ -0,0 +1,45 @@ +package org.baeldung.web.dto; + +import com.thoughtworks.xstream.annotations.XStreamAlias; + +@XStreamAlias("Foo") +public class Foo { + private long id; + private String name; + + public Foo() { + super(); + } + + public Foo(final String name) { + super(); + + this.name = name; + } + + public Foo(final long id, final String name) { + super(); + + this.id = id; + this.name = name; + } + + // API + + public long getId() { + return id; + } + + public void setId(final long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(final String name) { + this.name = name; + } + +} \ No newline at end of file diff --git a/spring-5/src/main/java/org/baeldung/web/dto/FooProtos.java b/spring-5/src/main/java/org/baeldung/web/dto/FooProtos.java new file mode 100644 index 0000000000..8ca96c38fc --- /dev/null +++ b/spring-5/src/main/java/org/baeldung/web/dto/FooProtos.java @@ -0,0 +1,597 @@ +// Generated by the protocol buffer compiler. DO NOT EDIT! +// source: FooProtos.proto + +package org.baeldung.web.dto; + +public final class FooProtos { + private FooProtos() { + } + + public static void registerAllExtensions(com.google.protobuf.ExtensionRegistry registry) { + } + + public interface FooOrBuilder extends + // @@protoc_insertion_point(interface_extends:baeldung.Foo) + com.google.protobuf.MessageOrBuilder { + + /** + * required int64 id = 1; + */ + boolean hasId(); + + /** + * required int64 id = 1; + */ + long getId(); + + /** + * required string name = 2; + */ + boolean hasName(); + + /** + * required string name = 2; + */ + java.lang.String getName(); + + /** + * required string name = 2; + */ + com.google.protobuf.ByteString getNameBytes(); + } + + /** + * Protobuf type {@code baeldung.Foo} + */ + public static final class Foo extends com.google.protobuf.GeneratedMessage implements + // @@protoc_insertion_point(message_implements:baeldung.Foo) + FooOrBuilder { + // Use Foo.newBuilder() to construct. + private Foo(com.google.protobuf.GeneratedMessage.Builder builder) { + super(builder); + this.unknownFields = builder.getUnknownFields(); + } + + private Foo(boolean noInit) { + this.unknownFields = com.google.protobuf.UnknownFieldSet.getDefaultInstance(); + } + + private static final Foo defaultInstance; + + public static Foo getDefaultInstance() { + return defaultInstance; + } + + public Foo getDefaultInstanceForType() { + return defaultInstance; + } + + private final com.google.protobuf.UnknownFieldSet unknownFields; + + @java.lang.Override + public final com.google.protobuf.UnknownFieldSet getUnknownFields() { + return this.unknownFields; + } + + private Foo(com.google.protobuf.CodedInputStream input, com.google.protobuf.ExtensionRegistryLite extensionRegistry) throws com.google.protobuf.InvalidProtocolBufferException { + initFields(); + int mutable_bitField0_ = 0; + com.google.protobuf.UnknownFieldSet.Builder unknownFields = com.google.protobuf.UnknownFieldSet.newBuilder(); + try { + boolean done = false; + while (!done) { + int tag = input.readTag(); + switch (tag) { + case 0: + done = true; + break; + default: { + if (!parseUnknownField(input, unknownFields, extensionRegistry, tag)) { + done = true; + } + break; + } + case 8: { + bitField0_ |= 0x00000001; + id_ = input.readInt64(); + break; + } + case 18: { + com.google.protobuf.ByteString bs = input.readBytes(); + bitField0_ |= 0x00000002; + name_ = bs; + break; + } + } + } + } catch (com.google.protobuf.InvalidProtocolBufferException e) { + throw e.setUnfinishedMessage(this); + } catch (java.io.IOException e) { + throw new com.google.protobuf.InvalidProtocolBufferException(e.getMessage()).setUnfinishedMessage(this); + } finally { + this.unknownFields = unknownFields.build(); + makeExtensionsImmutable(); + } + } + + public static final com.google.protobuf.Descriptors.Descriptor getDescriptor() { + return org.baeldung.web.dto.FooProtos.internal_static_baeldung_Foo_descriptor; + } + + protected com.google.protobuf.GeneratedMessage.FieldAccessorTable internalGetFieldAccessorTable() { + return org.baeldung.web.dto.FooProtos.internal_static_baeldung_Foo_fieldAccessorTable.ensureFieldAccessorsInitialized(org.baeldung.web.dto.FooProtos.Foo.class, org.baeldung.web.dto.FooProtos.Foo.Builder.class); + } + + public static com.google.protobuf.Parser PARSER = new com.google.protobuf.AbstractParser() { + public Foo parsePartialFrom(com.google.protobuf.CodedInputStream input, com.google.protobuf.ExtensionRegistryLite extensionRegistry) throws com.google.protobuf.InvalidProtocolBufferException { + return new Foo(input, extensionRegistry); + } + }; + + @java.lang.Override + public com.google.protobuf.Parser getParserForType() { + return PARSER; + } + + private int bitField0_; + public static final int ID_FIELD_NUMBER = 1; + private long id_; + + /** + * required int64 id = 1; + */ + public boolean hasId() { + return ((bitField0_ & 0x00000001) == 0x00000001); + } + + /** + * required int64 id = 1; + */ + public long getId() { + return id_; + } + + public static final int NAME_FIELD_NUMBER = 2; + private java.lang.Object name_; + + /** + * required string name = 2; + */ + public boolean hasName() { + return ((bitField0_ & 0x00000002) == 0x00000002); + } + + /** + * required string name = 2; + */ + public java.lang.String getName() { + java.lang.Object ref = name_; + if (ref instanceof java.lang.String) { + return (java.lang.String) ref; + } else { + com.google.protobuf.ByteString bs = (com.google.protobuf.ByteString) ref; + java.lang.String s = bs.toStringUtf8(); + if (bs.isValidUtf8()) { + name_ = s; + } + return s; + } + } + + /** + * required string name = 2; + */ + public com.google.protobuf.ByteString getNameBytes() { + java.lang.Object ref = name_; + if (ref instanceof java.lang.String) { + com.google.protobuf.ByteString b = com.google.protobuf.ByteString.copyFromUtf8((java.lang.String) ref); + name_ = b; + return b; + } else { + return (com.google.protobuf.ByteString) ref; + } + } + + private void initFields() { + id_ = 0L; + name_ = ""; + } + + private byte memoizedIsInitialized = -1; + + public final boolean isInitialized() { + byte isInitialized = memoizedIsInitialized; + if (isInitialized == 1) + return true; + if (isInitialized == 0) + return false; + + if (!hasId()) { + memoizedIsInitialized = 0; + return false; + } + if (!hasName()) { + memoizedIsInitialized = 0; + return false; + } + memoizedIsInitialized = 1; + return true; + } + + public void writeTo(com.google.protobuf.CodedOutputStream output) throws java.io.IOException { + getSerializedSize(); + if (((bitField0_ & 0x00000001) == 0x00000001)) { + output.writeInt64(1, id_); + } + if (((bitField0_ & 0x00000002) == 0x00000002)) { + output.writeBytes(2, getNameBytes()); + } + getUnknownFields().writeTo(output); + } + + private int memoizedSerializedSize = -1; + + public int getSerializedSize() { + int size = memoizedSerializedSize; + if (size != -1) + return size; + + size = 0; + if (((bitField0_ & 0x00000001) == 0x00000001)) { + size += com.google.protobuf.CodedOutputStream.computeInt64Size(1, id_); + } + if (((bitField0_ & 0x00000002) == 0x00000002)) { + size += com.google.protobuf.CodedOutputStream.computeBytesSize(2, getNameBytes()); + } + size += getUnknownFields().getSerializedSize(); + memoizedSerializedSize = size; + return size; + } + + private static final long serialVersionUID = 0L; + + @java.lang.Override + protected java.lang.Object writeReplace() throws java.io.ObjectStreamException { + return super.writeReplace(); + } + + public static org.baeldung.web.dto.FooProtos.Foo parseFrom(com.google.protobuf.ByteString data) throws com.google.protobuf.InvalidProtocolBufferException { + return PARSER.parseFrom(data); + } + + public static org.baeldung.web.dto.FooProtos.Foo parseFrom(com.google.protobuf.ByteString data, com.google.protobuf.ExtensionRegistryLite extensionRegistry) throws com.google.protobuf.InvalidProtocolBufferException { + return PARSER.parseFrom(data, extensionRegistry); + } + + public static org.baeldung.web.dto.FooProtos.Foo parseFrom(byte[] data) throws com.google.protobuf.InvalidProtocolBufferException { + return PARSER.parseFrom(data); + } + + public static org.baeldung.web.dto.FooProtos.Foo parseFrom(byte[] data, com.google.protobuf.ExtensionRegistryLite extensionRegistry) throws com.google.protobuf.InvalidProtocolBufferException { + return PARSER.parseFrom(data, extensionRegistry); + } + + public static org.baeldung.web.dto.FooProtos.Foo parseFrom(java.io.InputStream input) throws java.io.IOException { + return PARSER.parseFrom(input); + } + + public static org.baeldung.web.dto.FooProtos.Foo parseFrom(java.io.InputStream input, com.google.protobuf.ExtensionRegistryLite extensionRegistry) throws java.io.IOException { + return PARSER.parseFrom(input, extensionRegistry); + } + + public static org.baeldung.web.dto.FooProtos.Foo parseDelimitedFrom(java.io.InputStream input) throws java.io.IOException { + return PARSER.parseDelimitedFrom(input); + } + + public static org.baeldung.web.dto.FooProtos.Foo parseDelimitedFrom(java.io.InputStream input, com.google.protobuf.ExtensionRegistryLite extensionRegistry) throws java.io.IOException { + return PARSER.parseDelimitedFrom(input, extensionRegistry); + } + + public static org.baeldung.web.dto.FooProtos.Foo parseFrom(com.google.protobuf.CodedInputStream input) throws java.io.IOException { + return PARSER.parseFrom(input); + } + + public static org.baeldung.web.dto.FooProtos.Foo parseFrom(com.google.protobuf.CodedInputStream input, com.google.protobuf.ExtensionRegistryLite extensionRegistry) throws java.io.IOException { + return PARSER.parseFrom(input, extensionRegistry); + } + + public static Builder newBuilder() { + return Builder.create(); + } + + public Builder newBuilderForType() { + return newBuilder(); + } + + public static Builder newBuilder(org.baeldung.web.dto.FooProtos.Foo prototype) { + return newBuilder().mergeFrom(prototype); + } + + public Builder toBuilder() { + return newBuilder(this); + } + + @java.lang.Override + protected Builder newBuilderForType(com.google.protobuf.GeneratedMessage.BuilderParent parent) { + Builder builder = new Builder(parent); + return builder; + } + + /** + * Protobuf type {@code baeldung.Foo} + */ + public static final class Builder extends com.google.protobuf.GeneratedMessage.Builder implements + // @@protoc_insertion_point(builder_implements:baeldung.Foo) + org.baeldung.web.dto.FooProtos.FooOrBuilder { + public static final com.google.protobuf.Descriptors.Descriptor getDescriptor() { + return org.baeldung.web.dto.FooProtos.internal_static_baeldung_Foo_descriptor; + } + + protected com.google.protobuf.GeneratedMessage.FieldAccessorTable internalGetFieldAccessorTable() { + return org.baeldung.web.dto.FooProtos.internal_static_baeldung_Foo_fieldAccessorTable.ensureFieldAccessorsInitialized(org.baeldung.web.dto.FooProtos.Foo.class, org.baeldung.web.dto.FooProtos.Foo.Builder.class); + } + + // Construct using org.baeldung.web.dto.FooProtos.Foo.newBuilder() + private Builder() { + maybeForceBuilderInitialization(); + } + + private Builder(com.google.protobuf.GeneratedMessage.BuilderParent parent) { + super(parent); + maybeForceBuilderInitialization(); + } + + private void maybeForceBuilderInitialization() { + if (com.google.protobuf.GeneratedMessage.alwaysUseFieldBuilders) { + } + } + + private static Builder create() { + return new Builder(); + } + + public Builder clear() { + super.clear(); + id_ = 0L; + bitField0_ = (bitField0_ & ~0x00000001); + name_ = ""; + bitField0_ = (bitField0_ & ~0x00000002); + return this; + } + + public Builder clone() { + return create().mergeFrom(buildPartial()); + } + + public com.google.protobuf.Descriptors.Descriptor getDescriptorForType() { + return org.baeldung.web.dto.FooProtos.internal_static_baeldung_Foo_descriptor; + } + + public org.baeldung.web.dto.FooProtos.Foo getDefaultInstanceForType() { + return org.baeldung.web.dto.FooProtos.Foo.getDefaultInstance(); + } + + public org.baeldung.web.dto.FooProtos.Foo build() { + org.baeldung.web.dto.FooProtos.Foo result = buildPartial(); + if (!result.isInitialized()) { + throw newUninitializedMessageException(result); + } + return result; + } + + public org.baeldung.web.dto.FooProtos.Foo buildPartial() { + org.baeldung.web.dto.FooProtos.Foo result = new org.baeldung.web.dto.FooProtos.Foo(this); + int from_bitField0_ = bitField0_; + int to_bitField0_ = 0; + if (((from_bitField0_ & 0x00000001) == 0x00000001)) { + to_bitField0_ |= 0x00000001; + } + result.id_ = id_; + if (((from_bitField0_ & 0x00000002) == 0x00000002)) { + to_bitField0_ |= 0x00000002; + } + result.name_ = name_; + result.bitField0_ = to_bitField0_; + onBuilt(); + return result; + } + + public Builder mergeFrom(com.google.protobuf.Message other) { + if (other instanceof org.baeldung.web.dto.FooProtos.Foo) { + return mergeFrom((org.baeldung.web.dto.FooProtos.Foo) other); + } else { + super.mergeFrom(other); + return this; + } + } + + public Builder mergeFrom(org.baeldung.web.dto.FooProtos.Foo other) { + if (other == org.baeldung.web.dto.FooProtos.Foo.getDefaultInstance()) + return this; + if (other.hasId()) { + setId(other.getId()); + } + if (other.hasName()) { + bitField0_ |= 0x00000002; + name_ = other.name_; + onChanged(); + } + this.mergeUnknownFields(other.getUnknownFields()); + return this; + } + + public final boolean isInitialized() { + if (!hasId()) { + + return false; + } + if (!hasName()) { + + return false; + } + return true; + } + + public Builder mergeFrom(com.google.protobuf.CodedInputStream input, com.google.protobuf.ExtensionRegistryLite extensionRegistry) throws java.io.IOException { + org.baeldung.web.dto.FooProtos.Foo parsedMessage = null; + try { + parsedMessage = PARSER.parsePartialFrom(input, extensionRegistry); + } catch (com.google.protobuf.InvalidProtocolBufferException e) { + parsedMessage = (org.baeldung.web.dto.FooProtos.Foo) e.getUnfinishedMessage(); + throw e; + } finally { + if (parsedMessage != null) { + mergeFrom(parsedMessage); + } + } + return this; + } + + private int bitField0_; + + private long id_; + + /** + * required int64 id = 1; + */ + public boolean hasId() { + return ((bitField0_ & 0x00000001) == 0x00000001); + } + + /** + * required int64 id = 1; + */ + public long getId() { + return id_; + } + + /** + * required int64 id = 1; + */ + public Builder setId(long value) { + bitField0_ |= 0x00000001; + id_ = value; + onChanged(); + return this; + } + + /** + * required int64 id = 1; + */ + public Builder clearId() { + bitField0_ = (bitField0_ & ~0x00000001); + id_ = 0L; + onChanged(); + return this; + } + + private java.lang.Object name_ = ""; + + /** + * required string name = 2; + */ + public boolean hasName() { + return ((bitField0_ & 0x00000002) == 0x00000002); + } + + /** + * required string name = 2; + */ + public java.lang.String getName() { + java.lang.Object ref = name_; + if (!(ref instanceof java.lang.String)) { + com.google.protobuf.ByteString bs = (com.google.protobuf.ByteString) ref; + java.lang.String s = bs.toStringUtf8(); + if (bs.isValidUtf8()) { + name_ = s; + } + return s; + } else { + return (java.lang.String) ref; + } + } + + /** + * required string name = 2; + */ + public com.google.protobuf.ByteString getNameBytes() { + java.lang.Object ref = name_; + if (ref instanceof String) { + com.google.protobuf.ByteString b = com.google.protobuf.ByteString.copyFromUtf8((java.lang.String) ref); + name_ = b; + return b; + } else { + return (com.google.protobuf.ByteString) ref; + } + } + + /** + * required string name = 2; + */ + public Builder setName(java.lang.String value) { + if (value == null) { + throw new NullPointerException(); + } + bitField0_ |= 0x00000002; + name_ = value; + onChanged(); + return this; + } + + /** + * required string name = 2; + */ + public Builder clearName() { + bitField0_ = (bitField0_ & ~0x00000002); + name_ = getDefaultInstance().getName(); + onChanged(); + return this; + } + + /** + * required string name = 2; + */ + public Builder setNameBytes(com.google.protobuf.ByteString value) { + if (value == null) { + throw new NullPointerException(); + } + bitField0_ |= 0x00000002; + name_ = value; + onChanged(); + return this; + } + + // @@protoc_insertion_point(builder_scope:baeldung.Foo) + } + + static { + defaultInstance = new Foo(true); + defaultInstance.initFields(); + } + + // @@protoc_insertion_point(class_scope:baeldung.Foo) + } + + private static final com.google.protobuf.Descriptors.Descriptor internal_static_baeldung_Foo_descriptor; + private static com.google.protobuf.GeneratedMessage.FieldAccessorTable internal_static_baeldung_Foo_fieldAccessorTable; + + public static com.google.protobuf.Descriptors.FileDescriptor getDescriptor() { + return descriptor; + } + + private static com.google.protobuf.Descriptors.FileDescriptor descriptor; + static { + java.lang.String[] descriptorData = { "\n\017FooProtos.proto\022\010baeldung\"\037\n\003Foo\022\n\n\002id" + "\030\001 \002(\003\022\014\n\004name\030\002 \002(\tB!\n\024org.baeldung.web" + ".dtoB\tFooProtos" }; + com.google.protobuf.Descriptors.FileDescriptor.InternalDescriptorAssigner assigner = new com.google.protobuf.Descriptors.FileDescriptor.InternalDescriptorAssigner() { + public com.google.protobuf.ExtensionRegistry assignDescriptors(com.google.protobuf.Descriptors.FileDescriptor root) { + descriptor = root; + return null; + } + }; + com.google.protobuf.Descriptors.FileDescriptor.internalBuildGeneratedFileFrom(descriptorData, new com.google.protobuf.Descriptors.FileDescriptor[] {}, assigner); + internal_static_baeldung_Foo_descriptor = getDescriptor().getMessageTypes().get(0); + internal_static_baeldung_Foo_fieldAccessorTable = new com.google.protobuf.GeneratedMessage.FieldAccessorTable(internal_static_baeldung_Foo_descriptor, new java.lang.String[] { "Id", "Name", }); + } + + // @@protoc_insertion_point(outer_class_scope) +} diff --git a/spring-5/src/main/java/org/baeldung/web/dto/Item.java b/spring-5/src/main/java/org/baeldung/web/dto/Item.java new file mode 100644 index 0000000000..536c72020f --- /dev/null +++ b/spring-5/src/main/java/org/baeldung/web/dto/Item.java @@ -0,0 +1,36 @@ +package org.baeldung.web.dto; + +import com.fasterxml.jackson.annotation.JsonView; + +public class Item { + @JsonView(Views.Public.class) + public int id; + + @JsonView(Views.Public.class) + public String itemName; + + @JsonView(Views.Internal.class) + public String ownerName; + + public Item() { + super(); + } + + public Item(final int id, final String itemName, final String ownerName) { + this.id = id; + this.itemName = itemName; + this.ownerName = ownerName; + } + + public int getId() { + return id; + } + + public String getItemName() { + return itemName; + } + + public String getOwnerName() { + return ownerName; + } +} \ No newline at end of file diff --git a/spring-5/src/main/java/org/baeldung/web/dto/ItemManager.java b/spring-5/src/main/java/org/baeldung/web/dto/ItemManager.java new file mode 100644 index 0000000000..74ffada300 --- /dev/null +++ b/spring-5/src/main/java/org/baeldung/web/dto/ItemManager.java @@ -0,0 +1,9 @@ +package org.baeldung.web.dto; + +public class ItemManager { + + public static Item getById(final int id) { + final Item item = new Item(2, "book", "John"); + return item; + } +} diff --git a/spring-5/src/main/java/org/baeldung/web/dto/Views.java b/spring-5/src/main/java/org/baeldung/web/dto/Views.java new file mode 100644 index 0000000000..6231e12bcc --- /dev/null +++ b/spring-5/src/main/java/org/baeldung/web/dto/Views.java @@ -0,0 +1,9 @@ +package org.baeldung.web.dto; + +public class Views { + public static class Public { + } + + public static class Internal extends Public { + } +} diff --git a/spring-5/src/main/java/org/baeldung/web/exception/ResourceNotFoundException.java b/spring-5/src/main/java/org/baeldung/web/exception/ResourceNotFoundException.java new file mode 100644 index 0000000000..aab737b6ec --- /dev/null +++ b/spring-5/src/main/java/org/baeldung/web/exception/ResourceNotFoundException.java @@ -0,0 +1,8 @@ +package org.baeldung.web.exception; + +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.ResponseStatus; + +@ResponseStatus(value = HttpStatus.NOT_FOUND) +public class ResourceNotFoundException extends RuntimeException { +} diff --git a/spring-5/src/main/resources/application.properties b/spring-5/src/main/resources/application.properties new file mode 100644 index 0000000000..300589f561 --- /dev/null +++ b/spring-5/src/main/resources/application.properties @@ -0,0 +1,2 @@ +server.port= 8082 +server.context-path=/spring-rest \ No newline at end of file diff --git a/spring-5/src/main/resources/logback.xml b/spring-5/src/main/resources/logback.xml new file mode 100644 index 0000000000..1146dade63 --- /dev/null +++ b/spring-5/src/main/resources/logback.xml @@ -0,0 +1,20 @@ + + + + + web - %date [%thread] %-5level %logger{36} - %message%n + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/spring-5/src/main/webapp/WEB-INF/api-servlet.xml b/spring-5/src/main/webapp/WEB-INF/api-servlet.xml new file mode 100644 index 0000000000..0f80990c16 --- /dev/null +++ b/spring-5/src/main/webapp/WEB-INF/api-servlet.xml @@ -0,0 +1,56 @@ + + + + + + + + + + + + + + + + + + + + + + /WEB-INF/spring-views.xml + + + + + + + + + + + + + + + + + + + + + diff --git a/spring-5/src/main/webapp/WEB-INF/company.html b/spring-5/src/main/webapp/WEB-INF/company.html new file mode 100644 index 0000000000..d2072bfd3c --- /dev/null +++ b/spring-5/src/main/webapp/WEB-INF/company.html @@ -0,0 +1,44 @@ + + + + + Company Data + + + + + + + +
+ + + \ No newline at end of file diff --git a/spring-5/src/main/webapp/WEB-INF/spring-views.xml b/spring-5/src/main/webapp/WEB-INF/spring-views.xml new file mode 100644 index 0000000000..2944828d6d --- /dev/null +++ b/spring-5/src/main/webapp/WEB-INF/spring-views.xml @@ -0,0 +1,10 @@ + + + + + + + \ No newline at end of file diff --git a/spring-5/src/main/webapp/WEB-INF/web.xml b/spring-5/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000000..a439de8a05 --- /dev/null +++ b/spring-5/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,41 @@ + + + + Spring MVC Application + + + + contextClass + + org.springframework.web.context.support.AnnotationConfigWebApplicationContext + + + + contextConfigLocation + org.baeldung.config + + + + org.springframework.web.context.ContextLoaderListener + + + + + api + org.springframework.web.servlet.DispatcherServlet + 1 + + + api + / + + + + + + + diff --git a/spring-5/src/test/java/org/baeldung/client/Consts.java b/spring-5/src/test/java/org/baeldung/client/Consts.java new file mode 100644 index 0000000000..b40561d9c3 --- /dev/null +++ b/spring-5/src/test/java/org/baeldung/client/Consts.java @@ -0,0 +1,5 @@ +package org.baeldung.client; + +public interface Consts { + int APPLICATION_PORT = 8082; +} diff --git a/spring-5/src/test/java/org/baeldung/client/RestTemplateBasicLiveTest.java b/spring-5/src/test/java/org/baeldung/client/RestTemplateBasicLiveTest.java new file mode 100644 index 0000000000..a47c60e9d8 --- /dev/null +++ b/spring-5/src/test/java/org/baeldung/client/RestTemplateBasicLiveTest.java @@ -0,0 +1,216 @@ +package org.baeldung.client; + +import static org.apache.commons.codec.binary.Base64.encodeBase64; +import static org.baeldung.client.Consts.APPLICATION_PORT; +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.notNullValue; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.io.IOException; +import java.net.URI; +import java.util.Arrays; +import java.util.Set; + +import org.baeldung.web.dto.Foo; +import org.junit.Before; +import org.junit.Test; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; +import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.http.client.ClientHttpRequestFactory; +import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; +import org.springframework.web.client.HttpClientErrorException; +import org.springframework.web.client.RequestCallback; +import org.springframework.web.client.RestTemplate; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.common.base.Charsets; + +public class RestTemplateBasicLiveTest { + + private RestTemplate restTemplate; + private static final String fooResourceUrl = "http://localhost:" + APPLICATION_PORT + "/spring-rest/myfoos"; + + @Before + public void beforeTest() { + restTemplate = new RestTemplate(); + } + + // GET + + @Test + public void givenResourceUrl_whenSendGetForRequestEntity_thenStatusOk() throws IOException { + final ResponseEntity response = restTemplate.getForEntity(fooResourceUrl + "/1", String.class); + + assertThat(response.getStatusCode(), equalTo(HttpStatus.OK)); + } + + @Test + public void givenResourceUrl_whenSendGetForRequestEntity_thenBodyCorrect() throws IOException { + final ResponseEntity response = restTemplate.getForEntity(fooResourceUrl + "/1", String.class); + + final ObjectMapper mapper = new ObjectMapper(); + final JsonNode root = mapper.readTree(response.getBody()); + final JsonNode name = root.path("name"); + assertThat(name.asText(), notNullValue()); + } + + @Test + public void givenResourceUrl_whenRetrievingResource_thenCorrect() throws IOException { + final Foo foo = restTemplate.getForObject(fooResourceUrl + "/1", Foo.class); + + assertThat(foo.getName(), notNullValue()); + assertThat(foo.getId(), is(1L)); + } + + // HEAD, OPTIONS + + @Test + public void givenFooService_whenCallHeadForHeaders_thenReceiveAllHeadersForThatResource() { + final HttpHeaders httpHeaders = restTemplate.headForHeaders(fooResourceUrl); + + assertTrue(httpHeaders.getContentType().includes(MediaType.APPLICATION_JSON)); + } + + // POST + + @Test + public void givenFooService_whenPostForObject_thenCreatedObjectIsReturned() { + final HttpEntity request = new HttpEntity<>(new Foo("bar")); + final Foo foo = restTemplate.postForObject(fooResourceUrl, request, Foo.class); + assertThat(foo, notNullValue()); + assertThat(foo.getName(), is("bar")); + } + + @Test + public void givenFooService_whenPostForLocation_thenCreatedLocationIsReturned() { + final HttpEntity request = new HttpEntity<>(new Foo("bar")); + final URI location = restTemplate.postForLocation(fooResourceUrl, request); + assertThat(location, notNullValue()); + } + + @Test + public void givenFooService_whenPostResource_thenResourceIsCreated() { + final RestTemplate template = new RestTemplate(); + + final HttpEntity request = new HttpEntity<>(new Foo("bar")); + + final ResponseEntity response = template.exchange(fooResourceUrl, HttpMethod.POST, request, Foo.class); + assertThat(response.getStatusCode(), is(HttpStatus.CREATED)); + final Foo foo = response.getBody(); + assertThat(foo, notNullValue()); + assertThat(foo.getName(), is("bar")); + } + + @Test + public void givenFooService_whenCallOptionsForAllow_thenReceiveValueOfAllowHeader() { + final Set optionsForAllow = restTemplate.optionsForAllow(fooResourceUrl); + final HttpMethod[] supportedMethods = { HttpMethod.GET, HttpMethod.POST, HttpMethod.HEAD }; + + assertTrue(optionsForAllow.containsAll(Arrays.asList(supportedMethods))); + } + + // PUT + + @Test + public void givenFooService_whenPutExistingEntity_thenItIsUpdated() { + final RestTemplate template = new RestTemplate(); + final HttpHeaders headers = prepareBasicAuthHeaders(); + final HttpEntity request = new HttpEntity<>(new Foo("bar"), headers); + + // Create Resource + final ResponseEntity createResponse = template.exchange(fooResourceUrl, HttpMethod.POST, request, Foo.class); + + // Update Resource + final Foo updatedInstance = new Foo("newName"); + updatedInstance.setId(createResponse.getBody().getId()); + final String resourceUrl = fooResourceUrl + '/' + createResponse.getBody().getId(); + final HttpEntity requestUpdate = new HttpEntity<>(updatedInstance, headers); + template.exchange(resourceUrl, HttpMethod.PUT, requestUpdate, Void.class); + + // Check that Resource was updated + final ResponseEntity updateResponse = template.exchange(resourceUrl, HttpMethod.GET, new HttpEntity<>(headers), Foo.class); + final Foo foo = updateResponse.getBody(); + assertThat(foo.getName(), is(updatedInstance.getName())); + } + + @Test + public void givenFooService_whenPutExistingEntityWithCallback_thenItIsUpdated() { + final RestTemplate template = new RestTemplate(); + final HttpHeaders headers = prepareBasicAuthHeaders(); + final HttpEntity request = new HttpEntity<>(new Foo("bar"), headers); + + // Create entity + ResponseEntity response = template.exchange(fooResourceUrl, HttpMethod.POST, request, Foo.class); + assertThat(response.getStatusCode(), is(HttpStatus.CREATED)); + + // Update entity + final Foo updatedInstance = new Foo("newName"); + updatedInstance.setId(response.getBody().getId()); + final String resourceUrl = fooResourceUrl + '/' + response.getBody().getId(); + template.execute(resourceUrl, HttpMethod.PUT, requestCallback(updatedInstance), clientHttpResponse -> null); + + // Check that entity was updated + response = template.exchange(resourceUrl, HttpMethod.GET, new HttpEntity<>(headers), Foo.class); + final Foo foo = response.getBody(); + assertThat(foo.getName(), is(updatedInstance.getName())); + } + + // DELETE + + @Test + public void givenFooService_whenCallDelete_thenEntityIsRemoved() { + final Foo foo = new Foo("remove me"); + final ResponseEntity response = restTemplate.postForEntity(fooResourceUrl, foo, Foo.class); + assertThat(response.getStatusCode(), is(HttpStatus.CREATED)); + + final String entityUrl = fooResourceUrl + "/" + response.getBody().getId(); + restTemplate.delete(entityUrl); + try { + restTemplate.getForEntity(entityUrl, Foo.class); + fail(); + } catch (final HttpClientErrorException ex) { + assertThat(ex.getStatusCode(), is(HttpStatus.NOT_FOUND)); + } + } + + // + + private HttpHeaders prepareBasicAuthHeaders() { + final HttpHeaders headers = new HttpHeaders(); + final String encodedLogPass = getBase64EncodedLogPass(); + headers.add(HttpHeaders.AUTHORIZATION, "Basic " + encodedLogPass); + return headers; + } + + private String getBase64EncodedLogPass() { + final String logPass = "user1:user1Pass"; + final byte[] authHeaderBytes = encodeBase64(logPass.getBytes(Charsets.US_ASCII)); + return new String(authHeaderBytes, Charsets.US_ASCII); + } + + private RequestCallback requestCallback(final Foo updatedInstance) { + return clientHttpRequest -> { + final ObjectMapper mapper = new ObjectMapper(); + mapper.writeValue(clientHttpRequest.getBody(), updatedInstance); + clientHttpRequest.getHeaders().add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE); + clientHttpRequest.getHeaders().add(HttpHeaders.AUTHORIZATION, "Basic " + getBase64EncodedLogPass()); + }; + } + + // Simply setting restTemplate timeout using ClientHttpRequestFactory + + ClientHttpRequestFactory getSimpleClientHttpRequestFactory() { + final int timeout = 5; + final HttpComponentsClientHttpRequestFactory clientHttpRequestFactory = new HttpComponentsClientHttpRequestFactory(); + clientHttpRequestFactory.setConnectTimeout(timeout * 1000); + return clientHttpRequestFactory; + } +} diff --git a/spring-5/src/test/java/org/baeldung/okhttp/DefaultContentTypeInterceptor.java b/spring-5/src/test/java/org/baeldung/okhttp/DefaultContentTypeInterceptor.java new file mode 100644 index 0000000000..c4fc689fad --- /dev/null +++ b/spring-5/src/test/java/org/baeldung/okhttp/DefaultContentTypeInterceptor.java @@ -0,0 +1,24 @@ +package org.baeldung.okhttp; + +import java.io.IOException; + +import okhttp3.Interceptor; +import okhttp3.Request; +import okhttp3.Response; + +public class DefaultContentTypeInterceptor implements Interceptor { + + private final String contentType; + + public DefaultContentTypeInterceptor(String contentType) { + this.contentType = contentType; + } + + public Response intercept(Interceptor.Chain chain) throws IOException { + + Request originalRequest = chain.request(); + Request requestWithUserAgent = originalRequest.newBuilder().header("Content-Type", contentType).build(); + + return chain.proceed(requestWithUserAgent); + } +} diff --git a/spring-5/src/test/java/org/baeldung/okhttp/OkHttpFileUploadingLiveTest.java b/spring-5/src/test/java/org/baeldung/okhttp/OkHttpFileUploadingLiveTest.java new file mode 100644 index 0000000000..a33742b8de --- /dev/null +++ b/spring-5/src/test/java/org/baeldung/okhttp/OkHttpFileUploadingLiveTest.java @@ -0,0 +1,65 @@ +package org.baeldung.okhttp; + +import static org.baeldung.client.Consts.APPLICATION_PORT; +import static org.hamcrest.Matchers.equalTo; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertThat; + +import java.io.File; +import java.io.IOException; + +import okhttp3.Call; +import okhttp3.MediaType; +import okhttp3.MultipartBody; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.RequestBody; +import okhttp3.Response; + +import org.junit.Before; +import org.junit.Test; + +public class OkHttpFileUploadingLiveTest { + + private static final String BASE_URL = "http://localhost:" + APPLICATION_PORT + "/spring-rest"; + + OkHttpClient client; + + @Before + public void init() { + client = new OkHttpClient(); + } + + @Test + public void whenUploadFile_thenCorrect() throws IOException { + + final RequestBody requestBody = new MultipartBody.Builder().setType(MultipartBody.FORM).addFormDataPart("file", "file.txt", RequestBody.create(MediaType.parse("application/octet-stream"), new File("src/test/resources/test.txt"))).build(); + + final Request request = new Request.Builder().url(BASE_URL + "/users/upload").post(requestBody).build(); + + final Call call = client.newCall(request); + final Response response = call.execute(); + + assertThat(response.code(), equalTo(200)); + } + + @Test + public void whenGetUploadFileProgress_thenCorrect() throws IOException { + + final RequestBody requestBody = new MultipartBody.Builder().setType(MultipartBody.FORM).addFormDataPart("file", "file.txt", RequestBody.create(MediaType.parse("application/octet-stream"), new File("src/test/resources/test.txt"))).build(); + + final ProgressRequestWrapper countingBody = new ProgressRequestWrapper(requestBody, (long bytesWritten, long contentLength) -> { + + final float percentage = (100f * bytesWritten) / contentLength; + assertFalse(Float.compare(percentage, 100) > 0); + }); + + final Request request = new Request.Builder().url(BASE_URL + "/users/upload").post(countingBody).build(); + + final Call call = client.newCall(request); + final Response response = call.execute(); + + assertThat(response.code(), equalTo(200)); + + } +} diff --git a/spring-5/src/test/java/org/baeldung/okhttp/OkHttpGetLiveTest.java b/spring-5/src/test/java/org/baeldung/okhttp/OkHttpGetLiveTest.java new file mode 100644 index 0000000000..6aa33b06b1 --- /dev/null +++ b/spring-5/src/test/java/org/baeldung/okhttp/OkHttpGetLiveTest.java @@ -0,0 +1,77 @@ +package org.baeldung.okhttp; + +import static org.baeldung.client.Consts.APPLICATION_PORT; +import static org.hamcrest.CoreMatchers.equalTo; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.fail; + +import java.io.IOException; + +import okhttp3.Call; +import okhttp3.Callback; +import okhttp3.HttpUrl; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.Response; + +import org.junit.Before; +import org.junit.Test; + +public class OkHttpGetLiveTest { + + private static final String BASE_URL = "http://localhost:" + APPLICATION_PORT + "/spring-rest"; + + OkHttpClient client; + + @Before + public void init() { + + client = new OkHttpClient(); + } + + @Test + public void whenGetRequest_thenCorrect() throws IOException { + final Request request = new Request.Builder().url(BASE_URL + "/date").build(); + + final Call call = client.newCall(request); + final Response response = call.execute(); + + assertThat(response.code(), equalTo(200)); + } + + @Test + public void whenGetRequestWithQueryParameter_thenCorrect() throws IOException { + final HttpUrl.Builder urlBuilder = HttpUrl.parse(BASE_URL + "/ex/bars").newBuilder(); + urlBuilder.addQueryParameter("id", "1"); + + final String url = urlBuilder.build().toString(); + + final Request request = new Request.Builder().url(url).build(); + + final Call call = client.newCall(request); + final Response response = call.execute(); + + assertThat(response.code(), equalTo(200)); + } + + @Test + public void whenAsynchronousGetRequest_thenCorrect() throws InterruptedException { + final Request request = new Request.Builder().url(BASE_URL + "/date").build(); + + final Call call = client.newCall(request); + + call.enqueue(new Callback() { + @Override + public void onResponse(Call call, Response response) throws IOException { + System.out.println("OK"); + } + + @Override + public void onFailure(Call call, IOException e) { + fail(); + } + }); + + Thread.sleep(3000); + } +} diff --git a/spring-5/src/test/java/org/baeldung/okhttp/OkHttpHeaderLiveTest.java b/spring-5/src/test/java/org/baeldung/okhttp/OkHttpHeaderLiveTest.java new file mode 100644 index 0000000000..cfec119fe0 --- /dev/null +++ b/spring-5/src/test/java/org/baeldung/okhttp/OkHttpHeaderLiveTest.java @@ -0,0 +1,45 @@ +package org.baeldung.okhttp; + +import java.io.IOException; + +import org.junit.Before; +import org.junit.Test; + +import okhttp3.Call; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.Response; + +public class OkHttpHeaderLiveTest { + + private static final String SAMPLE_URL = "http://www.github.com"; + + OkHttpClient client; + + @Before + public void init() { + + client = new OkHttpClient(); + } + + @Test + public void whenSetHeader_thenCorrect() throws IOException { + Request request = new Request.Builder().url(SAMPLE_URL).addHeader("Content-Type", "application/json").build(); + + Call call = client.newCall(request); + Response response = call.execute(); + response.close(); + } + + @Test + public void whenSetDefaultHeader_thenCorrect() throws IOException { + + OkHttpClient clientWithInterceptor = new OkHttpClient.Builder().addInterceptor(new DefaultContentTypeInterceptor("application/json")).build(); + + Request request = new Request.Builder().url(SAMPLE_URL).build(); + + Call call = clientWithInterceptor.newCall(request); + Response response = call.execute(); + response.close(); + } +} diff --git a/spring-5/src/test/java/org/baeldung/okhttp/OkHttpMiscLiveTest.java b/spring-5/src/test/java/org/baeldung/okhttp/OkHttpMiscLiveTest.java new file mode 100644 index 0000000000..52662262e1 --- /dev/null +++ b/spring-5/src/test/java/org/baeldung/okhttp/OkHttpMiscLiveTest.java @@ -0,0 +1,99 @@ +package org.baeldung.okhttp; + +import static org.baeldung.client.Consts.APPLICATION_PORT; + +import java.io.File; +import java.io.IOException; +import java.net.SocketTimeoutException; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; + +import okhttp3.Cache; +import okhttp3.Call; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.Response; + +import org.junit.Before; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class OkHttpMiscLiveTest { + + private static final String BASE_URL = "http://localhost:" + APPLICATION_PORT + "/spring-rest"; + private static Logger logger = LoggerFactory.getLogger(OkHttpMiscLiveTest.class); + + OkHttpClient client; + + @Before + public void init() { + + client = new OkHttpClient(); + } + + @Test(expected = SocketTimeoutException.class) + public void whenSetRequestTimeout_thenFail() throws IOException { + final OkHttpClient clientWithTimeout = new OkHttpClient.Builder().readTimeout(1, TimeUnit.SECONDS).build(); + + final Request request = new Request.Builder().url(BASE_URL + "/delay/2") // This URL is served with a 2 second delay. + .build(); + + final Call call = clientWithTimeout.newCall(request); + final Response response = call.execute(); + response.close(); + } + + @Test(expected = IOException.class) + public void whenCancelRequest_thenCorrect() throws IOException { + final ScheduledExecutorService executor = Executors.newScheduledThreadPool(1); + + final Request request = new Request.Builder().url(BASE_URL + "/delay/2") // This URL is served with a 2 second delay. + .build(); + + final int seconds = 1; + final long startNanos = System.nanoTime(); + + final Call call = client.newCall(request); + + // Schedule a job to cancel the call in 1 second. + executor.schedule(() -> { + + logger.debug("Canceling call: " + ((System.nanoTime() - startNanos) / 1e9f)); + call.cancel(); + logger.debug("Canceled call: " + ((System.nanoTime() - startNanos) / 1e9f)); + + }, seconds, TimeUnit.SECONDS); + + logger.debug("Executing call: " + ((System.nanoTime() - startNanos) / 1e9f)); + final Response response = call.execute(); + logger.debug("Call completed: " + ((System.nanoTime() - startNanos) / 1e9f), response); + } + + @Test + public void whenSetResponseCache_thenCorrect() throws IOException { + + final int cacheSize = 10 * 1024 * 1024; // 10 MiB + final File cacheDirectory = new File("src/test/resources/cache"); + final Cache cache = new Cache(cacheDirectory, cacheSize); + + final OkHttpClient clientCached = new OkHttpClient.Builder().cache(cache).build(); + + final Request request = new Request.Builder().url("http://publicobject.com/helloworld.txt").build(); + + final Response response1 = clientCached.newCall(request).execute(); + logResponse(response1); + + final Response response2 = clientCached.newCall(request).execute(); + logResponse(response2); + } + + private void logResponse(Response response) throws IOException { + + logger.debug("Response response: " + response); + logger.debug("Response cache response: " + response.cacheResponse()); + logger.debug("Response network response: " + response.networkResponse()); + logger.debug("Response responseBody: " + response.body().string()); + } +} diff --git a/spring-5/src/test/java/org/baeldung/okhttp/OkHttpPostingLiveTest.java b/spring-5/src/test/java/org/baeldung/okhttp/OkHttpPostingLiveTest.java new file mode 100644 index 0000000000..77a78c2634 --- /dev/null +++ b/spring-5/src/test/java/org/baeldung/okhttp/OkHttpPostingLiveTest.java @@ -0,0 +1,85 @@ +package org.baeldung.okhttp; + +import static org.baeldung.client.Consts.APPLICATION_PORT; +import static org.hamcrest.Matchers.equalTo; +import static org.junit.Assert.assertThat; + +import java.io.File; +import java.io.IOException; + +import okhttp3.Call; +import okhttp3.Credentials; +import okhttp3.FormBody; +import okhttp3.MediaType; +import okhttp3.MultipartBody; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.RequestBody; +import okhttp3.Response; + +import org.junit.Before; +import org.junit.Test; + +public class OkHttpPostingLiveTest { + + private static final String BASE_URL = "http://localhost:" + APPLICATION_PORT + "/spring-rest"; + private static final String URL_SECURED_BY_BASIC_AUTHENTICATION = "http://browserspy.dk/password-ok.php"; + + OkHttpClient client; + + @Before + public void init() { + + client = new OkHttpClient(); + } + + @Test + public void whenSendPostRequest_thenCorrect() throws IOException { + final RequestBody formBody = new FormBody.Builder().add("username", "test").add("password", "test").build(); + + final Request request = new Request.Builder().url(BASE_URL + "/users").post(formBody).build(); + + final Call call = client.newCall(request); + final Response response = call.execute(); + + assertThat(response.code(), equalTo(200)); + } + + @Test + public void whenSendPostRequestWithAuthorization_thenCorrect() throws IOException { + final String postBody = "test post"; + + final Request request = new Request.Builder().url(URL_SECURED_BY_BASIC_AUTHENTICATION).addHeader("Authorization", Credentials.basic("test", "test")).post(RequestBody.create(MediaType.parse("text/x-markdown; charset=utf-8"), "test post")).build(); + + final Call call = client.newCall(request); + final Response response = call.execute(); + + assertThat(response.code(), equalTo(200)); + } + + @Test + public void whenPostJson_thenCorrect() throws IOException { + final String json = "{\"id\":1,\"name\":\"John\"}"; + + final RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), "{\"id\":1,\"name\":\"John\"}"); + final Request request = new Request.Builder().url(BASE_URL + "/users/detail").post(body).build(); + + final Call call = client.newCall(request); + final Response response = call.execute(); + + assertThat(response.code(), equalTo(200)); + } + + @Test + public void whenSendMultipartRequest_thenCorrect() throws IOException { + final RequestBody requestBody = new MultipartBody.Builder().setType(MultipartBody.FORM).addFormDataPart("username", "test").addFormDataPart("password", "test") + .addFormDataPart("file", "file.txt", RequestBody.create(MediaType.parse("application/octet-stream"), new File("src/test/resources/test.txt"))).build(); + + final Request request = new Request.Builder().url(BASE_URL + "/users/multipart").post(requestBody).build(); + + final Call call = client.newCall(request); + final Response response = call.execute(); + + assertThat(response.code(), equalTo(200)); + } +} diff --git a/spring-5/src/test/java/org/baeldung/okhttp/OkHttpRedirectLiveTest.java b/spring-5/src/test/java/org/baeldung/okhttp/OkHttpRedirectLiveTest.java new file mode 100644 index 0000000000..58f84f06a2 --- /dev/null +++ b/spring-5/src/test/java/org/baeldung/okhttp/OkHttpRedirectLiveTest.java @@ -0,0 +1,29 @@ +package org.baeldung.okhttp; + +import static org.hamcrest.Matchers.equalTo; +import static org.junit.Assert.assertThat; + +import java.io.IOException; + +import org.junit.Test; + +import okhttp3.Call; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.Response; + +public class OkHttpRedirectLiveTest { + + @Test + public void whenSetFollowRedirects_thenNotRedirected() throws IOException { + + OkHttpClient client = new OkHttpClient().newBuilder().followRedirects(false).build(); + + Request request = new Request.Builder().url("http://t.co/I5YYd9tddw").build(); + + Call call = client.newCall(request); + Response response = call.execute(); + + assertThat(response.code(), equalTo(301)); + } +} diff --git a/spring-5/src/test/java/org/baeldung/okhttp/ProgressRequestWrapper.java b/spring-5/src/test/java/org/baeldung/okhttp/ProgressRequestWrapper.java new file mode 100644 index 0000000000..fcae69c609 --- /dev/null +++ b/spring-5/src/test/java/org/baeldung/okhttp/ProgressRequestWrapper.java @@ -0,0 +1,73 @@ +package org.baeldung.okhttp; + +import okhttp3.RequestBody; +import okhttp3.MediaType; + +import java.io.IOException; + +import okio.Buffer; +import okio.BufferedSink; +import okio.ForwardingSink; +import okio.Okio; +import okio.Sink; + +public class ProgressRequestWrapper extends RequestBody { + + protected RequestBody delegate; + protected ProgressListener listener; + + protected CountingSink countingSink; + + public ProgressRequestWrapper(RequestBody delegate, ProgressListener listener) { + this.delegate = delegate; + this.listener = listener; + } + + @Override + public MediaType contentType() { + return delegate.contentType(); + } + + @Override + public long contentLength() throws IOException { + return delegate.contentLength(); + } + + @Override + public void writeTo(BufferedSink sink) throws IOException { + + BufferedSink bufferedSink; + + countingSink = new CountingSink(sink); + bufferedSink = Okio.buffer(countingSink); + + delegate.writeTo(bufferedSink); + + bufferedSink.flush(); + } + + protected final class CountingSink extends ForwardingSink { + + private long bytesWritten = 0; + + public CountingSink(Sink delegate) { + super(delegate); + } + + @Override + public void write(Buffer source, long byteCount) throws IOException { + + super.write(source, byteCount); + + bytesWritten += byteCount; + listener.onRequestProgress(bytesWritten, contentLength()); + } + + } + + public interface ProgressListener { + + void onRequestProgress(long bytesWritten, long contentLength); + + } +} diff --git a/spring-5/src/test/java/org/baeldung/uribuilder/SpringUriBuilderTest.java b/spring-5/src/test/java/org/baeldung/uribuilder/SpringUriBuilderTest.java new file mode 100644 index 0000000000..84ae1063d9 --- /dev/null +++ b/spring-5/src/test/java/org/baeldung/uribuilder/SpringUriBuilderTest.java @@ -0,0 +1,49 @@ +package org.baeldung.uribuilder; + +import static org.junit.Assert.assertEquals; + +import java.util.Collections; + +import org.junit.Test; +import org.springframework.web.util.UriComponents; +import org.springframework.web.util.UriComponentsBuilder; + +public class SpringUriBuilderTest { + + @Test + public void constructUri() { + UriComponents uriComponents = UriComponentsBuilder.newInstance().scheme("http").host("www.baeldung.com").path("/junit-5").build(); + + assertEquals("http://www.baeldung.com/junit-5", uriComponents.toUriString()); + } + + @Test + public void constructUriEncoded() { + UriComponents uriComponents = UriComponentsBuilder.newInstance().scheme("http").host("www.baeldung.com").path("/junit 5").build().encode(); + + assertEquals("http://www.baeldung.com/junit%205", uriComponents.toUriString()); + } + + @Test + public void constructUriFromTemplate() { + UriComponents uriComponents = UriComponentsBuilder.newInstance().scheme("http").host("www.baeldung.com").path("/{article-name}").buildAndExpand("junit-5"); + + assertEquals("http://www.baeldung.com/junit-5", uriComponents.toUriString()); + } + + @Test + public void constructUriWithQueryParameter() { + UriComponents uriComponents = UriComponentsBuilder.newInstance().scheme("http").host("www.google.com").path("/").query("q={keyword}").buildAndExpand("baeldung"); + + assertEquals("http://www.google.com/?q=baeldung", uriComponents.toUriString()); + } + + @Test + public void expandWithRegexVar() { + String template = "/myurl/{name:[a-z]{1,5}}/show"; + UriComponents uriComponents = UriComponentsBuilder.fromUriString(template).build(); + uriComponents = uriComponents.expand(Collections.singletonMap("name", "test")); + + assertEquals("/myurl/test/show", uriComponents.getPath()); + } +} diff --git a/spring-5/src/test/java/org/baeldung/web/controller/mediatypes/CustomMediaTypeControllerLiveTest.java b/spring-5/src/test/java/org/baeldung/web/controller/mediatypes/CustomMediaTypeControllerLiveTest.java new file mode 100644 index 0000000000..e8d5ff9f17 --- /dev/null +++ b/spring-5/src/test/java/org/baeldung/web/controller/mediatypes/CustomMediaTypeControllerLiveTest.java @@ -0,0 +1,37 @@ +package org.baeldung.web.controller.mediatypes; + +import com.jayway.restassured.http.ContentType; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.support.AnnotationConfigContextLoader; + +import static com.jayway.restassured.RestAssured.given; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = {TestConfig.class}, loader = AnnotationConfigContextLoader.class) +public class CustomMediaTypeControllerLiveTest { + private static final String URL_PREFIX = "http://localhost:8082/spring-rest"; + + @Test + public void givenServiceEndpoint_whenGetRequestFirstAPIVersion_thenShouldReturn200() { + given() + .accept("application/vnd.baeldung.api.v1+json") + .when() + .get(URL_PREFIX + "/public/api/items/1") + .then() + .contentType(ContentType.JSON).and().statusCode(200); + } + + + @Test + public void givenServiceEndpoint_whenGetRequestSecondAPIVersion_thenShouldReturn200() { + given() + .accept("application/vnd.baeldung.api.v2+json") + .when() + .get(URL_PREFIX + "/public/api/items/2") + .then() + .contentType(ContentType.JSON).and().statusCode(200); + } +} diff --git a/spring-5/src/test/java/org/baeldung/web/controller/mediatypes/CustomMediaTypeControllerTest.java b/spring-5/src/test/java/org/baeldung/web/controller/mediatypes/CustomMediaTypeControllerTest.java new file mode 100644 index 0000000000..a38177f78b --- /dev/null +++ b/spring-5/src/test/java/org/baeldung/web/controller/mediatypes/CustomMediaTypeControllerTest.java @@ -0,0 +1,42 @@ +package org.baeldung.web.controller.mediatypes; + +import org.baeldung.config.WebConfig; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.web.WebAppConfiguration; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; +import org.springframework.web.context.WebApplicationContext; + +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = WebConfig.class) +@WebAppConfiguration +public class CustomMediaTypeControllerTest { + + private MockMvc mockMvc; + + @Autowired + private WebApplicationContext webApplicationContext; + + @Before + public void setUp() { + mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build(); + } + + @Test + public void givenServiceUrl_whenGetWithProperAcceptHeaderFirstAPIVersion_thenReturn200() throws Exception { + mockMvc.perform(get("/public/api/items/1").accept("application/vnd.baeldung.api.v1+json")).andExpect(status().isOk()); + } + + @Test + public void givenServiceUrl_whenGetWithProperAcceptHeaderSecondVersion_thenReturn200() throws Exception { + mockMvc.perform(get("/public/api/items/2").accept("application/vnd.baeldung.api.v2+json")).andExpect(status().isOk()); + } +} \ No newline at end of file diff --git a/spring-5/src/test/java/org/baeldung/web/controller/mediatypes/TestConfig.java b/spring-5/src/test/java/org/baeldung/web/controller/mediatypes/TestConfig.java new file mode 100644 index 0000000000..66ffe4947d --- /dev/null +++ b/spring-5/src/test/java/org/baeldung/web/controller/mediatypes/TestConfig.java @@ -0,0 +1,11 @@ +package org.baeldung.web.controller.mediatypes; + +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; + + +@Configuration +@ComponentScan({ "org.baeldung.web" }) +public class TestConfig { + +} diff --git a/spring-5/src/test/java/org/baeldung/web/controller/redirect/RedirectControllerIntegrationTest.java b/spring-5/src/test/java/org/baeldung/web/controller/redirect/RedirectControllerIntegrationTest.java new file mode 100644 index 0000000000..c604db596a --- /dev/null +++ b/spring-5/src/test/java/org/baeldung/web/controller/redirect/RedirectControllerIntegrationTest.java @@ -0,0 +1,66 @@ +package org.baeldung.web.controller.redirect; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.flash; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.forwardedUrl; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.redirectedUrl; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view; +import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.web.WebAppConfiguration; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.web.context.WebApplicationContext; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration("file:src/main/webapp/WEB-INF/api-servlet.xml") +@WebAppConfiguration +public class RedirectControllerIntegrationTest { + + private MockMvc mockMvc; + + @Autowired + protected WebApplicationContext wac; + + @Before + public void setup() { + mockMvc = webAppContextSetup(wac).build(); + } + + @Test + public void whenRedirectOnUrlWithUsingXMLConfig_thenStatusRedirectionAndRedirectedOnUrl() throws Exception { + mockMvc.perform(get("/redirectWithXMLConfig")).andExpect(status().is3xxRedirection()).andExpect(view().name("RedirectedUrl")).andExpect(model().attribute("attribute", equalTo("redirectWithXMLConfig"))) + .andExpect(redirectedUrl("redirectedUrl?attribute=redirectWithXMLConfig")); + } + + @Test + public void whenRedirectOnUrlWithUsingRedirectPrefix_thenStatusRedirectionAndRedirectedOnUrl() throws Exception { + mockMvc.perform(get("/redirectWithRedirectPrefix")).andExpect(status().is3xxRedirection()).andExpect(view().name("redirect:/redirectedUrl")).andExpect(model().attribute("attribute", equalTo("redirectWithRedirectPrefix"))) + .andExpect(redirectedUrl("/redirectedUrl?attribute=redirectWithRedirectPrefix")); + } + + @Test + public void whenRedirectOnUrlWithUsingRedirectAttributes_thenStatusRedirectionAndRedirectedOnUrlAndAddedAttributeToFlashScope() throws Exception { + mockMvc.perform(get("/redirectWithRedirectAttributes")).andExpect(status().is3xxRedirection()).andExpect(flash().attribute("flashAttribute", equalTo("redirectWithRedirectAttributes"))) + .andExpect(model().attribute("attribute", equalTo("redirectWithRedirectAttributes"))).andExpect(model().attribute("flashAttribute", equalTo(null))).andExpect(redirectedUrl("redirectedUrl?attribute=redirectWithRedirectAttributes")); + } + + @Test + public void whenRedirectOnUrlWithUsingRedirectView_thenStatusRedirectionAndRedirectedOnUrlAndAddedAttributeToFlashScope() throws Exception { + mockMvc.perform(get("/redirectWithRedirectView")).andExpect(status().is3xxRedirection()).andExpect(model().attribute("attribute", equalTo("redirectWithRedirectView"))).andExpect(redirectedUrl("redirectedUrl?attribute=redirectWithRedirectView")); + } + + @Test + public void whenRedirectOnUrlWithUsingForwardPrefix_thenStatusOkAndForwardedOnUrl() throws Exception { + mockMvc.perform(get("/forwardWithForwardPrefix")).andExpect(status().isOk()).andExpect(view().name("forward:/redirectedUrl")).andExpect(model().attribute("attribute", equalTo("redirectWithForwardPrefix"))).andExpect(forwardedUrl("/redirectedUrl")); + } + +} diff --git a/spring-5/src/test/java/org/baeldung/web/controller/status/ExampleControllerIntegrationTest.java b/spring-5/src/test/java/org/baeldung/web/controller/status/ExampleControllerIntegrationTest.java new file mode 100644 index 0000000000..e29bef501e --- /dev/null +++ b/spring-5/src/test/java/org/baeldung/web/controller/status/ExampleControllerIntegrationTest.java @@ -0,0 +1,42 @@ +package org.baeldung.web.controller.status; + +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +import org.baeldung.config.WebConfig; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.web.WebAppConfiguration; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; +import org.springframework.web.context.WebApplicationContext; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = WebConfig.class) +@WebAppConfiguration +public class ExampleControllerIntegrationTest { + + private MockMvc mockMvc; + + @Autowired + private WebApplicationContext webApplicationContext; + + @Before + public void setUp() { + mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build(); + } + + @Test + public void whenGetRequestSentToController_thenReturnsStatusNotAcceptable() throws Exception { + mockMvc.perform(get("/controller")).andExpect(status().isNotAcceptable()); + } + + @Test + public void whenGetRequestSentToException_thenReturnsStatusForbidden() throws Exception { + mockMvc.perform(get("/exception")).andExpect(status().isForbidden()); + } +} diff --git a/spring-5/src/test/java/org/baeldung/web/test/RequestMappingLiveTest.java b/spring-5/src/test/java/org/baeldung/web/test/RequestMappingLiveTest.java new file mode 100644 index 0000000000..7828df7304 --- /dev/null +++ b/spring-5/src/test/java/org/baeldung/web/test/RequestMappingLiveTest.java @@ -0,0 +1,63 @@ +package org.baeldung.web.test; + +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.equalTo; + +import org.junit.Test; + +import com.jayway.restassured.RestAssured; + +public class RequestMappingLiveTest { + private static String BASE_URI = "http://localhost:8082/spring-rest/ex/"; + + @Test + public void givenSimplePath_whenGetFoos_thenOk() { + RestAssured.given().accept("text/html").get(BASE_URI + "foos").then().assertThat().body(equalTo("Simple Get some Foos")); + } + + @Test + public void whenPostFoos_thenOk() { + RestAssured.given().accept("text/html").post(BASE_URI + "foos").then().assertThat().body(equalTo("Post some Foos")); + } + + @Test + public void givenOneHeader_whenGetFoos_thenOk() { + RestAssured.given().accept("text/html").header("key", "val").get(BASE_URI + "foos").then().assertThat().body(equalTo("Get some Foos with Header")); + } + + @Test + public void givenMultipleHeaders_whenGetFoos_thenOk() { + RestAssured.given().accept("text/html").headers("key1", "val1", "key2", "val2").get(BASE_URI + "foos").then().assertThat().body(equalTo("Get some Foos with Header")); + } + + @Test + public void givenAcceptHeader_whenGetFoos_thenOk() { + RestAssured.given().accept("application/json").get(BASE_URI + "foos").then().assertThat().body(containsString("Get some Foos with Header New")); + } + + @Test + public void givenPathVariable_whenGetFoos_thenOk() { + RestAssured.given().accept("text/html").get(BASE_URI + "foos/1").then().assertThat().body(equalTo("Get a specific Foo with id=1")); + } + + @Test + public void givenMultiplePathVariable_whenGetFoos_thenOk() { + RestAssured.given().accept("text/html").get(BASE_URI + "foos/1/bar/2").then().assertThat().body(equalTo("Get a specific Bar with id=2 from a Foo with id=1")); + } + + @Test + public void givenPathVariable_whenGetBars_thenOk() { + RestAssured.given().accept("text/html").get(BASE_URI + "bars/1").then().assertThat().body(equalTo("Get a specific Bar with id=1")); + } + + @Test + public void givenParams_whenGetBars_thenOk() { + RestAssured.given().accept("text/html").get(BASE_URI + "bars?id=100&second=something").then().assertThat().body(equalTo("Get a specific Bar with id=100")); + } + + @Test + public void whenGetFoosOrBars_thenOk() { + RestAssured.given().accept("text/html").get(BASE_URI + "advanced/foos").then().assertThat().body(equalTo("Advanced - Get some Foos or Bars")); + RestAssured.given().accept("text/html").get(BASE_URI + "advanced/bars").then().assertThat().body(equalTo("Advanced - Get some Foos or Bars")); + } +} diff --git a/spring-5/src/test/java/org/baeldung/web/test/SpringHttpMessageConvertersLiveTest.java b/spring-5/src/test/java/org/baeldung/web/test/SpringHttpMessageConvertersLiveTest.java new file mode 100644 index 0000000000..7f250653ab --- /dev/null +++ b/spring-5/src/test/java/org/baeldung/web/test/SpringHttpMessageConvertersLiveTest.java @@ -0,0 +1,123 @@ +package org.baeldung.web.test; + +import static org.hamcrest.Matchers.notNullValue; +import static org.junit.Assert.assertThat; + +import java.util.Arrays; + +import org.baeldung.config.converter.KryoHttpMessageConverter; +import org.baeldung.web.dto.Foo; +import org.baeldung.web.dto.FooProtos; +import org.junit.Assert; +import org.junit.Test; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.http.converter.protobuf.ProtobufHttpMessageConverter; +import org.springframework.web.client.RestTemplate; + +/** + * Integration Test class. Tests methods hits the server's rest services. + */ +public class SpringHttpMessageConvertersLiveTest { + + private static String BASE_URI = "http://localhost:8082/spring-rest/"; + + /** + * Without specifying Accept Header, uses the default response from the + * server (in this case json) + */ + @Test + public void whenRetrievingAFoo_thenCorrect() { + final String URI = BASE_URI + "foos/{id}"; + + final RestTemplate restTemplate = new RestTemplate(); + final Foo resource = restTemplate.getForObject(URI, Foo.class, "1"); + + assertThat(resource, notNullValue()); + } + + @Test + public void givenConsumingXml_whenReadingTheFoo_thenCorrect() { + final String URI = BASE_URI + "foos/{id}"; + + final RestTemplate restTemplate = new RestTemplate(); + + final HttpHeaders headers = new HttpHeaders(); + headers.setAccept(Arrays.asList(MediaType.APPLICATION_XML)); + final HttpEntity entity = new HttpEntity(headers); + + final ResponseEntity response = restTemplate.exchange(URI, HttpMethod.GET, entity, Foo.class, "1"); + final Foo resource = response.getBody(); + + assertThat(resource, notNullValue()); + } + + @Test + public void givenConsumingJson_whenReadingTheFoo_thenCorrect() { + final String URI = BASE_URI + "foos/{id}"; + + final RestTemplate restTemplate = new RestTemplate(); + + final HttpHeaders headers = new HttpHeaders(); + headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON)); + final HttpEntity entity = new HttpEntity(headers); + + final ResponseEntity response = restTemplate.exchange(URI, HttpMethod.GET, entity, Foo.class, "1"); + final Foo resource = response.getBody(); + + assertThat(resource, notNullValue()); + } + + @Test + public void givenConsumingXml_whenWritingTheFoo_thenCorrect() { + final String URI = BASE_URI + "foos/{id}"; + final RestTemplate restTemplate = new RestTemplate(); + + final Foo resource = new Foo(4, "jason"); + final HttpHeaders headers = new HttpHeaders(); + headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON)); + headers.setContentType((MediaType.APPLICATION_XML)); + final HttpEntity entity = new HttpEntity(resource, headers); + + final ResponseEntity response = restTemplate.exchange(URI, HttpMethod.PUT, entity, Foo.class, resource.getId()); + final Foo fooResponse = response.getBody(); + + Assert.assertEquals(resource.getId(), fooResponse.getId()); + } + + @Test + public void givenConsumingProtobuf_whenReadingTheFoo_thenCorrect() { + final String URI = BASE_URI + "foos/{id}"; + + final RestTemplate restTemplate = new RestTemplate(); + restTemplate.setMessageConverters(Arrays.asList(new ProtobufHttpMessageConverter())); + final HttpHeaders headers = new HttpHeaders(); + headers.setAccept(Arrays.asList(ProtobufHttpMessageConverter.PROTOBUF)); + final HttpEntity entity = new HttpEntity(headers); + + final ResponseEntity response = restTemplate.exchange(URI, HttpMethod.GET, entity, FooProtos.Foo.class, "1"); + final FooProtos.Foo resource = response.getBody(); + + assertThat(resource, notNullValue()); + } + + @Test + public void givenConsumingKryo_whenReadingTheFoo_thenCorrect() { + final String URI = BASE_URI + "foos/{id}"; + + final RestTemplate restTemplate = new RestTemplate(); + restTemplate.setMessageConverters(Arrays.asList(new KryoHttpMessageConverter())); + final HttpHeaders headers = new HttpHeaders(); + headers.setAccept(Arrays.asList(KryoHttpMessageConverter.KRYO)); + final HttpEntity entity = new HttpEntity(headers); + + final ResponseEntity response = restTemplate.exchange(URI, HttpMethod.GET, entity, Foo.class, "1"); + final Foo resource = response.getBody(); + + assertThat(resource, notNullValue()); + } + +} diff --git a/spring-5/src/test/resources/.gitignore b/spring-5/src/test/resources/.gitignore new file mode 100644 index 0000000000..83c05e60c8 --- /dev/null +++ b/spring-5/src/test/resources/.gitignore @@ -0,0 +1,13 @@ +*.class + +#folders# +/target +/neoDb* +/data +/src/main/webapp/WEB-INF/classes +*/META-INF/* + +# Packaged files # +*.jar +*.war +*.ear \ No newline at end of file diff --git a/spring-5/src/test/resources/test.txt b/spring-5/src/test/resources/test.txt new file mode 100644 index 0000000000..95d09f2b10 --- /dev/null +++ b/spring-5/src/test/resources/test.txt @@ -0,0 +1 @@ +hello world \ No newline at end of file From 07876a2855fc353070b835e6fac5654240d9ed72 Mon Sep 17 00:00:00 2001 From: eugenp Date: Fri, 24 Feb 2017 13:57:32 +0200 Subject: [PATCH 43/68] work on initial state of the spring 5 project --- spring-5/pom.xml | 431 +++---------- .../java/com/baeldung/Spring5Application.java | 12 + .../java/org/baeldung/config/Application.java | 16 - .../java/org/baeldung/config/WebConfig.java | 57 -- .../converter/KryoHttpMessageConverter.java | 57 -- .../BarMappingExamplesController.java | 47 -- .../web/controller/CompanyController.java | 16 - .../web/controller/FooController.java | 45 -- .../FooMappingExamplesController.java | 101 --- .../web/controller/ItemController.java | 39 -- .../web/controller/MyFooController.java | 76 --- .../web/controller/SimplePostController.java | 73 --- .../advice/JsonpControllerAdvice.java | 13 - .../mediatypes/CustomMediaTypeController.java | 20 - .../redirect/RedirectController.java | 52 -- .../controller/status/ExampleController.java | 24 - .../controller/status/ForbiddenException.java | 10 - .../org/baeldung/web/dto/BaeldungItem.java | 13 - .../org/baeldung/web/dto/BaeldungItemV2.java | 14 - .../java/org/baeldung/web/dto/Company.java | 38 -- .../main/java/org/baeldung/web/dto/Foo.java | 45 -- .../java/org/baeldung/web/dto/FooProtos.java | 597 ------------------ .../main/java/org/baeldung/web/dto/Item.java | 36 -- .../org/baeldung/web/dto/ItemManager.java | 9 - .../main/java/org/baeldung/web/dto/Views.java | 9 - .../exception/ResourceNotFoundException.java | 8 - .../src/main/resources/application.properties | 2 - spring-5/src/main/resources/logback.xml | 20 - .../src/main/webapp/WEB-INF/api-servlet.xml | 56 -- spring-5/src/main/webapp/WEB-INF/company.html | 44 -- .../src/main/webapp/WEB-INF/spring-views.xml | 10 - spring-5/src/main/webapp/WEB-INF/web.xml | 41 -- .../com/baeldung/Spring5ApplicationTests.java | 16 + .../test/java/org/baeldung/client/Consts.java | 5 - .../client/RestTemplateBasicLiveTest.java | 216 ------- .../okhttp/DefaultContentTypeInterceptor.java | 24 - .../okhttp/OkHttpFileUploadingLiveTest.java | 65 -- .../baeldung/okhttp/OkHttpGetLiveTest.java | 77 --- .../baeldung/okhttp/OkHttpHeaderLiveTest.java | 45 -- .../baeldung/okhttp/OkHttpMiscLiveTest.java | 99 --- .../okhttp/OkHttpPostingLiveTest.java | 85 --- .../okhttp/OkHttpRedirectLiveTest.java | 29 - .../okhttp/ProgressRequestWrapper.java | 73 --- .../uribuilder/SpringUriBuilderTest.java | 49 -- .../CustomMediaTypeControllerLiveTest.java | 37 -- .../CustomMediaTypeControllerTest.java | 42 -- .../web/controller/mediatypes/TestConfig.java | 11 - .../RedirectControllerIntegrationTest.java | 66 -- .../ExampleControllerIntegrationTest.java | 42 -- .../web/test/RequestMappingLiveTest.java | 63 -- .../SpringHttpMessageConvertersLiveTest.java | 123 ---- spring-5/src/test/resources/.gitignore | 13 - spring-5/src/test/resources/test.txt | 1 - 53 files changed, 125 insertions(+), 3087 deletions(-) create mode 100644 spring-5/src/main/java/com/baeldung/Spring5Application.java delete mode 100644 spring-5/src/main/java/org/baeldung/config/Application.java delete mode 100644 spring-5/src/main/java/org/baeldung/config/WebConfig.java delete mode 100644 spring-5/src/main/java/org/baeldung/config/converter/KryoHttpMessageConverter.java delete mode 100644 spring-5/src/main/java/org/baeldung/web/controller/BarMappingExamplesController.java delete mode 100644 spring-5/src/main/java/org/baeldung/web/controller/CompanyController.java delete mode 100644 spring-5/src/main/java/org/baeldung/web/controller/FooController.java delete mode 100644 spring-5/src/main/java/org/baeldung/web/controller/FooMappingExamplesController.java delete mode 100644 spring-5/src/main/java/org/baeldung/web/controller/ItemController.java delete mode 100644 spring-5/src/main/java/org/baeldung/web/controller/MyFooController.java delete mode 100644 spring-5/src/main/java/org/baeldung/web/controller/SimplePostController.java delete mode 100644 spring-5/src/main/java/org/baeldung/web/controller/advice/JsonpControllerAdvice.java delete mode 100644 spring-5/src/main/java/org/baeldung/web/controller/mediatypes/CustomMediaTypeController.java delete mode 100644 spring-5/src/main/java/org/baeldung/web/controller/redirect/RedirectController.java delete mode 100644 spring-5/src/main/java/org/baeldung/web/controller/status/ExampleController.java delete mode 100644 spring-5/src/main/java/org/baeldung/web/controller/status/ForbiddenException.java delete mode 100644 spring-5/src/main/java/org/baeldung/web/dto/BaeldungItem.java delete mode 100644 spring-5/src/main/java/org/baeldung/web/dto/BaeldungItemV2.java delete mode 100644 spring-5/src/main/java/org/baeldung/web/dto/Company.java delete mode 100644 spring-5/src/main/java/org/baeldung/web/dto/Foo.java delete mode 100644 spring-5/src/main/java/org/baeldung/web/dto/FooProtos.java delete mode 100644 spring-5/src/main/java/org/baeldung/web/dto/Item.java delete mode 100644 spring-5/src/main/java/org/baeldung/web/dto/ItemManager.java delete mode 100644 spring-5/src/main/java/org/baeldung/web/dto/Views.java delete mode 100644 spring-5/src/main/java/org/baeldung/web/exception/ResourceNotFoundException.java delete mode 100644 spring-5/src/main/resources/logback.xml delete mode 100644 spring-5/src/main/webapp/WEB-INF/api-servlet.xml delete mode 100644 spring-5/src/main/webapp/WEB-INF/company.html delete mode 100644 spring-5/src/main/webapp/WEB-INF/spring-views.xml delete mode 100644 spring-5/src/main/webapp/WEB-INF/web.xml create mode 100644 spring-5/src/test/java/com/baeldung/Spring5ApplicationTests.java delete mode 100644 spring-5/src/test/java/org/baeldung/client/Consts.java delete mode 100644 spring-5/src/test/java/org/baeldung/client/RestTemplateBasicLiveTest.java delete mode 100644 spring-5/src/test/java/org/baeldung/okhttp/DefaultContentTypeInterceptor.java delete mode 100644 spring-5/src/test/java/org/baeldung/okhttp/OkHttpFileUploadingLiveTest.java delete mode 100644 spring-5/src/test/java/org/baeldung/okhttp/OkHttpGetLiveTest.java delete mode 100644 spring-5/src/test/java/org/baeldung/okhttp/OkHttpHeaderLiveTest.java delete mode 100644 spring-5/src/test/java/org/baeldung/okhttp/OkHttpMiscLiveTest.java delete mode 100644 spring-5/src/test/java/org/baeldung/okhttp/OkHttpPostingLiveTest.java delete mode 100644 spring-5/src/test/java/org/baeldung/okhttp/OkHttpRedirectLiveTest.java delete mode 100644 spring-5/src/test/java/org/baeldung/okhttp/ProgressRequestWrapper.java delete mode 100644 spring-5/src/test/java/org/baeldung/uribuilder/SpringUriBuilderTest.java delete mode 100644 spring-5/src/test/java/org/baeldung/web/controller/mediatypes/CustomMediaTypeControllerLiveTest.java delete mode 100644 spring-5/src/test/java/org/baeldung/web/controller/mediatypes/CustomMediaTypeControllerTest.java delete mode 100644 spring-5/src/test/java/org/baeldung/web/controller/mediatypes/TestConfig.java delete mode 100644 spring-5/src/test/java/org/baeldung/web/controller/redirect/RedirectControllerIntegrationTest.java delete mode 100644 spring-5/src/test/java/org/baeldung/web/controller/status/ExampleControllerIntegrationTest.java delete mode 100644 spring-5/src/test/java/org/baeldung/web/test/RequestMappingLiveTest.java delete mode 100644 spring-5/src/test/java/org/baeldung/web/test/SpringHttpMessageConvertersLiveTest.java delete mode 100644 spring-5/src/test/resources/.gitignore delete mode 100644 spring-5/src/test/resources/test.txt diff --git a/spring-5/pom.xml b/spring-5/pom.xml index c2545d920f..3b1d6c712b 100644 --- a/spring-5/pom.xml +++ b/spring-5/pom.xml @@ -1,345 +1,108 @@ + - 4.0.0 - com.baeldung - spring-5 - 0.1-SNAPSHOT - spring-5 - war + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + 4.0.0 - - org.springframework.boot - spring-boot-starter-parent - - 2.0.0.BUILD-SNAPSHOT - + com.baeldung + spring-5 + 0.0.1-SNAPSHOT + jar - + spring-5 + - + + org.springframework.boot + spring-boot-starter-parent + 2.0.0.BUILD-SNAPSHOT + + - - org.springframework.boot - spring-boot-starter-thymeleaf - - - org.springframework.boot - spring-boot-starter-actuator - - - org.springframework.boot - spring-boot-devtools - + + UTF-8 + UTF-8 + 1.8 + - + + + org.springframework.boot + spring-boot-starter-data-jpa + + + org.springframework.boot + spring-boot-starter-security + + + org.springframework.boot + spring-boot-starter-validation + + + org.springframework.boot + spring-boot-starter-web + - - org.springframework - spring-web - - - commons-logging - commons-logging - - - - - org.springframework - spring-webmvc - - - org.springframework - spring-oxm - + + org.springframework.boot + spring-boot-devtools + runtime + + + com.h2database + h2 + runtime + + + org.springframework.boot + spring-boot-starter-test + test + + - - commons-fileupload - commons-fileupload - ${commons-fileupload.version} - - + + + + org.springframework.boot + spring-boot-maven-plugin + + + - - javax.servlet - javax.servlet-api - provided - - - - javax.servlet - jstl - runtime - - - - - - com.fasterxml.jackson.core - jackson-databind - - - - com.fasterxml.jackson.dataformat - jackson-dataformat-xml - - - - com.thoughtworks.xstream - xstream - ${xstream.version} - - - - - - com.google.guava - guava - ${guava.version} - - - - org.apache.commons - commons-lang3 - ${commons-lang3.version} - - - - - - org.slf4j - slf4j-api - - - ch.qos.logback - logback-classic - - - - org.slf4j - jcl-over-slf4j - - - - org.slf4j - log4j-over-slf4j - - - - - - com.squareup.okhttp3 - okhttp - ${com.squareup.okhttp3.version} - - - - - - junit - junit - test - - - - org.hamcrest - hamcrest-core - test - - - org.hamcrest - hamcrest-library - test - - - - org.mockito - mockito-core - test - - - - org.springframework - spring-test - - - - com.jayway.restassured - rest-assured - ${rest-assured.version} - - - - - com.google.protobuf - protobuf-java - ${protobuf-java.version} - - - com.googlecode.protobuf-java-format - protobuf-java-format - ${protobuf-java-format.version} - - - - com.esotericsoftware - kryo - ${kryo.version} - - - - - spring-5 - - - src/main/resources - true - - - - - - - org.apache.maven.plugins - maven-compiler-plugin - - 1.8 - 1.8 - - - - - org.apache.maven.plugins - maven-war-plugin - - - - org.apache.maven.plugins - maven-surefire-plugin - - - **/*IntegrationTest.java - **/*LiveTest.java - - - - - - - - - org.codehaus.cargo - cargo-maven2-plugin - ${cargo-maven2-plugin.version} - - true - - tomcat8x - embedded - - - - - - - 8082 - - - - - - - - - - - - - integration - - - - org.apache.maven.plugins - maven-surefire-plugin - - - integration-test - - test - - - - none - - - **/*IntegrationTest.java - - - - - - - - - - - - - - 1.3.2 - 4.0.0 - 1.4 - 3.1.0 - 3.5 - 1.4.9 - - - 20.0 - 2.9.0 - - - 1.6.0 - 3.0.4 - - - 3.4.1 - - - - - - spring-snapshots - Spring Snapshots - https://repo.spring.io/snapshot - - true - - - - spring-milestones - Spring Milestones - https://repo.spring.io/milestone - - false - - - - - - spring-snapshots - Spring Snapshots - https://repo.spring.io/snapshot - - true - - - - spring-milestones - Spring Milestones - https://repo.spring.io/milestone - - false - - - + + + spring-snapshots + Spring Snapshots + https://repo.spring.io/snapshot + + true + + + + spring-milestones + Spring Milestones + https://repo.spring.io/milestone + + false + + + + + + spring-snapshots + Spring Snapshots + https://repo.spring.io/snapshot + + true + + + + spring-milestones + Spring Milestones + https://repo.spring.io/milestone + + false + + + diff --git a/spring-5/src/main/java/com/baeldung/Spring5Application.java b/spring-5/src/main/java/com/baeldung/Spring5Application.java new file mode 100644 index 0000000000..902af95afd --- /dev/null +++ b/spring-5/src/main/java/com/baeldung/Spring5Application.java @@ -0,0 +1,12 @@ +package com.baeldung; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class Spring5Application { + + public static void main(String[] args) { + SpringApplication.run(Spring5Application.class, args); + } +} diff --git a/spring-5/src/main/java/org/baeldung/config/Application.java b/spring-5/src/main/java/org/baeldung/config/Application.java deleted file mode 100644 index 077213b04d..0000000000 --- a/spring-5/src/main/java/org/baeldung/config/Application.java +++ /dev/null @@ -1,16 +0,0 @@ -package org.baeldung.config; - -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.EnableAutoConfiguration; -import org.springframework.context.annotation.ComponentScan; -import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; - -@EnableAutoConfiguration -@ComponentScan("org.baeldung") -public class Application extends WebMvcConfigurerAdapter { - - public static void main(final String[] args) { - SpringApplication.run(Application.class, args); - } - -} \ No newline at end of file diff --git a/spring-5/src/main/java/org/baeldung/config/WebConfig.java b/spring-5/src/main/java/org/baeldung/config/WebConfig.java deleted file mode 100644 index f40c9477d4..0000000000 --- a/spring-5/src/main/java/org/baeldung/config/WebConfig.java +++ /dev/null @@ -1,57 +0,0 @@ -package org.baeldung.config; - -import org.baeldung.config.converter.KryoHttpMessageConverter; -import org.springframework.context.annotation.ComponentScan; -import org.springframework.context.annotation.Configuration; -import org.springframework.http.converter.HttpMessageConverter; -import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder; -import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; -import org.springframework.http.converter.protobuf.ProtobufHttpMessageConverter; -import org.springframework.http.converter.xml.MarshallingHttpMessageConverter; -import org.springframework.oxm.xstream.XStreamMarshaller; -import org.springframework.web.servlet.config.annotation.EnableWebMvc; -import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; - -import java.text.SimpleDateFormat; -import java.util.List; - -/* - * Please note that main web configuration is in src/main/webapp/WEB-INF/api-servlet.xml - */ -@Configuration -@EnableWebMvc -@ComponentScan({ "org.baeldung.web" }) -public class WebConfig extends WebMvcConfigurerAdapter { - - public WebConfig() { - super(); - } - - // - - @Override - public void configureMessageConverters(final List> messageConverters) { - final Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder(); - builder.indentOutput(true).dateFormat(new SimpleDateFormat("dd-MM-yyyy hh:mm")); - messageConverters.add(new MappingJackson2HttpMessageConverter(builder.build())); - // messageConverters.add(new MappingJackson2XmlHttpMessageConverter(builder.createXmlMapper(true).build())); - - // messageConverters.add(createXmlHttpMessageConverter()); - // messageConverters.add(new MappingJackson2HttpMessageConverter()); - - messageConverters.add(new ProtobufHttpMessageConverter()); - messageConverters.add(new KryoHttpMessageConverter()); - super.configureMessageConverters(messageConverters); - } - - private HttpMessageConverter createXmlHttpMessageConverter() { - final MarshallingHttpMessageConverter xmlConverter = new MarshallingHttpMessageConverter(); - - final XStreamMarshaller xstreamMarshaller = new XStreamMarshaller(); - xmlConverter.setMarshaller(xstreamMarshaller); - xmlConverter.setUnmarshaller(xstreamMarshaller); - - return xmlConverter; - } - -} diff --git a/spring-5/src/main/java/org/baeldung/config/converter/KryoHttpMessageConverter.java b/spring-5/src/main/java/org/baeldung/config/converter/KryoHttpMessageConverter.java deleted file mode 100644 index 7e63a3ba9e..0000000000 --- a/spring-5/src/main/java/org/baeldung/config/converter/KryoHttpMessageConverter.java +++ /dev/null @@ -1,57 +0,0 @@ -package org.baeldung.config.converter; - -import java.io.IOException; - -import org.baeldung.web.dto.Foo; -import org.springframework.http.HttpInputMessage; -import org.springframework.http.HttpOutputMessage; -import org.springframework.http.MediaType; -import org.springframework.http.converter.AbstractHttpMessageConverter; - -import com.esotericsoftware.kryo.Kryo; -import com.esotericsoftware.kryo.io.Input; -import com.esotericsoftware.kryo.io.Output; - -/** - * An {@code HttpMessageConverter} that can read and write Kryo messages. - */ -public class KryoHttpMessageConverter extends AbstractHttpMessageConverter { - - public static final MediaType KRYO = new MediaType("application", "x-kryo"); - - private static final ThreadLocal kryoThreadLocal = new ThreadLocal() { - @Override - protected Kryo initialValue() { - final Kryo kryo = new Kryo(); - kryo.register(Foo.class, 1); - return kryo; - } - }; - - public KryoHttpMessageConverter() { - super(KRYO); - } - - @Override - protected boolean supports(final Class clazz) { - return Object.class.isAssignableFrom(clazz); - } - - @Override - protected Object readInternal(final Class clazz, final HttpInputMessage inputMessage) throws IOException { - final Input input = new Input(inputMessage.getBody()); - return kryoThreadLocal.get().readClassAndObject(input); - } - - @Override - protected void writeInternal(final Object object, final HttpOutputMessage outputMessage) throws IOException { - final Output output = new Output(outputMessage.getBody()); - kryoThreadLocal.get().writeClassAndObject(output, object); - output.flush(); - } - - @Override - protected MediaType getDefaultContentType(final Object object) { - return KRYO; - } -} diff --git a/spring-5/src/main/java/org/baeldung/web/controller/BarMappingExamplesController.java b/spring-5/src/main/java/org/baeldung/web/controller/BarMappingExamplesController.java deleted file mode 100644 index 1c3a1086ca..0000000000 --- a/spring-5/src/main/java/org/baeldung/web/controller/BarMappingExamplesController.java +++ /dev/null @@ -1,47 +0,0 @@ -package org.baeldung.web.controller; - -import org.springframework.stereotype.Controller; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.bind.annotation.ResponseBody; - -@Controller -@RequestMapping(value = "/ex") -public class BarMappingExamplesController { - - public BarMappingExamplesController() { - super(); - } - - // API - - // with @RequestParam - - @RequestMapping(value = "/bars") - @ResponseBody - public String getBarBySimplePathWithRequestParam(@RequestParam("id") final long id) { - return "Get a specific Bar with id=" + id; - } - - @RequestMapping(value = "/bars", params = "id") - @ResponseBody - public String getBarBySimplePathWithExplicitRequestParam(@RequestParam("id") final long id) { - return "Get a specific Bar with id=" + id; - } - - @RequestMapping(value = "/bars", params = { "id", "second" }) - @ResponseBody - public String getBarBySimplePathWithExplicitRequestParams(@RequestParam("id") final long id) { - return "Get a specific Bar with id=" + id; - } - - // with @PathVariable - - @RequestMapping(value = "/bars/{numericId:[\\d]+}") - @ResponseBody - public String getBarsBySimplePathWithPathVariable(@PathVariable final long numericId) { - return "Get a specific Bar with id=" + numericId; - } - -} diff --git a/spring-5/src/main/java/org/baeldung/web/controller/CompanyController.java b/spring-5/src/main/java/org/baeldung/web/controller/CompanyController.java deleted file mode 100644 index aa694c08ed..0000000000 --- a/spring-5/src/main/java/org/baeldung/web/controller/CompanyController.java +++ /dev/null @@ -1,16 +0,0 @@ -package org.baeldung.web.controller; - -import org.baeldung.web.dto.Company; -import org.springframework.http.MediaType; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; - -@RestController -public class CompanyController { - - @RequestMapping(value = "/companyRest", produces = MediaType.APPLICATION_JSON_VALUE) - public Company getCompanyRest() { - final Company company = new Company(1, "Xpto"); - return company; - } -} diff --git a/spring-5/src/main/java/org/baeldung/web/controller/FooController.java b/spring-5/src/main/java/org/baeldung/web/controller/FooController.java deleted file mode 100644 index 21ba3c6d13..0000000000 --- a/spring-5/src/main/java/org/baeldung/web/controller/FooController.java +++ /dev/null @@ -1,45 +0,0 @@ -package org.baeldung.web.controller; - -import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic; - -import org.baeldung.web.dto.Foo; -import org.baeldung.web.dto.FooProtos; -import org.springframework.http.HttpStatus; -import org.springframework.stereotype.Controller; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; -import org.springframework.web.bind.annotation.ResponseBody; -import org.springframework.web.bind.annotation.ResponseStatus; - -@Controller -public class FooController { - - public FooController() { - super(); - } - - // API - read - - @RequestMapping(method = RequestMethod.GET, value = "/foos/{id}") - @ResponseBody - public Foo findById(@PathVariable final long id) { - return new Foo(id, randomAlphabetic(4)); - } - - // API - write - - @RequestMapping(method = RequestMethod.PUT, value = "/foos/{id}") - @ResponseStatus(HttpStatus.OK) - @ResponseBody - public Foo updateFoo(@PathVariable("id") final String id, @RequestBody final Foo foo) { - return foo; - } - - @RequestMapping(method = RequestMethod.GET, value = "/foos/{id}", produces = { "application/x-protobuf" }) - @ResponseBody - public FooProtos.Foo findProtoById(@PathVariable final long id) { - return FooProtos.Foo.newBuilder().setId(1).setName("Foo Name").build(); - } -} diff --git a/spring-5/src/main/java/org/baeldung/web/controller/FooMappingExamplesController.java b/spring-5/src/main/java/org/baeldung/web/controller/FooMappingExamplesController.java deleted file mode 100644 index 5fb92d6d87..0000000000 --- a/spring-5/src/main/java/org/baeldung/web/controller/FooMappingExamplesController.java +++ /dev/null @@ -1,101 +0,0 @@ -package org.baeldung.web.controller; - -import org.springframework.stereotype.Controller; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; -import org.springframework.web.bind.annotation.ResponseBody; - -@Controller -@RequestMapping(value = "/ex") -public class FooMappingExamplesController { - - public FooMappingExamplesController() { - super(); - } - - // API - - // mapping examples - - @RequestMapping(value = "/foos") - @ResponseBody - public String getFoosBySimplePath() { - return "Simple Get some Foos"; - } - - // with @PathVariable - - @RequestMapping(value = "/foos/{id}") - @ResponseBody - public String getFoosBySimplePathWithPathVariable(@PathVariable final long id) { - return "Get a specific Foo with id=" + id; - } - - @RequestMapping(value = "/foos/{fooid}/bar/{barid}") - @ResponseBody - public String getFoosBySimplePathWithPathVariables(@PathVariable final long fooid, @PathVariable final long barid) { - return "Get a specific Bar with id=" + barid + " from a Foo with id=" + fooid; - } - - // other HTTP verbs - - @RequestMapping(value = "/foos", method = RequestMethod.POST) - @ResponseBody - public String postFoos() { - return "Post some Foos"; - } - - // with headers - - @RequestMapping(value = "/foos", headers = "key=val") - @ResponseBody - public String getFoosWithHeader() { - return "Get some Foos with Header"; - } - - @RequestMapping(value = "/foos", headers = { "key1=val1", "key2=val2" }) - @ResponseBody - public String getFoosWithHeaders() { - return "Get some Foos with Header"; - } - - // @RequestMapping(value = "/foos", method = RequestMethod.GET, headers = "Accept=application/json") - // @ResponseBody - // public String getFoosAsJsonFromBrowser() { - // return "Get some Foos with Header Old"; - // } - - @RequestMapping(value = "/foos", produces = { "application/json", "application/xml" }) - @ResponseBody - public String getFoosAsJsonFromREST() { - return "Get some Foos with Header New"; - } - - // advanced - multiple mappings - - @RequestMapping(value = { "/advanced/bars", "/advanced/foos" }) - @ResponseBody - public String getFoosOrBarsByPath() { - return "Advanced - Get some Foos or Bars"; - } - - @RequestMapping(value = "*") - @ResponseBody - public String getFallback() { - return "Fallback for GET Requests"; - } - - @RequestMapping(value = "*", method = { RequestMethod.GET, RequestMethod.POST }) - @ResponseBody - public String allFallback() { - return "Fallback for All Requests"; - } - - @RequestMapping(value = "/foos/multiple", method = { RequestMethod.PUT, RequestMethod.POST }) - @ResponseBody - public String putAndPostFoos() { - return "Advanced - PUT and POST within single method"; - } - -} diff --git a/spring-5/src/main/java/org/baeldung/web/controller/ItemController.java b/spring-5/src/main/java/org/baeldung/web/controller/ItemController.java deleted file mode 100644 index 1cc3eae432..0000000000 --- a/spring-5/src/main/java/org/baeldung/web/controller/ItemController.java +++ /dev/null @@ -1,39 +0,0 @@ -package org.baeldung.web.controller; - -import java.util.Date; - -import org.baeldung.web.dto.Item; -import org.baeldung.web.dto.ItemManager; -import org.baeldung.web.dto.Views; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; - -import com.fasterxml.jackson.annotation.JsonView; - -@RestController -public class ItemController { - - @JsonView(Views.Public.class) - @RequestMapping("/items/{id}") - public Item getItemPublic(@PathVariable final int id) { - return ItemManager.getById(id); - } - - @JsonView(Views.Internal.class) - @RequestMapping("/items/internal/{id}") - public Item getItemInternal(@PathVariable final int id) { - return ItemManager.getById(id); - } - - @RequestMapping("/date") - public Date getCurrentDate() throws Exception { - return new Date(); - } - - @RequestMapping("/delay/{seconds}") - public void getCurrentTime(@PathVariable final int seconds) throws Exception { - - Thread.sleep(seconds * 1000); - } -} \ No newline at end of file diff --git a/spring-5/src/main/java/org/baeldung/web/controller/MyFooController.java b/spring-5/src/main/java/org/baeldung/web/controller/MyFooController.java deleted file mode 100644 index f19ddca435..0000000000 --- a/spring-5/src/main/java/org/baeldung/web/controller/MyFooController.java +++ /dev/null @@ -1,76 +0,0 @@ -package org.baeldung.web.controller; - -import java.util.Collection; -import java.util.HashMap; -import java.util.Map; - -import javax.servlet.http.HttpServletResponse; - -import org.baeldung.web.dto.Foo; -import org.baeldung.web.exception.ResourceNotFoundException; -import org.springframework.http.HttpStatus; -import org.springframework.stereotype.Controller; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; -import org.springframework.web.bind.annotation.ResponseBody; -import org.springframework.web.bind.annotation.ResponseStatus; -import org.springframework.web.servlet.support.ServletUriComponentsBuilder; - -@Controller -@RequestMapping(value = "/myfoos") -public class MyFooController { - - private final Map myfoos; - - public MyFooController() { - super(); - myfoos = new HashMap(); - myfoos.put(1L, new Foo(1L, "sample foo")); - } - - // API - read - - @RequestMapping(method = RequestMethod.GET) - @ResponseBody - public Collection findAll() { - return myfoos.values(); - } - - @RequestMapping(method = RequestMethod.GET, value = "/{id}", produces = { "application/json" }) - @ResponseBody - public Foo findById(@PathVariable final long id) { - final Foo foo = myfoos.get(id); - if (foo == null) { - throw new ResourceNotFoundException(); - } - return foo; - } - - // API - write - - @RequestMapping(method = RequestMethod.PUT, value = "/{id}") - @ResponseStatus(HttpStatus.OK) - @ResponseBody - public Foo updateFoo(@PathVariable("id") final long id, @RequestBody final Foo foo) { - myfoos.put(id, foo); - return foo; - } - - @RequestMapping(method = RequestMethod.POST) - @ResponseStatus(HttpStatus.CREATED) - @ResponseBody - public Foo createFoo(@RequestBody final Foo foo, HttpServletResponse response) { - myfoos.put(foo.getId(), foo); - response.setHeader("Location", ServletUriComponentsBuilder.fromCurrentRequest().path("/" + foo.getId()).toUriString()); - return foo; - } - - @RequestMapping(method = RequestMethod.DELETE, value = "/{id}") - @ResponseStatus(HttpStatus.OK) - public void deleteById(@PathVariable final long id) { - myfoos.remove(id); - } - -} diff --git a/spring-5/src/main/java/org/baeldung/web/controller/SimplePostController.java b/spring-5/src/main/java/org/baeldung/web/controller/SimplePostController.java deleted file mode 100644 index f8407acb47..0000000000 --- a/spring-5/src/main/java/org/baeldung/web/controller/SimplePostController.java +++ /dev/null @@ -1,73 +0,0 @@ -package org.baeldung.web.controller; - -import java.io.BufferedOutputStream; -import java.io.File; -import java.io.FileOutputStream; -import java.text.DateFormat; -import java.text.SimpleDateFormat; -import java.util.Date; - -import org.baeldung.web.dto.Foo; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.bind.annotation.RestController; -import org.springframework.web.multipart.MultipartFile; - -// used to test HttpClientPostingTest -@RestController -public class SimplePostController { - - @RequestMapping(value = "/users", method = RequestMethod.POST) - public String postUser(@RequestParam final String username, @RequestParam final String password) { - return "Success" + username; - } - - @RequestMapping(value = "/users/detail", method = RequestMethod.POST) - public String postUserDetail(@RequestBody final Foo entity) { - return "Success" + entity.getId(); - } - - @RequestMapping(value = "/users/multipart", method = RequestMethod.POST) - public String uploadFile(@RequestParam final String username, @RequestParam final String password, @RequestParam("file") final MultipartFile file) { - if (!file.isEmpty()) { - try { - final DateFormat dateFormat = new SimpleDateFormat("yyyy_MM_dd_HH.mm.ss"); - final String fileName = dateFormat.format(new Date()); - final File fileServer = new File(fileName); - fileServer.createNewFile(); - final byte[] bytes = file.getBytes(); - final BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(fileServer)); - stream.write(bytes); - stream.close(); - return "You successfully uploaded " + username; - } catch (final Exception e) { - return "You failed to upload " + e.getMessage(); - } - } else { - return "You failed to upload because the file was empty."; - } - } - - @RequestMapping(value = "/users/upload", method = RequestMethod.POST) - public String postMultipart(@RequestParam("file") final MultipartFile file) { - if (!file.isEmpty()) { - try { - final DateFormat dateFormat = new SimpleDateFormat("yyyy_MM_dd_HH.mm.ss"); - final String fileName = dateFormat.format(new Date()); - final File fileServer = new File(fileName); - fileServer.createNewFile(); - final byte[] bytes = file.getBytes(); - final BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(fileServer)); - stream.write(bytes); - stream.close(); - return "You successfully uploaded "; - } catch (final Exception e) { - return "You failed to upload " + e.getMessage(); - } - } else { - return "You failed to upload because the file was empty."; - } - } -} diff --git a/spring-5/src/main/java/org/baeldung/web/controller/advice/JsonpControllerAdvice.java b/spring-5/src/main/java/org/baeldung/web/controller/advice/JsonpControllerAdvice.java deleted file mode 100644 index 996f229128..0000000000 --- a/spring-5/src/main/java/org/baeldung/web/controller/advice/JsonpControllerAdvice.java +++ /dev/null @@ -1,13 +0,0 @@ -package org.baeldung.web.controller.advice; - -import org.springframework.web.bind.annotation.ControllerAdvice; -import org.springframework.web.servlet.mvc.method.annotation.AbstractJsonpResponseBodyAdvice; - -@ControllerAdvice -public class JsonpControllerAdvice extends AbstractJsonpResponseBodyAdvice { - - public JsonpControllerAdvice() { - super("callback"); - } - -} diff --git a/spring-5/src/main/java/org/baeldung/web/controller/mediatypes/CustomMediaTypeController.java b/spring-5/src/main/java/org/baeldung/web/controller/mediatypes/CustomMediaTypeController.java deleted file mode 100644 index 410c72d34f..0000000000 --- a/spring-5/src/main/java/org/baeldung/web/controller/mediatypes/CustomMediaTypeController.java +++ /dev/null @@ -1,20 +0,0 @@ -package org.baeldung.web.controller.mediatypes; - -import org.baeldung.web.dto.BaeldungItem; -import org.baeldung.web.dto.BaeldungItemV2; -import org.springframework.web.bind.annotation.*; - -@RestController -@RequestMapping(value = "/", produces = "application/vnd.baeldung.api.v1+json") -public class CustomMediaTypeController { - - @RequestMapping(method = RequestMethod.GET, value = "/public/api/items/{id}", produces = "application/vnd.baeldung.api.v1+json") - public @ResponseBody BaeldungItem getItem(@PathVariable("id") String id) { - return new BaeldungItem("itemId1"); - } - - @RequestMapping(method = RequestMethod.GET, value = "/public/api/items/{id}", produces = "application/vnd.baeldung.api.v2+json") - public @ResponseBody BaeldungItemV2 getItemSecondAPIVersion(@PathVariable("id") String id) { - return new BaeldungItemV2("itemName"); - } -} diff --git a/spring-5/src/main/java/org/baeldung/web/controller/redirect/RedirectController.java b/spring-5/src/main/java/org/baeldung/web/controller/redirect/RedirectController.java deleted file mode 100644 index 472c0c8bf5..0000000000 --- a/spring-5/src/main/java/org/baeldung/web/controller/redirect/RedirectController.java +++ /dev/null @@ -1,52 +0,0 @@ -package org.baeldung.web.controller.redirect; - -import org.springframework.stereotype.Controller; -import org.springframework.ui.ModelMap; -import org.springframework.web.bind.annotation.ModelAttribute; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; -import org.springframework.web.servlet.ModelAndView; -import org.springframework.web.servlet.mvc.support.RedirectAttributes; -import org.springframework.web.servlet.view.RedirectView; - -@Controller -@RequestMapping("/") -public class RedirectController { - - @RequestMapping(value = "/redirectWithXMLConfig", method = RequestMethod.GET) - public ModelAndView redirectWithUsingXMLConfig(final ModelMap model) { - model.addAttribute("attribute", "redirectWithXMLConfig"); - return new ModelAndView("RedirectedUrl", model); - } - - @RequestMapping(value = "/redirectWithRedirectPrefix", method = RequestMethod.GET) - public ModelAndView redirectWithUsingRedirectPrefix(final ModelMap model) { - model.addAttribute("attribute", "redirectWithRedirectPrefix"); - return new ModelAndView("redirect:/redirectedUrl", model); - } - - @RequestMapping(value = "/redirectWithRedirectAttributes", method = RequestMethod.GET) - public RedirectView redirectWithRedirectAttributes(final RedirectAttributes redirectAttributes) { - redirectAttributes.addFlashAttribute("flashAttribute", "redirectWithRedirectAttributes"); - redirectAttributes.addAttribute("attribute", "redirectWithRedirectAttributes"); - return new RedirectView("redirectedUrl"); - } - - @RequestMapping(value = "/redirectWithRedirectView", method = RequestMethod.GET) - public RedirectView redirectWithUsingRedirectView(final ModelMap model) { - model.addAttribute("attribute", "redirectWithRedirectView"); - return new RedirectView("redirectedUrl"); - } - - @RequestMapping(value = "/forwardWithForwardPrefix", method = RequestMethod.GET) - public ModelAndView forwardWithUsingForwardPrefix(final ModelMap model) { - model.addAttribute("attribute", "redirectWithForwardPrefix"); - return new ModelAndView("forward:/redirectedUrl", model); - } - - @RequestMapping(value = "/redirectedUrl", method = RequestMethod.GET) - public ModelAndView redirection(final ModelMap model, @ModelAttribute("flashAttribute") final Object flashAttribute) { - model.addAttribute("redirectionAttribute", flashAttribute); - return new ModelAndView("redirection", model); - } -} \ No newline at end of file diff --git a/spring-5/src/main/java/org/baeldung/web/controller/status/ExampleController.java b/spring-5/src/main/java/org/baeldung/web/controller/status/ExampleController.java deleted file mode 100644 index ceda138768..0000000000 --- a/spring-5/src/main/java/org/baeldung/web/controller/status/ExampleController.java +++ /dev/null @@ -1,24 +0,0 @@ -package org.baeldung.web.controller.status; - -import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; -import org.springframework.stereotype.Controller; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; -import org.springframework.web.bind.annotation.ResponseBody; - -@Controller -public class ExampleController { - - @RequestMapping(value = "/controller", method = RequestMethod.GET) - @ResponseBody - public ResponseEntity sendViaResponseEntity() { - return new ResponseEntity(HttpStatus.NOT_ACCEPTABLE); - } - - @RequestMapping(value = "/exception", method = RequestMethod.GET) - @ResponseBody - public ResponseEntity sendViaException() { - throw new ForbiddenException(); - } -} diff --git a/spring-5/src/main/java/org/baeldung/web/controller/status/ForbiddenException.java b/spring-5/src/main/java/org/baeldung/web/controller/status/ForbiddenException.java deleted file mode 100644 index 458bdaf170..0000000000 --- a/spring-5/src/main/java/org/baeldung/web/controller/status/ForbiddenException.java +++ /dev/null @@ -1,10 +0,0 @@ -package org.baeldung.web.controller.status; - -import org.springframework.http.HttpStatus; -import org.springframework.web.bind.annotation.ResponseStatus; - -@ResponseStatus(value = HttpStatus.FORBIDDEN, reason = "To show an example of a custom message") -public class ForbiddenException extends RuntimeException { - private static final long serialVersionUID = 6826605655586311552L; - -} diff --git a/spring-5/src/main/java/org/baeldung/web/dto/BaeldungItem.java b/spring-5/src/main/java/org/baeldung/web/dto/BaeldungItem.java deleted file mode 100644 index 9b3ecd33b9..0000000000 --- a/spring-5/src/main/java/org/baeldung/web/dto/BaeldungItem.java +++ /dev/null @@ -1,13 +0,0 @@ -package org.baeldung.web.dto; - -public class BaeldungItem { - private final String itemId; - - public BaeldungItem(String itemId) { - this.itemId = itemId; - } - - public String getItemId() { - return itemId; - } -} diff --git a/spring-5/src/main/java/org/baeldung/web/dto/BaeldungItemV2.java b/spring-5/src/main/java/org/baeldung/web/dto/BaeldungItemV2.java deleted file mode 100644 index 64df20a14e..0000000000 --- a/spring-5/src/main/java/org/baeldung/web/dto/BaeldungItemV2.java +++ /dev/null @@ -1,14 +0,0 @@ -package org.baeldung.web.dto; - - -public class BaeldungItemV2 { - private final String itemName; - - public BaeldungItemV2(String itemName) { - this.itemName = itemName; - } - - public String getItemName() { - return itemName; - } -} diff --git a/spring-5/src/main/java/org/baeldung/web/dto/Company.java b/spring-5/src/main/java/org/baeldung/web/dto/Company.java deleted file mode 100644 index 3164d604ad..0000000000 --- a/spring-5/src/main/java/org/baeldung/web/dto/Company.java +++ /dev/null @@ -1,38 +0,0 @@ -package org.baeldung.web.dto; - -public class Company { - - private long id; - private String name; - - public Company() { - super(); - } - - public Company(final long id, final String name) { - this.id = id; - this.name = name; - } - - public String getName() { - return name; - } - - public void setName(final String name) { - this.name = name; - } - - public long getId() { - return id; - } - - public void setId(final long id) { - this.id = id; - } - - @Override - public String toString() { - return "Company [id=" + id + ", name=" + name + "]"; - } - -} diff --git a/spring-5/src/main/java/org/baeldung/web/dto/Foo.java b/spring-5/src/main/java/org/baeldung/web/dto/Foo.java deleted file mode 100644 index 240b368b50..0000000000 --- a/spring-5/src/main/java/org/baeldung/web/dto/Foo.java +++ /dev/null @@ -1,45 +0,0 @@ -package org.baeldung.web.dto; - -import com.thoughtworks.xstream.annotations.XStreamAlias; - -@XStreamAlias("Foo") -public class Foo { - private long id; - private String name; - - public Foo() { - super(); - } - - public Foo(final String name) { - super(); - - this.name = name; - } - - public Foo(final long id, final String name) { - super(); - - this.id = id; - this.name = name; - } - - // API - - public long getId() { - return id; - } - - public void setId(final long id) { - this.id = id; - } - - public String getName() { - return name; - } - - public void setName(final String name) { - this.name = name; - } - -} \ No newline at end of file diff --git a/spring-5/src/main/java/org/baeldung/web/dto/FooProtos.java b/spring-5/src/main/java/org/baeldung/web/dto/FooProtos.java deleted file mode 100644 index 8ca96c38fc..0000000000 --- a/spring-5/src/main/java/org/baeldung/web/dto/FooProtos.java +++ /dev/null @@ -1,597 +0,0 @@ -// Generated by the protocol buffer compiler. DO NOT EDIT! -// source: FooProtos.proto - -package org.baeldung.web.dto; - -public final class FooProtos { - private FooProtos() { - } - - public static void registerAllExtensions(com.google.protobuf.ExtensionRegistry registry) { - } - - public interface FooOrBuilder extends - // @@protoc_insertion_point(interface_extends:baeldung.Foo) - com.google.protobuf.MessageOrBuilder { - - /** - * required int64 id = 1; - */ - boolean hasId(); - - /** - * required int64 id = 1; - */ - long getId(); - - /** - * required string name = 2; - */ - boolean hasName(); - - /** - * required string name = 2; - */ - java.lang.String getName(); - - /** - * required string name = 2; - */ - com.google.protobuf.ByteString getNameBytes(); - } - - /** - * Protobuf type {@code baeldung.Foo} - */ - public static final class Foo extends com.google.protobuf.GeneratedMessage implements - // @@protoc_insertion_point(message_implements:baeldung.Foo) - FooOrBuilder { - // Use Foo.newBuilder() to construct. - private Foo(com.google.protobuf.GeneratedMessage.Builder builder) { - super(builder); - this.unknownFields = builder.getUnknownFields(); - } - - private Foo(boolean noInit) { - this.unknownFields = com.google.protobuf.UnknownFieldSet.getDefaultInstance(); - } - - private static final Foo defaultInstance; - - public static Foo getDefaultInstance() { - return defaultInstance; - } - - public Foo getDefaultInstanceForType() { - return defaultInstance; - } - - private final com.google.protobuf.UnknownFieldSet unknownFields; - - @java.lang.Override - public final com.google.protobuf.UnknownFieldSet getUnknownFields() { - return this.unknownFields; - } - - private Foo(com.google.protobuf.CodedInputStream input, com.google.protobuf.ExtensionRegistryLite extensionRegistry) throws com.google.protobuf.InvalidProtocolBufferException { - initFields(); - int mutable_bitField0_ = 0; - com.google.protobuf.UnknownFieldSet.Builder unknownFields = com.google.protobuf.UnknownFieldSet.newBuilder(); - try { - boolean done = false; - while (!done) { - int tag = input.readTag(); - switch (tag) { - case 0: - done = true; - break; - default: { - if (!parseUnknownField(input, unknownFields, extensionRegistry, tag)) { - done = true; - } - break; - } - case 8: { - bitField0_ |= 0x00000001; - id_ = input.readInt64(); - break; - } - case 18: { - com.google.protobuf.ByteString bs = input.readBytes(); - bitField0_ |= 0x00000002; - name_ = bs; - break; - } - } - } - } catch (com.google.protobuf.InvalidProtocolBufferException e) { - throw e.setUnfinishedMessage(this); - } catch (java.io.IOException e) { - throw new com.google.protobuf.InvalidProtocolBufferException(e.getMessage()).setUnfinishedMessage(this); - } finally { - this.unknownFields = unknownFields.build(); - makeExtensionsImmutable(); - } - } - - public static final com.google.protobuf.Descriptors.Descriptor getDescriptor() { - return org.baeldung.web.dto.FooProtos.internal_static_baeldung_Foo_descriptor; - } - - protected com.google.protobuf.GeneratedMessage.FieldAccessorTable internalGetFieldAccessorTable() { - return org.baeldung.web.dto.FooProtos.internal_static_baeldung_Foo_fieldAccessorTable.ensureFieldAccessorsInitialized(org.baeldung.web.dto.FooProtos.Foo.class, org.baeldung.web.dto.FooProtos.Foo.Builder.class); - } - - public static com.google.protobuf.Parser PARSER = new com.google.protobuf.AbstractParser() { - public Foo parsePartialFrom(com.google.protobuf.CodedInputStream input, com.google.protobuf.ExtensionRegistryLite extensionRegistry) throws com.google.protobuf.InvalidProtocolBufferException { - return new Foo(input, extensionRegistry); - } - }; - - @java.lang.Override - public com.google.protobuf.Parser getParserForType() { - return PARSER; - } - - private int bitField0_; - public static final int ID_FIELD_NUMBER = 1; - private long id_; - - /** - * required int64 id = 1; - */ - public boolean hasId() { - return ((bitField0_ & 0x00000001) == 0x00000001); - } - - /** - * required int64 id = 1; - */ - public long getId() { - return id_; - } - - public static final int NAME_FIELD_NUMBER = 2; - private java.lang.Object name_; - - /** - * required string name = 2; - */ - public boolean hasName() { - return ((bitField0_ & 0x00000002) == 0x00000002); - } - - /** - * required string name = 2; - */ - public java.lang.String getName() { - java.lang.Object ref = name_; - if (ref instanceof java.lang.String) { - return (java.lang.String) ref; - } else { - com.google.protobuf.ByteString bs = (com.google.protobuf.ByteString) ref; - java.lang.String s = bs.toStringUtf8(); - if (bs.isValidUtf8()) { - name_ = s; - } - return s; - } - } - - /** - * required string name = 2; - */ - public com.google.protobuf.ByteString getNameBytes() { - java.lang.Object ref = name_; - if (ref instanceof java.lang.String) { - com.google.protobuf.ByteString b = com.google.protobuf.ByteString.copyFromUtf8((java.lang.String) ref); - name_ = b; - return b; - } else { - return (com.google.protobuf.ByteString) ref; - } - } - - private void initFields() { - id_ = 0L; - name_ = ""; - } - - private byte memoizedIsInitialized = -1; - - public final boolean isInitialized() { - byte isInitialized = memoizedIsInitialized; - if (isInitialized == 1) - return true; - if (isInitialized == 0) - return false; - - if (!hasId()) { - memoizedIsInitialized = 0; - return false; - } - if (!hasName()) { - memoizedIsInitialized = 0; - return false; - } - memoizedIsInitialized = 1; - return true; - } - - public void writeTo(com.google.protobuf.CodedOutputStream output) throws java.io.IOException { - getSerializedSize(); - if (((bitField0_ & 0x00000001) == 0x00000001)) { - output.writeInt64(1, id_); - } - if (((bitField0_ & 0x00000002) == 0x00000002)) { - output.writeBytes(2, getNameBytes()); - } - getUnknownFields().writeTo(output); - } - - private int memoizedSerializedSize = -1; - - public int getSerializedSize() { - int size = memoizedSerializedSize; - if (size != -1) - return size; - - size = 0; - if (((bitField0_ & 0x00000001) == 0x00000001)) { - size += com.google.protobuf.CodedOutputStream.computeInt64Size(1, id_); - } - if (((bitField0_ & 0x00000002) == 0x00000002)) { - size += com.google.protobuf.CodedOutputStream.computeBytesSize(2, getNameBytes()); - } - size += getUnknownFields().getSerializedSize(); - memoizedSerializedSize = size; - return size; - } - - private static final long serialVersionUID = 0L; - - @java.lang.Override - protected java.lang.Object writeReplace() throws java.io.ObjectStreamException { - return super.writeReplace(); - } - - public static org.baeldung.web.dto.FooProtos.Foo parseFrom(com.google.protobuf.ByteString data) throws com.google.protobuf.InvalidProtocolBufferException { - return PARSER.parseFrom(data); - } - - public static org.baeldung.web.dto.FooProtos.Foo parseFrom(com.google.protobuf.ByteString data, com.google.protobuf.ExtensionRegistryLite extensionRegistry) throws com.google.protobuf.InvalidProtocolBufferException { - return PARSER.parseFrom(data, extensionRegistry); - } - - public static org.baeldung.web.dto.FooProtos.Foo parseFrom(byte[] data) throws com.google.protobuf.InvalidProtocolBufferException { - return PARSER.parseFrom(data); - } - - public static org.baeldung.web.dto.FooProtos.Foo parseFrom(byte[] data, com.google.protobuf.ExtensionRegistryLite extensionRegistry) throws com.google.protobuf.InvalidProtocolBufferException { - return PARSER.parseFrom(data, extensionRegistry); - } - - public static org.baeldung.web.dto.FooProtos.Foo parseFrom(java.io.InputStream input) throws java.io.IOException { - return PARSER.parseFrom(input); - } - - public static org.baeldung.web.dto.FooProtos.Foo parseFrom(java.io.InputStream input, com.google.protobuf.ExtensionRegistryLite extensionRegistry) throws java.io.IOException { - return PARSER.parseFrom(input, extensionRegistry); - } - - public static org.baeldung.web.dto.FooProtos.Foo parseDelimitedFrom(java.io.InputStream input) throws java.io.IOException { - return PARSER.parseDelimitedFrom(input); - } - - public static org.baeldung.web.dto.FooProtos.Foo parseDelimitedFrom(java.io.InputStream input, com.google.protobuf.ExtensionRegistryLite extensionRegistry) throws java.io.IOException { - return PARSER.parseDelimitedFrom(input, extensionRegistry); - } - - public static org.baeldung.web.dto.FooProtos.Foo parseFrom(com.google.protobuf.CodedInputStream input) throws java.io.IOException { - return PARSER.parseFrom(input); - } - - public static org.baeldung.web.dto.FooProtos.Foo parseFrom(com.google.protobuf.CodedInputStream input, com.google.protobuf.ExtensionRegistryLite extensionRegistry) throws java.io.IOException { - return PARSER.parseFrom(input, extensionRegistry); - } - - public static Builder newBuilder() { - return Builder.create(); - } - - public Builder newBuilderForType() { - return newBuilder(); - } - - public static Builder newBuilder(org.baeldung.web.dto.FooProtos.Foo prototype) { - return newBuilder().mergeFrom(prototype); - } - - public Builder toBuilder() { - return newBuilder(this); - } - - @java.lang.Override - protected Builder newBuilderForType(com.google.protobuf.GeneratedMessage.BuilderParent parent) { - Builder builder = new Builder(parent); - return builder; - } - - /** - * Protobuf type {@code baeldung.Foo} - */ - public static final class Builder extends com.google.protobuf.GeneratedMessage.Builder implements - // @@protoc_insertion_point(builder_implements:baeldung.Foo) - org.baeldung.web.dto.FooProtos.FooOrBuilder { - public static final com.google.protobuf.Descriptors.Descriptor getDescriptor() { - return org.baeldung.web.dto.FooProtos.internal_static_baeldung_Foo_descriptor; - } - - protected com.google.protobuf.GeneratedMessage.FieldAccessorTable internalGetFieldAccessorTable() { - return org.baeldung.web.dto.FooProtos.internal_static_baeldung_Foo_fieldAccessorTable.ensureFieldAccessorsInitialized(org.baeldung.web.dto.FooProtos.Foo.class, org.baeldung.web.dto.FooProtos.Foo.Builder.class); - } - - // Construct using org.baeldung.web.dto.FooProtos.Foo.newBuilder() - private Builder() { - maybeForceBuilderInitialization(); - } - - private Builder(com.google.protobuf.GeneratedMessage.BuilderParent parent) { - super(parent); - maybeForceBuilderInitialization(); - } - - private void maybeForceBuilderInitialization() { - if (com.google.protobuf.GeneratedMessage.alwaysUseFieldBuilders) { - } - } - - private static Builder create() { - return new Builder(); - } - - public Builder clear() { - super.clear(); - id_ = 0L; - bitField0_ = (bitField0_ & ~0x00000001); - name_ = ""; - bitField0_ = (bitField0_ & ~0x00000002); - return this; - } - - public Builder clone() { - return create().mergeFrom(buildPartial()); - } - - public com.google.protobuf.Descriptors.Descriptor getDescriptorForType() { - return org.baeldung.web.dto.FooProtos.internal_static_baeldung_Foo_descriptor; - } - - public org.baeldung.web.dto.FooProtos.Foo getDefaultInstanceForType() { - return org.baeldung.web.dto.FooProtos.Foo.getDefaultInstance(); - } - - public org.baeldung.web.dto.FooProtos.Foo build() { - org.baeldung.web.dto.FooProtos.Foo result = buildPartial(); - if (!result.isInitialized()) { - throw newUninitializedMessageException(result); - } - return result; - } - - public org.baeldung.web.dto.FooProtos.Foo buildPartial() { - org.baeldung.web.dto.FooProtos.Foo result = new org.baeldung.web.dto.FooProtos.Foo(this); - int from_bitField0_ = bitField0_; - int to_bitField0_ = 0; - if (((from_bitField0_ & 0x00000001) == 0x00000001)) { - to_bitField0_ |= 0x00000001; - } - result.id_ = id_; - if (((from_bitField0_ & 0x00000002) == 0x00000002)) { - to_bitField0_ |= 0x00000002; - } - result.name_ = name_; - result.bitField0_ = to_bitField0_; - onBuilt(); - return result; - } - - public Builder mergeFrom(com.google.protobuf.Message other) { - if (other instanceof org.baeldung.web.dto.FooProtos.Foo) { - return mergeFrom((org.baeldung.web.dto.FooProtos.Foo) other); - } else { - super.mergeFrom(other); - return this; - } - } - - public Builder mergeFrom(org.baeldung.web.dto.FooProtos.Foo other) { - if (other == org.baeldung.web.dto.FooProtos.Foo.getDefaultInstance()) - return this; - if (other.hasId()) { - setId(other.getId()); - } - if (other.hasName()) { - bitField0_ |= 0x00000002; - name_ = other.name_; - onChanged(); - } - this.mergeUnknownFields(other.getUnknownFields()); - return this; - } - - public final boolean isInitialized() { - if (!hasId()) { - - return false; - } - if (!hasName()) { - - return false; - } - return true; - } - - public Builder mergeFrom(com.google.protobuf.CodedInputStream input, com.google.protobuf.ExtensionRegistryLite extensionRegistry) throws java.io.IOException { - org.baeldung.web.dto.FooProtos.Foo parsedMessage = null; - try { - parsedMessage = PARSER.parsePartialFrom(input, extensionRegistry); - } catch (com.google.protobuf.InvalidProtocolBufferException e) { - parsedMessage = (org.baeldung.web.dto.FooProtos.Foo) e.getUnfinishedMessage(); - throw e; - } finally { - if (parsedMessage != null) { - mergeFrom(parsedMessage); - } - } - return this; - } - - private int bitField0_; - - private long id_; - - /** - * required int64 id = 1; - */ - public boolean hasId() { - return ((bitField0_ & 0x00000001) == 0x00000001); - } - - /** - * required int64 id = 1; - */ - public long getId() { - return id_; - } - - /** - * required int64 id = 1; - */ - public Builder setId(long value) { - bitField0_ |= 0x00000001; - id_ = value; - onChanged(); - return this; - } - - /** - * required int64 id = 1; - */ - public Builder clearId() { - bitField0_ = (bitField0_ & ~0x00000001); - id_ = 0L; - onChanged(); - return this; - } - - private java.lang.Object name_ = ""; - - /** - * required string name = 2; - */ - public boolean hasName() { - return ((bitField0_ & 0x00000002) == 0x00000002); - } - - /** - * required string name = 2; - */ - public java.lang.String getName() { - java.lang.Object ref = name_; - if (!(ref instanceof java.lang.String)) { - com.google.protobuf.ByteString bs = (com.google.protobuf.ByteString) ref; - java.lang.String s = bs.toStringUtf8(); - if (bs.isValidUtf8()) { - name_ = s; - } - return s; - } else { - return (java.lang.String) ref; - } - } - - /** - * required string name = 2; - */ - public com.google.protobuf.ByteString getNameBytes() { - java.lang.Object ref = name_; - if (ref instanceof String) { - com.google.protobuf.ByteString b = com.google.protobuf.ByteString.copyFromUtf8((java.lang.String) ref); - name_ = b; - return b; - } else { - return (com.google.protobuf.ByteString) ref; - } - } - - /** - * required string name = 2; - */ - public Builder setName(java.lang.String value) { - if (value == null) { - throw new NullPointerException(); - } - bitField0_ |= 0x00000002; - name_ = value; - onChanged(); - return this; - } - - /** - * required string name = 2; - */ - public Builder clearName() { - bitField0_ = (bitField0_ & ~0x00000002); - name_ = getDefaultInstance().getName(); - onChanged(); - return this; - } - - /** - * required string name = 2; - */ - public Builder setNameBytes(com.google.protobuf.ByteString value) { - if (value == null) { - throw new NullPointerException(); - } - bitField0_ |= 0x00000002; - name_ = value; - onChanged(); - return this; - } - - // @@protoc_insertion_point(builder_scope:baeldung.Foo) - } - - static { - defaultInstance = new Foo(true); - defaultInstance.initFields(); - } - - // @@protoc_insertion_point(class_scope:baeldung.Foo) - } - - private static final com.google.protobuf.Descriptors.Descriptor internal_static_baeldung_Foo_descriptor; - private static com.google.protobuf.GeneratedMessage.FieldAccessorTable internal_static_baeldung_Foo_fieldAccessorTable; - - public static com.google.protobuf.Descriptors.FileDescriptor getDescriptor() { - return descriptor; - } - - private static com.google.protobuf.Descriptors.FileDescriptor descriptor; - static { - java.lang.String[] descriptorData = { "\n\017FooProtos.proto\022\010baeldung\"\037\n\003Foo\022\n\n\002id" + "\030\001 \002(\003\022\014\n\004name\030\002 \002(\tB!\n\024org.baeldung.web" + ".dtoB\tFooProtos" }; - com.google.protobuf.Descriptors.FileDescriptor.InternalDescriptorAssigner assigner = new com.google.protobuf.Descriptors.FileDescriptor.InternalDescriptorAssigner() { - public com.google.protobuf.ExtensionRegistry assignDescriptors(com.google.protobuf.Descriptors.FileDescriptor root) { - descriptor = root; - return null; - } - }; - com.google.protobuf.Descriptors.FileDescriptor.internalBuildGeneratedFileFrom(descriptorData, new com.google.protobuf.Descriptors.FileDescriptor[] {}, assigner); - internal_static_baeldung_Foo_descriptor = getDescriptor().getMessageTypes().get(0); - internal_static_baeldung_Foo_fieldAccessorTable = new com.google.protobuf.GeneratedMessage.FieldAccessorTable(internal_static_baeldung_Foo_descriptor, new java.lang.String[] { "Id", "Name", }); - } - - // @@protoc_insertion_point(outer_class_scope) -} diff --git a/spring-5/src/main/java/org/baeldung/web/dto/Item.java b/spring-5/src/main/java/org/baeldung/web/dto/Item.java deleted file mode 100644 index 536c72020f..0000000000 --- a/spring-5/src/main/java/org/baeldung/web/dto/Item.java +++ /dev/null @@ -1,36 +0,0 @@ -package org.baeldung.web.dto; - -import com.fasterxml.jackson.annotation.JsonView; - -public class Item { - @JsonView(Views.Public.class) - public int id; - - @JsonView(Views.Public.class) - public String itemName; - - @JsonView(Views.Internal.class) - public String ownerName; - - public Item() { - super(); - } - - public Item(final int id, final String itemName, final String ownerName) { - this.id = id; - this.itemName = itemName; - this.ownerName = ownerName; - } - - public int getId() { - return id; - } - - public String getItemName() { - return itemName; - } - - public String getOwnerName() { - return ownerName; - } -} \ No newline at end of file diff --git a/spring-5/src/main/java/org/baeldung/web/dto/ItemManager.java b/spring-5/src/main/java/org/baeldung/web/dto/ItemManager.java deleted file mode 100644 index 74ffada300..0000000000 --- a/spring-5/src/main/java/org/baeldung/web/dto/ItemManager.java +++ /dev/null @@ -1,9 +0,0 @@ -package org.baeldung.web.dto; - -public class ItemManager { - - public static Item getById(final int id) { - final Item item = new Item(2, "book", "John"); - return item; - } -} diff --git a/spring-5/src/main/java/org/baeldung/web/dto/Views.java b/spring-5/src/main/java/org/baeldung/web/dto/Views.java deleted file mode 100644 index 6231e12bcc..0000000000 --- a/spring-5/src/main/java/org/baeldung/web/dto/Views.java +++ /dev/null @@ -1,9 +0,0 @@ -package org.baeldung.web.dto; - -public class Views { - public static class Public { - } - - public static class Internal extends Public { - } -} diff --git a/spring-5/src/main/java/org/baeldung/web/exception/ResourceNotFoundException.java b/spring-5/src/main/java/org/baeldung/web/exception/ResourceNotFoundException.java deleted file mode 100644 index aab737b6ec..0000000000 --- a/spring-5/src/main/java/org/baeldung/web/exception/ResourceNotFoundException.java +++ /dev/null @@ -1,8 +0,0 @@ -package org.baeldung.web.exception; - -import org.springframework.http.HttpStatus; -import org.springframework.web.bind.annotation.ResponseStatus; - -@ResponseStatus(value = HttpStatus.NOT_FOUND) -public class ResourceNotFoundException extends RuntimeException { -} diff --git a/spring-5/src/main/resources/application.properties b/spring-5/src/main/resources/application.properties index 300589f561..e69de29bb2 100644 --- a/spring-5/src/main/resources/application.properties +++ b/spring-5/src/main/resources/application.properties @@ -1,2 +0,0 @@ -server.port= 8082 -server.context-path=/spring-rest \ No newline at end of file diff --git a/spring-5/src/main/resources/logback.xml b/spring-5/src/main/resources/logback.xml deleted file mode 100644 index 1146dade63..0000000000 --- a/spring-5/src/main/resources/logback.xml +++ /dev/null @@ -1,20 +0,0 @@ - - - - - web - %date [%thread] %-5level %logger{36} - %message%n - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/spring-5/src/main/webapp/WEB-INF/api-servlet.xml b/spring-5/src/main/webapp/WEB-INF/api-servlet.xml deleted file mode 100644 index 0f80990c16..0000000000 --- a/spring-5/src/main/webapp/WEB-INF/api-servlet.xml +++ /dev/null @@ -1,56 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - /WEB-INF/spring-views.xml - - - - - - - - - - - - - - - - - - - - - diff --git a/spring-5/src/main/webapp/WEB-INF/company.html b/spring-5/src/main/webapp/WEB-INF/company.html deleted file mode 100644 index d2072bfd3c..0000000000 --- a/spring-5/src/main/webapp/WEB-INF/company.html +++ /dev/null @@ -1,44 +0,0 @@ - - - - - Company Data - - - - - - - -
- - - \ No newline at end of file diff --git a/spring-5/src/main/webapp/WEB-INF/spring-views.xml b/spring-5/src/main/webapp/WEB-INF/spring-views.xml deleted file mode 100644 index 2944828d6d..0000000000 --- a/spring-5/src/main/webapp/WEB-INF/spring-views.xml +++ /dev/null @@ -1,10 +0,0 @@ - - - - - - - \ No newline at end of file diff --git a/spring-5/src/main/webapp/WEB-INF/web.xml b/spring-5/src/main/webapp/WEB-INF/web.xml deleted file mode 100644 index a439de8a05..0000000000 --- a/spring-5/src/main/webapp/WEB-INF/web.xml +++ /dev/null @@ -1,41 +0,0 @@ - - - - Spring MVC Application - - - - contextClass - - org.springframework.web.context.support.AnnotationConfigWebApplicationContext - - - - contextConfigLocation - org.baeldung.config - - - - org.springframework.web.context.ContextLoaderListener - - - - - api - org.springframework.web.servlet.DispatcherServlet - 1 - - - api - / - - - - - - - diff --git a/spring-5/src/test/java/com/baeldung/Spring5ApplicationTests.java b/spring-5/src/test/java/com/baeldung/Spring5ApplicationTests.java new file mode 100644 index 0000000000..e2bfe3a880 --- /dev/null +++ b/spring-5/src/test/java/com/baeldung/Spring5ApplicationTests.java @@ -0,0 +1,16 @@ +package com.baeldung; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@SpringBootTest +public class Spring5ApplicationTests { + + @Test + public void contextLoads() { + } + +} diff --git a/spring-5/src/test/java/org/baeldung/client/Consts.java b/spring-5/src/test/java/org/baeldung/client/Consts.java deleted file mode 100644 index b40561d9c3..0000000000 --- a/spring-5/src/test/java/org/baeldung/client/Consts.java +++ /dev/null @@ -1,5 +0,0 @@ -package org.baeldung.client; - -public interface Consts { - int APPLICATION_PORT = 8082; -} diff --git a/spring-5/src/test/java/org/baeldung/client/RestTemplateBasicLiveTest.java b/spring-5/src/test/java/org/baeldung/client/RestTemplateBasicLiveTest.java deleted file mode 100644 index a47c60e9d8..0000000000 --- a/spring-5/src/test/java/org/baeldung/client/RestTemplateBasicLiveTest.java +++ /dev/null @@ -1,216 +0,0 @@ -package org.baeldung.client; - -import static org.apache.commons.codec.binary.Base64.encodeBase64; -import static org.baeldung.client.Consts.APPLICATION_PORT; -import static org.hamcrest.CoreMatchers.equalTo; -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.CoreMatchers.notNullValue; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; - -import java.io.IOException; -import java.net.URI; -import java.util.Arrays; -import java.util.Set; - -import org.baeldung.web.dto.Foo; -import org.junit.Before; -import org.junit.Test; -import org.springframework.http.HttpEntity; -import org.springframework.http.HttpHeaders; -import org.springframework.http.HttpMethod; -import org.springframework.http.HttpStatus; -import org.springframework.http.MediaType; -import org.springframework.http.ResponseEntity; -import org.springframework.http.client.ClientHttpRequestFactory; -import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; -import org.springframework.web.client.HttpClientErrorException; -import org.springframework.web.client.RequestCallback; -import org.springframework.web.client.RestTemplate; - -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.google.common.base.Charsets; - -public class RestTemplateBasicLiveTest { - - private RestTemplate restTemplate; - private static final String fooResourceUrl = "http://localhost:" + APPLICATION_PORT + "/spring-rest/myfoos"; - - @Before - public void beforeTest() { - restTemplate = new RestTemplate(); - } - - // GET - - @Test - public void givenResourceUrl_whenSendGetForRequestEntity_thenStatusOk() throws IOException { - final ResponseEntity response = restTemplate.getForEntity(fooResourceUrl + "/1", String.class); - - assertThat(response.getStatusCode(), equalTo(HttpStatus.OK)); - } - - @Test - public void givenResourceUrl_whenSendGetForRequestEntity_thenBodyCorrect() throws IOException { - final ResponseEntity response = restTemplate.getForEntity(fooResourceUrl + "/1", String.class); - - final ObjectMapper mapper = new ObjectMapper(); - final JsonNode root = mapper.readTree(response.getBody()); - final JsonNode name = root.path("name"); - assertThat(name.asText(), notNullValue()); - } - - @Test - public void givenResourceUrl_whenRetrievingResource_thenCorrect() throws IOException { - final Foo foo = restTemplate.getForObject(fooResourceUrl + "/1", Foo.class); - - assertThat(foo.getName(), notNullValue()); - assertThat(foo.getId(), is(1L)); - } - - // HEAD, OPTIONS - - @Test - public void givenFooService_whenCallHeadForHeaders_thenReceiveAllHeadersForThatResource() { - final HttpHeaders httpHeaders = restTemplate.headForHeaders(fooResourceUrl); - - assertTrue(httpHeaders.getContentType().includes(MediaType.APPLICATION_JSON)); - } - - // POST - - @Test - public void givenFooService_whenPostForObject_thenCreatedObjectIsReturned() { - final HttpEntity request = new HttpEntity<>(new Foo("bar")); - final Foo foo = restTemplate.postForObject(fooResourceUrl, request, Foo.class); - assertThat(foo, notNullValue()); - assertThat(foo.getName(), is("bar")); - } - - @Test - public void givenFooService_whenPostForLocation_thenCreatedLocationIsReturned() { - final HttpEntity request = new HttpEntity<>(new Foo("bar")); - final URI location = restTemplate.postForLocation(fooResourceUrl, request); - assertThat(location, notNullValue()); - } - - @Test - public void givenFooService_whenPostResource_thenResourceIsCreated() { - final RestTemplate template = new RestTemplate(); - - final HttpEntity request = new HttpEntity<>(new Foo("bar")); - - final ResponseEntity response = template.exchange(fooResourceUrl, HttpMethod.POST, request, Foo.class); - assertThat(response.getStatusCode(), is(HttpStatus.CREATED)); - final Foo foo = response.getBody(); - assertThat(foo, notNullValue()); - assertThat(foo.getName(), is("bar")); - } - - @Test - public void givenFooService_whenCallOptionsForAllow_thenReceiveValueOfAllowHeader() { - final Set optionsForAllow = restTemplate.optionsForAllow(fooResourceUrl); - final HttpMethod[] supportedMethods = { HttpMethod.GET, HttpMethod.POST, HttpMethod.HEAD }; - - assertTrue(optionsForAllow.containsAll(Arrays.asList(supportedMethods))); - } - - // PUT - - @Test - public void givenFooService_whenPutExistingEntity_thenItIsUpdated() { - final RestTemplate template = new RestTemplate(); - final HttpHeaders headers = prepareBasicAuthHeaders(); - final HttpEntity request = new HttpEntity<>(new Foo("bar"), headers); - - // Create Resource - final ResponseEntity createResponse = template.exchange(fooResourceUrl, HttpMethod.POST, request, Foo.class); - - // Update Resource - final Foo updatedInstance = new Foo("newName"); - updatedInstance.setId(createResponse.getBody().getId()); - final String resourceUrl = fooResourceUrl + '/' + createResponse.getBody().getId(); - final HttpEntity requestUpdate = new HttpEntity<>(updatedInstance, headers); - template.exchange(resourceUrl, HttpMethod.PUT, requestUpdate, Void.class); - - // Check that Resource was updated - final ResponseEntity updateResponse = template.exchange(resourceUrl, HttpMethod.GET, new HttpEntity<>(headers), Foo.class); - final Foo foo = updateResponse.getBody(); - assertThat(foo.getName(), is(updatedInstance.getName())); - } - - @Test - public void givenFooService_whenPutExistingEntityWithCallback_thenItIsUpdated() { - final RestTemplate template = new RestTemplate(); - final HttpHeaders headers = prepareBasicAuthHeaders(); - final HttpEntity request = new HttpEntity<>(new Foo("bar"), headers); - - // Create entity - ResponseEntity response = template.exchange(fooResourceUrl, HttpMethod.POST, request, Foo.class); - assertThat(response.getStatusCode(), is(HttpStatus.CREATED)); - - // Update entity - final Foo updatedInstance = new Foo("newName"); - updatedInstance.setId(response.getBody().getId()); - final String resourceUrl = fooResourceUrl + '/' + response.getBody().getId(); - template.execute(resourceUrl, HttpMethod.PUT, requestCallback(updatedInstance), clientHttpResponse -> null); - - // Check that entity was updated - response = template.exchange(resourceUrl, HttpMethod.GET, new HttpEntity<>(headers), Foo.class); - final Foo foo = response.getBody(); - assertThat(foo.getName(), is(updatedInstance.getName())); - } - - // DELETE - - @Test - public void givenFooService_whenCallDelete_thenEntityIsRemoved() { - final Foo foo = new Foo("remove me"); - final ResponseEntity response = restTemplate.postForEntity(fooResourceUrl, foo, Foo.class); - assertThat(response.getStatusCode(), is(HttpStatus.CREATED)); - - final String entityUrl = fooResourceUrl + "/" + response.getBody().getId(); - restTemplate.delete(entityUrl); - try { - restTemplate.getForEntity(entityUrl, Foo.class); - fail(); - } catch (final HttpClientErrorException ex) { - assertThat(ex.getStatusCode(), is(HttpStatus.NOT_FOUND)); - } - } - - // - - private HttpHeaders prepareBasicAuthHeaders() { - final HttpHeaders headers = new HttpHeaders(); - final String encodedLogPass = getBase64EncodedLogPass(); - headers.add(HttpHeaders.AUTHORIZATION, "Basic " + encodedLogPass); - return headers; - } - - private String getBase64EncodedLogPass() { - final String logPass = "user1:user1Pass"; - final byte[] authHeaderBytes = encodeBase64(logPass.getBytes(Charsets.US_ASCII)); - return new String(authHeaderBytes, Charsets.US_ASCII); - } - - private RequestCallback requestCallback(final Foo updatedInstance) { - return clientHttpRequest -> { - final ObjectMapper mapper = new ObjectMapper(); - mapper.writeValue(clientHttpRequest.getBody(), updatedInstance); - clientHttpRequest.getHeaders().add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE); - clientHttpRequest.getHeaders().add(HttpHeaders.AUTHORIZATION, "Basic " + getBase64EncodedLogPass()); - }; - } - - // Simply setting restTemplate timeout using ClientHttpRequestFactory - - ClientHttpRequestFactory getSimpleClientHttpRequestFactory() { - final int timeout = 5; - final HttpComponentsClientHttpRequestFactory clientHttpRequestFactory = new HttpComponentsClientHttpRequestFactory(); - clientHttpRequestFactory.setConnectTimeout(timeout * 1000); - return clientHttpRequestFactory; - } -} diff --git a/spring-5/src/test/java/org/baeldung/okhttp/DefaultContentTypeInterceptor.java b/spring-5/src/test/java/org/baeldung/okhttp/DefaultContentTypeInterceptor.java deleted file mode 100644 index c4fc689fad..0000000000 --- a/spring-5/src/test/java/org/baeldung/okhttp/DefaultContentTypeInterceptor.java +++ /dev/null @@ -1,24 +0,0 @@ -package org.baeldung.okhttp; - -import java.io.IOException; - -import okhttp3.Interceptor; -import okhttp3.Request; -import okhttp3.Response; - -public class DefaultContentTypeInterceptor implements Interceptor { - - private final String contentType; - - public DefaultContentTypeInterceptor(String contentType) { - this.contentType = contentType; - } - - public Response intercept(Interceptor.Chain chain) throws IOException { - - Request originalRequest = chain.request(); - Request requestWithUserAgent = originalRequest.newBuilder().header("Content-Type", contentType).build(); - - return chain.proceed(requestWithUserAgent); - } -} diff --git a/spring-5/src/test/java/org/baeldung/okhttp/OkHttpFileUploadingLiveTest.java b/spring-5/src/test/java/org/baeldung/okhttp/OkHttpFileUploadingLiveTest.java deleted file mode 100644 index a33742b8de..0000000000 --- a/spring-5/src/test/java/org/baeldung/okhttp/OkHttpFileUploadingLiveTest.java +++ /dev/null @@ -1,65 +0,0 @@ -package org.baeldung.okhttp; - -import static org.baeldung.client.Consts.APPLICATION_PORT; -import static org.hamcrest.Matchers.equalTo; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertThat; - -import java.io.File; -import java.io.IOException; - -import okhttp3.Call; -import okhttp3.MediaType; -import okhttp3.MultipartBody; -import okhttp3.OkHttpClient; -import okhttp3.Request; -import okhttp3.RequestBody; -import okhttp3.Response; - -import org.junit.Before; -import org.junit.Test; - -public class OkHttpFileUploadingLiveTest { - - private static final String BASE_URL = "http://localhost:" + APPLICATION_PORT + "/spring-rest"; - - OkHttpClient client; - - @Before - public void init() { - client = new OkHttpClient(); - } - - @Test - public void whenUploadFile_thenCorrect() throws IOException { - - final RequestBody requestBody = new MultipartBody.Builder().setType(MultipartBody.FORM).addFormDataPart("file", "file.txt", RequestBody.create(MediaType.parse("application/octet-stream"), new File("src/test/resources/test.txt"))).build(); - - final Request request = new Request.Builder().url(BASE_URL + "/users/upload").post(requestBody).build(); - - final Call call = client.newCall(request); - final Response response = call.execute(); - - assertThat(response.code(), equalTo(200)); - } - - @Test - public void whenGetUploadFileProgress_thenCorrect() throws IOException { - - final RequestBody requestBody = new MultipartBody.Builder().setType(MultipartBody.FORM).addFormDataPart("file", "file.txt", RequestBody.create(MediaType.parse("application/octet-stream"), new File("src/test/resources/test.txt"))).build(); - - final ProgressRequestWrapper countingBody = new ProgressRequestWrapper(requestBody, (long bytesWritten, long contentLength) -> { - - final float percentage = (100f * bytesWritten) / contentLength; - assertFalse(Float.compare(percentage, 100) > 0); - }); - - final Request request = new Request.Builder().url(BASE_URL + "/users/upload").post(countingBody).build(); - - final Call call = client.newCall(request); - final Response response = call.execute(); - - assertThat(response.code(), equalTo(200)); - - } -} diff --git a/spring-5/src/test/java/org/baeldung/okhttp/OkHttpGetLiveTest.java b/spring-5/src/test/java/org/baeldung/okhttp/OkHttpGetLiveTest.java deleted file mode 100644 index 6aa33b06b1..0000000000 --- a/spring-5/src/test/java/org/baeldung/okhttp/OkHttpGetLiveTest.java +++ /dev/null @@ -1,77 +0,0 @@ -package org.baeldung.okhttp; - -import static org.baeldung.client.Consts.APPLICATION_PORT; -import static org.hamcrest.CoreMatchers.equalTo; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.fail; - -import java.io.IOException; - -import okhttp3.Call; -import okhttp3.Callback; -import okhttp3.HttpUrl; -import okhttp3.OkHttpClient; -import okhttp3.Request; -import okhttp3.Response; - -import org.junit.Before; -import org.junit.Test; - -public class OkHttpGetLiveTest { - - private static final String BASE_URL = "http://localhost:" + APPLICATION_PORT + "/spring-rest"; - - OkHttpClient client; - - @Before - public void init() { - - client = new OkHttpClient(); - } - - @Test - public void whenGetRequest_thenCorrect() throws IOException { - final Request request = new Request.Builder().url(BASE_URL + "/date").build(); - - final Call call = client.newCall(request); - final Response response = call.execute(); - - assertThat(response.code(), equalTo(200)); - } - - @Test - public void whenGetRequestWithQueryParameter_thenCorrect() throws IOException { - final HttpUrl.Builder urlBuilder = HttpUrl.parse(BASE_URL + "/ex/bars").newBuilder(); - urlBuilder.addQueryParameter("id", "1"); - - final String url = urlBuilder.build().toString(); - - final Request request = new Request.Builder().url(url).build(); - - final Call call = client.newCall(request); - final Response response = call.execute(); - - assertThat(response.code(), equalTo(200)); - } - - @Test - public void whenAsynchronousGetRequest_thenCorrect() throws InterruptedException { - final Request request = new Request.Builder().url(BASE_URL + "/date").build(); - - final Call call = client.newCall(request); - - call.enqueue(new Callback() { - @Override - public void onResponse(Call call, Response response) throws IOException { - System.out.println("OK"); - } - - @Override - public void onFailure(Call call, IOException e) { - fail(); - } - }); - - Thread.sleep(3000); - } -} diff --git a/spring-5/src/test/java/org/baeldung/okhttp/OkHttpHeaderLiveTest.java b/spring-5/src/test/java/org/baeldung/okhttp/OkHttpHeaderLiveTest.java deleted file mode 100644 index cfec119fe0..0000000000 --- a/spring-5/src/test/java/org/baeldung/okhttp/OkHttpHeaderLiveTest.java +++ /dev/null @@ -1,45 +0,0 @@ -package org.baeldung.okhttp; - -import java.io.IOException; - -import org.junit.Before; -import org.junit.Test; - -import okhttp3.Call; -import okhttp3.OkHttpClient; -import okhttp3.Request; -import okhttp3.Response; - -public class OkHttpHeaderLiveTest { - - private static final String SAMPLE_URL = "http://www.github.com"; - - OkHttpClient client; - - @Before - public void init() { - - client = new OkHttpClient(); - } - - @Test - public void whenSetHeader_thenCorrect() throws IOException { - Request request = new Request.Builder().url(SAMPLE_URL).addHeader("Content-Type", "application/json").build(); - - Call call = client.newCall(request); - Response response = call.execute(); - response.close(); - } - - @Test - public void whenSetDefaultHeader_thenCorrect() throws IOException { - - OkHttpClient clientWithInterceptor = new OkHttpClient.Builder().addInterceptor(new DefaultContentTypeInterceptor("application/json")).build(); - - Request request = new Request.Builder().url(SAMPLE_URL).build(); - - Call call = clientWithInterceptor.newCall(request); - Response response = call.execute(); - response.close(); - } -} diff --git a/spring-5/src/test/java/org/baeldung/okhttp/OkHttpMiscLiveTest.java b/spring-5/src/test/java/org/baeldung/okhttp/OkHttpMiscLiveTest.java deleted file mode 100644 index 52662262e1..0000000000 --- a/spring-5/src/test/java/org/baeldung/okhttp/OkHttpMiscLiveTest.java +++ /dev/null @@ -1,99 +0,0 @@ -package org.baeldung.okhttp; - -import static org.baeldung.client.Consts.APPLICATION_PORT; - -import java.io.File; -import java.io.IOException; -import java.net.SocketTimeoutException; -import java.util.concurrent.Executors; -import java.util.concurrent.ScheduledExecutorService; -import java.util.concurrent.TimeUnit; - -import okhttp3.Cache; -import okhttp3.Call; -import okhttp3.OkHttpClient; -import okhttp3.Request; -import okhttp3.Response; - -import org.junit.Before; -import org.junit.Test; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class OkHttpMiscLiveTest { - - private static final String BASE_URL = "http://localhost:" + APPLICATION_PORT + "/spring-rest"; - private static Logger logger = LoggerFactory.getLogger(OkHttpMiscLiveTest.class); - - OkHttpClient client; - - @Before - public void init() { - - client = new OkHttpClient(); - } - - @Test(expected = SocketTimeoutException.class) - public void whenSetRequestTimeout_thenFail() throws IOException { - final OkHttpClient clientWithTimeout = new OkHttpClient.Builder().readTimeout(1, TimeUnit.SECONDS).build(); - - final Request request = new Request.Builder().url(BASE_URL + "/delay/2") // This URL is served with a 2 second delay. - .build(); - - final Call call = clientWithTimeout.newCall(request); - final Response response = call.execute(); - response.close(); - } - - @Test(expected = IOException.class) - public void whenCancelRequest_thenCorrect() throws IOException { - final ScheduledExecutorService executor = Executors.newScheduledThreadPool(1); - - final Request request = new Request.Builder().url(BASE_URL + "/delay/2") // This URL is served with a 2 second delay. - .build(); - - final int seconds = 1; - final long startNanos = System.nanoTime(); - - final Call call = client.newCall(request); - - // Schedule a job to cancel the call in 1 second. - executor.schedule(() -> { - - logger.debug("Canceling call: " + ((System.nanoTime() - startNanos) / 1e9f)); - call.cancel(); - logger.debug("Canceled call: " + ((System.nanoTime() - startNanos) / 1e9f)); - - }, seconds, TimeUnit.SECONDS); - - logger.debug("Executing call: " + ((System.nanoTime() - startNanos) / 1e9f)); - final Response response = call.execute(); - logger.debug("Call completed: " + ((System.nanoTime() - startNanos) / 1e9f), response); - } - - @Test - public void whenSetResponseCache_thenCorrect() throws IOException { - - final int cacheSize = 10 * 1024 * 1024; // 10 MiB - final File cacheDirectory = new File("src/test/resources/cache"); - final Cache cache = new Cache(cacheDirectory, cacheSize); - - final OkHttpClient clientCached = new OkHttpClient.Builder().cache(cache).build(); - - final Request request = new Request.Builder().url("http://publicobject.com/helloworld.txt").build(); - - final Response response1 = clientCached.newCall(request).execute(); - logResponse(response1); - - final Response response2 = clientCached.newCall(request).execute(); - logResponse(response2); - } - - private void logResponse(Response response) throws IOException { - - logger.debug("Response response: " + response); - logger.debug("Response cache response: " + response.cacheResponse()); - logger.debug("Response network response: " + response.networkResponse()); - logger.debug("Response responseBody: " + response.body().string()); - } -} diff --git a/spring-5/src/test/java/org/baeldung/okhttp/OkHttpPostingLiveTest.java b/spring-5/src/test/java/org/baeldung/okhttp/OkHttpPostingLiveTest.java deleted file mode 100644 index 77a78c2634..0000000000 --- a/spring-5/src/test/java/org/baeldung/okhttp/OkHttpPostingLiveTest.java +++ /dev/null @@ -1,85 +0,0 @@ -package org.baeldung.okhttp; - -import static org.baeldung.client.Consts.APPLICATION_PORT; -import static org.hamcrest.Matchers.equalTo; -import static org.junit.Assert.assertThat; - -import java.io.File; -import java.io.IOException; - -import okhttp3.Call; -import okhttp3.Credentials; -import okhttp3.FormBody; -import okhttp3.MediaType; -import okhttp3.MultipartBody; -import okhttp3.OkHttpClient; -import okhttp3.Request; -import okhttp3.RequestBody; -import okhttp3.Response; - -import org.junit.Before; -import org.junit.Test; - -public class OkHttpPostingLiveTest { - - private static final String BASE_URL = "http://localhost:" + APPLICATION_PORT + "/spring-rest"; - private static final String URL_SECURED_BY_BASIC_AUTHENTICATION = "http://browserspy.dk/password-ok.php"; - - OkHttpClient client; - - @Before - public void init() { - - client = new OkHttpClient(); - } - - @Test - public void whenSendPostRequest_thenCorrect() throws IOException { - final RequestBody formBody = new FormBody.Builder().add("username", "test").add("password", "test").build(); - - final Request request = new Request.Builder().url(BASE_URL + "/users").post(formBody).build(); - - final Call call = client.newCall(request); - final Response response = call.execute(); - - assertThat(response.code(), equalTo(200)); - } - - @Test - public void whenSendPostRequestWithAuthorization_thenCorrect() throws IOException { - final String postBody = "test post"; - - final Request request = new Request.Builder().url(URL_SECURED_BY_BASIC_AUTHENTICATION).addHeader("Authorization", Credentials.basic("test", "test")).post(RequestBody.create(MediaType.parse("text/x-markdown; charset=utf-8"), "test post")).build(); - - final Call call = client.newCall(request); - final Response response = call.execute(); - - assertThat(response.code(), equalTo(200)); - } - - @Test - public void whenPostJson_thenCorrect() throws IOException { - final String json = "{\"id\":1,\"name\":\"John\"}"; - - final RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), "{\"id\":1,\"name\":\"John\"}"); - final Request request = new Request.Builder().url(BASE_URL + "/users/detail").post(body).build(); - - final Call call = client.newCall(request); - final Response response = call.execute(); - - assertThat(response.code(), equalTo(200)); - } - - @Test - public void whenSendMultipartRequest_thenCorrect() throws IOException { - final RequestBody requestBody = new MultipartBody.Builder().setType(MultipartBody.FORM).addFormDataPart("username", "test").addFormDataPart("password", "test") - .addFormDataPart("file", "file.txt", RequestBody.create(MediaType.parse("application/octet-stream"), new File("src/test/resources/test.txt"))).build(); - - final Request request = new Request.Builder().url(BASE_URL + "/users/multipart").post(requestBody).build(); - - final Call call = client.newCall(request); - final Response response = call.execute(); - - assertThat(response.code(), equalTo(200)); - } -} diff --git a/spring-5/src/test/java/org/baeldung/okhttp/OkHttpRedirectLiveTest.java b/spring-5/src/test/java/org/baeldung/okhttp/OkHttpRedirectLiveTest.java deleted file mode 100644 index 58f84f06a2..0000000000 --- a/spring-5/src/test/java/org/baeldung/okhttp/OkHttpRedirectLiveTest.java +++ /dev/null @@ -1,29 +0,0 @@ -package org.baeldung.okhttp; - -import static org.hamcrest.Matchers.equalTo; -import static org.junit.Assert.assertThat; - -import java.io.IOException; - -import org.junit.Test; - -import okhttp3.Call; -import okhttp3.OkHttpClient; -import okhttp3.Request; -import okhttp3.Response; - -public class OkHttpRedirectLiveTest { - - @Test - public void whenSetFollowRedirects_thenNotRedirected() throws IOException { - - OkHttpClient client = new OkHttpClient().newBuilder().followRedirects(false).build(); - - Request request = new Request.Builder().url("http://t.co/I5YYd9tddw").build(); - - Call call = client.newCall(request); - Response response = call.execute(); - - assertThat(response.code(), equalTo(301)); - } -} diff --git a/spring-5/src/test/java/org/baeldung/okhttp/ProgressRequestWrapper.java b/spring-5/src/test/java/org/baeldung/okhttp/ProgressRequestWrapper.java deleted file mode 100644 index fcae69c609..0000000000 --- a/spring-5/src/test/java/org/baeldung/okhttp/ProgressRequestWrapper.java +++ /dev/null @@ -1,73 +0,0 @@ -package org.baeldung.okhttp; - -import okhttp3.RequestBody; -import okhttp3.MediaType; - -import java.io.IOException; - -import okio.Buffer; -import okio.BufferedSink; -import okio.ForwardingSink; -import okio.Okio; -import okio.Sink; - -public class ProgressRequestWrapper extends RequestBody { - - protected RequestBody delegate; - protected ProgressListener listener; - - protected CountingSink countingSink; - - public ProgressRequestWrapper(RequestBody delegate, ProgressListener listener) { - this.delegate = delegate; - this.listener = listener; - } - - @Override - public MediaType contentType() { - return delegate.contentType(); - } - - @Override - public long contentLength() throws IOException { - return delegate.contentLength(); - } - - @Override - public void writeTo(BufferedSink sink) throws IOException { - - BufferedSink bufferedSink; - - countingSink = new CountingSink(sink); - bufferedSink = Okio.buffer(countingSink); - - delegate.writeTo(bufferedSink); - - bufferedSink.flush(); - } - - protected final class CountingSink extends ForwardingSink { - - private long bytesWritten = 0; - - public CountingSink(Sink delegate) { - super(delegate); - } - - @Override - public void write(Buffer source, long byteCount) throws IOException { - - super.write(source, byteCount); - - bytesWritten += byteCount; - listener.onRequestProgress(bytesWritten, contentLength()); - } - - } - - public interface ProgressListener { - - void onRequestProgress(long bytesWritten, long contentLength); - - } -} diff --git a/spring-5/src/test/java/org/baeldung/uribuilder/SpringUriBuilderTest.java b/spring-5/src/test/java/org/baeldung/uribuilder/SpringUriBuilderTest.java deleted file mode 100644 index 84ae1063d9..0000000000 --- a/spring-5/src/test/java/org/baeldung/uribuilder/SpringUriBuilderTest.java +++ /dev/null @@ -1,49 +0,0 @@ -package org.baeldung.uribuilder; - -import static org.junit.Assert.assertEquals; - -import java.util.Collections; - -import org.junit.Test; -import org.springframework.web.util.UriComponents; -import org.springframework.web.util.UriComponentsBuilder; - -public class SpringUriBuilderTest { - - @Test - public void constructUri() { - UriComponents uriComponents = UriComponentsBuilder.newInstance().scheme("http").host("www.baeldung.com").path("/junit-5").build(); - - assertEquals("http://www.baeldung.com/junit-5", uriComponents.toUriString()); - } - - @Test - public void constructUriEncoded() { - UriComponents uriComponents = UriComponentsBuilder.newInstance().scheme("http").host("www.baeldung.com").path("/junit 5").build().encode(); - - assertEquals("http://www.baeldung.com/junit%205", uriComponents.toUriString()); - } - - @Test - public void constructUriFromTemplate() { - UriComponents uriComponents = UriComponentsBuilder.newInstance().scheme("http").host("www.baeldung.com").path("/{article-name}").buildAndExpand("junit-5"); - - assertEquals("http://www.baeldung.com/junit-5", uriComponents.toUriString()); - } - - @Test - public void constructUriWithQueryParameter() { - UriComponents uriComponents = UriComponentsBuilder.newInstance().scheme("http").host("www.google.com").path("/").query("q={keyword}").buildAndExpand("baeldung"); - - assertEquals("http://www.google.com/?q=baeldung", uriComponents.toUriString()); - } - - @Test - public void expandWithRegexVar() { - String template = "/myurl/{name:[a-z]{1,5}}/show"; - UriComponents uriComponents = UriComponentsBuilder.fromUriString(template).build(); - uriComponents = uriComponents.expand(Collections.singletonMap("name", "test")); - - assertEquals("/myurl/test/show", uriComponents.getPath()); - } -} diff --git a/spring-5/src/test/java/org/baeldung/web/controller/mediatypes/CustomMediaTypeControllerLiveTest.java b/spring-5/src/test/java/org/baeldung/web/controller/mediatypes/CustomMediaTypeControllerLiveTest.java deleted file mode 100644 index e8d5ff9f17..0000000000 --- a/spring-5/src/test/java/org/baeldung/web/controller/mediatypes/CustomMediaTypeControllerLiveTest.java +++ /dev/null @@ -1,37 +0,0 @@ -package org.baeldung.web.controller.mediatypes; - -import com.jayway.restassured.http.ContentType; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -import org.springframework.test.context.support.AnnotationConfigContextLoader; - -import static com.jayway.restassured.RestAssured.given; - -@RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration(classes = {TestConfig.class}, loader = AnnotationConfigContextLoader.class) -public class CustomMediaTypeControllerLiveTest { - private static final String URL_PREFIX = "http://localhost:8082/spring-rest"; - - @Test - public void givenServiceEndpoint_whenGetRequestFirstAPIVersion_thenShouldReturn200() { - given() - .accept("application/vnd.baeldung.api.v1+json") - .when() - .get(URL_PREFIX + "/public/api/items/1") - .then() - .contentType(ContentType.JSON).and().statusCode(200); - } - - - @Test - public void givenServiceEndpoint_whenGetRequestSecondAPIVersion_thenShouldReturn200() { - given() - .accept("application/vnd.baeldung.api.v2+json") - .when() - .get(URL_PREFIX + "/public/api/items/2") - .then() - .contentType(ContentType.JSON).and().statusCode(200); - } -} diff --git a/spring-5/src/test/java/org/baeldung/web/controller/mediatypes/CustomMediaTypeControllerTest.java b/spring-5/src/test/java/org/baeldung/web/controller/mediatypes/CustomMediaTypeControllerTest.java deleted file mode 100644 index a38177f78b..0000000000 --- a/spring-5/src/test/java/org/baeldung/web/controller/mediatypes/CustomMediaTypeControllerTest.java +++ /dev/null @@ -1,42 +0,0 @@ -package org.baeldung.web.controller.mediatypes; - -import org.baeldung.config.WebConfig; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -import org.springframework.test.context.web.WebAppConfiguration; -import org.springframework.test.web.servlet.MockMvc; -import org.springframework.test.web.servlet.setup.MockMvcBuilders; -import org.springframework.web.context.WebApplicationContext; - -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; - -@RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration(classes = WebConfig.class) -@WebAppConfiguration -public class CustomMediaTypeControllerTest { - - private MockMvc mockMvc; - - @Autowired - private WebApplicationContext webApplicationContext; - - @Before - public void setUp() { - mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build(); - } - - @Test - public void givenServiceUrl_whenGetWithProperAcceptHeaderFirstAPIVersion_thenReturn200() throws Exception { - mockMvc.perform(get("/public/api/items/1").accept("application/vnd.baeldung.api.v1+json")).andExpect(status().isOk()); - } - - @Test - public void givenServiceUrl_whenGetWithProperAcceptHeaderSecondVersion_thenReturn200() throws Exception { - mockMvc.perform(get("/public/api/items/2").accept("application/vnd.baeldung.api.v2+json")).andExpect(status().isOk()); - } -} \ No newline at end of file diff --git a/spring-5/src/test/java/org/baeldung/web/controller/mediatypes/TestConfig.java b/spring-5/src/test/java/org/baeldung/web/controller/mediatypes/TestConfig.java deleted file mode 100644 index 66ffe4947d..0000000000 --- a/spring-5/src/test/java/org/baeldung/web/controller/mediatypes/TestConfig.java +++ /dev/null @@ -1,11 +0,0 @@ -package org.baeldung.web.controller.mediatypes; - -import org.springframework.context.annotation.ComponentScan; -import org.springframework.context.annotation.Configuration; - - -@Configuration -@ComponentScan({ "org.baeldung.web" }) -public class TestConfig { - -} diff --git a/spring-5/src/test/java/org/baeldung/web/controller/redirect/RedirectControllerIntegrationTest.java b/spring-5/src/test/java/org/baeldung/web/controller/redirect/RedirectControllerIntegrationTest.java deleted file mode 100644 index c604db596a..0000000000 --- a/spring-5/src/test/java/org/baeldung/web/controller/redirect/RedirectControllerIntegrationTest.java +++ /dev/null @@ -1,66 +0,0 @@ -package org.baeldung.web.controller.redirect; - -import static org.hamcrest.CoreMatchers.equalTo; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.flash; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.forwardedUrl; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.redirectedUrl; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view; -import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup; - -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -import org.springframework.test.context.web.WebAppConfiguration; -import org.springframework.test.web.servlet.MockMvc; -import org.springframework.web.context.WebApplicationContext; - -@RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration("file:src/main/webapp/WEB-INF/api-servlet.xml") -@WebAppConfiguration -public class RedirectControllerIntegrationTest { - - private MockMvc mockMvc; - - @Autowired - protected WebApplicationContext wac; - - @Before - public void setup() { - mockMvc = webAppContextSetup(wac).build(); - } - - @Test - public void whenRedirectOnUrlWithUsingXMLConfig_thenStatusRedirectionAndRedirectedOnUrl() throws Exception { - mockMvc.perform(get("/redirectWithXMLConfig")).andExpect(status().is3xxRedirection()).andExpect(view().name("RedirectedUrl")).andExpect(model().attribute("attribute", equalTo("redirectWithXMLConfig"))) - .andExpect(redirectedUrl("redirectedUrl?attribute=redirectWithXMLConfig")); - } - - @Test - public void whenRedirectOnUrlWithUsingRedirectPrefix_thenStatusRedirectionAndRedirectedOnUrl() throws Exception { - mockMvc.perform(get("/redirectWithRedirectPrefix")).andExpect(status().is3xxRedirection()).andExpect(view().name("redirect:/redirectedUrl")).andExpect(model().attribute("attribute", equalTo("redirectWithRedirectPrefix"))) - .andExpect(redirectedUrl("/redirectedUrl?attribute=redirectWithRedirectPrefix")); - } - - @Test - public void whenRedirectOnUrlWithUsingRedirectAttributes_thenStatusRedirectionAndRedirectedOnUrlAndAddedAttributeToFlashScope() throws Exception { - mockMvc.perform(get("/redirectWithRedirectAttributes")).andExpect(status().is3xxRedirection()).andExpect(flash().attribute("flashAttribute", equalTo("redirectWithRedirectAttributes"))) - .andExpect(model().attribute("attribute", equalTo("redirectWithRedirectAttributes"))).andExpect(model().attribute("flashAttribute", equalTo(null))).andExpect(redirectedUrl("redirectedUrl?attribute=redirectWithRedirectAttributes")); - } - - @Test - public void whenRedirectOnUrlWithUsingRedirectView_thenStatusRedirectionAndRedirectedOnUrlAndAddedAttributeToFlashScope() throws Exception { - mockMvc.perform(get("/redirectWithRedirectView")).andExpect(status().is3xxRedirection()).andExpect(model().attribute("attribute", equalTo("redirectWithRedirectView"))).andExpect(redirectedUrl("redirectedUrl?attribute=redirectWithRedirectView")); - } - - @Test - public void whenRedirectOnUrlWithUsingForwardPrefix_thenStatusOkAndForwardedOnUrl() throws Exception { - mockMvc.perform(get("/forwardWithForwardPrefix")).andExpect(status().isOk()).andExpect(view().name("forward:/redirectedUrl")).andExpect(model().attribute("attribute", equalTo("redirectWithForwardPrefix"))).andExpect(forwardedUrl("/redirectedUrl")); - } - -} diff --git a/spring-5/src/test/java/org/baeldung/web/controller/status/ExampleControllerIntegrationTest.java b/spring-5/src/test/java/org/baeldung/web/controller/status/ExampleControllerIntegrationTest.java deleted file mode 100644 index e29bef501e..0000000000 --- a/spring-5/src/test/java/org/baeldung/web/controller/status/ExampleControllerIntegrationTest.java +++ /dev/null @@ -1,42 +0,0 @@ -package org.baeldung.web.controller.status; - -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; - -import org.baeldung.config.WebConfig; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -import org.springframework.test.context.web.WebAppConfiguration; -import org.springframework.test.web.servlet.MockMvc; -import org.springframework.test.web.servlet.setup.MockMvcBuilders; -import org.springframework.web.context.WebApplicationContext; - -@RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration(classes = WebConfig.class) -@WebAppConfiguration -public class ExampleControllerIntegrationTest { - - private MockMvc mockMvc; - - @Autowired - private WebApplicationContext webApplicationContext; - - @Before - public void setUp() { - mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build(); - } - - @Test - public void whenGetRequestSentToController_thenReturnsStatusNotAcceptable() throws Exception { - mockMvc.perform(get("/controller")).andExpect(status().isNotAcceptable()); - } - - @Test - public void whenGetRequestSentToException_thenReturnsStatusForbidden() throws Exception { - mockMvc.perform(get("/exception")).andExpect(status().isForbidden()); - } -} diff --git a/spring-5/src/test/java/org/baeldung/web/test/RequestMappingLiveTest.java b/spring-5/src/test/java/org/baeldung/web/test/RequestMappingLiveTest.java deleted file mode 100644 index 7828df7304..0000000000 --- a/spring-5/src/test/java/org/baeldung/web/test/RequestMappingLiveTest.java +++ /dev/null @@ -1,63 +0,0 @@ -package org.baeldung.web.test; - -import static org.hamcrest.Matchers.containsString; -import static org.hamcrest.Matchers.equalTo; - -import org.junit.Test; - -import com.jayway.restassured.RestAssured; - -public class RequestMappingLiveTest { - private static String BASE_URI = "http://localhost:8082/spring-rest/ex/"; - - @Test - public void givenSimplePath_whenGetFoos_thenOk() { - RestAssured.given().accept("text/html").get(BASE_URI + "foos").then().assertThat().body(equalTo("Simple Get some Foos")); - } - - @Test - public void whenPostFoos_thenOk() { - RestAssured.given().accept("text/html").post(BASE_URI + "foos").then().assertThat().body(equalTo("Post some Foos")); - } - - @Test - public void givenOneHeader_whenGetFoos_thenOk() { - RestAssured.given().accept("text/html").header("key", "val").get(BASE_URI + "foos").then().assertThat().body(equalTo("Get some Foos with Header")); - } - - @Test - public void givenMultipleHeaders_whenGetFoos_thenOk() { - RestAssured.given().accept("text/html").headers("key1", "val1", "key2", "val2").get(BASE_URI + "foos").then().assertThat().body(equalTo("Get some Foos with Header")); - } - - @Test - public void givenAcceptHeader_whenGetFoos_thenOk() { - RestAssured.given().accept("application/json").get(BASE_URI + "foos").then().assertThat().body(containsString("Get some Foos with Header New")); - } - - @Test - public void givenPathVariable_whenGetFoos_thenOk() { - RestAssured.given().accept("text/html").get(BASE_URI + "foos/1").then().assertThat().body(equalTo("Get a specific Foo with id=1")); - } - - @Test - public void givenMultiplePathVariable_whenGetFoos_thenOk() { - RestAssured.given().accept("text/html").get(BASE_URI + "foos/1/bar/2").then().assertThat().body(equalTo("Get a specific Bar with id=2 from a Foo with id=1")); - } - - @Test - public void givenPathVariable_whenGetBars_thenOk() { - RestAssured.given().accept("text/html").get(BASE_URI + "bars/1").then().assertThat().body(equalTo("Get a specific Bar with id=1")); - } - - @Test - public void givenParams_whenGetBars_thenOk() { - RestAssured.given().accept("text/html").get(BASE_URI + "bars?id=100&second=something").then().assertThat().body(equalTo("Get a specific Bar with id=100")); - } - - @Test - public void whenGetFoosOrBars_thenOk() { - RestAssured.given().accept("text/html").get(BASE_URI + "advanced/foos").then().assertThat().body(equalTo("Advanced - Get some Foos or Bars")); - RestAssured.given().accept("text/html").get(BASE_URI + "advanced/bars").then().assertThat().body(equalTo("Advanced - Get some Foos or Bars")); - } -} diff --git a/spring-5/src/test/java/org/baeldung/web/test/SpringHttpMessageConvertersLiveTest.java b/spring-5/src/test/java/org/baeldung/web/test/SpringHttpMessageConvertersLiveTest.java deleted file mode 100644 index 7f250653ab..0000000000 --- a/spring-5/src/test/java/org/baeldung/web/test/SpringHttpMessageConvertersLiveTest.java +++ /dev/null @@ -1,123 +0,0 @@ -package org.baeldung.web.test; - -import static org.hamcrest.Matchers.notNullValue; -import static org.junit.Assert.assertThat; - -import java.util.Arrays; - -import org.baeldung.config.converter.KryoHttpMessageConverter; -import org.baeldung.web.dto.Foo; -import org.baeldung.web.dto.FooProtos; -import org.junit.Assert; -import org.junit.Test; -import org.springframework.http.HttpEntity; -import org.springframework.http.HttpHeaders; -import org.springframework.http.HttpMethod; -import org.springframework.http.MediaType; -import org.springframework.http.ResponseEntity; -import org.springframework.http.converter.protobuf.ProtobufHttpMessageConverter; -import org.springframework.web.client.RestTemplate; - -/** - * Integration Test class. Tests methods hits the server's rest services. - */ -public class SpringHttpMessageConvertersLiveTest { - - private static String BASE_URI = "http://localhost:8082/spring-rest/"; - - /** - * Without specifying Accept Header, uses the default response from the - * server (in this case json) - */ - @Test - public void whenRetrievingAFoo_thenCorrect() { - final String URI = BASE_URI + "foos/{id}"; - - final RestTemplate restTemplate = new RestTemplate(); - final Foo resource = restTemplate.getForObject(URI, Foo.class, "1"); - - assertThat(resource, notNullValue()); - } - - @Test - public void givenConsumingXml_whenReadingTheFoo_thenCorrect() { - final String URI = BASE_URI + "foos/{id}"; - - final RestTemplate restTemplate = new RestTemplate(); - - final HttpHeaders headers = new HttpHeaders(); - headers.setAccept(Arrays.asList(MediaType.APPLICATION_XML)); - final HttpEntity entity = new HttpEntity(headers); - - final ResponseEntity response = restTemplate.exchange(URI, HttpMethod.GET, entity, Foo.class, "1"); - final Foo resource = response.getBody(); - - assertThat(resource, notNullValue()); - } - - @Test - public void givenConsumingJson_whenReadingTheFoo_thenCorrect() { - final String URI = BASE_URI + "foos/{id}"; - - final RestTemplate restTemplate = new RestTemplate(); - - final HttpHeaders headers = new HttpHeaders(); - headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON)); - final HttpEntity entity = new HttpEntity(headers); - - final ResponseEntity response = restTemplate.exchange(URI, HttpMethod.GET, entity, Foo.class, "1"); - final Foo resource = response.getBody(); - - assertThat(resource, notNullValue()); - } - - @Test - public void givenConsumingXml_whenWritingTheFoo_thenCorrect() { - final String URI = BASE_URI + "foos/{id}"; - final RestTemplate restTemplate = new RestTemplate(); - - final Foo resource = new Foo(4, "jason"); - final HttpHeaders headers = new HttpHeaders(); - headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON)); - headers.setContentType((MediaType.APPLICATION_XML)); - final HttpEntity entity = new HttpEntity(resource, headers); - - final ResponseEntity response = restTemplate.exchange(URI, HttpMethod.PUT, entity, Foo.class, resource.getId()); - final Foo fooResponse = response.getBody(); - - Assert.assertEquals(resource.getId(), fooResponse.getId()); - } - - @Test - public void givenConsumingProtobuf_whenReadingTheFoo_thenCorrect() { - final String URI = BASE_URI + "foos/{id}"; - - final RestTemplate restTemplate = new RestTemplate(); - restTemplate.setMessageConverters(Arrays.asList(new ProtobufHttpMessageConverter())); - final HttpHeaders headers = new HttpHeaders(); - headers.setAccept(Arrays.asList(ProtobufHttpMessageConverter.PROTOBUF)); - final HttpEntity entity = new HttpEntity(headers); - - final ResponseEntity response = restTemplate.exchange(URI, HttpMethod.GET, entity, FooProtos.Foo.class, "1"); - final FooProtos.Foo resource = response.getBody(); - - assertThat(resource, notNullValue()); - } - - @Test - public void givenConsumingKryo_whenReadingTheFoo_thenCorrect() { - final String URI = BASE_URI + "foos/{id}"; - - final RestTemplate restTemplate = new RestTemplate(); - restTemplate.setMessageConverters(Arrays.asList(new KryoHttpMessageConverter())); - final HttpHeaders headers = new HttpHeaders(); - headers.setAccept(Arrays.asList(KryoHttpMessageConverter.KRYO)); - final HttpEntity entity = new HttpEntity(headers); - - final ResponseEntity response = restTemplate.exchange(URI, HttpMethod.GET, entity, Foo.class, "1"); - final Foo resource = response.getBody(); - - assertThat(resource, notNullValue()); - } - -} diff --git a/spring-5/src/test/resources/.gitignore b/spring-5/src/test/resources/.gitignore deleted file mode 100644 index 83c05e60c8..0000000000 --- a/spring-5/src/test/resources/.gitignore +++ /dev/null @@ -1,13 +0,0 @@ -*.class - -#folders# -/target -/neoDb* -/data -/src/main/webapp/WEB-INF/classes -*/META-INF/* - -# Packaged files # -*.jar -*.war -*.ear \ No newline at end of file diff --git a/spring-5/src/test/resources/test.txt b/spring-5/src/test/resources/test.txt deleted file mode 100644 index 95d09f2b10..0000000000 --- a/spring-5/src/test/resources/test.txt +++ /dev/null @@ -1 +0,0 @@ -hello world \ No newline at end of file From f64e24e6de1e8c0295927af66094ac0275abb639 Mon Sep 17 00:00:00 2001 From: eugenp Date: Fri, 24 Feb 2017 14:08:44 +0200 Subject: [PATCH 44/68] fully working version of Spring 5 project --- spring-5/src/main/java/com/baeldung/Spring5Application.java | 6 +++--- spring-5/src/main/resources/application.properties | 1 + .../src/test/java/com/baeldung/Spring5ApplicationTests.java | 6 +++--- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/spring-5/src/main/java/com/baeldung/Spring5Application.java b/spring-5/src/main/java/com/baeldung/Spring5Application.java index 902af95afd..4f5d431b0f 100644 --- a/spring-5/src/main/java/com/baeldung/Spring5Application.java +++ b/spring-5/src/main/java/com/baeldung/Spring5Application.java @@ -6,7 +6,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Spring5Application { - public static void main(String[] args) { - SpringApplication.run(Spring5Application.class, args); - } + public static void main(String[] args) { + SpringApplication.run(Spring5Application.class, args); + } } diff --git a/spring-5/src/main/resources/application.properties b/spring-5/src/main/resources/application.properties index e69de29bb2..bafddced85 100644 --- a/spring-5/src/main/resources/application.properties +++ b/spring-5/src/main/resources/application.properties @@ -0,0 +1 @@ +server.port=8081 \ No newline at end of file diff --git a/spring-5/src/test/java/com/baeldung/Spring5ApplicationTests.java b/spring-5/src/test/java/com/baeldung/Spring5ApplicationTests.java index e2bfe3a880..537ec56a89 100644 --- a/spring-5/src/test/java/com/baeldung/Spring5ApplicationTests.java +++ b/spring-5/src/test/java/com/baeldung/Spring5ApplicationTests.java @@ -9,8 +9,8 @@ import org.springframework.test.context.junit4.SpringRunner; @SpringBootTest public class Spring5ApplicationTests { - @Test - public void contextLoads() { - } + @Test + public void contextLoads() { + } } From 355931f8260616faf25ef79f2c282d84fddc5733 Mon Sep 17 00:00:00 2001 From: eugenp Date: Fri, 24 Feb 2017 15:12:23 +0200 Subject: [PATCH 45/68] adding some simple persistence --- spring-5/pom.xml | 200 +++++++++--------- .../java/com/baeldung/Spring5Application.java | 1 + .../baeldung/persistence/DataSetupBean.java | 26 +++ .../baeldung/persistence/FooRepository.java | 10 + .../src/main/java/com/baeldung/web/Foo.java | 84 ++++++++ .../java/com/baeldung/web/FooController.java | 62 ++++++ .../src/main/resources/application.properties | 5 +- 7 files changed, 291 insertions(+), 97 deletions(-) create mode 100644 spring-5/src/main/java/com/baeldung/persistence/DataSetupBean.java create mode 100644 spring-5/src/main/java/com/baeldung/persistence/FooRepository.java create mode 100644 spring-5/src/main/java/com/baeldung/web/Foo.java create mode 100644 spring-5/src/main/java/com/baeldung/web/FooController.java diff --git a/spring-5/pom.xml b/spring-5/pom.xml index 3b1d6c712b..ab05918ae4 100644 --- a/spring-5/pom.xml +++ b/spring-5/pom.xml @@ -1,108 +1,116 @@ - 4.0.0 + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + 4.0.0 - com.baeldung - spring-5 - 0.0.1-SNAPSHOT - jar + com.baeldung + spring-5 + 0.0.1-SNAPSHOT + jar - spring-5 - + spring-5 + - - org.springframework.boot - spring-boot-starter-parent - 2.0.0.BUILD-SNAPSHOT - - + + org.springframework.boot + spring-boot-starter-parent + 2.0.0.BUILD-SNAPSHOT + + - - UTF-8 - UTF-8 - 1.8 - + + + org.springframework.boot + spring-boot-starter-data-jpa + + + org.springframework.boot + spring-boot-starter-security + + + org.springframework.boot + spring-boot-starter-validation + + + org.springframework.boot + spring-boot-starter-web + - - - org.springframework.boot - spring-boot-starter-data-jpa - - - org.springframework.boot - spring-boot-starter-security - - - org.springframework.boot - spring-boot-starter-validation - - - org.springframework.boot - spring-boot-starter-web - + + + org.apache.commons + commons-lang3 + - - org.springframework.boot - spring-boot-devtools - runtime - - - com.h2database - h2 - runtime - - - org.springframework.boot - spring-boot-starter-test - test - - + - - - - org.springframework.boot - spring-boot-maven-plugin - - - + + org.springframework.boot + spring-boot-devtools + runtime + + + com.h2database + h2 + runtime + + + org.springframework.boot + spring-boot-starter-test + test + + - - - spring-snapshots - Spring Snapshots - https://repo.spring.io/snapshot - - true - - - - spring-milestones - Spring Milestones - https://repo.spring.io/milestone - - false - - - - - - spring-snapshots - Spring Snapshots - https://repo.spring.io/snapshot - - true - - - - spring-milestones - Spring Milestones - https://repo.spring.io/milestone - - false - - - + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + + + spring-snapshots + Spring Snapshots + https://repo.spring.io/snapshot + + true + + + + spring-milestones + Spring Milestones + https://repo.spring.io/milestone + + false + + + + + + spring-snapshots + Spring Snapshots + https://repo.spring.io/snapshot + + true + + + + spring-milestones + Spring Milestones + https://repo.spring.io/milestone + + false + + + + + + UTF-8 + UTF-8 + 1.8 + diff --git a/spring-5/src/main/java/com/baeldung/Spring5Application.java b/spring-5/src/main/java/com/baeldung/Spring5Application.java index 4f5d431b0f..41b5c1eed1 100644 --- a/spring-5/src/main/java/com/baeldung/Spring5Application.java +++ b/spring-5/src/main/java/com/baeldung/Spring5Application.java @@ -9,4 +9,5 @@ public class Spring5Application { public static void main(String[] args) { SpringApplication.run(Spring5Application.class, args); } + } diff --git a/spring-5/src/main/java/com/baeldung/persistence/DataSetupBean.java b/spring-5/src/main/java/com/baeldung/persistence/DataSetupBean.java new file mode 100644 index 0000000000..7936a2b7af --- /dev/null +++ b/spring-5/src/main/java/com/baeldung/persistence/DataSetupBean.java @@ -0,0 +1,26 @@ +package com.baeldung.persistence; + +import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic; + +import java.util.stream.IntStream; + +import org.springframework.beans.factory.InitializingBean; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import com.baeldung.web.Foo; + +@Component +public class DataSetupBean implements InitializingBean { + + @Autowired + private FooRepository repo; + + // + + @Override + public void afterPropertiesSet() throws Exception { + IntStream.range(1, 20).forEach(i -> repo.save(new Foo(randomAlphabetic(8)))); + } + +} diff --git a/spring-5/src/main/java/com/baeldung/persistence/FooRepository.java b/spring-5/src/main/java/com/baeldung/persistence/FooRepository.java new file mode 100644 index 0000000000..1f1e071158 --- /dev/null +++ b/spring-5/src/main/java/com/baeldung/persistence/FooRepository.java @@ -0,0 +1,10 @@ +package com.baeldung.persistence; + +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.JpaSpecificationExecutor; + +import com.baeldung.web.Foo; + +public interface FooRepository extends JpaRepository, JpaSpecificationExecutor { + +} diff --git a/spring-5/src/main/java/com/baeldung/web/Foo.java b/spring-5/src/main/java/com/baeldung/web/Foo.java new file mode 100644 index 0000000000..c4868a9958 --- /dev/null +++ b/spring-5/src/main/java/com/baeldung/web/Foo.java @@ -0,0 +1,84 @@ +package com.baeldung.web; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; + +@Entity +public class Foo { + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private long id; + + private String name; + + public Foo() { + super(); + } + + public Foo(final String name) { + super(); + + this.name = name; + } + + public Foo(final long id, final String name) { + super(); + + this.id = id; + this.name = name; + } + + // API + + public long getId() { + return id; + } + + public void setId(final long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(final String name) { + this.name = name; + } + + // + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((name == null) ? 0 : name.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + Foo other = (Foo) obj; + if (name == null) { + if (other.name != null) + return false; + } else if (!name.equals(other.name)) + return false; + return true; + } + + @Override + public String toString() { + return "Foo [name=" + name + "]"; + } + +} \ No newline at end of file diff --git a/spring-5/src/main/java/com/baeldung/web/FooController.java b/spring-5/src/main/java/com/baeldung/web/FooController.java new file mode 100644 index 0000000000..de6928033e --- /dev/null +++ b/spring-5/src/main/java/com/baeldung/web/FooController.java @@ -0,0 +1,62 @@ +package com.baeldung.web; + +import java.util.List; + +import javax.validation.constraints.Max; +import javax.validation.constraints.Min; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; +import org.springframework.http.HttpStatus; +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.bind.annotation.ResponseStatus; +import org.springframework.web.bind.annotation.RestController; + +import com.baeldung.persistence.FooRepository; + +@RestController("/foos") +public class FooController { + + @Autowired + private FooRepository repo; + + // API - read + + @RequestMapping(method = RequestMethod.GET, value = "/foos/{id}") + @ResponseBody + @Validated + public Foo findById(@PathVariable @Min(0) final long id) { + return repo.findOne(id); + } + + @RequestMapping(method = RequestMethod.GET) + @ResponseBody + public List findAll() { + return repo.findAll(); + } + + @RequestMapping(params = { "page", "size" }, method = RequestMethod.GET) + @ResponseBody + @Validated + public List findPaginated(@RequestParam("page") @Min(0) final int page, @Max(100) @RequestParam("size") final int size) { + final Page resultPage = repo.findAll(new PageRequest(page, size)); + return resultPage.getContent(); + } + + // API - write + + @RequestMapping(method = RequestMethod.PUT, value = "/foos/{id}") + @ResponseStatus(HttpStatus.OK) + @ResponseBody + public Foo update(@PathVariable("id") final String id, @RequestBody final Foo foo) { + return foo; + } + +} diff --git a/spring-5/src/main/resources/application.properties b/spring-5/src/main/resources/application.properties index bafddced85..2e33b98523 100644 --- a/spring-5/src/main/resources/application.properties +++ b/spring-5/src/main/resources/application.properties @@ -1 +1,4 @@ -server.port=8081 \ No newline at end of file +server.port=8081 + +security.user.name=user +security.user.password=pass \ No newline at end of file From d67ace8e1e45a3b301a7358ae726f371468381dd Mon Sep 17 00:00:00 2001 From: Tomasz Lelek Date: Fri, 24 Feb 2017 16:19:28 +0100 Subject: [PATCH 46/68] BAEL-655 test for partially apllied function (#1227) * BAEL-655 test for partially apllied function * BAEL-655 example of tuple transformation --- jooq/src/test/java/com/baeldung/JOOLTest.java | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/jooq/src/test/java/com/baeldung/JOOLTest.java b/jooq/src/test/java/com/baeldung/JOOLTest.java index 18fca1f67a..13bf1a3ec3 100644 --- a/jooq/src/test/java/com/baeldung/JOOLTest.java +++ b/jooq/src/test/java/com/baeldung/JOOLTest.java @@ -1,8 +1,15 @@ package com.baeldung; +import junit.framework.Assert; import org.jooq.lambda.Seq; import org.jooq.lambda.Unchecked; +import org.jooq.lambda.function.Function1; +import org.jooq.lambda.function.Function2; +import org.jooq.lambda.tuple.Tuple; +import org.jooq.lambda.tuple.Tuple2; +import org.jooq.lambda.tuple.Tuple3; +import org.jooq.lambda.tuple.Tuple4; import org.junit.Test; import java.util.Arrays; @@ -203,4 +210,34 @@ public class JOOLTest { ); } + @Test + public void givenFunction_whenAppliedPartially_shouldAddNumberToPartialArgument() { + //given + Function2 addTwoNumbers = (v1, v2) -> v1 + v2; + addTwoNumbers.toBiFunction(); + Function1 addToTwo = addTwoNumbers.applyPartially(2); + + //when + Integer result = addToTwo.apply(5); + + //then + assertEquals(result, (Integer) 7); + } + + @Test + public void givenSeqOfTuples_whenTransformToLowerNumberOfTuples_shouldHaveProperResult() { + //given + Seq> personDetails = Seq.of(tuple("michael", "similar", 49), tuple("jodie", "variable", 43)); + Tuple2 tuple = tuple("winter", "summer"); + + //when + List> result = personDetails.map(t -> t.limit2().concat(tuple)).toList(); + + //then + assertEquals( + result, + Arrays.asList(tuple("michael", "similar", "winter", "summer"), tuple("jodie", "variable", "winter", "summer")) + ); + } + } From c7c3d822d94db058b8b2bb9f89ab8438a993c3c9 Mon Sep 17 00:00:00 2001 From: eugenp Date: Fri, 24 Feb 2017 18:04:53 +0200 Subject: [PATCH 47/68] testing work with JUnit 5 --- .../src/main/resources/application.properties | 4 ++- .../com/baeldung/IntegrationTestExample1.java | 29 +++++++++++++++++++ .../com/baeldung/IntegrationTestExample2.java | 29 +++++++++++++++++++ .../com/baeldung/ParallelTestExample.java | 24 +++++++++++++++ 4 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 spring-5/src/test/java/com/baeldung/IntegrationTestExample1.java create mode 100644 spring-5/src/test/java/com/baeldung/IntegrationTestExample2.java create mode 100644 spring-5/src/test/java/com/baeldung/ParallelTestExample.java diff --git a/spring-5/src/main/resources/application.properties b/spring-5/src/main/resources/application.properties index 2e33b98523..886ea1978b 100644 --- a/spring-5/src/main/resources/application.properties +++ b/spring-5/src/main/resources/application.properties @@ -1,4 +1,6 @@ server.port=8081 security.user.name=user -security.user.password=pass \ No newline at end of file +security.user.password=pass + +logging.level.root=INFO \ No newline at end of file diff --git a/spring-5/src/test/java/com/baeldung/IntegrationTestExample1.java b/spring-5/src/test/java/com/baeldung/IntegrationTestExample1.java new file mode 100644 index 0000000000..0a27be4a95 --- /dev/null +++ b/spring-5/src/test/java/com/baeldung/IntegrationTestExample1.java @@ -0,0 +1,29 @@ +package com.baeldung; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@SpringBootTest +public class IntegrationTestExample1 { + + @Test + public void test1a() { + block(3000); + } + + @Test + public void test1b() { + block(3000); + } + + public static void block(long ms) { + try { + Thread.sleep(ms); + } catch (InterruptedException e) { + System.out.println("Thread interrupted"); + } + } +} diff --git a/spring-5/src/test/java/com/baeldung/IntegrationTestExample2.java b/spring-5/src/test/java/com/baeldung/IntegrationTestExample2.java new file mode 100644 index 0000000000..0bb2d47ef5 --- /dev/null +++ b/spring-5/src/test/java/com/baeldung/IntegrationTestExample2.java @@ -0,0 +1,29 @@ +package com.baeldung; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@SpringBootTest +public class IntegrationTestExample2 { + + @Test + public void test1a() { + block(3000); + } + + @Test + public void test1b() { + block(3000); + } + + public static void block(long ms) { + try { + Thread.sleep(ms); + } catch (InterruptedException e) { + System.out.println("Thread Interrupted"); + } + } +} diff --git a/spring-5/src/test/java/com/baeldung/ParallelTestExample.java b/spring-5/src/test/java/com/baeldung/ParallelTestExample.java new file mode 100644 index 0000000000..e73b8d4649 --- /dev/null +++ b/spring-5/src/test/java/com/baeldung/ParallelTestExample.java @@ -0,0 +1,24 @@ +package com.baeldung; + +import org.junit.Test; +import org.junit.experimental.ParallelComputer; +import org.junit.runner.Computer; +import org.junit.runner.JUnitCore; + +public class ParallelTestExample { + + @Test + public void runTests() { + final Class[] classes = { IntegrationTestExample1.class, IntegrationTestExample2.class }; + + JUnitCore.runClasses(new Computer(), classes); + } + + @Test + public void runTestsInParallel() { + final Class[] classes = { IntegrationTestExample1.class, IntegrationTestExample2.class }; + + JUnitCore.runClasses(new ParallelComputer(true, true), classes); + } + +} \ No newline at end of file From 9c5ecd8f8db8392e0f4e94c7ba91cf41de52ebf1 Mon Sep 17 00:00:00 2001 From: Abhinab Kanrar Date: Fri, 24 Feb 2017 23:28:45 +0530 Subject: [PATCH 48/68] Adding spring-security-stormpath module (#1226) * rest with spark java * 4 * Update Application.java * indentation changes * spring @requestmapping shortcuts * removing spring requestmapping and pushing spring-mvc-java * Joining/Splitting Strings with Java and Stream API * adding more join/split functionality * changing package name * testcase change * adding webutils * adding testcase for WebUtils and ServletRequestUtils * adding testcase * spring-security-stormpath --- spring-security-stormpath/pom.xml | 85 +++++++++++++++++++ .../main/java/com/baeldung/Application.java | 25 ++++++ .../security/SecurityConfiguration.java | 24 ++++++ .../src/main/resources/application.properties | 6 ++ 4 files changed, 140 insertions(+) create mode 100644 spring-security-stormpath/pom.xml create mode 100644 spring-security-stormpath/src/main/java/com/baeldung/Application.java create mode 100644 spring-security-stormpath/src/main/java/com/baeldung/security/SecurityConfiguration.java create mode 100644 spring-security-stormpath/src/main/resources/application.properties diff --git a/spring-security-stormpath/pom.xml b/spring-security-stormpath/pom.xml new file mode 100644 index 0000000000..44c6595e64 --- /dev/null +++ b/spring-security-stormpath/pom.xml @@ -0,0 +1,85 @@ + + 4.0.0 + com.baeldung + spring-security-stormpath + war + 1.0-SNAPSHOT + spring-security-stormpath + http://maven.apache.org + + + + abhinabkanrar@gmail.com + Abhinab Kanrar + https://github.com/AbhinabKanrar + abhinabkanrar + + + + + UTF-8 + UTF-8 + 1.8 + + + + org.springframework.boot + spring-boot-starter-parent + 1.5.1.RELEASE + + + + + org.springframework.boot + spring-boot-starter-web + + + com.stormpath.spring + stormpath-default-spring-boot-starter + 1.5.4 + + + + + + spring-releases + Spring Releases + https://repo.spring.io/libs-release + + + + + spring-security-stormpath + + + src/main/resources + + + + + org.apache.maven.plugins + maven-compiler-plugin + + ${java.version} + ${java.version} + + + + org.springframework.boot + spring-boot-maven-plugin + + true + + + + + repackage + + + + + + + + diff --git a/spring-security-stormpath/src/main/java/com/baeldung/Application.java b/spring-security-stormpath/src/main/java/com/baeldung/Application.java new file mode 100644 index 0000000000..3d1409eaeb --- /dev/null +++ b/spring-security-stormpath/src/main/java/com/baeldung/Application.java @@ -0,0 +1,25 @@ +/** + * + */ +package com.baeldung; + +import org.springframework.boot.CommandLineRunner; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +/** + * @author abhinab + * + */ +@SpringBootApplication +public class Application implements CommandLineRunner { + + @Override + public void run(String... args) throws Exception { + } + + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + } + +} diff --git a/spring-security-stormpath/src/main/java/com/baeldung/security/SecurityConfiguration.java b/spring-security-stormpath/src/main/java/com/baeldung/security/SecurityConfiguration.java new file mode 100644 index 0000000000..5d75ecea8a --- /dev/null +++ b/spring-security-stormpath/src/main/java/com/baeldung/security/SecurityConfiguration.java @@ -0,0 +1,24 @@ +/** + * + */ +package com.baeldung.security; + +import org.springframework.context.annotation.Configuration; +import org.springframework.security.config.annotation.web.builders.HttpSecurity; +import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; + +import static com.stormpath.spring.config.StormpathWebSecurityConfigurer.stormpath; + +/** + * @author abhinab + * + */ +@Configuration +public class SecurityConfiguration extends WebSecurityConfigurerAdapter { + + @Override + protected void configure(HttpSecurity http) throws Exception { + http.apply(stormpath()); + } + +} diff --git a/spring-security-stormpath/src/main/resources/application.properties b/spring-security-stormpath/src/main/resources/application.properties new file mode 100644 index 0000000000..64a9ca456c --- /dev/null +++ b/spring-security-stormpath/src/main/resources/application.properties @@ -0,0 +1,6 @@ +security.basic.enabled = false + +stormpath.web.stormpathFilter.order = 0 + +stormpath.client.apiKey.id = 668HU0EOZQ7F4MT53ND2HSGBA +stormpath.client.apiKey.secret = RPTaYX07csTJR0AMKjM462KRdiP6q037kBWoDrBC3DI From 9dab68a48e32d632ecc120dd60d13b6a112cfe56 Mon Sep 17 00:00:00 2001 From: Tian Baoqiang Date: Sat, 25 Feb 2017 14:41:07 +0800 Subject: [PATCH 49/68] add redirection after login with Spring Security(BAEL-648) (#1214) --- spring-security-mvc-login/pom.xml | 15 ++- .../controller/SecuredResourceController.java | 17 ++++ .../RefererAuthenticationSuccessHandler.java | 13 +++ .../spring/RedirectionSecurityConfig.java | 44 +++++++++ .../RedirectionWebSecurityConfig.xml | 29 ++++++ .../RedirectionSecurityIntegrationTest.java | 94 +++++++++++++++++++ .../src/test/resources/mvc-servlet.xml | 8 ++ 7 files changed, 219 insertions(+), 1 deletion(-) create mode 100644 spring-security-mvc-login/src/main/java/org/baeldung/controller/SecuredResourceController.java create mode 100644 spring-security-mvc-login/src/main/java/org/baeldung/security/RefererAuthenticationSuccessHandler.java create mode 100644 spring-security-mvc-login/src/main/java/org/baeldung/spring/RedirectionSecurityConfig.java create mode 100644 spring-security-mvc-login/src/main/resources/RedirectionWebSecurityConfig.xml create mode 100644 spring-security-mvc-login/src/test/java/org/baeldung/security/RedirectionSecurityIntegrationTest.java create mode 100644 spring-security-mvc-login/src/test/resources/mvc-servlet.xml diff --git a/spring-security-mvc-login/pom.xml b/spring-security-mvc-login/pom.xml index 965f4fe1de..3809dc9f26 100644 --- a/spring-security-mvc-login/pom.xml +++ b/spring-security-mvc-login/pom.xml @@ -152,6 +152,19 @@ test + + org.springframework + spring-test + ${org.springframework.version} + test + + + org.springframework.security + spring-security-test + ${org.springframework.security.version} + test + + @@ -222,7 +235,7 @@ - 4.3.5.RELEASE + 4.3.6.RELEASE 4.2.1.RELEASE diff --git a/spring-security-mvc-login/src/main/java/org/baeldung/controller/SecuredResourceController.java b/spring-security-mvc-login/src/main/java/org/baeldung/controller/SecuredResourceController.java new file mode 100644 index 0000000000..4b68eee983 --- /dev/null +++ b/spring-security-mvc-login/src/main/java/org/baeldung/controller/SecuredResourceController.java @@ -0,0 +1,17 @@ +package org.baeldung.controller; + +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +@Controller +public class SecuredResourceController { + + @RequestMapping("/secured") + public void secureResource(HttpServletRequest request, HttpServletResponse response) { + System.out.println("accessing secured resource"); + } + +} diff --git a/spring-security-mvc-login/src/main/java/org/baeldung/security/RefererAuthenticationSuccessHandler.java b/spring-security-mvc-login/src/main/java/org/baeldung/security/RefererAuthenticationSuccessHandler.java new file mode 100644 index 0000000000..5b025d9fd1 --- /dev/null +++ b/spring-security-mvc-login/src/main/java/org/baeldung/security/RefererAuthenticationSuccessHandler.java @@ -0,0 +1,13 @@ +package org.baeldung.security; + +import org.springframework.security.web.authentication.AuthenticationSuccessHandler; +import org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler; + +public class RefererAuthenticationSuccessHandler extends SimpleUrlAuthenticationSuccessHandler implements AuthenticationSuccessHandler { + + public RefererAuthenticationSuccessHandler() { + super(); + setUseReferer(true); + } + +} \ No newline at end of file diff --git a/spring-security-mvc-login/src/main/java/org/baeldung/spring/RedirectionSecurityConfig.java b/spring-security-mvc-login/src/main/java/org/baeldung/spring/RedirectionSecurityConfig.java new file mode 100644 index 0000000000..b68e7eab50 --- /dev/null +++ b/spring-security-mvc-login/src/main/java/org/baeldung/spring/RedirectionSecurityConfig.java @@ -0,0 +1,44 @@ +package org.baeldung.spring; + +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Profile; +import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; +import org.springframework.security.config.annotation.web.builders.HttpSecurity; +import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; +import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; +import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler; + +@Configuration +//@ImportResource({ "classpath:RedirectionWebSecurityConfig.xml" }) +@EnableWebSecurity +@Profile("!https") +public class RedirectionSecurityConfig extends WebSecurityConfigurerAdapter { + + public RedirectionSecurityConfig() { + super(); + } + + @Override + protected void configure(final AuthenticationManagerBuilder auth) throws Exception { + auth + .inMemoryAuthentication() + .withUser("user1") + .password("user1Pass") + .roles("USER"); + } + + @Override + protected void configure(final HttpSecurity http) throws Exception { + http + .authorizeRequests() + .antMatchers("/login*") + .permitAll() + .anyRequest() + .authenticated() + .and() + .formLogin() + .successHandler(new SavedRequestAwareAuthenticationSuccessHandler()); + //.successHandler(new RefererAuthenticationSuccessHandler()) + } + +} diff --git a/spring-security-mvc-login/src/main/resources/RedirectionWebSecurityConfig.xml b/spring-security-mvc-login/src/main/resources/RedirectionWebSecurityConfig.xml new file mode 100644 index 0000000000..231b5ab57e --- /dev/null +++ b/spring-security-mvc-login/src/main/resources/RedirectionWebSecurityConfig.xml @@ -0,0 +1,29 @@ + + + + + + + + + + + + + + + + + + + + + + + diff --git a/spring-security-mvc-login/src/test/java/org/baeldung/security/RedirectionSecurityIntegrationTest.java b/spring-security-mvc-login/src/test/java/org/baeldung/security/RedirectionSecurityIntegrationTest.java new file mode 100644 index 0000000000..1d7fae8b60 --- /dev/null +++ b/spring-security-mvc-login/src/test/java/org/baeldung/security/RedirectionSecurityIntegrationTest.java @@ -0,0 +1,94 @@ +package org.baeldung.security; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.mock.web.MockHttpSession; +import org.springframework.security.core.userdetails.UserDetails; +import org.springframework.security.core.userdetails.UserDetailsService; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.web.WebAppConfiguration; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.MvcResult; +import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; +import org.springframework.web.context.WebApplicationContext; + +import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf; +import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.user; +import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.redirectedUrlPattern; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration({ "/RedirectionWebSecurityConfig.xml", "/mvc-servlet.xml" }) +@WebAppConfiguration +public class RedirectionSecurityIntegrationTest { + + @Autowired private WebApplicationContext context; + + @Autowired private UserDetailsService userDetailsService; + + private MockMvc mvc; + private UserDetails userDetails; + + @Before + public void setup() { + mvc = MockMvcBuilders + .webAppContextSetup(context) + .apply(springSecurity()) + .build(); + userDetails = userDetailsService.loadUserByUsername("user1"); + } + + @Test + public void givenSecuredResource_whenAccessUnauthenticated_thenRequiresAuthentication() throws Exception { + mvc + .perform(get("/secured")) + .andExpect(status().is3xxRedirection()) + .andExpect(redirectedUrlPattern("**/login")); + + } + + @Test + public void givenCredentials_whenAccessSecuredResource_thenSuccess() throws Exception { + mvc + .perform(get("/secured").with(user(userDetails))) + .andExpect(status().isOk()); + } + + @Test + public void givenAccessSecuredResource_whenAuthenticated_thenRedirectedBack() throws Exception { + MockHttpServletRequestBuilder securedResourceAccess = get("/secured"); + MvcResult unauthenticatedResult = mvc + .perform(securedResourceAccess) + .andExpect(status().is3xxRedirection()) + .andReturn(); + + MockHttpSession session = (MockHttpSession) unauthenticatedResult + .getRequest() + .getSession(); + String loginUrl = unauthenticatedResult + .getResponse() + .getRedirectedUrl(); + mvc + .perform(post(loginUrl) + .param("username", userDetails.getUsername()) + .param("password", userDetails.getPassword()) + .session(session) + .with(csrf())) + .andExpect(status().is3xxRedirection()) + .andExpect(redirectedUrlPattern("**/secured")) + .andReturn(); + + mvc + .perform(securedResourceAccess.session(session)) + .andExpect(status().isOk()); + + } + +} diff --git a/spring-security-mvc-login/src/test/resources/mvc-servlet.xml b/spring-security-mvc-login/src/test/resources/mvc-servlet.xml new file mode 100644 index 0000000000..aa5488b116 --- /dev/null +++ b/spring-security-mvc-login/src/test/resources/mvc-servlet.xml @@ -0,0 +1,8 @@ + + + + + + \ No newline at end of file From d260fb2ad63bcfcb6cfb4182295c66be96b08448 Mon Sep 17 00:00:00 2001 From: Aram89 Date: Sat, 25 Feb 2017 13:45:21 +0400 Subject: [PATCH 50/68] Added new module to demonstrate topic "Introduction to RabbitMQ". (#1197) --- pom.xml | 3 ++ rabbitmq/pom.xml | 43 +++++++++++++++++++ .../java/com/baeldung/consumer/Receiver.java | 31 +++++++++++++ .../java/com/baeldung/producer/Publisher.java | 27 ++++++++++++ 4 files changed, 104 insertions(+) create mode 100644 rabbitmq/pom.xml create mode 100644 rabbitmq/src/main/java/com/baeldung/consumer/Receiver.java create mode 100644 rabbitmq/src/main/java/com/baeldung/producer/Publisher.java diff --git a/pom.xml b/pom.xml index 9e95537775..1b7d0b419c 100644 --- a/pom.xml +++ b/pom.xml @@ -199,6 +199,9 @@ apache-velocity apache-solrj + rabbitmq + + diff --git a/rabbitmq/pom.xml b/rabbitmq/pom.xml new file mode 100644 index 0000000000..03f192e4e1 --- /dev/null +++ b/rabbitmq/pom.xml @@ -0,0 +1,43 @@ + + + + 4.0.0 + com.baeldung + rabbitmq + 0.1-SNAPSHOT + + rabbitmq + http://maven.apache.org + + + + com.rabbitmq + amqp-client + 3.6.6 + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + ${maven-compiler-plugin.version} + + 1.8 + 1.8 + + + + + + + + + UTF-8 + + 3.6.0 + + + \ No newline at end of file diff --git a/rabbitmq/src/main/java/com/baeldung/consumer/Receiver.java b/rabbitmq/src/main/java/com/baeldung/consumer/Receiver.java new file mode 100644 index 0000000000..d0612406e9 --- /dev/null +++ b/rabbitmq/src/main/java/com/baeldung/consumer/Receiver.java @@ -0,0 +1,31 @@ +package com.baeldung.consumer; + +import com.rabbitmq.client.*; + +import java.io.IOException; +import java.util.concurrent.TimeoutException; + +public class Receiver { + + private static final String QUEUE_NAME = "products_queue"; + + public static void main (String[] args) throws IOException, TimeoutException { + ConnectionFactory factory = new ConnectionFactory(); + factory.setHost("localhost"); + Connection connection = factory.newConnection(); + Channel channel = connection.createChannel(); + + channel.queueDeclare(QUEUE_NAME, false, false, false, null); + + Consumer consumer = new DefaultConsumer(channel) { + @Override + public void handleDelivery(String consumerTag, + Envelope envelope, AMQP.BasicProperties properties, + byte[] body) throws IOException { + String message = new String(body, "UTF-8"); + System.out.println(" [x] Received '" + message + "'"); + } + }; + channel.basicConsume(QUEUE_NAME, true, consumer); + } +} diff --git a/rabbitmq/src/main/java/com/baeldung/producer/Publisher.java b/rabbitmq/src/main/java/com/baeldung/producer/Publisher.java new file mode 100644 index 0000000000..f9130c5d86 --- /dev/null +++ b/rabbitmq/src/main/java/com/baeldung/producer/Publisher.java @@ -0,0 +1,27 @@ +package com.baeldung.producer; + +import com.rabbitmq.client.*; + +import java.io.IOException; +import java.util.concurrent.TimeoutException; + +public class Publisher { + + private final static String QUEUE_NAME = "products_queue"; + + public static void main(String[]args) throws IOException, TimeoutException { + ConnectionFactory factory = new ConnectionFactory(); + factory.setHost("localhost"); + Connection connection = factory.newConnection(); + Channel channel = connection.createChannel(); + + String message = "product details"; + channel.queueDeclare(QUEUE_NAME, false, false, false, null); + + channel.basicPublish("", QUEUE_NAME, null, message.getBytes()); + System.out.println(" [x] Sent '" + message + "'"); + + channel.close(); + connection.close(); + } +} From afee31e0814d79239978958440a3d11c6375e926 Mon Sep 17 00:00:00 2001 From: Daniele Demichelis Date: Sat, 25 Feb 2017 17:22:25 +0100 Subject: [PATCH 51/68] BAEL-554 - JUnit test that run server and clients (#1233) * Burlap & Hessian server added * Burlap & Hessian client work * Fixed main * Fixed formatting * Spring Remote example based on Burlap & Hessian runs in a JUnit test * Fixed main * Fixed formatting * Spring Remote example based on Burlap & Hessian runs in a JUnit test --- .../remoting-hessian-burlap/client/pom.xml | 24 ++++++ .../client/CabBookingServiceTest.java | 73 +++++++++++++++++++ .../src/test/resources/application.properties | 1 + .../remoting-hessian-burlap/server/pom.xml | 2 +- 4 files changed, 99 insertions(+), 1 deletion(-) create mode 100644 spring-remoting/remoting-hessian-burlap/client/src/test/java/com/baeldung/client/CabBookingServiceTest.java create mode 100644 spring-remoting/remoting-hessian-burlap/client/src/test/resources/application.properties diff --git a/spring-remoting/remoting-hessian-burlap/client/pom.xml b/spring-remoting/remoting-hessian-burlap/client/pom.xml index 11250e63d2..1ae9b10019 100644 --- a/spring-remoting/remoting-hessian-burlap/client/pom.xml +++ b/spring-remoting/remoting-hessian-burlap/client/pom.xml @@ -31,5 +31,29 @@ hessian 4.0.38 + + + + ${project.groupId} + remoting-hessian-burlap-server + ${project.version} + test + + + javax.servlet + javax.servlet-api + test + + + org.springframework.boot + spring-boot-starter-test + test + + + org.springframework.boot + spring-boot-starter-tomcat + test + + \ No newline at end of file diff --git a/spring-remoting/remoting-hessian-burlap/client/src/test/java/com/baeldung/client/CabBookingServiceTest.java b/spring-remoting/remoting-hessian-burlap/client/src/test/java/com/baeldung/client/CabBookingServiceTest.java new file mode 100644 index 0000000000..20746bbf09 --- /dev/null +++ b/spring-remoting/remoting-hessian-burlap/client/src/test/java/com/baeldung/client/CabBookingServiceTest.java @@ -0,0 +1,73 @@ +package com.baeldung.client; + +import com.baeldung.api.Booking; +import com.baeldung.api.BookingException; +import com.baeldung.api.CabBookingService; +import com.baeldung.server.Server; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.runner.RunWith; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +import static java.lang.Thread.sleep; + +@SpringBootTest(classes = {BurlapClient.class, HessianClient.class}) +@RunWith(SpringRunner.class) +public class CabBookingServiceTest { + + static Logger log = LoggerFactory.getLogger(CabBookingServiceTest.class); + @Autowired @Qualifier("burlapInvoker") CabBookingService burlapClient; + @Autowired @Qualifier("hessianInvoker") CabBookingService hessianClient; + static Thread serverThread; + + @BeforeClass + public static void startServer() throws InterruptedException { + serverThread = serverThread(); + log.info("Starting server."); + serverThread.start(); + sleep(4000); + } + + @org.junit.Test + public void bookACabWithBurlapClient() throws InterruptedException { + bookACab(this.burlapClient); + } + + @org.junit.Test + public void bookACabWithHessianClient() throws InterruptedException { + bookACab(this.hessianClient); + } + + private void bookACab(CabBookingService burlapClient) { + Booking booking; + try { + booking = burlapClient.bookRide("Duomo place"); + log.info("Booking success: {}", booking); + } catch (BookingException e) { + log.info("Booking failed: {}", e.getMessage()); + } + } + + @AfterClass + public static void stopServer() throws InterruptedException { + serverThread.interrupt(); + serverThread.join(); + log.info("Server terminated."); + } + + static Thread serverThread() { + Thread serverThread = new Thread(()-> { + log.info("Starting Burlap and Hessian server"); + Server.main(new String[]{}); + log.info("Burlap and Hessian server terminated"); + }); + serverThread.setDaemon(true); + return serverThread; + } + +} diff --git a/spring-remoting/remoting-hessian-burlap/client/src/test/resources/application.properties b/spring-remoting/remoting-hessian-burlap/client/src/test/resources/application.properties new file mode 100644 index 0000000000..13577dc391 --- /dev/null +++ b/spring-remoting/remoting-hessian-burlap/client/src/test/resources/application.properties @@ -0,0 +1 @@ +application.properties=9999 \ No newline at end of file diff --git a/spring-remoting/remoting-hessian-burlap/server/pom.xml b/spring-remoting/remoting-hessian-burlap/server/pom.xml index c97092b247..f1fed73ed8 100644 --- a/spring-remoting/remoting-hessian-burlap/server/pom.xml +++ b/spring-remoting/remoting-hessian-burlap/server/pom.xml @@ -15,7 +15,7 @@ com.baeldung spring-remoting-http-server - 1.0-SNAPSHOT + ${project.version} org.springframework.boot From 413bddc9935acd4ee688eb42e9326eb13aaea440 Mon Sep 17 00:00:00 2001 From: Mohamed Sanaulla Date: Sat, 25 Feb 2017 23:34:49 +0300 Subject: [PATCH 52/68] BAEL-654: Java 9 Process API Improvements (#1235) * code for BAEL-654: Java 9 Process API Improvements * incorporate review --- core-java-9/pom.xml | 9 +- .../process/ProcessAPIEnhancementsTest.java | 133 ++++++++++++++++++ 2 files changed, 140 insertions(+), 2 deletions(-) create mode 100644 core-java-9/src/test/java/com/baeldung/java9/process/ProcessAPIEnhancementsTest.java diff --git a/core-java-9/pom.xml b/core-java-9/pom.xml index 9d1ff29ef7..23473ff161 100644 --- a/core-java-9/pom.xml +++ b/core-java-9/pom.xml @@ -21,6 +21,11 @@ slf4j-api ${org.slf4j.version} + + ch.qos.logback + logback-classic + ${ch.qos.logback.version} + org.hamcrest @@ -76,9 +81,9 @@ 1.7.21 - + 1.2.1 - 3.6-jigsaw-SNAPSHOT + 3.6.0 2.19.1 diff --git a/core-java-9/src/test/java/com/baeldung/java9/process/ProcessAPIEnhancementsTest.java b/core-java-9/src/test/java/com/baeldung/java9/process/ProcessAPIEnhancementsTest.java new file mode 100644 index 0000000000..1129a10d17 --- /dev/null +++ b/core-java-9/src/test/java/com/baeldung/java9/process/ProcessAPIEnhancementsTest.java @@ -0,0 +1,133 @@ +package com.baeldung.java9.process; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.io.IOException; +import java.util.concurrent.CompletableFuture; +import java.util.stream.Stream; + +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Created by sanaulla on 2/23/2017. + */ + +public class ProcessAPIEnhancementsTest { + + Logger log = LoggerFactory.getLogger(ProcessAPIEnhancementsTest.class); + + @Test + public void givenCurrentProcess_whenInvokeGetInfo_thenSuccess() throws IOException { + ProcessHandle processHandle = ProcessHandle.current(); + ProcessHandle.Info processInfo = processHandle.info(); + assertNotNull(processHandle.getPid()); + assertEquals(false, processInfo.arguments() + .isPresent()); + assertEquals(true, processInfo.command() + .isPresent()); + assertTrue(processInfo.command() + .get() + .contains("java")); + + assertEquals(true, processInfo.startInstant() + .isPresent()); + assertEquals(true, processInfo.totalCpuDuration() + .isPresent()); + assertEquals(true, processInfo.user() + .isPresent()); + } + + @Test + public void givenSpawnProcess_whenInvokeGetInfo_thenSuccess() throws IOException { + + String javaCmd = ProcessUtils.getJavaCmd() + .getAbsolutePath(); + ProcessBuilder processBuilder = new ProcessBuilder(javaCmd, "-version"); + Process process = processBuilder.inheritIO() + .start(); + ProcessHandle processHandle = process.toHandle(); + ProcessHandle.Info processInfo = processHandle.info(); + assertNotNull(processHandle.getPid()); + assertEquals(false, processInfo.arguments() + .isPresent()); + assertEquals(true, processInfo.command() + .isPresent()); + assertTrue(processInfo.command() + .get() + .contains("java")); + assertEquals(true, processInfo.startInstant() + .isPresent()); + assertEquals(true, processInfo.totalCpuDuration() + .isPresent()); + assertEquals(true, processInfo.user() + .isPresent()); + } + + @Test + public void givenLiveProcesses_whenInvokeGetInfo_thenSuccess() { + Stream liveProcesses = ProcessHandle.allProcesses(); + liveProcesses.filter(ProcessHandle::isAlive) + .forEach(ph -> { + assertNotNull(ph.getPid()); + assertEquals(true, ph.info() + .command() + .isPresent()); + assertEquals(true, ph.info() + .startInstant() + .isPresent()); + assertEquals(true, ph.info() + .totalCpuDuration() + .isPresent()); + assertEquals(true, ph.info() + .user() + .isPresent()); + }); + } + + @Test + public void givenProcess_whenGetChildProcess_thenSuccess() throws IOException { + int childProcessCount = 5; + for (int i = 0; i < childProcessCount; i++) { + String javaCmd = ProcessUtils.getJavaCmd() + .getAbsolutePath(); + ProcessBuilder processBuilder + = new ProcessBuilder(javaCmd, "-version"); + processBuilder.inheritIO().start(); + } + + Stream children = ProcessHandle.current() + .children(); + children.filter(ProcessHandle::isAlive) + .forEach(ph -> log.info("PID: {}, Cmd: {}", ph.getPid(), ph.info() + .command())); + Stream descendants = ProcessHandle.current() + .descendants(); + descendants.filter(ProcessHandle::isAlive) + .forEach(ph -> log.info("PID: {}, Cmd: {}", ph.getPid(), ph.info() + .command())); + } + + @Test + public void givenProcess_whenAddExitCallback_thenSuccess() throws Exception { + String javaCmd = ProcessUtils.getJavaCmd() + .getAbsolutePath(); + ProcessBuilder processBuilder + = new ProcessBuilder(javaCmd, "-version"); + Process process = processBuilder.inheritIO() + .start(); + ProcessHandle processHandle = process.toHandle(); + + log.info("PID: {} has started", processHandle.getPid()); + CompletableFuture onProcessExit = processHandle.onExit(); + onProcessExit.get(); + assertEquals(false, processHandle.isAlive()); + onProcessExit.thenAccept(ph -> { + log.info("PID: {} has stopped", ph.getPid()); + }); + } + +} From d04bed81e5869a7953beaabb1bd5ca9c6b48baaf Mon Sep 17 00:00:00 2001 From: KevinGilmore Date: Sat, 25 Feb 2017 16:40:42 -0600 Subject: [PATCH 53/68] BAEL-278: Updated README.md (#1238) --- algorithms/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/algorithms/README.md b/algorithms/README.md index 4789768fad..42f696d9be 100644 --- a/algorithms/README.md +++ b/algorithms/README.md @@ -1,3 +1,4 @@ ## Relevant articles: - [Dijkstra Algorithm in Java](http://www.baeldung.com/java-dijkstra) +- [Introduction to Cobertura](http://www.baeldung.com/cobertura) From db5dc24fdb1e542912e747b20b7ec8f413ee30a7 Mon Sep 17 00:00:00 2001 From: baljeet20 Date: Sun, 26 Feb 2017 13:51:59 +0530 Subject: [PATCH 54/68] BAEL-700 Guide to Java Lock API (#1239) * BAEL-604 Introduction to apache velocity * BAEL-604 Introduction to apache velocity * BAEL-700 Guide to Java Lock APIs * BAEL-700 Guide to Java Lock APIs --- .../locks/ReentrantLockWithCondition.java | 83 ++++++++++++ .../locks/SharedObjectWithLock.java | 92 ++++++++++++++ .../concurrent/locks/StampedLockDemo.java | 104 +++++++++++++++ .../locks/SynchronizedHashMapWithRWLock.java | 120 ++++++++++++++++++ .../locks/SharedObjectWithLockManualTest.java | 75 +++++++++++ ...nchronizedHashMapWithRWLockManualTest.java | 58 +++++++++ 6 files changed, 532 insertions(+) create mode 100644 core-java/src/main/java/com/baeldung/concurrent/locks/ReentrantLockWithCondition.java create mode 100644 core-java/src/main/java/com/baeldung/concurrent/locks/SharedObjectWithLock.java create mode 100644 core-java/src/main/java/com/baeldung/concurrent/locks/StampedLockDemo.java create mode 100644 core-java/src/main/java/com/baeldung/concurrent/locks/SynchronizedHashMapWithRWLock.java create mode 100644 core-java/src/test/java/com/baeldung/concurrent/locks/SharedObjectWithLockManualTest.java create mode 100644 core-java/src/test/java/com/baeldung/concurrent/locks/SynchronizedHashMapWithRWLockManualTest.java diff --git a/core-java/src/main/java/com/baeldung/concurrent/locks/ReentrantLockWithCondition.java b/core-java/src/main/java/com/baeldung/concurrent/locks/ReentrantLockWithCondition.java new file mode 100644 index 0000000000..4f061d2efd --- /dev/null +++ b/core-java/src/main/java/com/baeldung/concurrent/locks/ReentrantLockWithCondition.java @@ -0,0 +1,83 @@ +package com.baeldung.concurrent.locks; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Stack; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.locks.Condition; +import java.util.concurrent.locks.ReentrantLock; + +import static java.lang.Thread.sleep; + +public class ReentrantLockWithCondition { + + static Logger logger = LoggerFactory.getLogger(ReentrantLockWithCondition.class); + + Stack stack = new Stack<>(); + int CAPACITY = 5; + + ReentrantLock lock = new ReentrantLock(); + Condition stackEmptyCondition = lock.newCondition(); + Condition stackFullCondition = lock.newCondition(); + + public void pushToStack(String item) throws InterruptedException { + try { + lock.lock(); + if (stack.size() == CAPACITY) { + logger.info(Thread.currentThread().getName() + " wait on stack full"); + stackFullCondition.await(); + } + logger.info("Pushing the item " + item); + stack.push(item); + stackEmptyCondition.signalAll(); + } finally { + lock.unlock(); + } + + } + + public String popFromStack() throws InterruptedException { + try { + lock.lock(); + if (stack.size() == 0) { + logger.info(Thread.currentThread().getName() + " wait on stack empty"); + stackEmptyCondition.await(); + } + return stack.pop(); + } finally { + stackFullCondition.signalAll(); + lock.unlock(); + } + } + + public static void main(String[] args) { + final int threadCount = 2; + ReentrantLockWithCondition object = new ReentrantLockWithCondition(); + final ExecutorService service = Executors.newFixedThreadPool(threadCount); + service.execute(() -> { + for (int i = 0; i < 10; i++) { + try { + object.pushToStack("Item " + i); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + + }); + + service.execute(() -> { + for (int i = 0; i < 10; i++) { + try { + logger.info("Item popped " + object.popFromStack()); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + + }); + + service.shutdown(); + } +} diff --git a/core-java/src/main/java/com/baeldung/concurrent/locks/SharedObjectWithLock.java b/core-java/src/main/java/com/baeldung/concurrent/locks/SharedObjectWithLock.java new file mode 100644 index 0000000000..b6a4615638 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/concurrent/locks/SharedObjectWithLock.java @@ -0,0 +1,92 @@ +package com.baeldung.concurrent.locks; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.locks.ReentrantLock; + +import static java.lang.Thread.sleep; + +public class SharedObjectWithLock { + + Logger logger = LoggerFactory.getLogger(SharedObjectWithLock.class); + + ReentrantLock lock = new ReentrantLock(true); + + int counter = 0; + + public void perform() { + + lock.lock(); + logger.info("Thread - " + Thread.currentThread().getName() + " acquired the lock"); + try { + logger.info("Thread - " + Thread.currentThread().getName() + " processing"); + counter++; + } catch (Exception exception) { + logger.error(" Interrupted Exception ", exception); + } finally { + lock.unlock(); + logger.info("Thread - " + Thread.currentThread().getName() + " released the lock"); + } + } + + public void performTryLock() { + + logger.info("Thread - " + Thread.currentThread().getName() + " attempting to acquire the lock"); + try { + boolean isLockAcquired = lock.tryLock(2, TimeUnit.SECONDS); + if (isLockAcquired) { + try { + logger.info("Thread - " + Thread.currentThread().getName() + " acquired the lock"); + + logger.info("Thread - " + Thread.currentThread().getName() + " processing"); + sleep(1000); + } finally { + lock.unlock(); + logger.info("Thread - " + Thread.currentThread().getName() + " released the lock"); + + } + } + } catch (InterruptedException exception) { + logger.error(" Interrupted Exception ", exception); + } + logger.info("Thread - " + Thread.currentThread().getName() + " could not acquire the lock"); + } + + public ReentrantLock getLock() { + return lock; + } + + boolean isLocked() { + return lock.isLocked(); + } + + boolean hasQueuedThreads() { + return lock.hasQueuedThreads(); + } + + int getCounter() { + return counter; + } + + public static void main(String[] args) { + + final int threadCount = 2; + final ExecutorService service = Executors.newFixedThreadPool(threadCount); + final SharedObjectWithLock object = new SharedObjectWithLock(); + + service.execute(() -> { + object.perform(); + }); + service.execute(() -> { + object.performTryLock(); + }); + + service.shutdown(); + + } + +} diff --git a/core-java/src/main/java/com/baeldung/concurrent/locks/StampedLockDemo.java b/core-java/src/main/java/com/baeldung/concurrent/locks/StampedLockDemo.java new file mode 100644 index 0000000000..0b0dbc72cb --- /dev/null +++ b/core-java/src/main/java/com/baeldung/concurrent/locks/StampedLockDemo.java @@ -0,0 +1,104 @@ +package com.baeldung.concurrent.locks; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.locks.StampedLock; + +import static java.lang.Thread.sleep; + +public class StampedLockDemo { + Map map = new HashMap<>(); + Logger logger = LoggerFactory.getLogger(StampedLockDemo.class); + + private final StampedLock lock = new StampedLock(); + + public void put(String key, String value) throws InterruptedException { + long stamp = lock.writeLock(); + + try { + logger.info(Thread.currentThread().getName() + " acquired the write lock with stamp " + stamp); + map.put(key, value); + } finally { + lock.unlockWrite(stamp); + logger.info(Thread.currentThread().getName() + " unlocked the write lock with stamp " + stamp); + } + } + + public String get(String key) throws InterruptedException { + long stamp = lock.readLock(); + logger.info(Thread.currentThread().getName() + " acquired the read lock with stamp " + stamp); + try { + sleep(5000); + return map.get(key); + + } finally { + lock.unlockRead(stamp); + logger.info(Thread.currentThread().getName() + " unlocked the read lock with stamp " + stamp); + + } + + } + + public String readWithOptimisticLock(String key) throws InterruptedException { + long stamp = lock.tryOptimisticRead(); + String value = map.get(key); + + if (!lock.validate(stamp)) { + stamp = lock.readLock(); + try { + sleep(5000); + return map.get(key); + + } finally { + lock.unlock(stamp); + logger.info(Thread.currentThread().getName() + " unlocked the read lock with stamp " + stamp); + + } + } + return value; + } + + public static void main(String[] args) { + final int threadCount = 4; + final ExecutorService service = Executors.newFixedThreadPool(threadCount); + StampedLockDemo object = new StampedLockDemo(); + + Runnable writeTask = () -> { + + try { + object.put("key1", "value1"); + } catch (InterruptedException e) { + e.printStackTrace(); + } + }; + Runnable readTask = () -> { + + try { + object.get("key1"); + } catch (InterruptedException e) { + e.printStackTrace(); + } + }; + Runnable readOptimisticTask = () -> { + + try { + object.readWithOptimisticLock("key1"); + } catch (InterruptedException e) { + e.printStackTrace(); + } + }; + service.submit(writeTask); + service.submit(writeTask); + service.submit(readTask); + service.submit(readOptimisticTask); + + service.shutdown(); + + } + +} diff --git a/core-java/src/main/java/com/baeldung/concurrent/locks/SynchronizedHashMapWithRWLock.java b/core-java/src/main/java/com/baeldung/concurrent/locks/SynchronizedHashMapWithRWLock.java new file mode 100644 index 0000000000..83b8b34fe9 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/concurrent/locks/SynchronizedHashMapWithRWLock.java @@ -0,0 +1,120 @@ +package com.baeldung.concurrent.locks; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.locks.Lock; +import java.util.concurrent.locks.ReadWriteLock; +import java.util.concurrent.locks.ReentrantReadWriteLock; + +import static java.lang.Thread.sleep; + +public class SynchronizedHashMapWithRWLock { + + static Map syncHashMap = new HashMap<>(); + Logger logger = LoggerFactory.getLogger(SynchronizedHashMapWithRWLock.class); + + private final ReadWriteLock lock = new ReentrantReadWriteLock(); + private final Lock readLock = lock.readLock(); + private final Lock writeLock = lock.writeLock(); + + public void put(String key, String value) throws InterruptedException { + + try { + writeLock.lock(); + logger.info(Thread.currentThread().getName() + " writing"); + syncHashMap.put(key, value); + sleep(1000); + } finally { + writeLock.unlock(); + } + + } + + public String get(String key) { + try { + readLock.lock(); + logger.info(Thread.currentThread().getName() + " reading"); + return syncHashMap.get(key); + } finally { + readLock.unlock(); + } + } + + public String remove(String key) { + try { + writeLock.lock(); + return syncHashMap.remove(key); + } finally { + writeLock.unlock(); + } + } + + public boolean containsKey(String key) { + try { + readLock.lock(); + return syncHashMap.containsKey(key); + } finally { + readLock.unlock(); + } + } + + boolean isReadLockAvailable() { + return readLock.tryLock(); + } + + public static void main(String[] args) throws InterruptedException { + + final int threadCount = 3; + final ExecutorService service = Executors.newFixedThreadPool(threadCount); + SynchronizedHashMapWithRWLock object = new SynchronizedHashMapWithRWLock(); + + service.execute(new Thread(new Writer(object), "Writer")); + service.execute(new Thread(new Reader(object), "Reader1")); + service.execute(new Thread(new Reader(object), "Reader2")); + + service.shutdown(); + } + + private static class Reader implements Runnable { + + SynchronizedHashMapWithRWLock object; + + public Reader(SynchronizedHashMapWithRWLock object) { + this.object = object; + } + + @Override + public void run() { + for (int i = 0; i < 10; i++) { + object.get("key" + i); + } + } + } + + private static class Writer implements Runnable { + + SynchronizedHashMapWithRWLock object; + + public Writer(SynchronizedHashMapWithRWLock object) { + this.object = object; + } + + @Override + public void run() { + for (int i = 0; i < 10; i++) { + try { + object.put("key" + i, "value" + i); + sleep(1000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + } + } + +} diff --git a/core-java/src/test/java/com/baeldung/concurrent/locks/SharedObjectWithLockManualTest.java b/core-java/src/test/java/com/baeldung/concurrent/locks/SharedObjectWithLockManualTest.java new file mode 100644 index 0000000000..9b82ced642 --- /dev/null +++ b/core-java/src/test/java/com/baeldung/concurrent/locks/SharedObjectWithLockManualTest.java @@ -0,0 +1,75 @@ +package com.baeldung.concurrent.locks; + +import org.junit.Test; + +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; + +import static junit.framework.TestCase.assertEquals; + +public class SharedObjectWithLockManualTest { + + @Test + public void whenLockAcquired_ThenLockedIsTrue() { + final SharedObjectWithLock object = new SharedObjectWithLock(); + + final int threadCount = 2; + final ExecutorService service = Executors.newFixedThreadPool(threadCount); + + executeThreads(object, threadCount, service); + + assertEquals(true, object.isLocked()); + + service.shutdown(); + } + + @Test + public void whenLocked_ThenQueuedThread() { + final int threadCount = 4; + final ExecutorService service = Executors.newFixedThreadPool(threadCount); + final SharedObjectWithLock object = new SharedObjectWithLock(); + + executeThreads(object, threadCount, service); + + assertEquals(object.hasQueuedThreads(), true); + + service.shutdown(); + + } + + public void whenTryLock_ThenQueuedThread() { + final SharedObjectWithLock object = new SharedObjectWithLock(); + + final int threadCount = 2; + final ExecutorService service = Executors.newFixedThreadPool(threadCount); + + executeThreads(object, threadCount, service); + + assertEquals(true, object.isLocked()); + + service.shutdown(); + } + + @Test + public void whenGetCount_ThenCorrectCount() throws InterruptedException { + final int threadCount = 4; + final ExecutorService service = Executors.newFixedThreadPool(threadCount); + final SharedObjectWithLock object = new SharedObjectWithLock(); + + executeThreads(object, threadCount, service); + Thread.sleep(1000); + assertEquals(object.getCounter(), 4); + + service.shutdown(); + + } + + private void executeThreads(SharedObjectWithLock object, int threadCount, ExecutorService service) { + for (int i = 0; i < threadCount; i++) { + service.execute(() -> { + object.perform(); + }); + } + } + +} diff --git a/core-java/src/test/java/com/baeldung/concurrent/locks/SynchronizedHashMapWithRWLockManualTest.java b/core-java/src/test/java/com/baeldung/concurrent/locks/SynchronizedHashMapWithRWLockManualTest.java new file mode 100644 index 0000000000..fd6cf08442 --- /dev/null +++ b/core-java/src/test/java/com/baeldung/concurrent/locks/SynchronizedHashMapWithRWLockManualTest.java @@ -0,0 +1,58 @@ +package com.baeldung.concurrent.locks; + +import jdk.nashorn.internal.ir.annotations.Ignore; +import org.junit.Test; + +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; + +import static junit.framework.TestCase.assertEquals; + +public class SynchronizedHashMapWithRWLockManualTest { + + @Test + public void whenWriting_ThenNoReading() { + SynchronizedHashMapWithRWLock object = new SynchronizedHashMapWithRWLock(); + final int threadCount = 3; + final ExecutorService service = Executors.newFixedThreadPool(threadCount); + + executeWriterThreads(object, threadCount, service); + + assertEquals(object.isReadLockAvailable(), false); + + service.shutdown(); + } + + @Test + public void whenReading_ThenMultipleReadingAllowed() { + SynchronizedHashMapWithRWLock object = new SynchronizedHashMapWithRWLock(); + final int threadCount = 5; + final ExecutorService service = Executors.newFixedThreadPool(threadCount); + + executeReaderThreads(object, threadCount, service); + + assertEquals(object.isReadLockAvailable(), true); + + service.shutdown(); + } + + private void executeWriterThreads(SynchronizedHashMapWithRWLock object, int threadCount, ExecutorService service) { + for (int i = 0; i < threadCount; i++) { + service.execute(() -> { + try { + object.put("key" + threadCount, "value" + threadCount); + } catch (InterruptedException e) { + e.printStackTrace(); + } + }); + } + } + + private void executeReaderThreads(SynchronizedHashMapWithRWLock object, int threadCount, ExecutorService service) { + for (int i = 0; i < threadCount; i++) + service.execute(() -> { + object.get("key" + threadCount); + }); + } + +} From 86029a348782a1a16ad91d1aa2eabdde30fcb6d0 Mon Sep 17 00:00:00 2001 From: Grzegorz Piwowarek Date: Sun, 26 Feb 2017 10:06:21 +0100 Subject: [PATCH 55/68] Rename module (#1234) Rename libraries module --- {cglib => libraries}/pom.xml | 3 ++- .../src/main/java/com/baeldung/cglib/mixin/Class1.java | 0 .../src/main/java/com/baeldung/cglib/mixin/Class2.java | 0 .../src/main/java/com/baeldung/cglib/mixin/Interface1.java | 0 .../src/main/java/com/baeldung/cglib/mixin/Interface2.java | 0 .../main/java/com/baeldung/cglib/mixin/MixinInterface.java | 0 .../main/java/com/baeldung/cglib/proxy/PersonService.java | 0 .../java/com/baeldung/cglib/proxy/BeanGeneratorTest.java | 0 .../src/test/java/com/baeldung/cglib/proxy/MixinTest.java | 0 .../src/test/java/com/baeldung/cglib/proxy/ProxyTest.java | 0 pom.xml | 7 +++---- 11 files changed, 5 insertions(+), 5 deletions(-) rename {cglib => libraries}/pom.xml (95%) rename {cglib => libraries}/src/main/java/com/baeldung/cglib/mixin/Class1.java (100%) rename {cglib => libraries}/src/main/java/com/baeldung/cglib/mixin/Class2.java (100%) rename {cglib => libraries}/src/main/java/com/baeldung/cglib/mixin/Interface1.java (100%) rename {cglib => libraries}/src/main/java/com/baeldung/cglib/mixin/Interface2.java (100%) rename {cglib => libraries}/src/main/java/com/baeldung/cglib/mixin/MixinInterface.java (100%) rename {cglib => libraries}/src/main/java/com/baeldung/cglib/proxy/PersonService.java (100%) rename {cglib => libraries}/src/test/java/com/baeldung/cglib/proxy/BeanGeneratorTest.java (100%) rename {cglib => libraries}/src/test/java/com/baeldung/cglib/proxy/MixinTest.java (100%) rename {cglib => libraries}/src/test/java/com/baeldung/cglib/proxy/ProxyTest.java (100%) diff --git a/cglib/pom.xml b/libraries/pom.xml similarity index 95% rename from cglib/pom.xml rename to libraries/pom.xml index 21309cf514..ee93ee934f 100644 --- a/cglib/pom.xml +++ b/libraries/pom.xml @@ -9,7 +9,8 @@ 4.0.0 - cglib + libraries + libraries diff --git a/cglib/src/main/java/com/baeldung/cglib/mixin/Class1.java b/libraries/src/main/java/com/baeldung/cglib/mixin/Class1.java similarity index 100% rename from cglib/src/main/java/com/baeldung/cglib/mixin/Class1.java rename to libraries/src/main/java/com/baeldung/cglib/mixin/Class1.java diff --git a/cglib/src/main/java/com/baeldung/cglib/mixin/Class2.java b/libraries/src/main/java/com/baeldung/cglib/mixin/Class2.java similarity index 100% rename from cglib/src/main/java/com/baeldung/cglib/mixin/Class2.java rename to libraries/src/main/java/com/baeldung/cglib/mixin/Class2.java diff --git a/cglib/src/main/java/com/baeldung/cglib/mixin/Interface1.java b/libraries/src/main/java/com/baeldung/cglib/mixin/Interface1.java similarity index 100% rename from cglib/src/main/java/com/baeldung/cglib/mixin/Interface1.java rename to libraries/src/main/java/com/baeldung/cglib/mixin/Interface1.java diff --git a/cglib/src/main/java/com/baeldung/cglib/mixin/Interface2.java b/libraries/src/main/java/com/baeldung/cglib/mixin/Interface2.java similarity index 100% rename from cglib/src/main/java/com/baeldung/cglib/mixin/Interface2.java rename to libraries/src/main/java/com/baeldung/cglib/mixin/Interface2.java diff --git a/cglib/src/main/java/com/baeldung/cglib/mixin/MixinInterface.java b/libraries/src/main/java/com/baeldung/cglib/mixin/MixinInterface.java similarity index 100% rename from cglib/src/main/java/com/baeldung/cglib/mixin/MixinInterface.java rename to libraries/src/main/java/com/baeldung/cglib/mixin/MixinInterface.java diff --git a/cglib/src/main/java/com/baeldung/cglib/proxy/PersonService.java b/libraries/src/main/java/com/baeldung/cglib/proxy/PersonService.java similarity index 100% rename from cglib/src/main/java/com/baeldung/cglib/proxy/PersonService.java rename to libraries/src/main/java/com/baeldung/cglib/proxy/PersonService.java diff --git a/cglib/src/test/java/com/baeldung/cglib/proxy/BeanGeneratorTest.java b/libraries/src/test/java/com/baeldung/cglib/proxy/BeanGeneratorTest.java similarity index 100% rename from cglib/src/test/java/com/baeldung/cglib/proxy/BeanGeneratorTest.java rename to libraries/src/test/java/com/baeldung/cglib/proxy/BeanGeneratorTest.java diff --git a/cglib/src/test/java/com/baeldung/cglib/proxy/MixinTest.java b/libraries/src/test/java/com/baeldung/cglib/proxy/MixinTest.java similarity index 100% rename from cglib/src/test/java/com/baeldung/cglib/proxy/MixinTest.java rename to libraries/src/test/java/com/baeldung/cglib/proxy/MixinTest.java diff --git a/cglib/src/test/java/com/baeldung/cglib/proxy/ProxyTest.java b/libraries/src/test/java/com/baeldung/cglib/proxy/ProxyTest.java similarity index 100% rename from cglib/src/test/java/com/baeldung/cglib/proxy/ProxyTest.java rename to libraries/src/test/java/com/baeldung/cglib/proxy/ProxyTest.java diff --git a/pom.xml b/pom.xml index 1b7d0b419c..9d6c5931e3 100644 --- a/pom.xml +++ b/pom.xml @@ -8,7 +8,6 @@ parent-modules pom - UTF-8 refs/heads/master @@ -28,7 +27,6 @@ autovalue cdi - cglib core-java couchbase-sdk @@ -76,6 +74,7 @@ kotlin + libraries log-mdc log4j log4j2 @@ -206,11 +205,11 @@ - + From 73026b65653fe38fded09d85212d108f5b43ab27 Mon Sep 17 00:00:00 2001 From: Andrew Morgan Date: Wed, 22 Feb 2017 20:32:38 +0000 Subject: [PATCH 56/68] Added reactor core --- pom.xml | 1 + reactor-core/pom.xml | 52 +++++++++ .../com/baeldung/reactor/ReactorTest.java | 107 ++++++++++++++++++ 3 files changed, 160 insertions(+) create mode 100644 reactor-core/pom.xml create mode 100644 reactor-core/src/test/java/com/baeldung/reactor/ReactorTest.java diff --git a/pom.xml b/pom.xml index 9d6c5931e3..014e4016c5 100644 --- a/pom.xml +++ b/pom.xml @@ -94,6 +94,7 @@ querydsl + reactor-core redis rest-assured rest-testing diff --git a/reactor-core/pom.xml b/reactor-core/pom.xml new file mode 100644 index 0000000000..017b59f42e --- /dev/null +++ b/reactor-core/pom.xml @@ -0,0 +1,52 @@ + + 4.0.0 + + org.baeldung + reactor-core + 0.0.1-SNAPSHOT + + + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.8 + 1.8 + + + + + + + + + io.projectreactor + reactor-core + 3.0.4.RELEASE + + + + junit + junit + 4.12 + test + + + + org.assertj + assertj-core + 3.6.1 + test + + + + ch.qos.logback + logback-classic + 1.1.3 + + + + + diff --git a/reactor-core/src/test/java/com/baeldung/reactor/ReactorTest.java b/reactor-core/src/test/java/com/baeldung/reactor/ReactorTest.java new file mode 100644 index 0000000000..a90346803e --- /dev/null +++ b/reactor-core/src/test/java/com/baeldung/reactor/ReactorTest.java @@ -0,0 +1,107 @@ +package com.baeldung.reactor; + +import org.junit.Test; +import org.reactivestreams.Subscriber; +import org.reactivestreams.Subscription; +import reactor.core.publisher.ConnectableFlux; +import reactor.core.publisher.Flux; +import reactor.core.publisher.FluxSink; +import reactor.core.scheduler.Schedulers; + +import java.time.Duration; +import java.time.LocalDateTime; +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Stream; + +import static java.time.Duration.ofSeconds; +import static java.util.stream.Collectors.toList; +import static org.assertj.core.api.Assertions.assertThat; + +public class ReactorTest { + + @Test + public void givenFlux_whenSubscribing_shouldStream() throws InterruptedException { + + List elements = new ArrayList<>(); + + Flux.just(1, 2, 3, 4) + .log() + .map(i -> i * 2) + .subscribe(elements::add); + + assertThat(elements).containsExactly(2, 4, 6, 8); + } + + @Test + public void givenFlux_whenZipping_shouldCombine() { + List elements = new ArrayList<>(); + + Flux.just(1, 2, 3, 4) + .log() + .map(i -> i * 2) + .zipWith(Flux.range(0, Integer.MAX_VALUE).log(), (two, one) -> String.format("First Flux: %d, Second Flux: %d", one, two)) + .subscribe(elements::add); + + assertThat(elements).containsExactly( + "First Flux: 0, Second Flux: 2", + "First Flux: 1, Second Flux: 4", + "First Flux: 2, Second Flux: 6", + "First Flux: 3, Second Flux: 8"); + } + + @Test + public void givenFlux_whenApplyingBackPressure_shouldPushLessElements() throws InterruptedException { + + List elements = new ArrayList<>(); + + Flux.just(1, 2, 3, 4) + .log() + .map(i -> i * 2) + .onBackpressureBuffer() + .subscribe(new Subscriber() { + private Subscription s; + int onNextAmount; + + @Override + public void onSubscribe(final Subscription s) { + this.s = s; + s.request(2); + } + + @Override + public void onNext(final Integer integer) { + elements.add(integer); + onNextAmount++; + if (onNextAmount % 2 == 0) { + s.request(2); + } + } + + @Override + public void onError(final Throwable t) { + } + + @Override + public void onComplete() { + int ham = 2; + } + }); + + assertThat(elements).containsExactly(2, 4, 6, 8); + } + + @Test + public void givenFlux_whenInParalle_shouldSubscribeInDifferentThreads() { + List elements = new ArrayList<>(); + + Flux.just(1, 2, 3, 4) + .log() + .map(i -> i * 2) + .subscribeOn(Schedulers.parallel()) + .subscribe(elements::add); + + assertThat(elements).containsExactly(2, 4, 6, 8); + } + +} From 6bbb4cfef447868e6378f6995a9ac2d9c2e73cfe Mon Sep 17 00:00:00 2001 From: Andrew Morgan Date: Wed, 22 Feb 2017 20:36:42 +0000 Subject: [PATCH 57/68] Fixed failing reactive test --- .../src/test/java/com/baeldung/reactor/ReactorTest.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/reactor-core/src/test/java/com/baeldung/reactor/ReactorTest.java b/reactor-core/src/test/java/com/baeldung/reactor/ReactorTest.java index a90346803e..6c30691f26 100644 --- a/reactor-core/src/test/java/com/baeldung/reactor/ReactorTest.java +++ b/reactor-core/src/test/java/com/baeldung/reactor/ReactorTest.java @@ -92,7 +92,7 @@ public class ReactorTest { } @Test - public void givenFlux_whenInParalle_shouldSubscribeInDifferentThreads() { + public void givenFlux_whenInParalle_shouldSubscribeInDifferentThreads() throws InterruptedException { List elements = new ArrayList<>(); Flux.just(1, 2, 3, 4) @@ -101,6 +101,8 @@ public class ReactorTest { .subscribeOn(Schedulers.parallel()) .subscribe(elements::add); + Thread.sleep(1000); + assertThat(elements).containsExactly(2, 4, 6, 8); } From 13b639cdff56ee436917c6cb2160892109540eb3 Mon Sep 17 00:00:00 2001 From: Andrew Morgan Date: Sun, 26 Feb 2017 12:32:23 +0000 Subject: [PATCH 58/68] Renamed tests and added connectableFlux test --- reactor-core/pom.xml | 15 +++++-- .../com/baeldung/reactor/ReactorTest.java | 43 ++++++++++++------- 2 files changed, 39 insertions(+), 19 deletions(-) diff --git a/reactor-core/pom.xml b/reactor-core/pom.xml index 017b59f42e..2be8892983 100644 --- a/reactor-core/pom.xml +++ b/reactor-core/pom.xml @@ -24,29 +24,36 @@ io.projectreactor reactor-core - 3.0.4.RELEASE + ${reactor-core.version} junit junit - 4.12 + ${junit.version} test org.assertj assertj-core - 3.6.1 + ${assertj.version} test ch.qos.logback logback-classic - 1.1.3 + ${logback.version} + + 3.0.4.RELEASE + 4.12 + 3.6.1 + 1.1.3 + + diff --git a/reactor-core/src/test/java/com/baeldung/reactor/ReactorTest.java b/reactor-core/src/test/java/com/baeldung/reactor/ReactorTest.java index 6c30691f26..46b774c30e 100644 --- a/reactor-core/src/test/java/com/baeldung/reactor/ReactorTest.java +++ b/reactor-core/src/test/java/com/baeldung/reactor/ReactorTest.java @@ -5,36 +5,33 @@ import org.reactivestreams.Subscriber; import org.reactivestreams.Subscription; import reactor.core.publisher.ConnectableFlux; import reactor.core.publisher.Flux; -import reactor.core.publisher.FluxSink; import reactor.core.scheduler.Schedulers; -import java.time.Duration; -import java.time.LocalDateTime; import java.util.ArrayList; import java.util.List; -import java.util.stream.Stream; -import static java.time.Duration.ofSeconds; -import static java.util.stream.Collectors.toList; import static org.assertj.core.api.Assertions.assertThat; public class ReactorTest { @Test - public void givenFlux_whenSubscribing_shouldStream() throws InterruptedException { + public void givenFlux_whenSubscribing_thenStream() throws InterruptedException { List elements = new ArrayList<>(); Flux.just(1, 2, 3, 4) .log() - .map(i -> i * 2) + .map(i -> { + System.out.println(i + ":" + Thread.currentThread()); + return i * 2; + }) .subscribe(elements::add); assertThat(elements).containsExactly(2, 4, 6, 8); } @Test - public void givenFlux_whenZipping_shouldCombine() { + public void givenFlux_whenZipping_thenCombine() { List elements = new ArrayList<>(); Flux.just(1, 2, 3, 4) @@ -51,7 +48,7 @@ public class ReactorTest { } @Test - public void givenFlux_whenApplyingBackPressure_shouldPushLessElements() throws InterruptedException { + public void givenFlux_whenApplyingBackPressure_thenPushElementsInBatches() throws InterruptedException { List elements = new ArrayList<>(); @@ -92,18 +89,34 @@ public class ReactorTest { } @Test - public void givenFlux_whenInParalle_shouldSubscribeInDifferentThreads() throws InterruptedException { - List elements = new ArrayList<>(); + public void givenFlux_whenInParallel_thenSubscribeInDifferentThreads() throws InterruptedException { + List threadNames = new ArrayList<>(); Flux.just(1, 2, 3, 4) .log() - .map(i -> i * 2) + .map(i -> Thread.currentThread().getName()) .subscribeOn(Schedulers.parallel()) - .subscribe(elements::add); + .subscribe(threadNames::add); Thread.sleep(1000); - assertThat(elements).containsExactly(2, 4, 6, 8); + assertThat(threadNames).containsExactly("parallel-1", "parallel-1", "parallel-1", "parallel-1"); + } + + @Test + public void givenConnectableFlux_thenShouldStream_onConnect() { + + List elements = new ArrayList<>(); + + final ConnectableFlux publish = Flux.just(1, 2, 3, 4).publish(); + + publish.subscribe(elements::add); + + assertThat(elements).isEmpty(); + + publish.connect(); + + assertThat(elements).containsExactly(1, 2, 3, 4); } } From 2d43e32690a0a84da6fea18b2a35b5f42e3146ec Mon Sep 17 00:00:00 2001 From: Grzegorz Piwowarek Date: Sun, 26 Feb 2017 16:25:51 +0100 Subject: [PATCH 59/68] POM packaging for struts module (#1248) --- struts2/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/struts2/pom.xml b/struts2/pom.xml index 983f18903b..25a374549d 100644 --- a/struts2/pom.xml +++ b/struts2/pom.xml @@ -4,7 +4,7 @@ com.baeldung MyStrutsApp 0.0.1-SNAPSHOT - war + pom struts src/main/java From d4576d97620e2b1a10291377df6bde86515ab116 Mon Sep 17 00:00:00 2001 From: Daniele Demichelis Date: Sun, 26 Feb 2017 18:49:01 +0100 Subject: [PATCH 60/68] BAEL-554 - Increased timeout (#1242) * Burlap & Hessian server added * Burlap & Hessian client work * Fixed main * Fixed formatting * Spring Remote example based on Burlap & Hessian runs in a JUnit test * Fixed main * Fixed formatting * Spring Remote example based on Burlap & Hessian runs in a JUnit test * Spring Remote example based on Burlap & Hessian runs in a JUnit test * Burlap & Hessian client work * Fixed main * Fixed main * Fixed formatting * Fixed formatting * Spring Remote example based on Burlap & Hessian runs in a JUnit test * Spring Remote example based on Burlap & Hessian runs in a JUnit test * Fixed POM --- .../test/java/com/baeldung/client/CabBookingServiceTest.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/spring-remoting/remoting-hessian-burlap/client/src/test/java/com/baeldung/client/CabBookingServiceTest.java b/spring-remoting/remoting-hessian-burlap/client/src/test/java/com/baeldung/client/CabBookingServiceTest.java index 20746bbf09..373701f714 100644 --- a/spring-remoting/remoting-hessian-burlap/client/src/test/java/com/baeldung/client/CabBookingServiceTest.java +++ b/spring-remoting/remoting-hessian-burlap/client/src/test/java/com/baeldung/client/CabBookingServiceTest.java @@ -30,7 +30,8 @@ public class CabBookingServiceTest { serverThread = serverThread(); log.info("Starting server."); serverThread.start(); - sleep(4000); + // increase this enough to let the server start + sleep(6000); } @org.junit.Test From 325466a7820aed6352b7213592243e1147796c5e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Muhammet=20O=C4=9Fuz=20=C3=96ZCAN?= Date: Sun, 26 Feb 2017 19:53:32 +0200 Subject: [PATCH 61/68] Master (#1250) * Bean Injection Project is added Different Types of Bean Injection article codes are added. * Java-based configuration added Java-based configuration and tests are added. Coding styles are fixed. * List of Lists Article Codes added. List of Lists Article Codes added. --- .../com/baeldung/list/listoflist/Pen.java | 18 +++++++ .../com/baeldung/list/listoflist/Pencil.java | 18 +++++++ .../com/baeldung/list/listoflist/Rubber.java | 18 +++++++ .../baeldung/list/listoflist/Stationery.java | 5 ++ .../list/listoflist/ListOfListsTest.java | 50 +++++++++++++++++++ .../config/ConstructorBasedShipConfig.java | 20 ++++++++ .../bean/config/SetterBasedShipConfig.java | 18 +++++++ .../org/baeldung/bean/injection/Helm.java | 14 ++++++ .../org/baeldung/bean/injection/Ship.java | 26 ++++++++++ .../resources/basicConfigForPropertiesTwo.xml | 13 ++--- .../resources/beanInjection-constructor.xml | 15 ++++++ .../main/resources/beanInjection-setter.xml | 15 ++++++ ...rBasedBeanInjectionWithJavaConfigTest.java | 23 +++++++++ ...orBasedBeanInjectionWithXMLConfigTest.java | 20 ++++++++ ...rBasedBeanInjectionWithJavaConfigTest.java | 24 +++++++++ ...erBasedBeanInjectionWithXMLConfigTest.java | 20 ++++++++ 16 files changed, 311 insertions(+), 6 deletions(-) create mode 100644 core-java/src/main/java/com/baeldung/list/listoflist/Pen.java create mode 100644 core-java/src/main/java/com/baeldung/list/listoflist/Pencil.java create mode 100644 core-java/src/main/java/com/baeldung/list/listoflist/Rubber.java create mode 100644 core-java/src/main/java/com/baeldung/list/listoflist/Stationery.java create mode 100644 core-java/src/test/java/com/baeldung/list/listoflist/ListOfListsTest.java create mode 100644 spring-all/src/main/java/org/baeldung/bean/config/ConstructorBasedShipConfig.java create mode 100644 spring-all/src/main/java/org/baeldung/bean/config/SetterBasedShipConfig.java create mode 100644 spring-all/src/main/java/org/baeldung/bean/injection/Helm.java create mode 100644 spring-all/src/main/java/org/baeldung/bean/injection/Ship.java create mode 100644 spring-all/src/main/resources/beanInjection-constructor.xml create mode 100644 spring-all/src/main/resources/beanInjection-setter.xml create mode 100644 spring-all/src/test/java/org/baeldung/bean/injection/ConstructorBasedBeanInjectionWithJavaConfigTest.java create mode 100644 spring-all/src/test/java/org/baeldung/bean/injection/ConstructorBasedBeanInjectionWithXMLConfigTest.java create mode 100644 spring-all/src/test/java/org/baeldung/bean/injection/SetterBasedBeanInjectionWithJavaConfigTest.java create mode 100644 spring-all/src/test/java/org/baeldung/bean/injection/SetterBasedBeanInjectionWithXMLConfigTest.java diff --git a/core-java/src/main/java/com/baeldung/list/listoflist/Pen.java b/core-java/src/main/java/com/baeldung/list/listoflist/Pen.java new file mode 100644 index 0000000000..efbd88d0b1 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/list/listoflist/Pen.java @@ -0,0 +1,18 @@ +package com.baeldung.list.listoflist; + +public class Pen implements Stationery { + + public String name; + + public Pen(String name) { + this.name = name; + } + + public void setName(String name) { + this.name = name; + } + + public String getName() { + return this.name; + } +} \ No newline at end of file diff --git a/core-java/src/main/java/com/baeldung/list/listoflist/Pencil.java b/core-java/src/main/java/com/baeldung/list/listoflist/Pencil.java new file mode 100644 index 0000000000..7dfa119703 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/list/listoflist/Pencil.java @@ -0,0 +1,18 @@ +package com.baeldung.list.listoflist; + +public class Pencil implements Stationery{ + + public String name; + + public Pencil(String name) { + this.name = name; + } + + public void setName(String name) { + this.name = name; + } + + public String getName() { + return this.name; + } +} \ No newline at end of file diff --git a/core-java/src/main/java/com/baeldung/list/listoflist/Rubber.java b/core-java/src/main/java/com/baeldung/list/listoflist/Rubber.java new file mode 100644 index 0000000000..7511139e14 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/list/listoflist/Rubber.java @@ -0,0 +1,18 @@ +package com.baeldung.list.listoflist; + +public class Rubber implements Stationery { + + public String name; + + public Rubber(String name) { + this.name = name; + } + + public void setName(String name) { + this.name = name; + } + + public String getName() { + return this.name; + } +} \ No newline at end of file diff --git a/core-java/src/main/java/com/baeldung/list/listoflist/Stationery.java b/core-java/src/main/java/com/baeldung/list/listoflist/Stationery.java new file mode 100644 index 0000000000..d914397cb8 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/list/listoflist/Stationery.java @@ -0,0 +1,5 @@ +package com.baeldung.list.listoflist; + +public interface Stationery { + +} diff --git a/core-java/src/test/java/com/baeldung/list/listoflist/ListOfListsTest.java b/core-java/src/test/java/com/baeldung/list/listoflist/ListOfListsTest.java new file mode 100644 index 0000000000..ce24ff24bc --- /dev/null +++ b/core-java/src/test/java/com/baeldung/list/listoflist/ListOfListsTest.java @@ -0,0 +1,50 @@ +package com.baeldung.list.listoflist; + +import java.util.ArrayList; +import java.util.List; + +import static org.junit.Assert.assertEquals; +import org.junit.Before; +import org.junit.Test; + +public class ListOfListsTest { + + private List> listOfLists = new ArrayList>(); + private ArrayList penList = new ArrayList<>(); + private ArrayList pencilList = new ArrayList<>(); + private ArrayList rubberList = new ArrayList<>(); + + @SuppressWarnings("unchecked") + @Before + public void init() { + listOfLists.add(penList); + listOfLists.add(pencilList); + listOfLists.add(rubberList); + + ((ArrayList) listOfLists.get(0)).add(new Pen("Pen 1")); + ((ArrayList) listOfLists.get(1)).add(new Pencil("Pencil 1")); + ((ArrayList) listOfLists.get(2)).add(new Rubber("Rubber 1")); + } + + @Test + public void givenListOfLists_thenCheckNames() { + assertEquals("Pen 1", ((Pen) listOfLists.get(0) + .get(0)).getName()); + assertEquals("Pencil 1", ((Pencil) listOfLists.get(1) + .get(0)).getName()); + assertEquals("Rubber 1", ((Rubber) listOfLists.get(2) + .get(0)).getName()); + } + + @SuppressWarnings("unchecked") + @Test + public void givenListOfLists_whenRemovingElements_thenCheckNames() { + ((ArrayList) listOfLists.get(1)).remove(0); + listOfLists.remove(1); + assertEquals("Rubber 1", ((Rubber) listOfLists.get(1) + .get(0)).getName()); + listOfLists.remove(0); + assertEquals("Rubber 1", ((Rubber) listOfLists.get(0) + .get(0)).getName()); + } +} diff --git a/spring-all/src/main/java/org/baeldung/bean/config/ConstructorBasedShipConfig.java b/spring-all/src/main/java/org/baeldung/bean/config/ConstructorBasedShipConfig.java new file mode 100644 index 0000000000..f0e6e8868e --- /dev/null +++ b/spring-all/src/main/java/org/baeldung/bean/config/ConstructorBasedShipConfig.java @@ -0,0 +1,20 @@ +package org.baeldung.bean.config; + +import org.baeldung.bean.injection.Helm; +import org.baeldung.bean.injection.Ship; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class ConstructorBasedShipConfig { + + @Bean + public Ship ship() { + return new Ship(helm()); + } + + @Bean + public Helm helm() { + return new Helm(); + } +} diff --git a/spring-all/src/main/java/org/baeldung/bean/config/SetterBasedShipConfig.java b/spring-all/src/main/java/org/baeldung/bean/config/SetterBasedShipConfig.java new file mode 100644 index 0000000000..7cd6de1c74 --- /dev/null +++ b/spring-all/src/main/java/org/baeldung/bean/config/SetterBasedShipConfig.java @@ -0,0 +1,18 @@ +package org.baeldung.bean.config; + +import org.baeldung.bean.injection.Helm; +import org.baeldung.bean.injection.Ship; +import org.springframework.context.annotation.Bean; + +public class SetterBasedShipConfig { + + @Bean + public Ship ship() { + return new Ship(); + } + + @Bean + public Helm helm() { + return new Helm(); + } +} diff --git a/spring-all/src/main/java/org/baeldung/bean/injection/Helm.java b/spring-all/src/main/java/org/baeldung/bean/injection/Helm.java new file mode 100644 index 0000000000..6d6ea85482 --- /dev/null +++ b/spring-all/src/main/java/org/baeldung/bean/injection/Helm.java @@ -0,0 +1,14 @@ +package org.baeldung.bean.injection; + +public class Helm { + + private String brandOfHelm = "HelmBrand"; + + public String getBrandOfHelm() { + return brandOfHelm; + } + + public void setBrandOfHelm(String brandOfHelm) { + this.brandOfHelm = brandOfHelm; + } +} diff --git a/spring-all/src/main/java/org/baeldung/bean/injection/Ship.java b/spring-all/src/main/java/org/baeldung/bean/injection/Ship.java new file mode 100644 index 0000000000..69d9fa1276 --- /dev/null +++ b/spring-all/src/main/java/org/baeldung/bean/injection/Ship.java @@ -0,0 +1,26 @@ +package org.baeldung.bean.injection; + +import org.springframework.beans.factory.annotation.Autowired; + +public class Ship { + + @Autowired + private Helm helm; + + public Ship() { + helm = new Helm(); + } + + public Ship(Helm helm) { + this.helm = helm; + } + + @Autowired + public void setHelm(Helm helm) { + this.helm = helm; + } + + public Helm getHelm() { + return this.helm; + } +} diff --git a/spring-all/src/main/resources/basicConfigForPropertiesTwo.xml b/spring-all/src/main/resources/basicConfigForPropertiesTwo.xml index bd6588104f..1d470c4340 100644 --- a/spring-all/src/main/resources/basicConfigForPropertiesTwo.xml +++ b/spring-all/src/main/resources/basicConfigForPropertiesTwo.xml @@ -1,12 +1,13 @@ - + http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd"> - + \ No newline at end of file diff --git a/spring-all/src/main/resources/beanInjection-constructor.xml b/spring-all/src/main/resources/beanInjection-constructor.xml new file mode 100644 index 0000000000..a0713fd9f8 --- /dev/null +++ b/spring-all/src/main/resources/beanInjection-constructor.xml @@ -0,0 +1,15 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/spring-all/src/main/resources/beanInjection-setter.xml b/spring-all/src/main/resources/beanInjection-setter.xml new file mode 100644 index 0000000000..b07826c31e --- /dev/null +++ b/spring-all/src/main/resources/beanInjection-setter.xml @@ -0,0 +1,15 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/spring-all/src/test/java/org/baeldung/bean/injection/ConstructorBasedBeanInjectionWithJavaConfigTest.java b/spring-all/src/test/java/org/baeldung/bean/injection/ConstructorBasedBeanInjectionWithJavaConfigTest.java new file mode 100644 index 0000000000..68f8fc13d9 --- /dev/null +++ b/spring-all/src/test/java/org/baeldung/bean/injection/ConstructorBasedBeanInjectionWithJavaConfigTest.java @@ -0,0 +1,23 @@ +package org.baeldung.bean.injection; + +import org.baeldung.bean.config.ConstructorBasedShipConfig; +import org.junit.Assert; +import org.junit.Test; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; + +public class ConstructorBasedBeanInjectionWithJavaConfigTest { + private static final String HELM_NAME = "HelmBrand"; + + @Test + public void givenJavaConfigFile_whenUsingConstructorBasedBeanInjection_thenCorrectHelmName() { + + AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); + ctx.register(ConstructorBasedShipConfig.class); + ctx.refresh(); + + Ship ship = ctx.getBean(Ship.class); + + Assert.assertEquals(HELM_NAME, ship.getHelm() + .getBrandOfHelm()); + } +} diff --git a/spring-all/src/test/java/org/baeldung/bean/injection/ConstructorBasedBeanInjectionWithXMLConfigTest.java b/spring-all/src/test/java/org/baeldung/bean/injection/ConstructorBasedBeanInjectionWithXMLConfigTest.java new file mode 100644 index 0000000000..e69de124b2 --- /dev/null +++ b/spring-all/src/test/java/org/baeldung/bean/injection/ConstructorBasedBeanInjectionWithXMLConfigTest.java @@ -0,0 +1,20 @@ +package org.baeldung.bean.injection; + +import org.junit.Assert; +import org.junit.Test; +import org.springframework.context.ApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +public class ConstructorBasedBeanInjectionWithXMLConfigTest { + + private static final String HELM_NAME = "HelmBrand"; + + @Test + public void givenXMLConfigFile_whenUsingConstructorBasedBeanInjection_thenCorrectHelmName() { + final ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beanInjection-constructor.xml"); + + final Ship shipConstructorBean = (Ship) applicationContext.getBean("ship"); + Assert.assertEquals(HELM_NAME, shipConstructorBean.getHelm() + .getBrandOfHelm()); + } +} diff --git a/spring-all/src/test/java/org/baeldung/bean/injection/SetterBasedBeanInjectionWithJavaConfigTest.java b/spring-all/src/test/java/org/baeldung/bean/injection/SetterBasedBeanInjectionWithJavaConfigTest.java new file mode 100644 index 0000000000..8705995acd --- /dev/null +++ b/spring-all/src/test/java/org/baeldung/bean/injection/SetterBasedBeanInjectionWithJavaConfigTest.java @@ -0,0 +1,24 @@ +package org.baeldung.bean.injection; + +import org.baeldung.bean.config.SetterBasedShipConfig; +import org.junit.Assert; +import org.junit.Test; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; + +public class SetterBasedBeanInjectionWithJavaConfigTest { + + private static final String HELM_NAME = "HelmBrand"; + + @Test + public void givenJavaConfigFile_whenUsingSetterBasedBeanInjection_thenCorrectHelmName() { + + AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); + ctx.register(SetterBasedShipConfig.class); + ctx.refresh(); + + Ship ship = ctx.getBean(Ship.class); + + Assert.assertEquals(HELM_NAME, ship.getHelm() + .getBrandOfHelm()); + } +} diff --git a/spring-all/src/test/java/org/baeldung/bean/injection/SetterBasedBeanInjectionWithXMLConfigTest.java b/spring-all/src/test/java/org/baeldung/bean/injection/SetterBasedBeanInjectionWithXMLConfigTest.java new file mode 100644 index 0000000000..1958761d78 --- /dev/null +++ b/spring-all/src/test/java/org/baeldung/bean/injection/SetterBasedBeanInjectionWithXMLConfigTest.java @@ -0,0 +1,20 @@ +package org.baeldung.bean.injection; + +import org.junit.Assert; +import org.junit.Test; +import org.springframework.context.ApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +public class SetterBasedBeanInjectionWithXMLConfigTest { + + private static final String HELM_NAME = "HelmBrand"; + + @Test + public void givenXMLConfigFile_whenUsingSetterBasedBeanInjection_thenCorrectHelmName() { + final ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beanInjection-setter.xml"); + + final Ship shipSetterBean = (Ship) applicationContext.getBean("ship"); + Assert.assertEquals(HELM_NAME, shipSetterBean.getHelm() + .getBrandOfHelm()); + } +} From 7f20437cb05b1474450b17a0639c120f7ac55a0b Mon Sep 17 00:00:00 2001 From: Sandeep4odesk Date: Mon, 27 Feb 2017 00:26:52 +0530 Subject: [PATCH 62/68] Adding Hibernate 5 Project (#1129) * Changes in assert statement * Adding Hibernate 5 Project * Adding Hibernate 5 Project --- spring-hibernate5/.gitignore | 13 + spring-hibernate5/pom.xml | 239 +++++++++++++++ .../hibernate/criteria/model/Item.java | 81 +++++ .../criteria/util/HibernateUtil.java | 17 ++ .../criteria/view/ApplicationView.java | 284 +++++++++++++++++ .../hibernate/fetching/model/OrderDetail.java | 58 ++++ .../hibernate/fetching/model/UserEager.java | 71 +++++ .../hibernate/fetching/model/UserLazy.java | 71 +++++ .../fetching/util/HibernateUtil.java | 28 ++ .../fetching/view/FetchingAppView.java | 68 +++++ .../persistence/dao/IBarAuditableDao.java | 8 + .../persistence/dao/IBarCrudRepository.java | 10 + .../com/baeldung/persistence/dao/IBarDao.java | 8 + .../baeldung/persistence/dao/IChildDao.java | 8 + .../persistence/dao/IFooAuditableDao.java | 8 + .../com/baeldung/persistence/dao/IFooDao.java | 8 + .../baeldung/persistence/dao/IParentDao.java | 8 + .../persistence/dao/common/AbstractDao.java | 14 + .../common/AbstractHibernateAuditableDao.java | 37 +++ .../dao/common/AbstractHibernateDao.java | 59 ++++ .../dao/common/AbstractJpaDao.java | 56 ++++ .../dao/common/GenericHibernateDao.java | 13 + .../dao/common/IAuditOperations.java | 14 + .../persistence/dao/common/IGenericDao.java | 7 + .../persistence/dao/common/IOperations.java | 20 ++ .../persistence/dao/impl/BarAuditableDao.java | 28 ++ .../baeldung/persistence/dao/impl/BarDao.java | 19 ++ .../persistence/dao/impl/BarJpaDao.java | 19 ++ .../persistence/dao/impl/ChildDao.java | 19 ++ .../persistence/dao/impl/FooAuditableDao.java | 17 ++ .../baeldung/persistence/dao/impl/FooDao.java | 19 ++ .../persistence/dao/impl/ParentDao.java | 19 ++ .../com/baeldung/persistence/model/Bar.java | 242 +++++++++++++++ .../com/baeldung/persistence/model/Child.java | 51 ++++ .../com/baeldung/persistence/model/Foo.java | 105 +++++++ .../baeldung/persistence/model/Parent.java | 60 ++++ .../baeldung/persistence/model/Person.java | 31 ++ .../service/IBarAuditableService.java | 8 + .../persistence/service/IBarService.java | 8 + .../persistence/service/IChildService.java | 8 + .../service/IFooAuditableService.java | 8 + .../persistence/service/IFooService.java | 8 + .../persistence/service/IParentService.java | 8 + .../AbstractHibernateAuditableService.java | 31 ++ .../common/AbstractHibernateService.java | 43 +++ .../service/common/AbstractJpaService.java | 42 +++ .../service/common/AbstractService.java | 42 +++ .../common/AbstractSpringDataJpaService.java | 46 +++ .../service/impl/BarAuditableService.java | 41 +++ .../service/impl/BarJpaService.java | 30 ++ .../persistence/service/impl/BarService.java | 30 ++ .../service/impl/BarSpringDataJpaService.java | 26 ++ .../service/impl/ChildService.java | 28 ++ .../service/impl/FooAuditableService.java | 41 +++ .../persistence/service/impl/FooService.java | 30 ++ .../service/impl/ParentService.java | 28 ++ .../baeldung/spring/PersistenceConfig.java | 173 +++++++++++ .../baeldung/spring/PersistenceXmlConfig.java | 14 + .../src/main/resources/criteria.cfg.xml | 17 ++ .../resources/criteria_create_queries.sql | 7 + .../src/main/resources/fetching.cfg.xml | 20 ++ .../src/main/resources/fetchingLazy.cfg.xml | 17 ++ .../resources/fetching_create_queries.sql | 14 + .../src/main/resources/hibernate5Config.xml | 34 +++ .../src/main/resources/insert_statements.sql | 31 ++ .../src/main/resources/logback.xml | 22 ++ .../resources/persistence-mysql.properties | 13 + .../src/main/resources/stored_procedure.sql | 20 ++ .../src/main/resources/webSecurityConfig.xml | 37 +++ .../HibernateCriteriaIntegrationTest.java | 194 ++++++++++++ .../criteria/HibernateCriteriaTestRunner.java | 15 + .../criteria/HibernateCriteriaTestSuite.java | 11 + .../HibernateFetchingIntegrationTest.java | 42 +++ .../persistence/IntegrationTestSuite.java | 25 ++ .../persistence/audit/AuditTestSuite.java | 14 + .../EnversFooBarAuditIntegrationTest.java | 142 +++++++++ .../audit/JPABarAuditIntegrationTest.java | 102 +++++++ .../SpringDataJPABarAuditIntegrationTest.java | 76 +++++ .../persistence/hibernate/FooFixtures.java | 101 +++++++ ...oPaginationPersistenceIntegrationTest.java | 185 ++++++++++++ .../FooSortingPersistenceIntegrationTest.java | 179 +++++++++++ .../persistence/save/SaveMethodsTest.java | 285 ++++++++++++++++++ ...erviceBasicPersistenceIntegrationTest.java | 54 ++++ .../FooServicePersistenceIntegrationTest.java | 63 ++++ .../FooStoredProceduresIntegrationTest.java | 114 +++++++ ...rentServicePersistenceIntegrationTest.java | 69 +++++ .../src/test/resources/.gitignore | 13 + .../hibernate/criteria/model/Item.hbm.xml | 22 ++ .../src/test/resources/criteria.cfg.xml | 16 + .../src/test/resources/fetching.cfg.xml | 18 ++ .../src/test/resources/fetchingLazy.cfg.xml | 18 ++ 91 files changed, 4520 insertions(+) create mode 100644 spring-hibernate5/.gitignore create mode 100644 spring-hibernate5/pom.xml create mode 100644 spring-hibernate5/src/main/java/com/baeldung/hibernate/criteria/model/Item.java create mode 100644 spring-hibernate5/src/main/java/com/baeldung/hibernate/criteria/util/HibernateUtil.java create mode 100644 spring-hibernate5/src/main/java/com/baeldung/hibernate/criteria/view/ApplicationView.java create mode 100644 spring-hibernate5/src/main/java/com/baeldung/hibernate/fetching/model/OrderDetail.java create mode 100644 spring-hibernate5/src/main/java/com/baeldung/hibernate/fetching/model/UserEager.java create mode 100644 spring-hibernate5/src/main/java/com/baeldung/hibernate/fetching/model/UserLazy.java create mode 100644 spring-hibernate5/src/main/java/com/baeldung/hibernate/fetching/util/HibernateUtil.java create mode 100644 spring-hibernate5/src/main/java/com/baeldung/hibernate/fetching/view/FetchingAppView.java create mode 100644 spring-hibernate5/src/main/java/com/baeldung/persistence/dao/IBarAuditableDao.java create mode 100644 spring-hibernate5/src/main/java/com/baeldung/persistence/dao/IBarCrudRepository.java create mode 100644 spring-hibernate5/src/main/java/com/baeldung/persistence/dao/IBarDao.java create mode 100644 spring-hibernate5/src/main/java/com/baeldung/persistence/dao/IChildDao.java create mode 100644 spring-hibernate5/src/main/java/com/baeldung/persistence/dao/IFooAuditableDao.java create mode 100644 spring-hibernate5/src/main/java/com/baeldung/persistence/dao/IFooDao.java create mode 100644 spring-hibernate5/src/main/java/com/baeldung/persistence/dao/IParentDao.java create mode 100644 spring-hibernate5/src/main/java/com/baeldung/persistence/dao/common/AbstractDao.java create mode 100644 spring-hibernate5/src/main/java/com/baeldung/persistence/dao/common/AbstractHibernateAuditableDao.java create mode 100644 spring-hibernate5/src/main/java/com/baeldung/persistence/dao/common/AbstractHibernateDao.java create mode 100644 spring-hibernate5/src/main/java/com/baeldung/persistence/dao/common/AbstractJpaDao.java create mode 100644 spring-hibernate5/src/main/java/com/baeldung/persistence/dao/common/GenericHibernateDao.java create mode 100644 spring-hibernate5/src/main/java/com/baeldung/persistence/dao/common/IAuditOperations.java create mode 100644 spring-hibernate5/src/main/java/com/baeldung/persistence/dao/common/IGenericDao.java create mode 100644 spring-hibernate5/src/main/java/com/baeldung/persistence/dao/common/IOperations.java create mode 100644 spring-hibernate5/src/main/java/com/baeldung/persistence/dao/impl/BarAuditableDao.java create mode 100644 spring-hibernate5/src/main/java/com/baeldung/persistence/dao/impl/BarDao.java create mode 100644 spring-hibernate5/src/main/java/com/baeldung/persistence/dao/impl/BarJpaDao.java create mode 100644 spring-hibernate5/src/main/java/com/baeldung/persistence/dao/impl/ChildDao.java create mode 100644 spring-hibernate5/src/main/java/com/baeldung/persistence/dao/impl/FooAuditableDao.java create mode 100644 spring-hibernate5/src/main/java/com/baeldung/persistence/dao/impl/FooDao.java create mode 100644 spring-hibernate5/src/main/java/com/baeldung/persistence/dao/impl/ParentDao.java create mode 100644 spring-hibernate5/src/main/java/com/baeldung/persistence/model/Bar.java create mode 100644 spring-hibernate5/src/main/java/com/baeldung/persistence/model/Child.java create mode 100644 spring-hibernate5/src/main/java/com/baeldung/persistence/model/Foo.java create mode 100644 spring-hibernate5/src/main/java/com/baeldung/persistence/model/Parent.java create mode 100644 spring-hibernate5/src/main/java/com/baeldung/persistence/model/Person.java create mode 100644 spring-hibernate5/src/main/java/com/baeldung/persistence/service/IBarAuditableService.java create mode 100644 spring-hibernate5/src/main/java/com/baeldung/persistence/service/IBarService.java create mode 100644 spring-hibernate5/src/main/java/com/baeldung/persistence/service/IChildService.java create mode 100644 spring-hibernate5/src/main/java/com/baeldung/persistence/service/IFooAuditableService.java create mode 100644 spring-hibernate5/src/main/java/com/baeldung/persistence/service/IFooService.java create mode 100644 spring-hibernate5/src/main/java/com/baeldung/persistence/service/IParentService.java create mode 100644 spring-hibernate5/src/main/java/com/baeldung/persistence/service/common/AbstractHibernateAuditableService.java create mode 100644 spring-hibernate5/src/main/java/com/baeldung/persistence/service/common/AbstractHibernateService.java create mode 100644 spring-hibernate5/src/main/java/com/baeldung/persistence/service/common/AbstractJpaService.java create mode 100644 spring-hibernate5/src/main/java/com/baeldung/persistence/service/common/AbstractService.java create mode 100644 spring-hibernate5/src/main/java/com/baeldung/persistence/service/common/AbstractSpringDataJpaService.java create mode 100644 spring-hibernate5/src/main/java/com/baeldung/persistence/service/impl/BarAuditableService.java create mode 100644 spring-hibernate5/src/main/java/com/baeldung/persistence/service/impl/BarJpaService.java create mode 100644 spring-hibernate5/src/main/java/com/baeldung/persistence/service/impl/BarService.java create mode 100644 spring-hibernate5/src/main/java/com/baeldung/persistence/service/impl/BarSpringDataJpaService.java create mode 100644 spring-hibernate5/src/main/java/com/baeldung/persistence/service/impl/ChildService.java create mode 100644 spring-hibernate5/src/main/java/com/baeldung/persistence/service/impl/FooAuditableService.java create mode 100644 spring-hibernate5/src/main/java/com/baeldung/persistence/service/impl/FooService.java create mode 100644 spring-hibernate5/src/main/java/com/baeldung/persistence/service/impl/ParentService.java create mode 100644 spring-hibernate5/src/main/java/com/baeldung/spring/PersistenceConfig.java create mode 100644 spring-hibernate5/src/main/java/com/baeldung/spring/PersistenceXmlConfig.java create mode 100644 spring-hibernate5/src/main/resources/criteria.cfg.xml create mode 100644 spring-hibernate5/src/main/resources/criteria_create_queries.sql create mode 100644 spring-hibernate5/src/main/resources/fetching.cfg.xml create mode 100644 spring-hibernate5/src/main/resources/fetchingLazy.cfg.xml create mode 100644 spring-hibernate5/src/main/resources/fetching_create_queries.sql create mode 100644 spring-hibernate5/src/main/resources/hibernate5Config.xml create mode 100644 spring-hibernate5/src/main/resources/insert_statements.sql create mode 100644 spring-hibernate5/src/main/resources/logback.xml create mode 100644 spring-hibernate5/src/main/resources/persistence-mysql.properties create mode 100644 spring-hibernate5/src/main/resources/stored_procedure.sql create mode 100644 spring-hibernate5/src/main/resources/webSecurityConfig.xml create mode 100644 spring-hibernate5/src/test/java/com/baeldung/hibernate/criteria/HibernateCriteriaIntegrationTest.java create mode 100644 spring-hibernate5/src/test/java/com/baeldung/hibernate/criteria/HibernateCriteriaTestRunner.java create mode 100644 spring-hibernate5/src/test/java/com/baeldung/hibernate/criteria/HibernateCriteriaTestSuite.java create mode 100644 spring-hibernate5/src/test/java/com/baeldung/hibernate/fetching/HibernateFetchingIntegrationTest.java create mode 100644 spring-hibernate5/src/test/java/com/baeldung/persistence/IntegrationTestSuite.java create mode 100644 spring-hibernate5/src/test/java/com/baeldung/persistence/audit/AuditTestSuite.java create mode 100644 spring-hibernate5/src/test/java/com/baeldung/persistence/audit/EnversFooBarAuditIntegrationTest.java create mode 100644 spring-hibernate5/src/test/java/com/baeldung/persistence/audit/JPABarAuditIntegrationTest.java create mode 100644 spring-hibernate5/src/test/java/com/baeldung/persistence/audit/SpringDataJPABarAuditIntegrationTest.java create mode 100644 spring-hibernate5/src/test/java/com/baeldung/persistence/hibernate/FooFixtures.java create mode 100644 spring-hibernate5/src/test/java/com/baeldung/persistence/hibernate/FooPaginationPersistenceIntegrationTest.java create mode 100644 spring-hibernate5/src/test/java/com/baeldung/persistence/hibernate/FooSortingPersistenceIntegrationTest.java create mode 100644 spring-hibernate5/src/test/java/com/baeldung/persistence/save/SaveMethodsTest.java create mode 100644 spring-hibernate5/src/test/java/com/baeldung/persistence/service/FooServiceBasicPersistenceIntegrationTest.java create mode 100644 spring-hibernate5/src/test/java/com/baeldung/persistence/service/FooServicePersistenceIntegrationTest.java create mode 100644 spring-hibernate5/src/test/java/com/baeldung/persistence/service/FooStoredProceduresIntegrationTest.java create mode 100644 spring-hibernate5/src/test/java/com/baeldung/persistence/service/ParentServicePersistenceIntegrationTest.java create mode 100644 spring-hibernate5/src/test/resources/.gitignore create mode 100644 spring-hibernate5/src/test/resources/com/baeldung/hibernate/criteria/model/Item.hbm.xml create mode 100644 spring-hibernate5/src/test/resources/criteria.cfg.xml create mode 100644 spring-hibernate5/src/test/resources/fetching.cfg.xml create mode 100644 spring-hibernate5/src/test/resources/fetchingLazy.cfg.xml diff --git a/spring-hibernate5/.gitignore b/spring-hibernate5/.gitignore new file mode 100644 index 0000000000..83c05e60c8 --- /dev/null +++ b/spring-hibernate5/.gitignore @@ -0,0 +1,13 @@ +*.class + +#folders# +/target +/neoDb* +/data +/src/main/webapp/WEB-INF/classes +*/META-INF/* + +# Packaged files # +*.jar +*.war +*.ear \ No newline at end of file diff --git a/spring-hibernate5/pom.xml b/spring-hibernate5/pom.xml new file mode 100644 index 0000000000..81f8084d74 --- /dev/null +++ b/spring-hibernate5/pom.xml @@ -0,0 +1,239 @@ + + 4.0.0 + com.baeldung + spring-hibernate5 + 0.1-SNAPSHOT + + spring-hibernate5 + + + + + + + org.springframework + spring-context + ${org.springframework.version} + + + org.springframework + spring-aspects + ${org.springframework.version} + + + org.springframework.security + spring-security-core + ${org.springframework.security.version} + + + + + + org.springframework + spring-orm + ${org.springframework.version} + + + org.springframework.data + spring-data-jpa + ${org.springframework.data.version} + + + org.hibernate + hibernate-core + ${hibernate.version} + + + org.hibernate + hibernate-envers + ${hibernate-envers.version} + + + javax.transaction + jta + ${jta.version} + + + mysql + mysql-connector-java + ${mysql-connector-java.version} + + + + org.apache.tomcat + tomcat-dbcp + ${tomcat-dbcp.version} + + + + + + org.hibernate + hibernate-validator + ${hibernate-validator.version} + + + javax.el + javax.el-api + ${javax.el-api.version} + + + + + + com.google.guava + guava + ${guava.version} + + + + + + org.apache.commons + commons-lang3 + ${commons-lang3.version} + test + + + + org.springframework + spring-test + ${org.springframework.version} + test + + + + junit + junit + ${junit.version} + test + + + + org.springframework.security + spring-security-test + ${org.springframework.security.version} + test + + + + org.hamcrest + hamcrest-core + ${org.hamcrest.version} + test + + + + junit + junit-dep + 4.11 + test + + + + org.hamcrest + hamcrest-library + ${org.hamcrest.version} + test + + + + org.mockito + mockito-core + ${mockito.version} + test + + + + org.hsqldb + hsqldb + ${hsqldb.version} + test + + + + + + spring-hibernate5 + + + src/main/resources + true + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + ${maven-compiler-plugin.version} + + 1.8 + 1.8 + + + + + org.apache.maven.plugins + maven-surefire-plugin + ${maven-surefire-plugin.version} + + + **/*IntegrationTest.java + + + + + + + + + + + + + + 4.3.5.RELEASE + 4.2.1.RELEASE + 1.10.6.RELEASE + + + 5.2.8.Final + ${hibernate.version} + 6.0.5 + 8.5.11 + 1.1 + 2.3.4 + + + 1.7.21 + 1.1.7 + + + 5.4.0.Final + 2.2.5 + + + 21.0 + 3.5 + + + 1.3 + 4.12 + 2.6.8 + + 4.4.1 + 4.5 + + 2.9.0 + + + 3.6.0 + 2.19.1 + 2.7 + 1.6.1 + + + + diff --git a/spring-hibernate5/src/main/java/com/baeldung/hibernate/criteria/model/Item.java b/spring-hibernate5/src/main/java/com/baeldung/hibernate/criteria/model/Item.java new file mode 100644 index 0000000000..957207b7e6 --- /dev/null +++ b/spring-hibernate5/src/main/java/com/baeldung/hibernate/criteria/model/Item.java @@ -0,0 +1,81 @@ +package com.baeldung.hibernate.criteria.model; + +import java.io.Serializable; + +public class Item implements Serializable { + + private static final long serialVersionUID = 1L; + private Integer itemId; + private String itemName; + private String itemDescription; + private Integer itemPrice; + + // constructors + public Item() { + + } + + public Item(final Integer itemId, final String itemName, final String itemDescription) { + super(); + this.itemId = itemId; + this.itemName = itemName; + this.itemDescription = itemDescription; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((itemId == null) ? 0 : itemId.hashCode()); + return result; + } + + @Override + public boolean equals(final Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + final Item other = (Item) obj; + if (itemId == null) { + if (other.itemId != null) + return false; + } else if (!itemId.equals(other.itemId)) + return false; + return true; + } + + public Integer getItemId() { + return itemId; + } + + public void setItemId(final Integer itemId) { + this.itemId = itemId; + } + + public String getItemName() { + return itemName; + } + + public void setItemName(final String itemName) { + this.itemName = itemName; + } + + public String getItemDescription() { + return itemDescription; + } + + public Integer getItemPrice() { + return itemPrice; + } + + public void setItemPrice(final Integer itemPrice) { + this.itemPrice = itemPrice; + } + + public void setItemDescription(final String itemDescription) { + this.itemDescription = itemDescription; + } +} diff --git a/spring-hibernate5/src/main/java/com/baeldung/hibernate/criteria/util/HibernateUtil.java b/spring-hibernate5/src/main/java/com/baeldung/hibernate/criteria/util/HibernateUtil.java new file mode 100644 index 0000000000..9024ba9e4b --- /dev/null +++ b/spring-hibernate5/src/main/java/com/baeldung/hibernate/criteria/util/HibernateUtil.java @@ -0,0 +1,17 @@ +package com.baeldung.hibernate.criteria.util; + +import org.hibernate.Session; +import org.hibernate.SessionFactory; +import org.hibernate.cfg.Configuration; + +public class HibernateUtil { + + public static Session getHibernateSession() { + + final SessionFactory sf = new Configuration().configure("criteria.cfg.xml").buildSessionFactory(); + + final Session session = sf.openSession(); + return session; + } + +} diff --git a/spring-hibernate5/src/main/java/com/baeldung/hibernate/criteria/view/ApplicationView.java b/spring-hibernate5/src/main/java/com/baeldung/hibernate/criteria/view/ApplicationView.java new file mode 100644 index 0000000000..a854b51753 --- /dev/null +++ b/spring-hibernate5/src/main/java/com/baeldung/hibernate/criteria/view/ApplicationView.java @@ -0,0 +1,284 @@ +/** + * ApplicationViewer is the class that starts the application + * First it creates the session object and then creates the + * criteria query. + * + * @author Sandeep Kumar + * @version 1.0 + * @since 01/13/2017 + */ + +package com.baeldung.hibernate.criteria.view; + +import java.util.ArrayList; +import java.util.List; + +import javax.persistence.Tuple; +import javax.persistence.criteria.CriteriaBuilder; +import javax.persistence.criteria.CriteriaQuery; +import javax.persistence.criteria.Order; +import javax.persistence.criteria.Predicate; +import javax.persistence.criteria.Root; + +import org.hibernate.Session; +import org.hibernate.Transaction; + +import com.baeldung.hibernate.criteria.model.Item; +import com.baeldung.hibernate.criteria.util.HibernateUtil; + +public class ApplicationView { + + public ApplicationView() { + + } + + @SuppressWarnings("unchecked") + public boolean checkIfCriteriaTimeLower() { + final Session session = HibernateUtil.getHibernateSession(); + CriteriaBuilder builder = session.getCriteriaBuilder(); + CriteriaQuery criteriaItem = builder.createQuery(Item.class); + Root rootItem = criteriaItem.from(Item.class); + Transaction tx = null; + + // calculate the time taken by criteria + final long startTimeCriteria = System.nanoTime(); + criteriaItem.select(rootItem).where(builder.like(rootItem.get("itemName"), "%item One%")); + final List results = session.createQuery(criteriaItem).getResultList(); + final long endTimeCriteria = System.nanoTime(); + final long durationCriteria = (endTimeCriteria - startTimeCriteria) / 1000; + + // calculate the time taken by HQL + final long startTimeHQL = System.nanoTime(); + tx = session.beginTransaction(); + final List items = session.createQuery("FROM Item where itemName like '%item One%'").getResultList(); + final long endTimeHQL = System.nanoTime(); + final long durationHQL = (endTimeHQL - startTimeHQL) / 1000; + + if (durationCriteria > durationHQL) { + return false; + } else { + return true; + } + } + + // To get items having price more than 1000 + public String[] greaterThanCriteria() { + final Session session = HibernateUtil.getHibernateSession(); + CriteriaBuilder builder = session.getCriteriaBuilder(); + CriteriaQuery criteriaItem = builder.createQuery(Item.class); + Root rootItem = criteriaItem.from(Item.class); + criteriaItem.select(rootItem).where(builder.greaterThan(rootItem.get("itemPrice"), 1000)); + final List greaterThanItemsList = session.createQuery(criteriaItem).getResultList(); + final String greaterThanItems[] = new String[greaterThanItemsList.size()]; + for (int i = 0; i < greaterThanItemsList.size(); i++) { + greaterThanItems[i] = greaterThanItemsList.get(i).getItemName(); + } + session.close(); + return greaterThanItems; + } + + // To get items having price less than 1000 + public String[] lessThanCriteria() { + final Session session = HibernateUtil.getHibernateSession(); + CriteriaBuilder builder = session.getCriteriaBuilder(); + CriteriaQuery criteriaItem = builder.createQuery(Item.class); + Root rootItem = criteriaItem.from(Item.class); + criteriaItem.select(rootItem).where(builder.lessThan(rootItem.get("itemPrice"), 1000)); + final List lessThanItemsList = session.createQuery(criteriaItem).getResultList(); + final String lessThanItems[] = new String[lessThanItemsList.size()]; + for (int i = 0; i < lessThanItemsList.size(); i++) { + lessThanItems[i] = lessThanItemsList.get(i).getItemName(); + } + session.close(); + return lessThanItems; + } + + // To get items whose Name start with Chair + public String[] likeCriteria() { + final Session session = HibernateUtil.getHibernateSession(); + + CriteriaBuilder builder = session.getCriteriaBuilder(); + CriteriaQuery criteriaItem = builder.createQuery(Item.class); + Root rootItem = criteriaItem.from(Item.class); + criteriaItem.select(rootItem).where(builder.like(rootItem.get("itemName"), "%chair%")); + final List likeItemsList = session.createQuery(criteriaItem).getResultList(); + final String likeItems[] = new String[likeItemsList.size()]; + for (int i = 0; i < likeItemsList.size(); i++) { + likeItems[i] = likeItemsList.get(i).getItemName(); + } + session.close(); + return likeItems; + } + + // Case sensitive search + public String[] likeCaseCriteria() { + final Session session = HibernateUtil.getHibernateSession(); + CriteriaBuilder builder = session.getCriteriaBuilder(); + CriteriaQuery criteriaItem = builder.createQuery(Item.class); + Root rootItem = criteriaItem.from(Item.class); + criteriaItem.select(rootItem).where(builder.like(rootItem.get("itemName"), "%Chair%")); + final List ilikeItemsList = session.createQuery(criteriaItem).getResultList(); + final String ilikeItems[] = new String[ilikeItemsList.size()]; + for (int i = 0; i < ilikeItemsList.size(); i++) { + ilikeItems[i] = ilikeItemsList.get(i).getItemName(); + } + session.close(); + return ilikeItems; + } + + // To get records having itemPrice in between 100 and 200 + public String[] betweenCriteria() { + final Session session = HibernateUtil.getHibernateSession(); + CriteriaBuilder builder = session.getCriteriaBuilder(); + CriteriaQuery criteriaItem = builder.createQuery(Item.class); + Root rootItem = criteriaItem.from(Item.class); + // To get items having price more than 1000 + criteriaItem.select(rootItem).where(builder.between(rootItem.get("itemPrice"), 100, 200)); + final List betweenItemsList = session.createQuery(criteriaItem).getResultList(); + final String betweenItems[] = new String[betweenItemsList.size()]; + for (int i = 0; i < betweenItemsList.size(); i++) { + betweenItems[i] = betweenItemsList.get(i).getItemName(); + } + session.close(); + return betweenItems; + } + + // To check if the given property is null + public String[] nullCriteria() { + final Session session = HibernateUtil.getHibernateSession(); + CriteriaBuilder builder = session.getCriteriaBuilder(); + CriteriaQuery criteriaItem = builder.createQuery(Item.class); + Root rootItem = criteriaItem.from(Item.class); + criteriaItem.select(rootItem).where(builder.isNull(rootItem.get("itemDescription"))); + final List nullItemsList = session.createQuery(criteriaItem).getResultList(); + final String nullDescItems[] = new String[nullItemsList.size()]; + for (int i = 0; i < nullItemsList.size(); i++) { + nullDescItems[i] = nullItemsList.get(i).getItemName(); + } + session.close(); + return nullDescItems; + } + + // To check if the given property is not null + public String[] notNullCriteria() { + final Session session = HibernateUtil.getHibernateSession(); + CriteriaBuilder builder = session.getCriteriaBuilder(); + CriteriaQuery criteriaItem = builder.createQuery(Item.class); + Root rootItem = criteriaItem.from(Item.class); + criteriaItem.select(rootItem).where(builder.isNotNull(rootItem.get("itemDescription"))); + final List notNullItemsList = session.createQuery(criteriaItem).getResultList(); + final String notNullDescItems[] = new String[notNullItemsList.size()]; + for (int i = 0; i < notNullItemsList.size(); i++) { + notNullDescItems[i] = notNullItemsList.get(i).getItemName(); + } + session.close(); + return notNullDescItems; + } + + // Adding more than one expression in one cr + public String[] twoCriteria() { + final Session session = HibernateUtil.getHibernateSession(); + CriteriaBuilder builder = session.getCriteriaBuilder(); + CriteriaQuery criteriaItem = builder.createQuery(Item.class); + Root rootItem = criteriaItem.from(Item.class); + criteriaItem.select(rootItem).where(builder.isNull(rootItem.get("itemDescription"))) + .where(builder.like(rootItem.get("itemName"), "chair%")); + final List notNullItemsList = session.createQuery(criteriaItem).getResultList(); + final String notNullDescItems[] = new String[notNullItemsList.size()]; + for (int i = 0; i < notNullItemsList.size(); i++) { + notNullDescItems[i] = notNullItemsList.get(i).getItemName(); + } + session.close(); + return notNullDescItems; + } + + // To get items matching with the above defined conditions joined + // with Logical AND + public String[] andLogicalCriteria() { + List predicates = new ArrayList(); + final Session session = HibernateUtil.getHibernateSession(); + CriteriaBuilder builder = session.getCriteriaBuilder(); + CriteriaQuery criteriaItem = builder.createQuery(Item.class); + Root rootItem = criteriaItem.from(Item.class); + predicates.add(builder.greaterThan(rootItem.get("itemPrice"), 1000)); + predicates.add(builder.like(rootItem.get("itemName"), "Chair%")); + Predicate andPredicate = builder.and(predicates.toArray(new Predicate[] {})); + criteriaItem.select(rootItem).where(andPredicate); + final List andItemsList = session.createQuery(criteriaItem).getResultList(); + final String andItems[] = new String[andItemsList.size()]; + for (int i = 0; i < andItemsList.size(); i++) { + andItems[i] = andItemsList.get(i).getItemName(); + } + session.close(); + return andItems; + } + + // To get items matching with the above defined conditions joined + // with Logical OR + public String[] orLogicalCriteria() { + List predicates = new ArrayList(); + final Session session = HibernateUtil.getHibernateSession(); + CriteriaBuilder builder = session.getCriteriaBuilder(); + CriteriaQuery criteriaItem = builder.createQuery(Item.class); + Root rootItem = criteriaItem.from(Item.class); + predicates.add(builder.greaterThan(rootItem.get("itemPrice"), 1000)); + predicates.add(builder.like(rootItem.get("itemName"), "Chair%")); + Predicate orPredicate = builder.or(predicates.toArray(new Predicate[] {})); + criteriaItem.select(rootItem).where(orPredicate); + final List orItemsList = session.createQuery(criteriaItem).getResultList(); + final String orItems[] = new String[orItemsList.size()]; + for (int i = 0; i < orItemsList.size(); i++) { + orItems[i] = orItemsList.get(i).getItemName(); + } + session.close(); + return orItems; + } + + // Sorting example + public String[] sortingCriteria() { + List listOrders = new ArrayList(); + final Session session = HibernateUtil.getHibernateSession(); + CriteriaBuilder builder = session.getCriteriaBuilder(); + CriteriaQuery criteriaItem = builder.createQuery(Item.class); + Root rootItem = criteriaItem.from(Item.class); + listOrders.add(builder.asc(rootItem.get("itemName"))); + listOrders.add(builder.desc(rootItem.get("itemPrice"))); + criteriaItem.orderBy(listOrders.toArray(new Order[] {})); + final List sortedItemsList = session.createQuery(criteriaItem).getResultList(); + final String sortedItems[] = new String[sortedItemsList.size()]; + for (int i = 0; i < sortedItemsList.size(); i++) { + sortedItems[i] = sortedItemsList.get(i).getItemName(); + } + session.close(); + return sortedItems; + } + + // Set projections Row Count + public Long[] projectionRowCount() { + final Session session = HibernateUtil.getHibernateSession(); + CriteriaBuilder builder = session.getCriteriaBuilder(); + CriteriaQuery criteriaItem = builder.createQuery(Tuple.class); + Root rootItem = criteriaItem.from(Item.class); + criteriaItem.multiselect(builder.count(rootItem)); + final List itemProjected = session.createQuery(criteriaItem).getResultList(); + final Long projectedRowCount[] = new Long[1]; + projectedRowCount[0] = (long) itemProjected.get(0).get(0); + session.close(); + return projectedRowCount; + } + + // Set projections average of itemPrice + public Double[] projectionAverage() { + final Session session = HibernateUtil.getHibernateSession(); + CriteriaBuilder builder = session.getCriteriaBuilder(); + CriteriaQuery criteriaItem = builder.createQuery(Tuple.class); + Root rootItem = criteriaItem.from(Item.class); + criteriaItem.multiselect(builder.avg(rootItem.get("itemPrice"))); + final List itemProjected = session.createQuery(criteriaItem).getResultList(); + Double avgItemPrice[] = new Double[1]; + avgItemPrice[0] = Double.valueOf(itemProjected.get(0).get(0).toString()); + session.close(); + return avgItemPrice; + } + +} diff --git a/spring-hibernate5/src/main/java/com/baeldung/hibernate/fetching/model/OrderDetail.java b/spring-hibernate5/src/main/java/com/baeldung/hibernate/fetching/model/OrderDetail.java new file mode 100644 index 0000000000..f4a9b8a678 --- /dev/null +++ b/spring-hibernate5/src/main/java/com/baeldung/hibernate/fetching/model/OrderDetail.java @@ -0,0 +1,58 @@ +package com.baeldung.hibernate.fetching.model; + +import javax.persistence.*; +import java.io.Serializable; +import java.sql.Date; + +@Entity +@Table(name = "USER_ORDER") +public class OrderDetail implements Serializable { + + private static final long serialVersionUID = 1L; + + @Id + @GeneratedValue + @Column(name = "ORDER_ID") + private Long orderId; + + public OrderDetail() { + } + + public OrderDetail(Date orderDate, String orderDesc) { + super(); + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((orderId == null) ? 0 : orderId.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + OrderDetail other = (OrderDetail) obj; + if (orderId == null) { + if (other.orderId != null) + return false; + } else if (!orderId.equals(other.orderId)) + return false; + + return true; + } + + public Long getOrderId() { + return orderId; + } + + public void setOrderId(Long orderId) { + this.orderId = orderId; + } +} diff --git a/spring-hibernate5/src/main/java/com/baeldung/hibernate/fetching/model/UserEager.java b/spring-hibernate5/src/main/java/com/baeldung/hibernate/fetching/model/UserEager.java new file mode 100644 index 0000000000..a1aa746399 --- /dev/null +++ b/spring-hibernate5/src/main/java/com/baeldung/hibernate/fetching/model/UserEager.java @@ -0,0 +1,71 @@ +package com.baeldung.hibernate.fetching.model; + +import javax.persistence.*; +import java.io.Serializable; +import java.util.HashSet; +import java.util.Set; + +@Entity +@Table(name = "USER") +public class UserEager implements Serializable { + + private static final long serialVersionUID = 1L; + + @Id + @GeneratedValue + @Column(name = "USER_ID") + private Long userId; + + @OneToMany(fetch = FetchType.EAGER, mappedBy = "user") + private Set orderDetail = new HashSet(); + + public UserEager() { + } + + public UserEager(final Long userId) { + super(); + this.userId = userId; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((userId == null) ? 0 : userId.hashCode()); + return result; + } + + @Override + public boolean equals(final Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + final UserEager other = (UserEager) obj; + if (userId == null) { + if (other.userId != null) + return false; + } else if (!userId.equals(other.userId)) + return false; + return true; + } + + public Long getUserId() { + return userId; + } + + public void setUserId(final Long userId) { + this.userId = userId; + } + + public Set getOrderDetail() { + return orderDetail; + } + + public void setOrderDetail(Set orderDetail) { + this.orderDetail = orderDetail; + } + +} diff --git a/spring-hibernate5/src/main/java/com/baeldung/hibernate/fetching/model/UserLazy.java b/spring-hibernate5/src/main/java/com/baeldung/hibernate/fetching/model/UserLazy.java new file mode 100644 index 0000000000..e1354b5d24 --- /dev/null +++ b/spring-hibernate5/src/main/java/com/baeldung/hibernate/fetching/model/UserLazy.java @@ -0,0 +1,71 @@ +package com.baeldung.hibernate.fetching.model; + +import javax.persistence.*; +import java.io.Serializable; +import java.util.HashSet; +import java.util.Set; + +@Entity +@Table(name = "USER") +public class UserLazy implements Serializable { + + private static final long serialVersionUID = 1L; + + @Id + @GeneratedValue + @Column(name = "USER_ID") + private Long userId; + + @OneToMany(fetch = FetchType.LAZY, mappedBy = "user") + private Set orderDetail = new HashSet(); + + public UserLazy() { + } + + public UserLazy(final Long userId) { + super(); + this.userId = userId; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((userId == null) ? 0 : userId.hashCode()); + return result; + } + + @Override + public boolean equals(final Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + final UserLazy other = (UserLazy) obj; + if (userId == null) { + if (other.userId != null) + return false; + } else if (!userId.equals(other.userId)) + return false; + return true; + } + + public Long getUserId() { + return userId; + } + + public void setUserId(final Long userId) { + this.userId = userId; + } + + public Set getOrderDetail() { + return orderDetail; + } + + public void setOrderDetail(Set orderDetail) { + this.orderDetail = orderDetail; + } + +} diff --git a/spring-hibernate5/src/main/java/com/baeldung/hibernate/fetching/util/HibernateUtil.java b/spring-hibernate5/src/main/java/com/baeldung/hibernate/fetching/util/HibernateUtil.java new file mode 100644 index 0000000000..422de2ea93 --- /dev/null +++ b/spring-hibernate5/src/main/java/com/baeldung/hibernate/fetching/util/HibernateUtil.java @@ -0,0 +1,28 @@ +package com.baeldung.hibernate.fetching.util; + +import org.hibernate.Session; +import org.hibernate.SessionFactory; +import org.hibernate.cfg.Configuration; + +public class HibernateUtil { + + public static Session getHibernateSession(String fetchMethod) { + // two config files are there + // one with lazy loading enabled + // another lazy = false + SessionFactory sf; + if ("lazy".equals(fetchMethod)) { + sf = new Configuration().configure("fetchingLazy.cfg.xml").buildSessionFactory(); + } else { + sf = new Configuration().configure("fetching.cfg.xml").buildSessionFactory(); + } + + // fetching.cfg.xml is used for this example + return sf.openSession(); + } + + public static Session getHibernateSession() { + return new Configuration().configure("fetching.cfg.xml").buildSessionFactory().openSession(); + } + +} diff --git a/spring-hibernate5/src/main/java/com/baeldung/hibernate/fetching/view/FetchingAppView.java b/spring-hibernate5/src/main/java/com/baeldung/hibernate/fetching/view/FetchingAppView.java new file mode 100644 index 0000000000..7fc543f693 --- /dev/null +++ b/spring-hibernate5/src/main/java/com/baeldung/hibernate/fetching/view/FetchingAppView.java @@ -0,0 +1,68 @@ +package com.baeldung.hibernate.fetching.view; + +import com.baeldung.hibernate.fetching.model.OrderDetail; +import com.baeldung.hibernate.fetching.model.UserEager; +import com.baeldung.hibernate.fetching.model.UserLazy; +import com.baeldung.hibernate.fetching.util.HibernateUtil; +import org.hibernate.Session; +import org.hibernate.Transaction; + +import java.util.List; +import java.util.Set; + +public class FetchingAppView { + + public FetchingAppView() { + + } + + // lazily loaded + public Set lazyLoaded() { + final Session sessionLazy = HibernateUtil.getHibernateSession("lazy"); + List users = sessionLazy.createQuery("From UserLazy").getResultList(); + UserLazy userLazyLoaded = users.get(3); + // since data is lazyloaded so data won't be initialized + return (userLazyLoaded.getOrderDetail()); + } + + // eagerly loaded + public Set eagerLoaded() { + final Session sessionEager = HibernateUtil.getHibernateSession(); + // data should be loaded in the following line + // also note the queries generated + List user = sessionEager.createQuery("From UserEager").getResultList(); + UserEager userEagerLoaded = user.get(3); + return userEagerLoaded.getOrderDetail(); + } + + // creates test data + // call this method to create the data in the database + public void createTestData() { + + final Session session = HibernateUtil.getHibernateSession("lazy"); + Transaction tx = session.beginTransaction(); + final UserLazy user1 = new UserLazy(); + final UserLazy user2 = new UserLazy(); + final UserLazy user3 = new UserLazy(); + + session.save(user1); + session.save(user2); + session.save(user3); + + final OrderDetail order1 = new OrderDetail(); + final OrderDetail order2 = new OrderDetail(); + final OrderDetail order3 = new OrderDetail(); + final OrderDetail order4 = new OrderDetail(); + final OrderDetail order5 = new OrderDetail(); + + session.saveOrUpdate(order1); + session.saveOrUpdate(order2); + session.saveOrUpdate(order3); + session.saveOrUpdate(order4); + session.saveOrUpdate(order5); + + tx.commit(); + session.close(); + + } +} diff --git a/spring-hibernate5/src/main/java/com/baeldung/persistence/dao/IBarAuditableDao.java b/spring-hibernate5/src/main/java/com/baeldung/persistence/dao/IBarAuditableDao.java new file mode 100644 index 0000000000..182b493592 --- /dev/null +++ b/spring-hibernate5/src/main/java/com/baeldung/persistence/dao/IBarAuditableDao.java @@ -0,0 +1,8 @@ +package com.baeldung.persistence.dao; + +import com.baeldung.persistence.dao.common.IAuditOperations; +import com.baeldung.persistence.model.Bar; + +public interface IBarAuditableDao extends IBarDao, IAuditOperations { + // +} diff --git a/spring-hibernate5/src/main/java/com/baeldung/persistence/dao/IBarCrudRepository.java b/spring-hibernate5/src/main/java/com/baeldung/persistence/dao/IBarCrudRepository.java new file mode 100644 index 0000000000..4d7db64240 --- /dev/null +++ b/spring-hibernate5/src/main/java/com/baeldung/persistence/dao/IBarCrudRepository.java @@ -0,0 +1,10 @@ +package com.baeldung.persistence.dao; + +import java.io.Serializable; + +import com.baeldung.persistence.model.Bar; +import org.springframework.data.repository.CrudRepository; + +public interface IBarCrudRepository extends CrudRepository { + // +} diff --git a/spring-hibernate5/src/main/java/com/baeldung/persistence/dao/IBarDao.java b/spring-hibernate5/src/main/java/com/baeldung/persistence/dao/IBarDao.java new file mode 100644 index 0000000000..7896a2a84a --- /dev/null +++ b/spring-hibernate5/src/main/java/com/baeldung/persistence/dao/IBarDao.java @@ -0,0 +1,8 @@ +package com.baeldung.persistence.dao; + +import com.baeldung.persistence.dao.common.IOperations; +import com.baeldung.persistence.model.Bar; + +public interface IBarDao extends IOperations { + // +} diff --git a/spring-hibernate5/src/main/java/com/baeldung/persistence/dao/IChildDao.java b/spring-hibernate5/src/main/java/com/baeldung/persistence/dao/IChildDao.java new file mode 100644 index 0000000000..a55a0b0598 --- /dev/null +++ b/spring-hibernate5/src/main/java/com/baeldung/persistence/dao/IChildDao.java @@ -0,0 +1,8 @@ +package com.baeldung.persistence.dao; + +import com.baeldung.persistence.model.Child; +import com.baeldung.persistence.dao.common.IOperations; + +public interface IChildDao extends IOperations { + // +} diff --git a/spring-hibernate5/src/main/java/com/baeldung/persistence/dao/IFooAuditableDao.java b/spring-hibernate5/src/main/java/com/baeldung/persistence/dao/IFooAuditableDao.java new file mode 100644 index 0000000000..ddbb685988 --- /dev/null +++ b/spring-hibernate5/src/main/java/com/baeldung/persistence/dao/IFooAuditableDao.java @@ -0,0 +1,8 @@ +package com.baeldung.persistence.dao; + +import com.baeldung.persistence.dao.common.IAuditOperations; +import com.baeldung.persistence.model.Foo; + +public interface IFooAuditableDao extends IFooDao, IAuditOperations { + // +} \ No newline at end of file diff --git a/spring-hibernate5/src/main/java/com/baeldung/persistence/dao/IFooDao.java b/spring-hibernate5/src/main/java/com/baeldung/persistence/dao/IFooDao.java new file mode 100644 index 0000000000..0935772dbd --- /dev/null +++ b/spring-hibernate5/src/main/java/com/baeldung/persistence/dao/IFooDao.java @@ -0,0 +1,8 @@ +package com.baeldung.persistence.dao; + +import com.baeldung.persistence.model.Foo; +import com.baeldung.persistence.dao.common.IOperations; + +public interface IFooDao extends IOperations { + // +} diff --git a/spring-hibernate5/src/main/java/com/baeldung/persistence/dao/IParentDao.java b/spring-hibernate5/src/main/java/com/baeldung/persistence/dao/IParentDao.java new file mode 100644 index 0000000000..03680158bb --- /dev/null +++ b/spring-hibernate5/src/main/java/com/baeldung/persistence/dao/IParentDao.java @@ -0,0 +1,8 @@ +package com.baeldung.persistence.dao; + +import com.baeldung.persistence.model.Parent; +import com.baeldung.persistence.dao.common.IOperations; + +public interface IParentDao extends IOperations { + // +} diff --git a/spring-hibernate5/src/main/java/com/baeldung/persistence/dao/common/AbstractDao.java b/spring-hibernate5/src/main/java/com/baeldung/persistence/dao/common/AbstractDao.java new file mode 100644 index 0000000000..5a6c76a93a --- /dev/null +++ b/spring-hibernate5/src/main/java/com/baeldung/persistence/dao/common/AbstractDao.java @@ -0,0 +1,14 @@ +package com.baeldung.persistence.dao.common; + +import java.io.Serializable; + +import com.google.common.base.Preconditions; + +public abstract class AbstractDao implements IOperations { + + protected Class clazz; + + protected final void setClazz(final Class clazzToSet) { + clazz = Preconditions.checkNotNull(clazzToSet); + } +} diff --git a/spring-hibernate5/src/main/java/com/baeldung/persistence/dao/common/AbstractHibernateAuditableDao.java b/spring-hibernate5/src/main/java/com/baeldung/persistence/dao/common/AbstractHibernateAuditableDao.java new file mode 100644 index 0000000000..41184669ad --- /dev/null +++ b/spring-hibernate5/src/main/java/com/baeldung/persistence/dao/common/AbstractHibernateAuditableDao.java @@ -0,0 +1,37 @@ +package com.baeldung.persistence.dao.common; + +import java.io.Serializable; +import java.util.List; + +import org.hibernate.envers.AuditReader; +import org.hibernate.envers.AuditReaderFactory; +import org.hibernate.envers.query.AuditQuery; + +@SuppressWarnings("unchecked") +public class AbstractHibernateAuditableDao extends AbstractHibernateDao implements IAuditOperations { + + @Override + public List getEntitiesAtRevision(final Number revision) { + final AuditReader auditReader = AuditReaderFactory.get(getCurrentSession()); + final AuditQuery query = auditReader.createQuery().forEntitiesAtRevision(clazz, revision); + final List resultList = query.getResultList(); + return resultList; + } + + @Override + public List getEntitiesModifiedAtRevision(final Number revision) { + final AuditReader auditReader = AuditReaderFactory.get(getCurrentSession()); + final AuditQuery query = auditReader.createQuery().forEntitiesModifiedAtRevision(clazz, revision); + final List resultList = query.getResultList(); + return resultList; + } + + @Override + public List getRevisions() { + final AuditReader auditReader = AuditReaderFactory.get(getCurrentSession()); + final AuditQuery query = auditReader.createQuery().forRevisionsOfEntity(clazz, true, true); + final List resultList = query.getResultList(); + return resultList; + } + +} diff --git a/spring-hibernate5/src/main/java/com/baeldung/persistence/dao/common/AbstractHibernateDao.java b/spring-hibernate5/src/main/java/com/baeldung/persistence/dao/common/AbstractHibernateDao.java new file mode 100644 index 0000000000..f34866d883 --- /dev/null +++ b/spring-hibernate5/src/main/java/com/baeldung/persistence/dao/common/AbstractHibernateDao.java @@ -0,0 +1,59 @@ +package com.baeldung.persistence.dao.common; + +import java.io.Serializable; +import java.util.List; + +import org.hibernate.Session; +import org.hibernate.SessionFactory; +import org.springframework.beans.factory.annotation.Autowired; + +import com.google.common.base.Preconditions; + +@SuppressWarnings("unchecked") +public abstract class AbstractHibernateDao extends AbstractDao implements IOperations { + + @Autowired + protected SessionFactory sessionFactory; + + // API + + @Override + public T findOne(final long id) { + return (T) getCurrentSession().get(clazz, id); + } + + @Override + public List findAll() { + return getCurrentSession().createQuery("from " + clazz.getName()).getResultList(); + } + + @Override + public void create(final T entity) { + Preconditions.checkNotNull(entity); + getCurrentSession().saveOrUpdate(entity); + } + + @Override + public T update(final T entity) { + Preconditions.checkNotNull(entity); + return (T) getCurrentSession().merge(entity); + } + + @Override + public void delete(final T entity) { + Preconditions.checkNotNull(entity); + getCurrentSession().delete(entity); + } + + @Override + public void deleteById(final long entityId) { + final T entity = findOne(entityId); + Preconditions.checkState(entity != null); + delete(entity); + } + + protected Session getCurrentSession() { + return sessionFactory.getCurrentSession(); + } + +} \ No newline at end of file diff --git a/spring-hibernate5/src/main/java/com/baeldung/persistence/dao/common/AbstractJpaDao.java b/spring-hibernate5/src/main/java/com/baeldung/persistence/dao/common/AbstractJpaDao.java new file mode 100644 index 0000000000..69f8e58c25 --- /dev/null +++ b/spring-hibernate5/src/main/java/com/baeldung/persistence/dao/common/AbstractJpaDao.java @@ -0,0 +1,56 @@ +package com.baeldung.persistence.dao.common; + +import java.io.Serializable; +import java.util.List; + +import javax.persistence.EntityManager; +import javax.persistence.PersistenceContext; +import javax.persistence.TypedQuery; +import javax.persistence.criteria.CriteriaBuilder; +import javax.persistence.criteria.CriteriaQuery; +import javax.persistence.criteria.Root; + +public class AbstractJpaDao extends AbstractDao implements IOperations { + + @PersistenceContext + private EntityManager em; + + // API + + @Override + public T findOne(final long id) { + return em.find(clazz, Long.valueOf(id).intValue()); + } + + @Override + public List findAll() { + final CriteriaBuilder cb = em.getCriteriaBuilder(); + final CriteriaQuery cq = cb.createQuery(clazz); + final Root rootEntry = cq.from(clazz); + final CriteriaQuery all = cq.select(rootEntry); + final TypedQuery allQuery = em.createQuery(all); + return allQuery.getResultList(); + } + + @Override + public void create(final T entity) { + em.persist(entity); + } + + @Override + public T update(final T entity) { + em.merge(entity); + return entity; + } + + @Override + public void delete(final T entity) { + em.remove(entity); + } + + @Override + public void deleteById(final long entityId) { + delete(findOne(entityId)); + } + +} diff --git a/spring-hibernate5/src/main/java/com/baeldung/persistence/dao/common/GenericHibernateDao.java b/spring-hibernate5/src/main/java/com/baeldung/persistence/dao/common/GenericHibernateDao.java new file mode 100644 index 0000000000..18b16fa033 --- /dev/null +++ b/spring-hibernate5/src/main/java/com/baeldung/persistence/dao/common/GenericHibernateDao.java @@ -0,0 +1,13 @@ +package com.baeldung.persistence.dao.common; + +import java.io.Serializable; + +import org.springframework.beans.factory.config.BeanDefinition; +import org.springframework.context.annotation.Scope; +import org.springframework.stereotype.Repository; + +@Repository +@Scope(BeanDefinition.SCOPE_PROTOTYPE) +public class GenericHibernateDao extends AbstractHibernateDao implements IGenericDao { + // +} \ No newline at end of file diff --git a/spring-hibernate5/src/main/java/com/baeldung/persistence/dao/common/IAuditOperations.java b/spring-hibernate5/src/main/java/com/baeldung/persistence/dao/common/IAuditOperations.java new file mode 100644 index 0000000000..169d3fed72 --- /dev/null +++ b/spring-hibernate5/src/main/java/com/baeldung/persistence/dao/common/IAuditOperations.java @@ -0,0 +1,14 @@ +package com.baeldung.persistence.dao.common; + +import java.io.Serializable; +import java.util.List; + +public interface IAuditOperations { + + List getEntitiesAtRevision(Number revision); + + List getEntitiesModifiedAtRevision(Number revision); + + List getRevisions(); + +} diff --git a/spring-hibernate5/src/main/java/com/baeldung/persistence/dao/common/IGenericDao.java b/spring-hibernate5/src/main/java/com/baeldung/persistence/dao/common/IGenericDao.java new file mode 100644 index 0000000000..8d8af18394 --- /dev/null +++ b/spring-hibernate5/src/main/java/com/baeldung/persistence/dao/common/IGenericDao.java @@ -0,0 +1,7 @@ +package com.baeldung.persistence.dao.common; + +import java.io.Serializable; + +public interface IGenericDao extends IOperations { + // +} diff --git a/spring-hibernate5/src/main/java/com/baeldung/persistence/dao/common/IOperations.java b/spring-hibernate5/src/main/java/com/baeldung/persistence/dao/common/IOperations.java new file mode 100644 index 0000000000..4ef99221ab --- /dev/null +++ b/spring-hibernate5/src/main/java/com/baeldung/persistence/dao/common/IOperations.java @@ -0,0 +1,20 @@ +package com.baeldung.persistence.dao.common; + +import java.io.Serializable; +import java.util.List; + +public interface IOperations { + + T findOne(final long id); + + List findAll(); + + void create(final T entity); + + T update(final T entity); + + void delete(final T entity); + + void deleteById(final long entityId); + +} diff --git a/spring-hibernate5/src/main/java/com/baeldung/persistence/dao/impl/BarAuditableDao.java b/spring-hibernate5/src/main/java/com/baeldung/persistence/dao/impl/BarAuditableDao.java new file mode 100644 index 0000000000..e12b6ae2da --- /dev/null +++ b/spring-hibernate5/src/main/java/com/baeldung/persistence/dao/impl/BarAuditableDao.java @@ -0,0 +1,28 @@ +package com.baeldung.persistence.dao.impl; + +import java.util.List; + +import com.baeldung.persistence.dao.IBarAuditableDao; +import com.baeldung.persistence.dao.common.AbstractHibernateAuditableDao; +import com.baeldung.persistence.model.Bar; + +public class BarAuditableDao extends AbstractHibernateAuditableDao implements IBarAuditableDao { + + public BarAuditableDao() { + super(); + + setClazz(Bar.class); + } + + // API + + @Override + public List getRevisions() { + final List resultList = super.getRevisions(); + for (final Bar bar : resultList) { + bar.getFooSet().size(); // force FooSet initialization + } + return resultList; + } + +} \ No newline at end of file diff --git a/spring-hibernate5/src/main/java/com/baeldung/persistence/dao/impl/BarDao.java b/spring-hibernate5/src/main/java/com/baeldung/persistence/dao/impl/BarDao.java new file mode 100644 index 0000000000..0ead802dc5 --- /dev/null +++ b/spring-hibernate5/src/main/java/com/baeldung/persistence/dao/impl/BarDao.java @@ -0,0 +1,19 @@ +package com.baeldung.persistence.dao.impl; + +import com.baeldung.persistence.dao.common.AbstractHibernateDao; +import com.baeldung.persistence.dao.IBarDao; +import com.baeldung.persistence.model.Bar; +import org.springframework.stereotype.Repository; + +@Repository +public class BarDao extends AbstractHibernateDao implements IBarDao { + + public BarDao() { + super(); + + setClazz(Bar.class); + } + + // API + +} diff --git a/spring-hibernate5/src/main/java/com/baeldung/persistence/dao/impl/BarJpaDao.java b/spring-hibernate5/src/main/java/com/baeldung/persistence/dao/impl/BarJpaDao.java new file mode 100644 index 0000000000..e0fa382d41 --- /dev/null +++ b/spring-hibernate5/src/main/java/com/baeldung/persistence/dao/impl/BarJpaDao.java @@ -0,0 +1,19 @@ +package com.baeldung.persistence.dao.impl; + +import com.baeldung.persistence.dao.IBarDao; +import com.baeldung.persistence.dao.common.AbstractJpaDao; +import com.baeldung.persistence.model.Bar; +import org.springframework.stereotype.Repository; + +@Repository +public class BarJpaDao extends AbstractJpaDao implements IBarDao { + + public BarJpaDao() { + super(); + + setClazz(Bar.class); + } + + // API + +} diff --git a/spring-hibernate5/src/main/java/com/baeldung/persistence/dao/impl/ChildDao.java b/spring-hibernate5/src/main/java/com/baeldung/persistence/dao/impl/ChildDao.java new file mode 100644 index 0000000000..b55da6e43a --- /dev/null +++ b/spring-hibernate5/src/main/java/com/baeldung/persistence/dao/impl/ChildDao.java @@ -0,0 +1,19 @@ +package com.baeldung.persistence.dao.impl; + +import com.baeldung.persistence.dao.common.AbstractHibernateDao; +import com.baeldung.persistence.model.Child; +import com.baeldung.persistence.dao.IChildDao; +import org.springframework.stereotype.Repository; + +@Repository +public class ChildDao extends AbstractHibernateDao implements IChildDao { + + public ChildDao() { + super(); + + setClazz(Child.class); + } + + // API + +} diff --git a/spring-hibernate5/src/main/java/com/baeldung/persistence/dao/impl/FooAuditableDao.java b/spring-hibernate5/src/main/java/com/baeldung/persistence/dao/impl/FooAuditableDao.java new file mode 100644 index 0000000000..05064c1478 --- /dev/null +++ b/spring-hibernate5/src/main/java/com/baeldung/persistence/dao/impl/FooAuditableDao.java @@ -0,0 +1,17 @@ +package com.baeldung.persistence.dao.impl; + +import com.baeldung.persistence.dao.common.AbstractHibernateAuditableDao; +import com.baeldung.persistence.model.Foo; +import com.baeldung.persistence.dao.IFooAuditableDao; + +public class FooAuditableDao extends AbstractHibernateAuditableDao implements IFooAuditableDao { + + public FooAuditableDao() { + super(); + + setClazz(Foo.class); + } + + // API + +} diff --git a/spring-hibernate5/src/main/java/com/baeldung/persistence/dao/impl/FooDao.java b/spring-hibernate5/src/main/java/com/baeldung/persistence/dao/impl/FooDao.java new file mode 100644 index 0000000000..787c449b1d --- /dev/null +++ b/spring-hibernate5/src/main/java/com/baeldung/persistence/dao/impl/FooDao.java @@ -0,0 +1,19 @@ +package com.baeldung.persistence.dao.impl; + +import com.baeldung.persistence.dao.common.AbstractHibernateDao; +import com.baeldung.persistence.dao.IFooDao; +import com.baeldung.persistence.model.Foo; +import org.springframework.stereotype.Repository; + +@Repository +public class FooDao extends AbstractHibernateDao implements IFooDao { + + public FooDao() { + super(); + + setClazz(Foo.class); + } + + // API + +} diff --git a/spring-hibernate5/src/main/java/com/baeldung/persistence/dao/impl/ParentDao.java b/spring-hibernate5/src/main/java/com/baeldung/persistence/dao/impl/ParentDao.java new file mode 100644 index 0000000000..4602b5f30e --- /dev/null +++ b/spring-hibernate5/src/main/java/com/baeldung/persistence/dao/impl/ParentDao.java @@ -0,0 +1,19 @@ +package com.baeldung.persistence.dao.impl; + +import com.baeldung.persistence.dao.IParentDao; +import com.baeldung.persistence.dao.common.AbstractHibernateDao; +import com.baeldung.persistence.model.Parent; +import org.springframework.stereotype.Repository; + +@Repository +public class ParentDao extends AbstractHibernateDao implements IParentDao { + + public ParentDao() { + super(); + + setClazz(Parent.class); + } + + // API + +} diff --git a/spring-hibernate5/src/main/java/com/baeldung/persistence/model/Bar.java b/spring-hibernate5/src/main/java/com/baeldung/persistence/model/Bar.java new file mode 100644 index 0000000000..c7f05254cc --- /dev/null +++ b/spring-hibernate5/src/main/java/com/baeldung/persistence/model/Bar.java @@ -0,0 +1,242 @@ +package com.baeldung.persistence.model; + +import java.io.Serializable; +import java.util.Date; +import java.util.Set; + +import javax.persistence.CascadeType; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.EntityListeners; +import javax.persistence.FetchType; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.NamedQuery; +import javax.persistence.OneToMany; +import javax.persistence.PrePersist; +import javax.persistence.PreRemove; +import javax.persistence.PreUpdate; + +import org.hibernate.annotations.OrderBy; +import org.hibernate.envers.Audited; +import org.jboss.logging.Logger; +import org.springframework.data.annotation.CreatedBy; +import org.springframework.data.annotation.CreatedDate; +import org.springframework.data.annotation.LastModifiedBy; +import org.springframework.data.annotation.LastModifiedDate; +import org.springframework.data.jpa.domain.support.AuditingEntityListener; + +import com.google.common.collect.Sets; + +@Entity +@NamedQuery(name = "Bar.findAll", query = "SELECT b FROM Bar b") +@Audited +@EntityListeners(AuditingEntityListener.class) +public class Bar implements Serializable { + + private static Logger logger = Logger.getLogger(Bar.class); + + public enum OPERATION { + INSERT, UPDATE, DELETE; + private String value; + + OPERATION() { + value = toString(); + } + + public String getValue() { + return value; + } + + public static OPERATION parse(final String value) { + OPERATION operation = null; + for (final OPERATION op : OPERATION.values()) { + if (op.getValue().equals(value)) { + operation = op; + break; + } + } + return operation; + } + }; + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + @Column(name = "id") + private int id; + + @Column(name = "name") + private String name; + + @OneToMany(mappedBy = "bar", cascade = CascadeType.ALL, fetch = FetchType.LAZY) + @OrderBy(clause = "NAME DESC") + // @NotAudited + private Set fooSet = Sets.newHashSet(); + + @Column(name = "operation") + private String operation; + + @Column(name = "timestamp") + private long timestamp; + + @Column(name = "created_date", updatable = false, nullable = false) + @CreatedDate + private long createdDate; + + @Column(name = "modified_date") + @LastModifiedDate + private long modifiedDate; + + @Column(name = "created_by") + @CreatedBy + private String createdBy; + + @Column(name = "modified_by") + @LastModifiedBy + private String modifiedBy; + + public Bar() { + super(); + } + + public Bar(final String name) { + super(); + + this.name = name; + } + + // API + + public Set getFooSet() { + return fooSet; + } + + public void setFooSet(final Set fooSet) { + this.fooSet = fooSet; + } + + public int getId() { + return id; + } + + public void setId(final int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(final String name) { + this.name = name; + } + + public OPERATION getOperation() { + return OPERATION.parse(operation); + } + + public void setOperation(final OPERATION operation) { + this.operation = operation.getValue(); + } + + public long getTimestamp() { + return timestamp; + } + + public void setTimestamp(final long timestamp) { + this.timestamp = timestamp; + } + + public long getCreatedDate() { + return createdDate; + } + + public void setCreatedDate(final long createdDate) { + this.createdDate = createdDate; + } + + public long getModifiedDate() { + return modifiedDate; + } + + public void setModifiedDate(final long modifiedDate) { + this.modifiedDate = modifiedDate; + } + + public String getCreatedBy() { + return createdBy; + } + + public void setCreatedBy(final String createdBy) { + this.createdBy = createdBy; + } + + public String getModifiedBy() { + return modifiedBy; + } + + public void setModifiedBy(final String modifiedBy) { + this.modifiedBy = modifiedBy; + } + + public void setOperation(final String operation) { + this.operation = operation; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((name == null) ? 0 : name.hashCode()); + return result; + } + + @Override + public boolean equals(final Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + final Bar other = (Bar) obj; + if (name == null) { + if (other.name != null) + return false; + } else if (!name.equals(other.name)) + return false; + return true; + } + + @Override + public String toString() { + final StringBuilder builder = new StringBuilder(); + builder.append("Bar [name=").append(name).append("]"); + return builder.toString(); + } + + @PrePersist + public void onPrePersist() { + logger.info("@PrePersist"); + audit(OPERATION.INSERT); + } + + @PreUpdate + public void onPreUpdate() { + logger.info("@PreUpdate"); + audit(OPERATION.UPDATE); + } + + @PreRemove + public void onPreRemove() { + logger.info("@PreRemove"); + audit(OPERATION.DELETE); + } + + private void audit(final OPERATION operation) { + setOperation(operation); + setTimestamp((new Date()).getTime()); + } + +} diff --git a/spring-hibernate5/src/main/java/com/baeldung/persistence/model/Child.java b/spring-hibernate5/src/main/java/com/baeldung/persistence/model/Child.java new file mode 100644 index 0000000000..19cfb2e237 --- /dev/null +++ b/spring-hibernate5/src/main/java/com/baeldung/persistence/model/Child.java @@ -0,0 +1,51 @@ +package com.baeldung.persistence.model; + +import java.io.Serializable; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.OneToOne; + +@Entity +public class Child implements Serializable { + + @Id + @GeneratedValue + private long id; + + @OneToOne(mappedBy = "child") + private Parent parent; + + public Child() { + super(); + } + + // API + + public long getId() { + return id; + } + + public void setId(final long id) { + this.id = id; + } + + public Parent getParent() { + return parent; + } + + public void setParent(final Parent parent) { + this.parent = parent; + } + + // + + @Override + public String toString() { + final StringBuilder builder = new StringBuilder(); + builder.append("Child [id=").append(id).append("]"); + return builder.toString(); + } + +} diff --git a/spring-hibernate5/src/main/java/com/baeldung/persistence/model/Foo.java b/spring-hibernate5/src/main/java/com/baeldung/persistence/model/Foo.java new file mode 100644 index 0000000000..d36a1e58cf --- /dev/null +++ b/spring-hibernate5/src/main/java/com/baeldung/persistence/model/Foo.java @@ -0,0 +1,105 @@ +package com.baeldung.persistence.model; + +import java.io.Serializable; + +import javax.persistence.CascadeType; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.ManyToOne; +import javax.persistence.NamedNativeQueries; +import javax.persistence.NamedNativeQuery; + +import org.hibernate.envers.Audited; + +@NamedNativeQueries({ @NamedNativeQuery(name = "callGetAllFoos", query = "CALL GetAllFoos()", resultClass = Foo.class), @NamedNativeQuery(name = "callGetFoosByName", query = "CALL GetFoosByName(:fooName)", resultClass = Foo.class) }) +@Entity +@Audited +// @Proxy(lazy = false) +public class Foo implements Serializable { + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + @Column(name = "id") + private long id; + + @Column(name = "name") + private String name; + + @ManyToOne(targetEntity = Bar.class, cascade = CascadeType.ALL, fetch = FetchType.EAGER) + @JoinColumn(name = "BAR_ID") + private Bar bar = new Bar(); + + public Foo() { + super(); + } + + public Foo(final String name) { + super(); + this.name = name; + } + + // + + public Bar getBar() { + return bar; + } + + public void setBar(final Bar bar) { + this.bar = bar; + } + + public long getId() { + return id; + } + + public void setId(final long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(final String name) { + this.name = name; + } + + // + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((name == null) ? 0 : name.hashCode()); + return result; + } + + @Override + public boolean equals(final Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + final Foo other = (Foo) obj; + if (name == null) { + if (other.name != null) + return false; + } else if (!name.equals(other.name)) + return false; + return true; + } + + @Override + public String toString() { + final StringBuilder builder = new StringBuilder(); + builder.append("Foo [name=").append(name).append("]"); + return builder.toString(); + } +} diff --git a/spring-hibernate5/src/main/java/com/baeldung/persistence/model/Parent.java b/spring-hibernate5/src/main/java/com/baeldung/persistence/model/Parent.java new file mode 100644 index 0000000000..fa6948990b --- /dev/null +++ b/spring-hibernate5/src/main/java/com/baeldung/persistence/model/Parent.java @@ -0,0 +1,60 @@ +package com.baeldung.persistence.model; + +import java.io.Serializable; + +import javax.persistence.CascadeType; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.OneToOne; + +@Entity +public class Parent implements Serializable { + + @Id + @GeneratedValue + private long id; + + @OneToOne(cascade = { CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH, CascadeType.DETACH }) + @JoinColumn(name = "child_fk") + private Child child; + + public Parent() { + super(); + } + + public Parent(final Child child) { + super(); + + this.child = child; + } + + // API + + public long getId() { + return id; + } + + public void setId(final long id) { + this.id = id; + } + + public Child getChild() { + return child; + } + + public void setChild(final Child child) { + this.child = child; + } + + // + + @Override + public String toString() { + final StringBuilder builder = new StringBuilder(); + builder.append("Parent [id=").append(id).append("]"); + return builder.toString(); + } + +} diff --git a/spring-hibernate5/src/main/java/com/baeldung/persistence/model/Person.java b/spring-hibernate5/src/main/java/com/baeldung/persistence/model/Person.java new file mode 100644 index 0000000000..6a95a7acf5 --- /dev/null +++ b/spring-hibernate5/src/main/java/com/baeldung/persistence/model/Person.java @@ -0,0 +1,31 @@ +package com.baeldung.persistence.model; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; + +@Entity +public class Person { + + @Id + @GeneratedValue + private Long id; + + private String name; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/spring-hibernate5/src/main/java/com/baeldung/persistence/service/IBarAuditableService.java b/spring-hibernate5/src/main/java/com/baeldung/persistence/service/IBarAuditableService.java new file mode 100644 index 0000000000..33e5634d12 --- /dev/null +++ b/spring-hibernate5/src/main/java/com/baeldung/persistence/service/IBarAuditableService.java @@ -0,0 +1,8 @@ +package com.baeldung.persistence.service; + +import com.baeldung.persistence.dao.common.IAuditOperations; +import com.baeldung.persistence.model.Bar; + +public interface IBarAuditableService extends IBarService, IAuditOperations { + +} diff --git a/spring-hibernate5/src/main/java/com/baeldung/persistence/service/IBarService.java b/spring-hibernate5/src/main/java/com/baeldung/persistence/service/IBarService.java new file mode 100644 index 0000000000..21185b5990 --- /dev/null +++ b/spring-hibernate5/src/main/java/com/baeldung/persistence/service/IBarService.java @@ -0,0 +1,8 @@ +package com.baeldung.persistence.service; + +import com.baeldung.persistence.dao.common.IOperations; +import com.baeldung.persistence.model.Bar; + +public interface IBarService extends IOperations { + // +} diff --git a/spring-hibernate5/src/main/java/com/baeldung/persistence/service/IChildService.java b/spring-hibernate5/src/main/java/com/baeldung/persistence/service/IChildService.java new file mode 100644 index 0000000000..afe67a70c2 --- /dev/null +++ b/spring-hibernate5/src/main/java/com/baeldung/persistence/service/IChildService.java @@ -0,0 +1,8 @@ +package com.baeldung.persistence.service; + +import com.baeldung.persistence.model.Child; +import com.baeldung.persistence.dao.common.IOperations; + +public interface IChildService extends IOperations { + // +} diff --git a/spring-hibernate5/src/main/java/com/baeldung/persistence/service/IFooAuditableService.java b/spring-hibernate5/src/main/java/com/baeldung/persistence/service/IFooAuditableService.java new file mode 100644 index 0000000000..b787e7fe91 --- /dev/null +++ b/spring-hibernate5/src/main/java/com/baeldung/persistence/service/IFooAuditableService.java @@ -0,0 +1,8 @@ +package com.baeldung.persistence.service; + +import com.baeldung.persistence.dao.common.IAuditOperations; +import com.baeldung.persistence.model.Foo; + +public interface IFooAuditableService extends IFooService, IAuditOperations { + // +} diff --git a/spring-hibernate5/src/main/java/com/baeldung/persistence/service/IFooService.java b/spring-hibernate5/src/main/java/com/baeldung/persistence/service/IFooService.java new file mode 100644 index 0000000000..ffdb53964a --- /dev/null +++ b/spring-hibernate5/src/main/java/com/baeldung/persistence/service/IFooService.java @@ -0,0 +1,8 @@ +package com.baeldung.persistence.service; + +import com.baeldung.persistence.model.Foo; +import com.baeldung.persistence.dao.common.IOperations; + +public interface IFooService extends IOperations { + // +} diff --git a/spring-hibernate5/src/main/java/com/baeldung/persistence/service/IParentService.java b/spring-hibernate5/src/main/java/com/baeldung/persistence/service/IParentService.java new file mode 100644 index 0000000000..f941416aac --- /dev/null +++ b/spring-hibernate5/src/main/java/com/baeldung/persistence/service/IParentService.java @@ -0,0 +1,8 @@ +package com.baeldung.persistence.service; + +import com.baeldung.persistence.model.Parent; +import com.baeldung.persistence.dao.common.IOperations; + +public interface IParentService extends IOperations { + // +} diff --git a/spring-hibernate5/src/main/java/com/baeldung/persistence/service/common/AbstractHibernateAuditableService.java b/spring-hibernate5/src/main/java/com/baeldung/persistence/service/common/AbstractHibernateAuditableService.java new file mode 100644 index 0000000000..8e2df15519 --- /dev/null +++ b/spring-hibernate5/src/main/java/com/baeldung/persistence/service/common/AbstractHibernateAuditableService.java @@ -0,0 +1,31 @@ +package com.baeldung.persistence.service.common; + +import java.io.Serializable; +import java.util.List; + +import com.baeldung.persistence.dao.common.IAuditOperations; +import com.baeldung.persistence.dao.common.IOperations; +import org.springframework.transaction.annotation.Transactional; + +@Transactional(value = "hibernateTransactionManager") +public abstract class AbstractHibernateAuditableService extends AbstractHibernateService + implements IOperations, IAuditOperations { + + @Override + public List getEntitiesAtRevision(final Number revision) { + return getAuditableDao().getEntitiesAtRevision(revision); + } + + @Override + public List getEntitiesModifiedAtRevision(final Number revision) { + return getAuditableDao().getEntitiesModifiedAtRevision(revision); + } + + @Override + public List getRevisions() { + return getAuditableDao().getRevisions(); + } + + abstract protected IAuditOperations getAuditableDao(); + +} diff --git a/spring-hibernate5/src/main/java/com/baeldung/persistence/service/common/AbstractHibernateService.java b/spring-hibernate5/src/main/java/com/baeldung/persistence/service/common/AbstractHibernateService.java new file mode 100644 index 0000000000..5da2f299f1 --- /dev/null +++ b/spring-hibernate5/src/main/java/com/baeldung/persistence/service/common/AbstractHibernateService.java @@ -0,0 +1,43 @@ +package com.baeldung.persistence.service.common; + +import java.io.Serializable; +import java.util.List; + +import com.baeldung.persistence.dao.common.IOperations; +import org.springframework.transaction.annotation.Transactional; + +@Transactional(value = "hibernateTransactionManager") +public abstract class AbstractHibernateService extends AbstractService implements + IOperations { + + @Override + public T findOne(final long id) { + return super.findOne(id); + } + + @Override + public List findAll() { + return super.findAll(); + } + + @Override + public void create(final T entity) { + super.create(entity); + } + + @Override + public T update(final T entity) { + return super.update(entity); + } + + @Override + public void delete(final T entity) { + super.delete(entity); + } + + @Override + public void deleteById(final long entityId) { + super.deleteById(entityId); + } + +} diff --git a/spring-hibernate5/src/main/java/com/baeldung/persistence/service/common/AbstractJpaService.java b/spring-hibernate5/src/main/java/com/baeldung/persistence/service/common/AbstractJpaService.java new file mode 100644 index 0000000000..a1c6fe9edf --- /dev/null +++ b/spring-hibernate5/src/main/java/com/baeldung/persistence/service/common/AbstractJpaService.java @@ -0,0 +1,42 @@ +package com.baeldung.persistence.service.common; + +import java.io.Serializable; +import java.util.List; + +import com.baeldung.persistence.dao.common.IOperations; +import org.springframework.transaction.annotation.Transactional; + +@Transactional(value = "jpaTransactionManager") +public abstract class AbstractJpaService extends AbstractService implements IOperations { + + @Override + public T findOne(final long id) { + return super.findOne(id); + } + + @Override + public List findAll() { + return super.findAll(); + } + + @Override + public void create(final T entity) { + super.create(entity); + } + + @Override + public T update(final T entity) { + return super.update(entity); + } + + @Override + public void delete(final T entity) { + super.delete(entity); + } + + @Override + public void deleteById(final long entityId) { + super.deleteById(entityId); + } + +} diff --git a/spring-hibernate5/src/main/java/com/baeldung/persistence/service/common/AbstractService.java b/spring-hibernate5/src/main/java/com/baeldung/persistence/service/common/AbstractService.java new file mode 100644 index 0000000000..9b001b1fac --- /dev/null +++ b/spring-hibernate5/src/main/java/com/baeldung/persistence/service/common/AbstractService.java @@ -0,0 +1,42 @@ +package com.baeldung.persistence.service.common; + +import java.io.Serializable; +import java.util.List; + +import com.baeldung.persistence.dao.common.IOperations; + +public abstract class AbstractService implements IOperations { + + @Override + public T findOne(final long id) { + return getDao().findOne(id); + } + + @Override + public List findAll() { + return getDao().findAll(); + } + + @Override + public void create(final T entity) { + getDao().create(entity); + } + + @Override + public T update(final T entity) { + return getDao().update(entity); + } + + @Override + public void delete(final T entity) { + getDao().delete(entity); + } + + @Override + public void deleteById(final long entityId) { + getDao().deleteById(entityId); + } + + protected abstract IOperations getDao(); + +} diff --git a/spring-hibernate5/src/main/java/com/baeldung/persistence/service/common/AbstractSpringDataJpaService.java b/spring-hibernate5/src/main/java/com/baeldung/persistence/service/common/AbstractSpringDataJpaService.java new file mode 100644 index 0000000000..cef483e6bf --- /dev/null +++ b/spring-hibernate5/src/main/java/com/baeldung/persistence/service/common/AbstractSpringDataJpaService.java @@ -0,0 +1,46 @@ +package com.baeldung.persistence.service.common; + +import java.io.Serializable; +import java.util.List; + +import com.baeldung.persistence.dao.common.IOperations; +import org.springframework.data.repository.CrudRepository; +import org.springframework.transaction.annotation.Transactional; + +import com.google.common.collect.Lists; + +@Transactional(value = "jpaTransactionManager") +public abstract class AbstractSpringDataJpaService implements IOperations { + + @Override + public T findOne(final long id) { + return getDao().findOne(Long.valueOf(id)); + } + + @Override + public List findAll() { + return Lists.newArrayList(getDao().findAll()); + } + + @Override + public void create(final T entity) { + getDao().save(entity); + } + + @Override + public T update(final T entity) { + return getDao().save(entity); + } + + @Override + public void delete(final T entity) { + getDao().delete(entity); + } + + @Override + public void deleteById(final long entityId) { + getDao().delete(Long.valueOf(entityId)); + } + + protected abstract CrudRepository getDao(); +} diff --git a/spring-hibernate5/src/main/java/com/baeldung/persistence/service/impl/BarAuditableService.java b/spring-hibernate5/src/main/java/com/baeldung/persistence/service/impl/BarAuditableService.java new file mode 100644 index 0000000000..d84c28caa5 --- /dev/null +++ b/spring-hibernate5/src/main/java/com/baeldung/persistence/service/impl/BarAuditableService.java @@ -0,0 +1,41 @@ +package com.baeldung.persistence.service.impl; + +import com.baeldung.persistence.dao.common.IAuditOperations; +import com.baeldung.persistence.service.common.AbstractHibernateAuditableService; +import com.baeldung.persistence.dao.IBarAuditableDao; +import com.baeldung.persistence.dao.IBarDao; +import com.baeldung.persistence.dao.common.IOperations; +import com.baeldung.persistence.model.Bar; +import com.baeldung.persistence.service.IBarAuditableService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.stereotype.Service; + +@Service +public class BarAuditableService extends AbstractHibernateAuditableService implements IBarAuditableService { + + @Autowired + @Qualifier("barHibernateDao") + private IBarDao dao; + + @Autowired + @Qualifier("barHibernateAuditableDao") + private IBarAuditableDao auditDao; + + public BarAuditableService() { + super(); + } + + // API + + @Override + protected IOperations getDao() { + return dao; + } + + @Override + protected IAuditOperations getAuditableDao() { + return auditDao; + } + +} diff --git a/spring-hibernate5/src/main/java/com/baeldung/persistence/service/impl/BarJpaService.java b/spring-hibernate5/src/main/java/com/baeldung/persistence/service/impl/BarJpaService.java new file mode 100644 index 0000000000..1c1b7a2274 --- /dev/null +++ b/spring-hibernate5/src/main/java/com/baeldung/persistence/service/impl/BarJpaService.java @@ -0,0 +1,30 @@ +package com.baeldung.persistence.service.impl; + +import com.baeldung.persistence.dao.IBarDao; +import com.baeldung.persistence.dao.common.IOperations; +import com.baeldung.persistence.model.Bar; +import com.baeldung.persistence.service.IBarService; +import com.baeldung.persistence.service.common.AbstractJpaService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.stereotype.Service; + +@Service +public class BarJpaService extends AbstractJpaService implements IBarService { + + @Autowired + @Qualifier("barJpaDao") + private IBarDao dao; + + public BarJpaService() { + super(); + } + + // API + + @Override + protected IOperations getDao() { + return dao; + } + +} \ No newline at end of file diff --git a/spring-hibernate5/src/main/java/com/baeldung/persistence/service/impl/BarService.java b/spring-hibernate5/src/main/java/com/baeldung/persistence/service/impl/BarService.java new file mode 100644 index 0000000000..32d1f919c5 --- /dev/null +++ b/spring-hibernate5/src/main/java/com/baeldung/persistence/service/impl/BarService.java @@ -0,0 +1,30 @@ +package com.baeldung.persistence.service.impl; + +import com.baeldung.persistence.dao.IBarDao; +import com.baeldung.persistence.dao.common.IOperations; +import com.baeldung.persistence.model.Bar; +import com.baeldung.persistence.service.IBarService; +import com.baeldung.persistence.service.common.AbstractHibernateService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.stereotype.Service; + +@Service +public class BarService extends AbstractHibernateService implements IBarService { + + @Autowired + @Qualifier("barHibernateDao") + private IBarDao dao; + + public BarService() { + super(); + } + + // API + + @Override + protected IOperations getDao() { + return dao; + } + +} diff --git a/spring-hibernate5/src/main/java/com/baeldung/persistence/service/impl/BarSpringDataJpaService.java b/spring-hibernate5/src/main/java/com/baeldung/persistence/service/impl/BarSpringDataJpaService.java new file mode 100644 index 0000000000..4a55d08a35 --- /dev/null +++ b/spring-hibernate5/src/main/java/com/baeldung/persistence/service/impl/BarSpringDataJpaService.java @@ -0,0 +1,26 @@ +package com.baeldung.persistence.service.impl; + +import java.io.Serializable; + +import com.baeldung.persistence.service.common.AbstractSpringDataJpaService; +import com.baeldung.persistence.dao.IBarCrudRepository; +import com.baeldung.persistence.model.Bar; +import com.baeldung.persistence.service.IBarService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.repository.CrudRepository; + +public class BarSpringDataJpaService extends AbstractSpringDataJpaService implements IBarService { + + @Autowired + private IBarCrudRepository dao; + + public BarSpringDataJpaService() { + super(); + } + + @Override + protected CrudRepository getDao() { + return dao; + } + +} diff --git a/spring-hibernate5/src/main/java/com/baeldung/persistence/service/impl/ChildService.java b/spring-hibernate5/src/main/java/com/baeldung/persistence/service/impl/ChildService.java new file mode 100644 index 0000000000..417fe2c49a --- /dev/null +++ b/spring-hibernate5/src/main/java/com/baeldung/persistence/service/impl/ChildService.java @@ -0,0 +1,28 @@ +package com.baeldung.persistence.service.impl; + +import com.baeldung.persistence.model.Child; +import com.baeldung.persistence.service.IChildService; +import com.baeldung.persistence.dao.IChildDao; +import com.baeldung.persistence.dao.common.IOperations; +import com.baeldung.persistence.service.common.AbstractHibernateService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +@Service +public class ChildService extends AbstractHibernateService implements IChildService { + + @Autowired + private IChildDao dao; + + public ChildService() { + super(); + } + + // API + + @Override + protected IOperations getDao() { + return dao; + } + +} diff --git a/spring-hibernate5/src/main/java/com/baeldung/persistence/service/impl/FooAuditableService.java b/spring-hibernate5/src/main/java/com/baeldung/persistence/service/impl/FooAuditableService.java new file mode 100644 index 0000000000..45ad315c42 --- /dev/null +++ b/spring-hibernate5/src/main/java/com/baeldung/persistence/service/impl/FooAuditableService.java @@ -0,0 +1,41 @@ +package com.baeldung.persistence.service.impl; + +import com.baeldung.persistence.dao.common.IAuditOperations; +import com.baeldung.persistence.service.IFooAuditableService; +import com.baeldung.persistence.service.common.AbstractHibernateAuditableService; +import com.baeldung.persistence.dao.IFooAuditableDao; +import com.baeldung.persistence.dao.IFooDao; +import com.baeldung.persistence.dao.common.IOperations; +import com.baeldung.persistence.model.Foo; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.stereotype.Service; + +@Service +public class FooAuditableService extends AbstractHibernateAuditableService implements IFooAuditableService { + + @Autowired + @Qualifier("fooHibernateDao") + private IFooDao dao; + + @Autowired + @Qualifier("fooHibernateAuditableDao") + private IFooAuditableDao auditDao; + + public FooAuditableService() { + super(); + } + + // API + + @Override + protected IOperations getDao() { + return dao; + } + + @Override + protected IAuditOperations getAuditableDao() { + return auditDao; + } + +} diff --git a/spring-hibernate5/src/main/java/com/baeldung/persistence/service/impl/FooService.java b/spring-hibernate5/src/main/java/com/baeldung/persistence/service/impl/FooService.java new file mode 100644 index 0000000000..84cf018fee --- /dev/null +++ b/spring-hibernate5/src/main/java/com/baeldung/persistence/service/impl/FooService.java @@ -0,0 +1,30 @@ +package com.baeldung.persistence.service.impl; + +import com.baeldung.persistence.dao.IFooDao; +import com.baeldung.persistence.dao.common.IOperations; +import com.baeldung.persistence.model.Foo; +import com.baeldung.persistence.service.IFooService; +import com.baeldung.persistence.service.common.AbstractHibernateService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.stereotype.Service; + +@Service +public class FooService extends AbstractHibernateService implements IFooService { + + @Autowired + @Qualifier("fooHibernateDao") + private IFooDao dao; + + public FooService() { + super(); + } + + // API + + @Override + protected IOperations getDao() { + return dao; + } + +} diff --git a/spring-hibernate5/src/main/java/com/baeldung/persistence/service/impl/ParentService.java b/spring-hibernate5/src/main/java/com/baeldung/persistence/service/impl/ParentService.java new file mode 100644 index 0000000000..078acfc369 --- /dev/null +++ b/spring-hibernate5/src/main/java/com/baeldung/persistence/service/impl/ParentService.java @@ -0,0 +1,28 @@ +package com.baeldung.persistence.service.impl; + +import com.baeldung.persistence.model.Parent; +import com.baeldung.persistence.service.IParentService; +import com.baeldung.persistence.dao.IParentDao; +import com.baeldung.persistence.dao.common.IOperations; +import com.baeldung.persistence.service.common.AbstractHibernateService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +@Service +public class ParentService extends AbstractHibernateService implements IParentService { + + @Autowired + private IParentDao dao; + + public ParentService() { + super(); + } + + // API + + @Override + protected IOperations getDao() { + return dao; + } + +} diff --git a/spring-hibernate5/src/main/java/com/baeldung/spring/PersistenceConfig.java b/spring-hibernate5/src/main/java/com/baeldung/spring/PersistenceConfig.java new file mode 100644 index 0000000000..67ab97e516 --- /dev/null +++ b/spring-hibernate5/src/main/java/com/baeldung/spring/PersistenceConfig.java @@ -0,0 +1,173 @@ +package com.baeldung.spring; + +import java.util.Properties; + +import javax.sql.DataSource; + +import org.apache.tomcat.dbcp.dbcp2.BasicDataSource; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.PropertySource; +import org.springframework.core.env.Environment; +import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor; +import org.springframework.data.jpa.repository.config.EnableJpaAuditing; +import org.springframework.data.jpa.repository.config.EnableJpaRepositories; +import org.springframework.orm.hibernate5.HibernateTransactionManager; +import org.springframework.orm.hibernate5.LocalSessionFactoryBean; +import org.springframework.orm.jpa.JpaTransactionManager; +import org.springframework.orm.jpa.JpaVendorAdapter; +import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; +import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; +import org.springframework.transaction.PlatformTransactionManager; +import org.springframework.transaction.annotation.EnableTransactionManagement; + +import com.baeldung.persistence.dao.IBarAuditableDao; +import com.baeldung.persistence.dao.IBarDao; +import com.baeldung.persistence.dao.IFooAuditableDao; +import com.baeldung.persistence.dao.IFooDao; +import com.baeldung.persistence.dao.impl.BarAuditableDao; +import com.baeldung.persistence.dao.impl.BarDao; +import com.baeldung.persistence.dao.impl.BarJpaDao; +import com.baeldung.persistence.dao.impl.FooAuditableDao; +import com.baeldung.persistence.dao.impl.FooDao; +import com.baeldung.persistence.service.IBarAuditableService; +import com.baeldung.persistence.service.IBarService; +import com.baeldung.persistence.service.IFooAuditableService; +import com.baeldung.persistence.service.IFooService; +import com.baeldung.persistence.service.impl.BarAuditableService; +import com.baeldung.persistence.service.impl.BarJpaService; +import com.baeldung.persistence.service.impl.BarSpringDataJpaService; +import com.baeldung.persistence.service.impl.FooAuditableService; +import com.baeldung.persistence.service.impl.FooService; +import com.google.common.base.Preconditions; + +@Configuration +@EnableTransactionManagement +@EnableJpaRepositories(basePackages = { "com.baeldung.persistence" }, transactionManagerRef = "jpaTransactionManager") +@EnableJpaAuditing +@PropertySource({ "classpath:persistence-mysql.properties" }) +@ComponentScan({ "com.baeldung.persistence" }) +public class PersistenceConfig { + + @Autowired + private Environment env; + + @Bean + public LocalSessionFactoryBean sessionFactory() { + final LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean(); + sessionFactory.setDataSource(restDataSource()); + sessionFactory.setPackagesToScan(new String[] { "com.baeldung.persistence.model" }); + sessionFactory.setHibernateProperties(hibernateProperties()); + + return sessionFactory; + } + + @Bean + public LocalContainerEntityManagerFactoryBean entityManagerFactory() { + final LocalContainerEntityManagerFactoryBean emf = new LocalContainerEntityManagerFactoryBean(); + emf.setDataSource(restDataSource()); + emf.setPackagesToScan(new String[] { "com.baeldung.persistence.model" }); + + final JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter(); + emf.setJpaVendorAdapter(vendorAdapter); + emf.setJpaProperties(hibernateProperties()); + + return emf; + } + + @Bean + public DataSource restDataSource() { + final BasicDataSource dataSource = new BasicDataSource(); + dataSource.setDriverClassName(Preconditions.checkNotNull(env.getProperty("jdbc.driverClassName"))); + dataSource.setUrl(Preconditions.checkNotNull(env.getProperty("jdbc.url"))); + dataSource.setUsername(Preconditions.checkNotNull(env.getProperty("jdbc.user"))); + dataSource.setPassword(Preconditions.checkNotNull(env.getProperty("jdbc.pass"))); + + return dataSource; + } + + @Bean + public PlatformTransactionManager hibernateTransactionManager() { + final HibernateTransactionManager transactionManager = new HibernateTransactionManager(); + transactionManager.setSessionFactory(sessionFactory().getObject()); + return transactionManager; + } + + @Bean + public PlatformTransactionManager jpaTransactionManager() { + final JpaTransactionManager transactionManager = new JpaTransactionManager(); + transactionManager.setEntityManagerFactory(entityManagerFactory().getObject()); + return transactionManager; + } + + @Bean + public PersistenceExceptionTranslationPostProcessor exceptionTranslation() { + return new PersistenceExceptionTranslationPostProcessor(); + } + + @Bean + public IBarService barJpaService() { + return new BarJpaService(); + } + + @Bean + public IBarService barSpringDataJpaService() { + return new BarSpringDataJpaService(); + } + + @Bean + public IFooService fooHibernateService() { + return new FooService(); + } + + @Bean + public IBarAuditableService barHibernateAuditableService() { + return new BarAuditableService(); + } + + @Bean + public IFooAuditableService fooHibernateAuditableService() { + return new FooAuditableService(); + } + + @Bean + public IBarDao barJpaDao() { + return new BarJpaDao(); + } + + @Bean + public IBarDao barHibernateDao() { + return new BarDao(); + } + + @Bean + public IBarAuditableDao barHibernateAuditableDao() { + return new BarAuditableDao(); + } + + @Bean + public IFooDao fooHibernateDao() { + return new FooDao(); + } + + @Bean + public IFooAuditableDao fooHibernateAuditableDao() { + return new FooAuditableDao(); + } + + private final Properties hibernateProperties() { + final Properties hibernateProperties = new Properties(); + hibernateProperties.setProperty("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto")); + hibernateProperties.setProperty("hibernate.dialect", env.getProperty("hibernate.dialect")); + + hibernateProperties.setProperty("hibernate.show_sql", "true"); + + // Envers properties + hibernateProperties.setProperty("org.hibernate.envers.audit_table_suffix", env.getProperty("envers.audit_table_suffix")); + + return hibernateProperties; + } + +} \ No newline at end of file diff --git a/spring-hibernate5/src/main/java/com/baeldung/spring/PersistenceXmlConfig.java b/spring-hibernate5/src/main/java/com/baeldung/spring/PersistenceXmlConfig.java new file mode 100644 index 0000000000..e93fcca3e1 --- /dev/null +++ b/spring-hibernate5/src/main/java/com/baeldung/spring/PersistenceXmlConfig.java @@ -0,0 +1,14 @@ +package com.baeldung.spring; + +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.ImportResource; +import org.springframework.transaction.annotation.EnableTransactionManagement; + +@Configuration +@EnableTransactionManagement +@ComponentScan({ "com.baeldung.persistence.dao", "com.baeldung.persistence.service" }) +@ImportResource({ "classpath:hibernate5Config.xml" }) +public class PersistenceXmlConfig { + +} \ No newline at end of file diff --git a/spring-hibernate5/src/main/resources/criteria.cfg.xml b/spring-hibernate5/src/main/resources/criteria.cfg.xml new file mode 100644 index 0000000000..0b865fc4d1 --- /dev/null +++ b/spring-hibernate5/src/main/resources/criteria.cfg.xml @@ -0,0 +1,17 @@ + + + + + + + com.mysql.jdbc.Driver + jdbc:mysql://localhost:3306/test + root + root + org.hibernate.dialect.MySQLDialect + true + + + \ No newline at end of file diff --git a/spring-hibernate5/src/main/resources/criteria_create_queries.sql b/spring-hibernate5/src/main/resources/criteria_create_queries.sql new file mode 100644 index 0000000000..3a627dd38c --- /dev/null +++ b/spring-hibernate5/src/main/resources/criteria_create_queries.sql @@ -0,0 +1,7 @@ +CREATE TABLE `item` ( + `ITEM_ID` int(11) NOT NULL AUTO_INCREMENT, + `ITEM_DESC` varchar(100) DEFAULT NULL, + `ITEM_PRICE` int(11) NOT NULL, + `ITEM_NAME` varchar(255) NOT NULL, + PRIMARY KEY (`ITEM_ID`) +) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=latin1; diff --git a/spring-hibernate5/src/main/resources/fetching.cfg.xml b/spring-hibernate5/src/main/resources/fetching.cfg.xml new file mode 100644 index 0000000000..b1ffecafce --- /dev/null +++ b/spring-hibernate5/src/main/resources/fetching.cfg.xml @@ -0,0 +1,20 @@ + + + + + + com.mysql.jdbc.Driver + jdbc:mysql://localhost:3306/test + root + root + org.hibernate.dialect.MySQLDialect + true + validate + + + + + + \ No newline at end of file diff --git a/spring-hibernate5/src/main/resources/fetchingLazy.cfg.xml b/spring-hibernate5/src/main/resources/fetchingLazy.cfg.xml new file mode 100644 index 0000000000..a18fdbfa99 --- /dev/null +++ b/spring-hibernate5/src/main/resources/fetchingLazy.cfg.xml @@ -0,0 +1,17 @@ + + + + + + com.mysql.jdbc.Driver + jdbc:mysql://localhost:3306/test + root + root + org.hibernate.dialect.MySQLDialect + true + + + + \ No newline at end of file diff --git a/spring-hibernate5/src/main/resources/fetching_create_queries.sql b/spring-hibernate5/src/main/resources/fetching_create_queries.sql new file mode 100644 index 0000000000..b36d9828f1 --- /dev/null +++ b/spring-hibernate5/src/main/resources/fetching_create_queries.sql @@ -0,0 +1,14 @@ +CREATE TABLE `user` ( + `user_id` int(10) NOT NULL AUTO_INCREMENT, + PRIMARY KEY (`user_id`) +) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=latin1 ; + + +CREATE TABLE `user_order` ( + `ORDER_ID` int(10) NOT NULL AUTO_INCREMENT, + `USER_ID` int(10) NOT NULL DEFAULT '0', + PRIMARY KEY (`ORDER_ID`,`USER_ID`), + KEY `USER_ID` (`USER_ID`), + CONSTRAINT `user_order_ibfk_1` FOREIGN KEY (`USER_ID`) REFERENCES `USER` (`user_id`) +) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=latin1; + diff --git a/spring-hibernate5/src/main/resources/hibernate5Config.xml b/spring-hibernate5/src/main/resources/hibernate5Config.xml new file mode 100644 index 0000000000..55546a862a --- /dev/null +++ b/spring-hibernate5/src/main/resources/hibernate5Config.xml @@ -0,0 +1,34 @@ + + + + + + + + + + + ${hibernate.hbm2ddl.auto} + ${hibernate.dialect} + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/spring-hibernate5/src/main/resources/insert_statements.sql b/spring-hibernate5/src/main/resources/insert_statements.sql new file mode 100644 index 0000000000..ae008f29bc --- /dev/null +++ b/spring-hibernate5/src/main/resources/insert_statements.sql @@ -0,0 +1,31 @@ +insert into item (item_id, item_name, item_desc, item_price) +values(1,'item One', 'test 1', 35.12); + +insert into item (item_id, item_name, item_desc, item_price) +values(2,'Pogo stick', 'Pogo stick', 466.12); +insert into item (item_id, item_name, item_desc, item_price) +values(3,'Raft', 'Raft', 345.12); + +insert into item (item_id, item_name, item_desc, item_price) +values(4,'Skate Board', 'Skating', 135.71); + +insert into item (item_id, item_name, item_desc, item_price) +values(5,'Umbrella', 'Umbrella for Rain', 619.25); + +insert into item (item_id, item_name, item_desc, item_price) +values(6,'Glue', 'Glue for home', 432.73); + +insert into item (item_id, item_name, item_desc, item_price) +values(7,'Paint', 'Paint for Room', 1311.40); + +insert into item (item_id, item_name, item_desc, item_price) +values(8,'Red paint', 'Red paint for room', 1135.71); + +insert into item (item_id, item_name, item_desc, item_price) +values(9,'Household Chairs', 'Chairs for house', 25.71); + +insert into item (item_id, item_name, item_desc, item_price) +values(10,'Office Chairs', 'Chairs for office', 395.98); + +insert into item (item_id, item_name, item_desc, item_price) +values(11,'Outdoor Chairs', 'Chairs for outdoor activities', 1234.36); diff --git a/spring-hibernate5/src/main/resources/logback.xml b/spring-hibernate5/src/main/resources/logback.xml new file mode 100644 index 0000000000..71a6d50a58 --- /dev/null +++ b/spring-hibernate5/src/main/resources/logback.xml @@ -0,0 +1,22 @@ + + + + + web - %date [%thread] %-5level %logger{36} - %message%n + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/spring-hibernate5/src/main/resources/persistence-mysql.properties b/spring-hibernate5/src/main/resources/persistence-mysql.properties new file mode 100644 index 0000000000..1180929b30 --- /dev/null +++ b/spring-hibernate5/src/main/resources/persistence-mysql.properties @@ -0,0 +1,13 @@ +# jdbc.X +jdbc.driverClassName=com.mysql.cj.jdbc.Driver +jdbc.url=jdbc:mysql://localhost:3306/spring_hibernate5_01?createDatabaseIfNotExist=true +jdbc.user=tutorialuser +jdbc.pass=tutorialmy5ql + +# hibernate.X +hibernate.dialect=org.hibernate.dialect.MySQL5Dialect +hibernate.show_sql=false +hibernate.hbm2ddl.auto=create-drop + +# envers.X +envers.audit_table_suffix=_audit_log diff --git a/spring-hibernate5/src/main/resources/stored_procedure.sql b/spring-hibernate5/src/main/resources/stored_procedure.sql new file mode 100644 index 0000000000..9cedb75c37 --- /dev/null +++ b/spring-hibernate5/src/main/resources/stored_procedure.sql @@ -0,0 +1,20 @@ +DELIMITER // + CREATE PROCEDURE GetFoosByName(IN fooName VARCHAR(255)) + LANGUAGE SQL + DETERMINISTIC + SQL SECURITY DEFINER + BEGIN + SELECT * FROM foo WHERE name = fooName; + END // +DELIMITER ; + + +DELIMITER // + CREATE PROCEDURE GetAllFoos() + LANGUAGE SQL + DETERMINISTIC + SQL SECURITY DEFINER + BEGIN + SELECT * FROM foo; + END // +DELIMITER ; \ No newline at end of file diff --git a/spring-hibernate5/src/main/resources/webSecurityConfig.xml b/spring-hibernate5/src/main/resources/webSecurityConfig.xml new file mode 100644 index 0000000000..e5c19a4ad7 --- /dev/null +++ b/spring-hibernate5/src/main/resources/webSecurityConfig.xml @@ -0,0 +1,37 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/spring-hibernate5/src/test/java/com/baeldung/hibernate/criteria/HibernateCriteriaIntegrationTest.java b/spring-hibernate5/src/test/java/com/baeldung/hibernate/criteria/HibernateCriteriaIntegrationTest.java new file mode 100644 index 0000000000..7caa02f156 --- /dev/null +++ b/spring-hibernate5/src/test/java/com/baeldung/hibernate/criteria/HibernateCriteriaIntegrationTest.java @@ -0,0 +1,194 @@ +package com.baeldung.hibernate.criteria; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertTrue; + +import java.util.List; + +import org.hibernate.Session; +import org.junit.Test; + +import com.baeldung.hibernate.criteria.model.Item; +import com.baeldung.hibernate.criteria.util.HibernateUtil; +import com.baeldung.hibernate.criteria.view.ApplicationView; + +public class HibernateCriteriaIntegrationTest { + + final private ApplicationView av = new ApplicationView(); + + @Test + public void testPerformanceOfCriteria() { + assertTrue(av.checkIfCriteriaTimeLower()); + } + + @Test + public void testLikeCriteriaQuery() { + final Session session = HibernateUtil.getHibernateSession(); + final List expectedLikeList = session.createQuery("From Item where itemName like '%chair%'") + .getResultList(); + final String expectedLikeItems[] = new String[expectedLikeList.size()]; + for (int i = 0; i < expectedLikeList.size(); i++) { + expectedLikeItems[i] = expectedLikeList.get(i).getItemName(); + } + session.close(); + assertArrayEquals(expectedLikeItems, av.likeCriteria()); + } + + @Test + public void testILikeCriteriaQuery() { + final Session session = HibernateUtil.getHibernateSession(); + final List expectedChairCaseList = session.createQuery("From Item where itemName like '%Chair%'") + .getResultList(); + final String expectedChairCaseItems[] = new String[expectedChairCaseList.size()]; + for (int i = 0; i < expectedChairCaseList.size(); i++) { + expectedChairCaseItems[i] = expectedChairCaseList.get(i).getItemName(); + } + session.close(); + assertArrayEquals(expectedChairCaseItems, av.likeCaseCriteria()); + + } + + @Test + public void testNullCriteriaQuery() { + final Session session = HibernateUtil.getHibernateSession(); + final List expectedIsNullDescItemsList = session.createQuery("From Item where itemDescription is null") + .getResultList(); + final String expectedIsNullDescItems[] = new String[expectedIsNullDescItemsList.size()]; + for (int i = 0; i < expectedIsNullDescItemsList.size(); i++) { + expectedIsNullDescItems[i] = expectedIsNullDescItemsList.get(i).getItemName(); + } + session.close(); + assertArrayEquals(expectedIsNullDescItems, av.nullCriteria()); + } + + @Test + public void testIsNotNullCriteriaQuery() { + final Session session = HibernateUtil.getHibernateSession(); + final List expectedIsNotNullDescItemsList = session.createQuery( + "From Item where itemDescription is not null").getResultList(); + final String expectedIsNotNullDescItems[] = new String[expectedIsNotNullDescItemsList.size()]; + for (int i = 0; i < expectedIsNotNullDescItemsList.size(); i++) { + expectedIsNotNullDescItems[i] = expectedIsNotNullDescItemsList.get(i).getItemName(); + } + session.close(); + assertArrayEquals(expectedIsNotNullDescItems, av.notNullCriteria()); + + } + + @Test + public void testAverageProjection() { + final Session session = HibernateUtil.getHibernateSession(); + final List expectedAvgProjItemsList = session.createQuery("Select avg(itemPrice) from Item item") + .getResultList(); + + final Double expectedAvgProjItems[] = new Double[expectedAvgProjItemsList.size()]; + for (int i = 0; i < expectedAvgProjItemsList.size(); i++) { + expectedAvgProjItems[i] = expectedAvgProjItemsList.get(i); + } + session.close(); + assertArrayEquals(expectedAvgProjItems, av.projectionAverage()); + + } + + @Test + public void testRowCountProjection() { + final Session session = HibernateUtil.getHibernateSession(); + final List expectedCountProjItemsList = session.createQuery("Select count(*) from Item").getResultList(); + final Long expectedCountProjItems[] = new Long[expectedCountProjItemsList.size()]; + for (int i = 0; i < expectedCountProjItemsList.size(); i++) { + expectedCountProjItems[i] = expectedCountProjItemsList.get(i); + } + session.close(); + assertArrayEquals(expectedCountProjItems, av.projectionRowCount()); + } + + @Test + public void testOrCriteriaQuery() { + final Session session = HibernateUtil.getHibernateSession(); + final List expectedOrCritItemsList = session.createQuery( + "From Item where itemPrice>1000 or itemName like 'Chair%'").getResultList(); + final String expectedOrCritItems[] = new String[expectedOrCritItemsList.size()]; + for (int i = 0; i < expectedOrCritItemsList.size(); i++) { + expectedOrCritItems[i] = expectedOrCritItemsList.get(i).getItemName(); + } + session.close(); + assertArrayEquals(expectedOrCritItems, av.orLogicalCriteria()); + } + + @Test + public void testAndCriteriaQuery() { + final Session session = HibernateUtil.getHibernateSession(); + final List expectedAndCritItemsList = session.createQuery( + "From Item where itemPrice>1000 and itemName like 'Chair%'").getResultList(); + final String expectedAndCritItems[] = new String[expectedAndCritItemsList.size()]; + for (int i = 0; i < expectedAndCritItemsList.size(); i++) { + expectedAndCritItems[i] = expectedAndCritItemsList.get(i).getItemName(); + } + session.close(); + assertArrayEquals(expectedAndCritItems, av.andLogicalCriteria()); + } + + @Test + public void testMultiCriteriaQuery() { + final Session session = HibernateUtil.getHibernateSession(); + final List expectedMultiCritItemsList = session.createQuery( + "From Item where itemDescription is null and itemName like'chair%'").getResultList(); + final String expectedMultiCritItems[] = new String[expectedMultiCritItemsList.size()]; + for (int i = 0; i < expectedMultiCritItemsList.size(); i++) { + expectedMultiCritItems[i] = expectedMultiCritItemsList.get(i).getItemName(); + } + session.close(); + assertArrayEquals(expectedMultiCritItems, av.twoCriteria()); + } + + @Test + public void testSortCriteriaQuery() { + final Session session = HibernateUtil.getHibernateSession(); + final List expectedSortCritItemsList = session.createQuery( + "From Item order by itemName asc, itemPrice desc").getResultList(); + final String expectedSortCritItems[] = new String[expectedSortCritItemsList.size()]; + for (int i = 0; i < expectedSortCritItemsList.size(); i++) { + expectedSortCritItems[i] = expectedSortCritItemsList.get(i).getItemName(); + } + session.close(); + assertArrayEquals(expectedSortCritItems, av.sortingCriteria()); + } + + @Test + public void testGreaterThanCriteriaQuery() { + final Session session = HibernateUtil.getHibernateSession(); + final List expectedGreaterThanList = session.createQuery("From Item where itemPrice>1000") + .getResultList(); + final String expectedGreaterThanItems[] = new String[expectedGreaterThanList.size()]; + for (int i = 0; i < expectedGreaterThanList.size(); i++) { + expectedGreaterThanItems[i] = expectedGreaterThanList.get(i).getItemName(); + } + session.close(); + assertArrayEquals(expectedGreaterThanItems, av.greaterThanCriteria()); + } + + @Test + public void testLessThanCriteriaQuery() { + final Session session = HibernateUtil.getHibernateSession(); + final List expectedLessList = session.createQuery("From Item where itemPrice<1000").getResultList(); + final String expectedLessThanItems[] = new String[expectedLessList.size()]; + for (int i = 0; i < expectedLessList.size(); i++) { + expectedLessThanItems[i] = expectedLessList.get(i).getItemName(); + } + session.close(); + assertArrayEquals(expectedLessThanItems, av.lessThanCriteria()); + } + + @Test + public void betweenCriteriaQuery() { + final Session session = HibernateUtil.getHibernateSession(); + final List expectedBetweenList = session.createQuery("From Item where itemPrice between 100 and 200") + .getResultList(); + final String expectedPriceBetweenItems[] = new String[expectedBetweenList.size()]; + for (int i = 0; i < expectedBetweenList.size(); i++) { + expectedPriceBetweenItems[i] = expectedBetweenList.get(i).getItemName(); + } + session.close(); + assertArrayEquals(expectedPriceBetweenItems, av.betweenCriteria()); + } +} diff --git a/spring-hibernate5/src/test/java/com/baeldung/hibernate/criteria/HibernateCriteriaTestRunner.java b/spring-hibernate5/src/test/java/com/baeldung/hibernate/criteria/HibernateCriteriaTestRunner.java new file mode 100644 index 0000000000..99164efb7a --- /dev/null +++ b/spring-hibernate5/src/test/java/com/baeldung/hibernate/criteria/HibernateCriteriaTestRunner.java @@ -0,0 +1,15 @@ +package com.baeldung.hibernate.criteria; + +import org.junit.runner.JUnitCore; +import org.junit.runner.Result; +import org.junit.runner.notification.Failure; + +public class HibernateCriteriaTestRunner { + + public static void main(final String[] args) { + Result result = JUnitCore.runClasses(HibernateCriteriaTestSuite.class); + for (Failure failure : result.getFailures()) { + + } + } +} diff --git a/spring-hibernate5/src/test/java/com/baeldung/hibernate/criteria/HibernateCriteriaTestSuite.java b/spring-hibernate5/src/test/java/com/baeldung/hibernate/criteria/HibernateCriteriaTestSuite.java new file mode 100644 index 0000000000..dc1040734f --- /dev/null +++ b/spring-hibernate5/src/test/java/com/baeldung/hibernate/criteria/HibernateCriteriaTestSuite.java @@ -0,0 +1,11 @@ +package com.baeldung.hibernate.criteria; + +import org.junit.runner.RunWith; +import org.junit.runners.Suite; + +@RunWith(Suite.class) +@Suite.SuiteClasses({ HibernateCriteriaIntegrationTest.class }) + +public class HibernateCriteriaTestSuite { + +} diff --git a/spring-hibernate5/src/test/java/com/baeldung/hibernate/fetching/HibernateFetchingIntegrationTest.java b/spring-hibernate5/src/test/java/com/baeldung/hibernate/fetching/HibernateFetchingIntegrationTest.java new file mode 100644 index 0000000000..65bf36f8bf --- /dev/null +++ b/spring-hibernate5/src/test/java/com/baeldung/hibernate/fetching/HibernateFetchingIntegrationTest.java @@ -0,0 +1,42 @@ +package com.baeldung.hibernate.fetching; + +import com.baeldung.hibernate.fetching.model.OrderDetail; +import com.baeldung.hibernate.fetching.view.FetchingAppView; +import org.hibernate.Hibernate; +import org.junit.Before; +import org.junit.Test; + +import java.util.Set; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +public class HibernateFetchingIntegrationTest { + + // this loads sample data in the database + @Before + public void addFecthingTestData() { + FetchingAppView fav = new FetchingAppView(); + fav.createTestData(); + } + + // testLazyFetching() tests the lazy loading + // Since it lazily loaded so orderDetalSetLazy won't + // be initialized + @Test + public void testLazyFetching() { + FetchingAppView fav = new FetchingAppView(); + Set orderDetalSetLazy = fav.lazyLoaded(); + assertFalse(Hibernate.isInitialized(orderDetalSetLazy)); + } + + // testEagerFetching() tests the eager loading + // Since it eagerly loaded so orderDetalSetLazy would + // be initialized + @Test + public void testEagerFetching() { + FetchingAppView fav = new FetchingAppView(); + Set orderDetalSetEager = fav.eagerLoaded(); + assertTrue(Hibernate.isInitialized(orderDetalSetEager)); + } +} diff --git a/spring-hibernate5/src/test/java/com/baeldung/persistence/IntegrationTestSuite.java b/spring-hibernate5/src/test/java/com/baeldung/persistence/IntegrationTestSuite.java new file mode 100644 index 0000000000..f5c45a5d6f --- /dev/null +++ b/spring-hibernate5/src/test/java/com/baeldung/persistence/IntegrationTestSuite.java @@ -0,0 +1,25 @@ +package com.baeldung.persistence; + +import org.junit.runner.RunWith; +import org.junit.runners.Suite; + +import com.baeldung.persistence.audit.AuditTestSuite; +import com.baeldung.persistence.hibernate.FooPaginationPersistenceIntegrationTest; +import com.baeldung.persistence.hibernate.FooSortingPersistenceIntegrationTest; +import com.baeldung.persistence.service.FooServiceBasicPersistenceIntegrationTest; +import com.baeldung.persistence.service.FooServicePersistenceIntegrationTest; +import com.baeldung.persistence.service.ParentServicePersistenceIntegrationTest; + +@RunWith(Suite.class) +@Suite.SuiteClasses({ // @formatter:off + AuditTestSuite.class + ,FooServiceBasicPersistenceIntegrationTest.class + ,FooPaginationPersistenceIntegrationTest.class + ,FooServicePersistenceIntegrationTest.class + ,ParentServicePersistenceIntegrationTest.class + ,FooSortingPersistenceIntegrationTest.class + +}) // @formatter:on +public class IntegrationTestSuite { + // +} diff --git a/spring-hibernate5/src/test/java/com/baeldung/persistence/audit/AuditTestSuite.java b/spring-hibernate5/src/test/java/com/baeldung/persistence/audit/AuditTestSuite.java new file mode 100644 index 0000000000..34c725d62b --- /dev/null +++ b/spring-hibernate5/src/test/java/com/baeldung/persistence/audit/AuditTestSuite.java @@ -0,0 +1,14 @@ +package com.baeldung.persistence.audit; + +import org.junit.runner.RunWith; +import org.junit.runners.Suite; + +@RunWith(Suite.class) +@Suite.SuiteClasses({ // @formatter:off + EnversFooBarAuditIntegrationTest.class, + JPABarAuditIntegrationTest.class, + SpringDataJPABarAuditIntegrationTest.class +}) // @formatter:on +public class AuditTestSuite { + // +} \ No newline at end of file diff --git a/spring-hibernate5/src/test/java/com/baeldung/persistence/audit/EnversFooBarAuditIntegrationTest.java b/spring-hibernate5/src/test/java/com/baeldung/persistence/audit/EnversFooBarAuditIntegrationTest.java new file mode 100644 index 0000000000..ed2e111c8f --- /dev/null +++ b/spring-hibernate5/src/test/java/com/baeldung/persistence/audit/EnversFooBarAuditIntegrationTest.java @@ -0,0 +1,142 @@ +package com.baeldung.persistence.audit; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import java.util.List; + +import com.baeldung.persistence.model.Foo; +import com.baeldung.persistence.service.IBarAuditableService; +import com.baeldung.persistence.service.IFooAuditableService; +import com.baeldung.spring.PersistenceConfig; +import com.baeldung.persistence.model.Bar; +import org.hibernate.Session; +import org.hibernate.SessionFactory; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.support.AnnotationConfigContextLoader; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = { PersistenceConfig.class }, loader = AnnotationConfigContextLoader.class) +public class EnversFooBarAuditIntegrationTest { + + private static Logger logger = LoggerFactory.getLogger(EnversFooBarAuditIntegrationTest.class); + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + logger.info("setUpBeforeClass()"); + } + + @AfterClass + public static void tearDownAfterClass() throws Exception { + logger.info("tearDownAfterClass()"); + } + + @Autowired + @Qualifier("fooHibernateAuditableService") + private IFooAuditableService fooService; + + @Autowired + @Qualifier("barHibernateAuditableService") + private IBarAuditableService barService; + + @Autowired + private SessionFactory sessionFactory; + + private Session session; + + @Before + public void setUp() throws Exception { + logger.info("setUp()"); + makeRevisions(); + session = sessionFactory.openSession(); + } + + @After + public void tearDown() throws Exception { + logger.info("tearDown()"); + session.close(); + } + + private void makeRevisions() { + final Bar bar = rev1(); + rev2(bar); + rev3(bar); + rev4(bar); + } + + // REV #1: insert BAR & FOO1 + private Bar rev1() { + final Bar bar = new Bar("BAR"); + final Foo foo1 = new Foo("FOO1"); + foo1.setBar(bar); + fooService.create(foo1); + return bar; + } + + // REV #2: insert FOO2 & update BAR + private void rev2(final Bar bar) { + final Foo foo2 = new Foo("FOO2"); + foo2.setBar(bar); + fooService.create(foo2); + } + + // REV #3: update BAR + private void rev3(final Bar bar) { + + bar.setName("BAR1"); + barService.update(bar); + } + + // REV #4: insert FOO3 & update BAR + private void rev4(final Bar bar) { + + final Foo foo3 = new Foo("FOO3"); + foo3.setBar(bar); + fooService.create(foo3); + } + + @Test + public final void whenFooBarsModified_thenFooBarsAudited() { + + List barRevisionList; + List fooRevisionList; + + // test Bar revisions + + barRevisionList = barService.getRevisions(); + + assertNotNull(barRevisionList); + assertEquals(4, barRevisionList.size()); + + assertEquals("BAR", barRevisionList.get(0).getName()); + assertEquals("BAR", barRevisionList.get(1).getName()); + assertEquals("BAR1", barRevisionList.get(2).getName()); + assertEquals("BAR1", barRevisionList.get(3).getName()); + + assertEquals(1, barRevisionList.get(0).getFooSet().size()); + assertEquals(2, barRevisionList.get(1).getFooSet().size()); + assertEquals(2, barRevisionList.get(2).getFooSet().size()); + assertEquals(3, barRevisionList.get(3).getFooSet().size()); + + // test Foo revisions + + fooRevisionList = fooService.getRevisions(); + assertNotNull(fooRevisionList); + assertEquals(3, fooRevisionList.size()); + assertEquals("FOO1", fooRevisionList.get(0).getName()); + assertEquals("FOO2", fooRevisionList.get(1).getName()); + assertEquals("FOO3", fooRevisionList.get(2).getName()); + } + +} diff --git a/spring-hibernate5/src/test/java/com/baeldung/persistence/audit/JPABarAuditIntegrationTest.java b/spring-hibernate5/src/test/java/com/baeldung/persistence/audit/JPABarAuditIntegrationTest.java new file mode 100644 index 0000000000..b63a4b989b --- /dev/null +++ b/spring-hibernate5/src/test/java/com/baeldung/persistence/audit/JPABarAuditIntegrationTest.java @@ -0,0 +1,102 @@ +package com.baeldung.persistence.audit; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; + +import com.baeldung.persistence.model.Bar; +import com.baeldung.persistence.model.Bar.OPERATION; +import com.baeldung.persistence.service.IBarService; +import com.baeldung.spring.PersistenceConfig; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.support.AnnotationConfigContextLoader; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = { PersistenceConfig.class }, loader = AnnotationConfigContextLoader.class) +public class JPABarAuditIntegrationTest { + + private static Logger logger = LoggerFactory.getLogger(JPABarAuditIntegrationTest.class); + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + logger.info("setUpBeforeClass()"); + } + + @AfterClass + public static void tearDownAfterClass() throws Exception { + logger.info("tearDownAfterClass()"); + } + + @Autowired + @Qualifier("barJpaService") + private IBarService barService; + + @Autowired + private EntityManagerFactory entityManagerFactory; + + private EntityManager em; + + @Before + public void setUp() throws Exception { + logger.info("setUp()"); + em = entityManagerFactory.createEntityManager(); + } + + @After + public void tearDown() throws Exception { + logger.info("tearDown()"); + em.close(); + } + + @Test + public final void whenBarsModified_thenBarsAudited() { + + // insert BAR1 + Bar bar1 = new Bar("BAR1"); + barService.create(bar1); + + // update BAR1 + bar1.setName("BAR1a"); + barService.update(bar1); + + // insert BAR2 + Bar bar2 = new Bar("BAR2"); + barService.create(bar2); + + // update BAR1 + bar1.setName("BAR1b"); + barService.update(bar1); + + // get BAR1 and BAR2 from the DB and check the audit values + // detach instances from persistence context to make sure we fire db + em.detach(bar1); + em.detach(bar2); + bar1 = barService.findOne(bar1.getId()); + bar2 = barService.findOne(bar2.getId()); + + assertNotNull(bar1); + assertNotNull(bar2); + assertEquals(OPERATION.UPDATE, bar1.getOperation()); + assertEquals(OPERATION.INSERT, bar2.getOperation()); + assertTrue(bar1.getTimestamp() > bar2.getTimestamp()); + + barService.deleteById(bar1.getId()); + barService.deleteById(bar2.getId()); + + } + +} \ No newline at end of file diff --git a/spring-hibernate5/src/test/java/com/baeldung/persistence/audit/SpringDataJPABarAuditIntegrationTest.java b/spring-hibernate5/src/test/java/com/baeldung/persistence/audit/SpringDataJPABarAuditIntegrationTest.java new file mode 100644 index 0000000000..e794b282f6 --- /dev/null +++ b/spring-hibernate5/src/test/java/com/baeldung/persistence/audit/SpringDataJPABarAuditIntegrationTest.java @@ -0,0 +1,76 @@ +package com.baeldung.persistence.audit; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; + +import com.baeldung.persistence.model.Bar; +import com.baeldung.persistence.service.IBarService; +import com.baeldung.spring.PersistenceConfig; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.security.test.context.support.WithMockUser; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.support.AnnotationConfigContextLoader; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = { PersistenceConfig.class }, loader = AnnotationConfigContextLoader.class) +public class SpringDataJPABarAuditIntegrationTest { + + private static Logger logger = LoggerFactory.getLogger(SpringDataJPABarAuditIntegrationTest.class); + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + logger.info("setUpBeforeClass()"); + } + + @AfterClass + public static void tearDownAfterClass() throws Exception { + logger.info("tearDownAfterClass()"); + } + + @Autowired + @Qualifier("barSpringDataJpaService") + private IBarService barService; + + @Autowired + private EntityManagerFactory entityManagerFactory; + + private EntityManager em; + + @Before + public void setUp() throws Exception { + logger.info("setUp()"); + em = entityManagerFactory.createEntityManager(); + } + + @After + public void tearDown() throws Exception { + logger.info("tearDown()"); + em.close(); + } + + @Test + @WithMockUser(username = "tutorialuser") + public final void whenBarsModified_thenBarsAudited() { + Bar bar = new Bar("BAR1"); + barService.create(bar); + assertEquals(bar.getCreatedDate(), bar.getModifiedDate()); + assertEquals("tutorialuser", bar.getCreatedBy(), bar.getModifiedBy()); + bar.setName("BAR2"); + bar = barService.update(bar); + assertTrue(bar.getCreatedDate() < bar.getModifiedDate()); + assertEquals("tutorialuser", bar.getCreatedBy(), bar.getModifiedBy()); + } +} diff --git a/spring-hibernate5/src/test/java/com/baeldung/persistence/hibernate/FooFixtures.java b/spring-hibernate5/src/test/java/com/baeldung/persistence/hibernate/FooFixtures.java new file mode 100644 index 0000000000..da840dc027 --- /dev/null +++ b/spring-hibernate5/src/test/java/com/baeldung/persistence/hibernate/FooFixtures.java @@ -0,0 +1,101 @@ +package com.baeldung.persistence.hibernate; + +import java.util.List; + +import com.baeldung.persistence.model.Foo; +import com.baeldung.persistence.model.Bar; +import org.hibernate.HibernateException; +import org.hibernate.Session; +import org.hibernate.SessionFactory; +import org.hibernate.Transaction; + +import com.google.common.collect.Lists; + +public class FooFixtures { + private SessionFactory sessionFactory; + + public FooFixtures(final SessionFactory sessionFactory) { + super(); + + this.sessionFactory = sessionFactory; + } + + // API + + public void createBars() { + Session session = null; + Transaction tx = null; + session = sessionFactory.openSession(); + tx = session.getTransaction(); + try { + tx.begin(); + for (int i = 156; i < 160; i++) { + final Bar bar = new Bar(); + bar.setName("Bar_" + i); + final Foo foo = new Foo("Foo_" + (i + 120)); + foo.setBar(bar); + session.save(foo); + final Foo foo2 = new Foo(null); + if (i % 2 == 0) + foo2.setName("LuckyFoo" + (i + 120)); + foo2.setBar(bar); + session.save(foo2); + bar.getFooSet().add(foo); + bar.getFooSet().add(foo2); + session.merge(bar); + } + tx.commit(); + session.flush(); + } catch (final HibernateException he) { + if (tx != null) + tx.rollback(); + System.out.println("Not able to open session"); + he.printStackTrace(); + } catch (final Exception e) { + e.printStackTrace(); + } finally { + if (session != null) + session.close(); + } + + } + + public void createFoos() { + Session session = null; + Transaction tx = null; + session = sessionFactory.openSession(); + tx = session.getTransaction(); + final List fooList = Lists.newArrayList(); + for (int i = 35; i < 46; i++) { + + final Foo foo = new Foo(); + foo.setName("Foo_" + (i + 120)); + final Bar bar = new Bar("bar_" + i); + bar.getFooSet().add(foo); + foo.setBar(bar); + fooList.add(foo); + + } + try { + tx.begin(); + for (final Foo foo : fooList) { + + session.save(foo.getBar()); + session.save(foo); + } + tx.commit(); + session.flush(); + } catch (final HibernateException he) { + if (tx != null) + tx.rollback(); + System.out.println("Not able to open session"); + he.printStackTrace(); + } catch (final Exception e) { + e.printStackTrace(); + } finally { + if (session != null) + session.close(); + } + } + +} diff --git a/spring-hibernate5/src/test/java/com/baeldung/persistence/hibernate/FooPaginationPersistenceIntegrationTest.java b/spring-hibernate5/src/test/java/com/baeldung/persistence/hibernate/FooPaginationPersistenceIntegrationTest.java new file mode 100644 index 0000000000..c9152b4d85 --- /dev/null +++ b/spring-hibernate5/src/test/java/com/baeldung/persistence/hibernate/FooPaginationPersistenceIntegrationTest.java @@ -0,0 +1,185 @@ +package com.baeldung.persistence.hibernate; + +import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic; +import static org.hamcrest.Matchers.hasSize; +import static org.hamcrest.Matchers.lessThan; +import static org.junit.Assert.assertThat; + +import java.util.List; + +import javax.persistence.Tuple; +import javax.persistence.criteria.CriteriaBuilder; +import javax.persistence.criteria.CriteriaQuery; +import javax.persistence.criteria.Root; + +import org.hibernate.ScrollMode; +import org.hibernate.ScrollableResults; +import org.hibernate.Session; +import org.hibernate.SessionFactory; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.support.AnnotationConfigContextLoader; + +import com.baeldung.persistence.model.Foo; +import com.baeldung.persistence.service.IFooService; +import com.baeldung.spring.PersistenceConfig; +import com.google.common.collect.Lists; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = { PersistenceConfig.class }, loader = AnnotationConfigContextLoader.class) +public class FooPaginationPersistenceIntegrationTest { + + @Autowired + private IFooService fooService; + + @Autowired + private SessionFactory sessionFactory; + + private Session session; + + // tests + + @Before + public final void before() { + final int minimalNumberOfEntities = 25; + if (fooService.findAll().size() <= minimalNumberOfEntities) { + for (int i = 0; i < minimalNumberOfEntities; i++) { + fooService.create(new Foo(randomAlphabetic(6))); + } + } + + session = sessionFactory.openSession(); + } + + @After + public final void after() { + session.close(); + } + + // tests + + @Test + public final void whenContextIsBootstrapped_thenNoExceptions() { + // + } + + @SuppressWarnings("unchecked") + @Test + public final void whenRetrievingPaginatedEntities_thenCorrectSize() { + final int pageNumber = 1; + final int pageSize = 10; + final List fooList = session.createQuery("From Foo").setFirstResult((pageNumber - 1) * pageSize) + .setMaxResults(pageSize).getResultList(); + assertThat(fooList, hasSize(pageSize)); + } + + @SuppressWarnings("unchecked") + @Test + public final void whenRetrievingAllPages_thenCorrect() { + int pageNumber = 1; + final int pageSize = 10; + + final String countQ = "Select count (f.id) from Foo f"; + final Long countResult = (Long) session.createQuery(countQ).uniqueResult(); + + final List fooList = Lists.newArrayList(); + int totalEntities = 0; + while (totalEntities < countResult) { + fooList.addAll(session.createQuery("From Foo").setFirstResult((pageNumber - 1) * pageSize) + .setMaxResults(pageSize).getResultList()); + totalEntities = fooList.size(); + pageNumber++; + } + } + + @SuppressWarnings("unchecked") + @Test + public final void whenRetrievingLastPage_thenCorrectSize() { + final int pageSize = 10; + + final String countQ = "Select count (f.id) from Foo f"; + final Long countResults = (Long) session.createQuery(countQ).uniqueResult(); + final int lastPageNumber = (int) ((countResults / pageSize) + 1); + + final List lastPage = session.createQuery("From Foo").setFirstResult((lastPageNumber - 1) * pageSize) + .setMaxResults(pageSize).getResultList(); + + assertThat(lastPage, hasSize(lessThan(pageSize + 1))); + } + + // testing - scrollable + + @Test + public final void givenUsingTheScrollableApi_whenRetrievingPaginatedData_thenCorrect() { + final int pageSize = 10; + final String hql = "FROM Foo f order by f.name"; + + final ScrollableResults resultScroll = session.createQuery(hql).scroll(ScrollMode.FORWARD_ONLY); + + // resultScroll.last(); + // final int totalResults = resultScroll.getRowNumber() + 1; + + resultScroll.first(); + resultScroll.scroll(0); + final List fooPage = Lists.newArrayList(); + int i = 0; + while (pageSize > i++) { + fooPage.add((Foo) resultScroll.get(0)); + if (!resultScroll.next()) { + break; + } + } + + assertThat(fooPage, hasSize(lessThan(10 + 1))); + } + + @SuppressWarnings("unchecked") + @Test + public final void givenUsingTheCriteriaApi_whenRetrievingFirstPage_thenCorrect() { + final int pageSize = 10; + + CriteriaBuilder builder = session.getCriteriaBuilder(); + CriteriaQuery criteriaItem = builder.createQuery(Foo.class); + Root rootItem = criteriaItem.from(Foo.class); + criteriaItem.select(rootItem); + final List firstPage = session.createQuery(criteriaItem).setFirstResult(0).setMaxResults(pageSize) + .getResultList(); + + assertThat(firstPage, hasSize(pageSize)); + } + + @SuppressWarnings("unchecked") + @Test + public final void givenUsingTheCriteriaApi_whenRetrievingPaginatedData_thenCorrect() { + + CriteriaBuilder builder = session.getCriteriaBuilder(); + CriteriaQuery criteriaItem = builder.createQuery(Tuple.class); + Root rootItem = criteriaItem.from(Foo.class); + criteriaItem.multiselect(builder.count(rootItem)); + final List itemProjected = session.createQuery(criteriaItem).getResultList(); + final Long count = (Long) itemProjected.get(0).get(0); + + int pageNumber = 1; + final int pageSize = 10; + final List fooList = Lists.newArrayList(); + + CriteriaBuilder builderFoo = session.getCriteriaBuilder(); + CriteriaQuery criteriaFoo = builderFoo.createQuery(Foo.class); + Root rootFoo = criteriaFoo.from(Foo.class); + criteriaFoo.select(rootFoo); + + int totalEntities = 0; + while (totalEntities < count.intValue()) { + fooList.addAll(session.createQuery(criteriaFoo).setFirstResult((pageNumber - 1) * pageSize) + .setMaxResults(pageSize).getResultList()); + totalEntities = fooList.size(); + pageNumber++; + } + } + +} diff --git a/spring-hibernate5/src/test/java/com/baeldung/persistence/hibernate/FooSortingPersistenceIntegrationTest.java b/spring-hibernate5/src/test/java/com/baeldung/persistence/hibernate/FooSortingPersistenceIntegrationTest.java new file mode 100644 index 0000000000..813fb65641 --- /dev/null +++ b/spring-hibernate5/src/test/java/com/baeldung/persistence/hibernate/FooSortingPersistenceIntegrationTest.java @@ -0,0 +1,179 @@ +package com.baeldung.persistence.hibernate; + +import static org.junit.Assert.assertNull; + +import java.util.ArrayList; +import java.util.List; +import java.util.Set; + +import javax.persistence.criteria.CriteriaBuilder; +import javax.persistence.criteria.CriteriaQuery; +import javax.persistence.criteria.Order; +import javax.persistence.criteria.Root; + +import org.hibernate.Criteria; +import org.hibernate.NullPrecedence; +import org.hibernate.Session; +import org.hibernate.SessionFactory; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.support.AnnotationConfigContextLoader; + +import com.baeldung.persistence.model.Bar; +import com.baeldung.persistence.model.Foo; +import com.baeldung.spring.PersistenceConfig; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = { PersistenceConfig.class }, loader = AnnotationConfigContextLoader.class) +@SuppressWarnings("unchecked") +public class FooSortingPersistenceIntegrationTest { + + @Autowired + private SessionFactory sessionFactory; + + private Session session; + + @Before + public void before() { + session = sessionFactory.openSession(); + + session.beginTransaction(); + + final FooFixtures fooData = new FooFixtures(sessionFactory); + fooData.createBars(); + } + + @After + public void after() { + session.getTransaction().commit(); + session.close(); + } + + @Test + public final void whenHQlSortingByOneAttribute_thenPrintSortedResults() { + final String hql = "FROM Foo f ORDER BY f.name"; + final List fooList = session.createQuery(hql).getResultList(); + for (final Foo foo : fooList) { + System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId()); + } + } + + @Test + public final void whenHQlSortingByStringNullLast_thenLastNull() { + final String hql = "FROM Foo f ORDER BY f.name NULLS LAST"; + final List fooList = session.createQuery(hql).getResultList(); + + assertNull(fooList.get(fooList.toArray().length - 1).getName()); + for (final Foo foo : fooList) { + System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId()); + } + } + + @Test + public final void whenSortingByStringNullsFirst_thenReturnNullsFirst() { + final String hql = "FROM Foo f ORDER BY f.name NULLS FIRST"; + final List fooList = session.createQuery(hql).getResultList(); + assertNull(fooList.get(0).getName()); + for (final Foo foo : fooList) { + System.out.println("Name:" + foo.getName()); + + } + } + + @Test + public final void whenHQlSortingByOneAttribute_andOrderDirection_thenPrintSortedResults() { + final String hql = "FROM Foo f ORDER BY f.name ASC"; + final List fooList = session.createQuery(hql).getResultList(); + for (final Foo foo : fooList) { + System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId()); + } + } + + @Test + public final void whenHQlSortingByMultipleAttributes_thenSortedResults() { + final String hql = "FROM Foo f ORDER BY f.name, f.id"; + final List fooList = session.createQuery(hql).getResultList(); + for (final Foo foo : fooList) { + System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId()); + } + } + + @Test + public final void whenHQlSortingByMultipleAttributes_andOrderDirection_thenPrintSortedResults() { + final String hql = "FROM Foo f ORDER BY f.name DESC, f.id ASC"; + final List fooList = session.createQuery(hql).getResultList(); + for (final Foo foo : fooList) { + System.out.println("Name: " + foo.getName() + ", Id: " + foo.getId()); + } + } + + @Test + public final void whenHQLCriteriaSortingByOneAttr_thenPrintSortedResults() { + List listOrders = new ArrayList(); + CriteriaBuilder builder = session.getCriteriaBuilder(); + CriteriaQuery criteriaItem = builder.createQuery(Foo.class); + Root rootItem = criteriaItem.from(Foo.class); + listOrders.add(builder.asc(rootItem.get("id"))); + criteriaItem.orderBy(listOrders.toArray(new Order[] {})); + final List fooList = session.createQuery(criteriaItem).getResultList(); + for (final Foo foo : fooList) { + System.out.println("Id: " + foo.getId() + ", FirstName: " + foo.getName()); + } + } + + @Test + public final void whenHQLCriteriaSortingByMultipAttr_thenSortedResults() { + List listOrders = new ArrayList(); + CriteriaBuilder builder = session.getCriteriaBuilder(); + CriteriaQuery criteriaItem = builder.createQuery(Foo.class); + Root rootItem = criteriaItem.from(Foo.class); + listOrders.add(builder.asc(rootItem.get("name"))); + listOrders.add(builder.asc(rootItem.get("id"))); + criteriaItem.orderBy(listOrders.toArray(new Order[] {})); + final List fooList = session.createQuery(criteriaItem).getResultList(); + for (final Foo foo : fooList) { + System.out.println("Id: " + foo.getId() + ", FirstName: " + foo.getName()); + } + } + + /*@Test + public final void whenCriteriaSortingStringNullsLastAsc_thenNullsLast() { + final Criteria criteria = session.createCriteria(Foo.class, "FOO"); + criteria.addOrder(Order.asc("name").nulls(NullPrecedence.LAST)); + final List fooList = criteria.list(); + assertNull(fooList.get(fooList.toArray().length - 1).getName()); + for (final Foo foo : fooList) { + System.out.println("Id: " + foo.getId() + ", FirstName: " + foo.getName()); + } + } + + @Test + public final void whenCriteriaSortingStringNullsFirstDesc_thenNullsFirst() { + final Criteria criteria = session.createCriteria(Foo.class, "FOO"); + criteria.addOrder(Order.desc("name").nulls(NullPrecedence.FIRST)); + final List fooList = criteria.list(); + assertNull(fooList.get(0).getName()); + for (final Foo foo : fooList) { + System.out.println("Id: " + foo.getId() + ", FirstName: " + foo.getName()); + } + }*/ + + @Test + public final void whenSortingBars_thenBarsWithSortedFoos() { + final String hql = "FROM Bar b ORDER BY b.id"; + final List barList = session.createQuery(hql).getResultList(); + for (final Bar bar : barList) { + final Set fooSet = bar.getFooSet(); + System.out.println("Bar Id:" + bar.getId()); + for (final Foo foo : fooSet) { + System.out.println("FooName:" + foo.getName()); + } + } + } + +} diff --git a/spring-hibernate5/src/test/java/com/baeldung/persistence/save/SaveMethodsTest.java b/spring-hibernate5/src/test/java/com/baeldung/persistence/save/SaveMethodsTest.java new file mode 100644 index 0000000000..b6cde868d3 --- /dev/null +++ b/spring-hibernate5/src/test/java/com/baeldung/persistence/save/SaveMethodsTest.java @@ -0,0 +1,285 @@ +package com.baeldung.persistence.save; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNotSame; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertSame; + +import javax.persistence.PersistenceException; + +import org.hibernate.HibernateException; +import org.hibernate.Session; +import org.hibernate.SessionFactory; +import org.hibernate.TransactionException; +import org.hibernate.boot.registry.StandardServiceRegistryBuilder; +import org.hibernate.cfg.Configuration; +import org.hibernate.dialect.HSQLDialect; +import org.hibernate.service.ServiceRegistry; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +import com.baeldung.persistence.model.Person; + +/** + * Testing specific implementation details for different methods: persist, save, + * merge, update, saveOrUpdate. + */ +public class SaveMethodsTest { + + private static SessionFactory sessionFactory; + + private Session session; + + @BeforeClass + public static void beforeTests() { + Configuration configuration = new Configuration().addAnnotatedClass(Person.class) + .setProperty("hibernate.dialect", HSQLDialect.class.getName()) + .setProperty("hibernate.connection.driver_class", org.hsqldb.jdbcDriver.class.getName()) + .setProperty("hibernate.connection.url", "jdbc:hsqldb:mem:test") + .setProperty("hibernate.connection.username", "sa").setProperty("hibernate.connection.password", "") + .setProperty("hibernate.hbm2ddl.auto", "update"); + ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings( + configuration.getProperties()).build(); + sessionFactory = configuration.buildSessionFactory(serviceRegistry); + } + + @Before + public void setUp() { + session = sessionFactory.openSession(); + session.beginTransaction(); + } + + @Test + public void whenPersistTransient_thenSavedToDatabaseOnCommit() { + + Person person = new Person(); + person.setName("John"); + session.persist(person); + + session.getTransaction().commit(); + session.close(); + + session = sessionFactory.openSession(); + session.beginTransaction(); + + assertNotNull(session.get(Person.class, person.getId())); + + } + + @Test + public void whenPersistPersistent_thenNothingHappens() { + + Person person = new Person(); + person.setName("John"); + + session.persist(person); + Long id1 = person.getId(); + + session.persist(person); + Long id2 = person.getId(); + + assertEquals(id1, id2); + } + + @Test(expected = PersistenceException.class) + public void whenPersistDetached_thenThrowsException() { + + Person person = new Person(); + person.setName("John"); + session.persist(person); + session.evict(person); + + session.persist(person); + + } + + @Test + public void whenSaveTransient_thenIdGeneratedImmediately() { + + Person person = new Person(); + person.setName("John"); + + assertNull(person.getId()); + + Long id = (Long) session.save(person); + + assertNotNull(id); + + session.getTransaction().commit(); + session.close(); + + assertEquals(id, person.getId()); + + session = sessionFactory.openSession(); + session.beginTransaction(); + + assertNotNull(session.get(Person.class, person.getId())); + + } + + @Test + public void whenSavePersistent_thenNothingHappens() { + + Person person = new Person(); + person.setName("John"); + Long id1 = (Long) session.save(person); + Long id2 = (Long) session.save(person); + assertEquals(id1, id2); + + } + + @Test + public void whenSaveDetached_thenNewInstancePersisted() { + + Person person = new Person(); + person.setName("John"); + Long id1 = (Long) session.save(person); + session.evict(person); + + Long id2 = (Long) session.save(person); + assertNotEquals(id1, id2); + + } + + @Test + public void whenMergeDetached_thenEntityUpdatedFromDatabase() { + + Person person = new Person(); + person.setName("John"); + session.save(person); + session.flush(); + session.evict(person); + + person.setName("Mary"); + Person mergedPerson = (Person) session.merge(person); + + assertNotSame(person, mergedPerson); + assertEquals("Mary", mergedPerson.getName()); + + } + + @Test + public void whenMergeTransient_thenNewEntitySavedToDatabase() { + + Person person = new Person(); + person.setName("John"); + Person mergedPerson = (Person) session.merge(person); + + session.getTransaction().commit(); + session.beginTransaction(); + + assertNull(person.getId()); + assertNotNull(mergedPerson.getId()); + + } + + @Test + public void whenMergePersistent_thenReturnsSameObject() { + + Person person = new Person(); + person.setName("John"); + session.save(person); + + Person mergedPerson = (Person) session.merge(person); + + assertSame(person, mergedPerson); + + } + + @Test + public void whenUpdateDetached_thenEntityUpdatedFromDatabase() { + + Person person = new Person(); + person.setName("John"); + session.save(person); + session.evict(person); + + person.setName("Mary"); + session.update(person); + assertEquals("Mary", person.getName()); + + } + + @Test(expected = HibernateException.class) + public void whenUpdateTransient_thenThrowsException() { + + Person person = new Person(); + person.setName("John"); + session.update(person); + + } + + @Test + public void whenUpdatePersistent_thenNothingHappens() { + + Person person = new Person(); + person.setName("John"); + session.save(person); + + session.update(person); + + } + + @Test + public void whenSaveOrUpdateDetached_thenEntityUpdatedFromDatabase() { + + Person person = new Person(); + person.setName("John"); + session.save(person); + session.evict(person); + + person.setName("Mary"); + session.saveOrUpdate(person); + assertEquals("Mary", person.getName()); + + } + + @Test + public void whenSaveOrUpdateTransient_thenSavedToDatabaseOnCommit() { + + Person person = new Person(); + person.setName("John"); + session.saveOrUpdate(person); + + session.getTransaction().commit(); + session.close(); + + session = sessionFactory.openSession(); + session.beginTransaction(); + + assertNotNull(session.get(Person.class, person.getId())); + + } + + @Test + public void whenSaveOrUpdatePersistent_thenNothingHappens() { + + Person person = new Person(); + person.setName("John"); + session.save(person); + + session.saveOrUpdate(person); + + } + + @After + public void tearDown() { + try{ + session.getTransaction().commit(); + session.close(); + }catch(TransactionException ex){ + ex.printStackTrace(); + } + } + + @AfterClass + public static void afterTests() { + sessionFactory.close(); + } + +} diff --git a/spring-hibernate5/src/test/java/com/baeldung/persistence/service/FooServiceBasicPersistenceIntegrationTest.java b/spring-hibernate5/src/test/java/com/baeldung/persistence/service/FooServiceBasicPersistenceIntegrationTest.java new file mode 100644 index 0000000000..c77f5dfb95 --- /dev/null +++ b/spring-hibernate5/src/test/java/com/baeldung/persistence/service/FooServiceBasicPersistenceIntegrationTest.java @@ -0,0 +1,54 @@ +package com.baeldung.persistence.service; + +import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic; + +import com.baeldung.persistence.model.Foo; +import com.baeldung.spring.PersistenceConfig; +import org.hibernate.Session; +import org.hibernate.SessionFactory; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.support.AnnotationConfigContextLoader; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = { PersistenceConfig.class }, loader = AnnotationConfigContextLoader.class) +public class FooServiceBasicPersistenceIntegrationTest { + + @Autowired + private SessionFactory sessionFactory; + + @Autowired + private IFooService fooService; + + private Session session; + + // tests + + @Before + public final void before() { + session = sessionFactory.openSession(); + } + + @After + public final void after() { + session.close(); + } + + // tests + + @Test + public final void whenContextIsBootstrapped_thenNoExceptions() { + // + } + + @Test + public final void whenEntityIsCreated_thenNoExceptions() { + fooService.create(new Foo(randomAlphabetic(6))); + } + +} diff --git a/spring-hibernate5/src/test/java/com/baeldung/persistence/service/FooServicePersistenceIntegrationTest.java b/spring-hibernate5/src/test/java/com/baeldung/persistence/service/FooServicePersistenceIntegrationTest.java new file mode 100644 index 0000000000..b82d4621ab --- /dev/null +++ b/spring-hibernate5/src/test/java/com/baeldung/persistence/service/FooServicePersistenceIntegrationTest.java @@ -0,0 +1,63 @@ +package com.baeldung.persistence.service; + +import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic; + +import com.baeldung.persistence.model.Foo; +import com.baeldung.spring.PersistenceConfig; +import org.junit.Ignore; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.dao.DataAccessException; +import org.springframework.dao.DataIntegrityViolationException; +import org.springframework.dao.InvalidDataAccessApiUsageException; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.support.AnnotationConfigContextLoader; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = { PersistenceConfig.class }, loader = AnnotationConfigContextLoader.class) +public class FooServicePersistenceIntegrationTest { + + @Autowired + @Qualifier("fooHibernateService") + private IFooService service; + + // tests + + @Test + public final void whenContextIsBootstrapped_thenNoExceptions() { + // + } + + @Test + public final void whenEntityIsCreated_thenNoExceptions() { + service.create(new Foo(randomAlphabetic(6))); + } + + @Test(expected = DataIntegrityViolationException.class) + @Ignore("work in progress") + public final void whenInvalidEntityIsCreated_thenDataException() { + service.create(new Foo()); + } + + @Test(expected = DataIntegrityViolationException.class) + public final void whenEntityWithLongNameIsCreated_thenDataException() { + service.create(new Foo(randomAlphabetic(2048))); + } + + @Test(expected = InvalidDataAccessApiUsageException.class) + @Ignore("Right now, persist has saveOrUpdate semantics, so this will no longer fail") + public final void whenSameEntityIsCreatedTwice_thenDataException() { + final Foo entity = new Foo(randomAlphabetic(8)); + service.create(entity); + service.create(entity); + } + + @Test(expected = DataAccessException.class) + public final void temp_whenInvalidEntityIsCreated_thenDataException() { + service.create(new Foo(randomAlphabetic(2048))); + } + +} diff --git a/spring-hibernate5/src/test/java/com/baeldung/persistence/service/FooStoredProceduresIntegrationTest.java b/spring-hibernate5/src/test/java/com/baeldung/persistence/service/FooStoredProceduresIntegrationTest.java new file mode 100644 index 0000000000..cfd3844079 --- /dev/null +++ b/spring-hibernate5/src/test/java/com/baeldung/persistence/service/FooStoredProceduresIntegrationTest.java @@ -0,0 +1,114 @@ +package com.baeldung.persistence.service; + +import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic; +import static org.junit.Assert.assertEquals; + +import java.util.List; + +import org.hibernate.Session; +import org.hibernate.SessionFactory; +import org.hibernate.exception.SQLGrammarException; +import org.junit.After; +import org.junit.Assume; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.support.AnnotationConfigContextLoader; + +import com.baeldung.persistence.model.Foo; +import com.baeldung.spring.PersistenceConfig; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = { PersistenceConfig.class }, loader = AnnotationConfigContextLoader.class) +public class FooStoredProceduresIntegrationTest { + + private static final Logger LOGGER = LoggerFactory.getLogger(FooStoredProceduresIntegrationTest.class); + + @Autowired + private SessionFactory sessionFactory; + + @Autowired + private IFooService fooService; + + private Session session; + + @Before + public final void before() { + session = sessionFactory.openSession(); + Assume.assumeTrue(getAllFoosExists()); + Assume.assumeTrue(getFoosByNameExists()); + } + + private boolean getFoosByNameExists() { + try { + session.createQuery("CALL GetAllFoos()", Foo.class).getResultList(); + return true; + } catch (SQLGrammarException e) { + LOGGER.error("WARNING : GetFoosByName() Procedure is may be missing ", e); + return false; + } + } + + private boolean getAllFoosExists() { + try { + session.createQuery("CALL GetAllFoos()", Foo.class).getResultList(); + return true; + } catch (SQLGrammarException e) { + LOGGER.error("WARNING : GetAllFoos() Procedure is may be missing ", e); + return false; + } + } + + @After + public final void after() { + session.close(); + } + + @Test + public final void getAllFoosUsingStoredProcedures() { + + fooService.create(new Foo(randomAlphabetic(6))); + + // Stored procedure getAllFoos using createSQLQuery + List allFoos = session.createQuery("CALL GetAllFoos()", Foo.class).getResultList(); + for (Foo foo : allFoos) { + LOGGER.info("getAllFoos() SQL Query result : {}", foo.getName()); + } + assertEquals(allFoos.size(), fooService.findAll().size()); + + // Stored procedure getAllFoos using a Named Query + @SuppressWarnings("unchecked") + List allFoos2 = session.getNamedQuery("callGetAllFoos").getResultList(); + for (Foo foo : allFoos2) { + LOGGER.info("getAllFoos() NamedQuery result : {}", foo.getName()); + } + assertEquals(allFoos2.size(), fooService.findAll().size()); + } + + @Test + public final void getFoosByNameUsingStoredProcedures() { + + fooService.create(new Foo("NewFooName")); + + // Stored procedure getFoosByName using createSQLQuery() + List allFoosByName = session.createQuery("CALL GetFoosByName(:fooName)", Foo.class) + .setParameter("fooName", "NewFooName").getResultList(); + for (Foo foo : allFoosByName) { + LOGGER.info("getFoosByName() using SQL Query : found => {}", foo.toString()); + } + + // Stored procedure getFoosByName using getNamedQuery() + @SuppressWarnings("unchecked") + List allFoosByName2 = session.getNamedQuery("callGetFoosByName").setParameter("fooName", "NewFooName") + .getResultList(); + for (Foo foo : allFoosByName2) { + LOGGER.info("getFoosByName() using Native Query : found => {}", foo.toString()); + } + + } +} diff --git a/spring-hibernate5/src/test/java/com/baeldung/persistence/service/ParentServicePersistenceIntegrationTest.java b/spring-hibernate5/src/test/java/com/baeldung/persistence/service/ParentServicePersistenceIntegrationTest.java new file mode 100644 index 0000000000..9e8c4aba92 --- /dev/null +++ b/spring-hibernate5/src/test/java/com/baeldung/persistence/service/ParentServicePersistenceIntegrationTest.java @@ -0,0 +1,69 @@ +package com.baeldung.persistence.service; + +import com.baeldung.persistence.model.Child; +import com.baeldung.persistence.model.Parent; +import com.baeldung.spring.PersistenceConfig; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.dao.DataIntegrityViolationException; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.support.AnnotationConfigContextLoader; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = { PersistenceConfig.class }, loader = AnnotationConfigContextLoader.class) +public class ParentServicePersistenceIntegrationTest { + + @Autowired + private IParentService service; + + @Autowired + private IChildService childService; + + // tests + + @Test + public final void whenContextIsBootstrapped_thenNoExceptions() { + // + } + + @Test + public final void whenOneToOneEntitiesAreCreated_thenNoExceptions() { + final Child childEntity = new Child(); + childService.create(childEntity); + + final Parent parentEntity = new Parent(childEntity); + service.create(parentEntity); + + System.out.println("Child = " + childService.findOne(childEntity.getId())); + System.out.println("Child - parent = " + childService.findOne(childEntity.getId()).getParent()); + + System.out.println("Parent = " + service.findOne(parentEntity.getId())); + System.out.println("Parent - child = " + service.findOne(parentEntity.getId()).getChild()); + } + + @Test(expected = DataIntegrityViolationException.class) + public final void whenChildIsDeletedWhileParentStillHasForeignKeyToIt_thenDataException() { + final Child childEntity = new Child(); + childService.create(childEntity); + + final Parent parentEntity = new Parent(childEntity); + service.create(parentEntity); + + childService.delete(childEntity); + } + + @Test + public final void whenChildIsDeletedAfterTheParent_thenNoExceptions() { + final Child childEntity = new Child(); + childService.create(childEntity); + + final Parent parentEntity = new Parent(childEntity); + service.create(parentEntity); + + service.delete(parentEntity); + childService.delete(childEntity); + } + +} diff --git a/spring-hibernate5/src/test/resources/.gitignore b/spring-hibernate5/src/test/resources/.gitignore new file mode 100644 index 0000000000..83c05e60c8 --- /dev/null +++ b/spring-hibernate5/src/test/resources/.gitignore @@ -0,0 +1,13 @@ +*.class + +#folders# +/target +/neoDb* +/data +/src/main/webapp/WEB-INF/classes +*/META-INF/* + +# Packaged files # +*.jar +*.war +*.ear \ No newline at end of file diff --git a/spring-hibernate5/src/test/resources/com/baeldung/hibernate/criteria/model/Item.hbm.xml b/spring-hibernate5/src/test/resources/com/baeldung/hibernate/criteria/model/Item.hbm.xml new file mode 100644 index 0000000000..9e0109aae2 --- /dev/null +++ b/spring-hibernate5/src/test/resources/com/baeldung/hibernate/criteria/model/Item.hbm.xml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/spring-hibernate5/src/test/resources/criteria.cfg.xml b/spring-hibernate5/src/test/resources/criteria.cfg.xml new file mode 100644 index 0000000000..5bc3a32077 --- /dev/null +++ b/spring-hibernate5/src/test/resources/criteria.cfg.xml @@ -0,0 +1,16 @@ + + + + + + com.mysql.jdbc.Driver + jdbc:mysql://localhost:3306/test + root + root + org.hibernate.dialect.MySQLDialect + true + + + \ No newline at end of file diff --git a/spring-hibernate5/src/test/resources/fetching.cfg.xml b/spring-hibernate5/src/test/resources/fetching.cfg.xml new file mode 100644 index 0000000000..c47b062a2f --- /dev/null +++ b/spring-hibernate5/src/test/resources/fetching.cfg.xml @@ -0,0 +1,18 @@ + + + + + + com.mysql.jdbc.Driver + jdbc:mysql://localhost:3306/test + root + root + org.hibernate.dialect.MySQLDialect + true + + + + + \ No newline at end of file diff --git a/spring-hibernate5/src/test/resources/fetchingLazy.cfg.xml b/spring-hibernate5/src/test/resources/fetchingLazy.cfg.xml new file mode 100644 index 0000000000..2e252e9307 --- /dev/null +++ b/spring-hibernate5/src/test/resources/fetchingLazy.cfg.xml @@ -0,0 +1,18 @@ + + + + + + com.mysql.jdbc.Driver + jdbc:mysql://localhost:3306/test + root + root + org.hibernate.dialect.MySQLDialect + true + + + + + \ No newline at end of file From 1a067bc945ae886ee67ff36cd954b4a9ea635ebc Mon Sep 17 00:00:00 2001 From: lor6 Date: Sun, 26 Feb 2017 21:30:47 +0200 Subject: [PATCH 63/68] models, repos, test (#1225) * models, repos, test * update models and test * updated test location * update test location --- .../java/com/baeldung/models/Address.java | 53 +++++++++ .../main/java/com/baeldung/models/Author.java | 59 ++++++++++ .../main/java/com/baeldung/models/Book.java | 70 ++++++++++++ .../java/com/baeldung/models/Library.java | 73 +++++++++++++ .../repositories/AddressRepository.java | 9 ++ .../repositories/AuthorRepository.java | 9 ++ .../baeldung/repositories/BookRepository.java | 9 ++ .../repositories/LibraryRepository.java | 9 ++ .../SpringDataRelationshipsTest.java | 103 ++++++++++++++++++ 9 files changed, 394 insertions(+) create mode 100644 spring-data-rest/src/main/java/com/baeldung/models/Address.java create mode 100644 spring-data-rest/src/main/java/com/baeldung/models/Author.java create mode 100644 spring-data-rest/src/main/java/com/baeldung/models/Book.java create mode 100644 spring-data-rest/src/main/java/com/baeldung/models/Library.java create mode 100644 spring-data-rest/src/main/java/com/baeldung/repositories/AddressRepository.java create mode 100644 spring-data-rest/src/main/java/com/baeldung/repositories/AuthorRepository.java create mode 100644 spring-data-rest/src/main/java/com/baeldung/repositories/BookRepository.java create mode 100644 spring-data-rest/src/main/java/com/baeldung/repositories/LibraryRepository.java create mode 100644 spring-data-rest/src/test/java/com/baeldung/relationships/SpringDataRelationshipsTest.java diff --git a/spring-data-rest/src/main/java/com/baeldung/models/Address.java b/spring-data-rest/src/main/java/com/baeldung/models/Address.java new file mode 100644 index 0000000000..98cf5f0869 --- /dev/null +++ b/spring-data-rest/src/main/java/com/baeldung/models/Address.java @@ -0,0 +1,53 @@ +package com.baeldung.models; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.OneToOne; + +@Entity +public class Address { + + @Id + @GeneratedValue + private long id; + + @Column(nullable = false) + private String location; + + @OneToOne(mappedBy = "address") + private Library library; + + public Address() { + } + + public Address(String location) { + super(); + this.location = location; + } + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + public String getLocation() { + return location; + } + + public void setLocation(String location) { + this.location = location; + } + + public Library getLibrary() { + return library; + } + + public void setLibrary(Library library) { + this.library = library; + } +} diff --git a/spring-data-rest/src/main/java/com/baeldung/models/Author.java b/spring-data-rest/src/main/java/com/baeldung/models/Author.java new file mode 100644 index 0000000000..7025fa4ad3 --- /dev/null +++ b/spring-data-rest/src/main/java/com/baeldung/models/Author.java @@ -0,0 +1,59 @@ +package com.baeldung.models; + +import java.util.List; + +import javax.persistence.CascadeType; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.JoinTable; +import javax.persistence.ManyToMany; + +@Entity +public class Author { + + @Id + @GeneratedValue + private long id; + + @Column(nullable = false) + private String name; + + @ManyToMany(cascade = CascadeType.ALL) + @JoinTable(name = "book_author", joinColumns = @JoinColumn(name = "book_id", referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name = "author_id", referencedColumnName = "id")) + private List books; + + public Author() { + } + + public Author(String name) { + super(); + this.name = name; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + public List getBooks() { + return books; + } + + public void setBooks(List books) { + this.books = books; + } +} diff --git a/spring-data-rest/src/main/java/com/baeldung/models/Book.java b/spring-data-rest/src/main/java/com/baeldung/models/Book.java new file mode 100644 index 0000000000..8f836a259a --- /dev/null +++ b/spring-data-rest/src/main/java/com/baeldung/models/Book.java @@ -0,0 +1,70 @@ +package com.baeldung.models; + +import java.util.List; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.ManyToMany; +import javax.persistence.ManyToOne; + +@Entity +public class Book { + + @Id + @GeneratedValue + private long id; + + @Column(nullable = false) + private String title; + + @ManyToOne + @JoinColumn(name = "library_id") + private Library library; + + @ManyToMany(mappedBy = "books") + private List authors; + + public Book() { + } + + public Book(String title) { + super(); + this.title = title; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + public Library getLibrary() { + return library; + } + + public void setLibrary(Library library) { + this.library = library; + } + + public List getAuthors() { + return authors; + } + + public void setAuthors(List authors) { + this.authors = authors; + } + +} diff --git a/spring-data-rest/src/main/java/com/baeldung/models/Library.java b/spring-data-rest/src/main/java/com/baeldung/models/Library.java new file mode 100644 index 0000000000..61eead16ea --- /dev/null +++ b/spring-data-rest/src/main/java/com/baeldung/models/Library.java @@ -0,0 +1,73 @@ +package com.baeldung.models; + +import java.util.List; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.OneToMany; +import javax.persistence.OneToOne; + +import org.springframework.data.rest.core.annotation.RestResource; + +@Entity +public class Library { + + @Id + @GeneratedValue + private long id; + + @Column + private String name; + + @OneToOne + @JoinColumn(name = "address_id") + @RestResource(path = "libraryAddress") + private Address address; + + @OneToMany(mappedBy = "library") + private List books; + + public Library() { + } + + public Library(String name) { + super(); + this.name = name; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + public Address getAddress() { + return address; + } + + public void setAddress(Address address) { + this.address = address; + } + + public List getBooks() { + return books; + } + + public void setBooks(List books) { + this.books = books; + } + +} diff --git a/spring-data-rest/src/main/java/com/baeldung/repositories/AddressRepository.java b/spring-data-rest/src/main/java/com/baeldung/repositories/AddressRepository.java new file mode 100644 index 0000000000..1cc7527e80 --- /dev/null +++ b/spring-data-rest/src/main/java/com/baeldung/repositories/AddressRepository.java @@ -0,0 +1,9 @@ +package com.baeldung.repositories; + +import org.springframework.data.repository.CrudRepository; + +import com.baeldung.models.Address; + +public interface AddressRepository extends CrudRepository { + +} diff --git a/spring-data-rest/src/main/java/com/baeldung/repositories/AuthorRepository.java b/spring-data-rest/src/main/java/com/baeldung/repositories/AuthorRepository.java new file mode 100644 index 0000000000..2d470367ef --- /dev/null +++ b/spring-data-rest/src/main/java/com/baeldung/repositories/AuthorRepository.java @@ -0,0 +1,9 @@ +package com.baeldung.repositories; + +import org.springframework.data.repository.CrudRepository; + +import com.baeldung.models.Author; + +public interface AuthorRepository extends CrudRepository { + +} diff --git a/spring-data-rest/src/main/java/com/baeldung/repositories/BookRepository.java b/spring-data-rest/src/main/java/com/baeldung/repositories/BookRepository.java new file mode 100644 index 0000000000..f9176032ab --- /dev/null +++ b/spring-data-rest/src/main/java/com/baeldung/repositories/BookRepository.java @@ -0,0 +1,9 @@ +package com.baeldung.repositories; + +import org.springframework.data.repository.CrudRepository; + +import com.baeldung.models.Book; + +public interface BookRepository extends CrudRepository { + +} diff --git a/spring-data-rest/src/main/java/com/baeldung/repositories/LibraryRepository.java b/spring-data-rest/src/main/java/com/baeldung/repositories/LibraryRepository.java new file mode 100644 index 0000000000..c00787f03c --- /dev/null +++ b/spring-data-rest/src/main/java/com/baeldung/repositories/LibraryRepository.java @@ -0,0 +1,9 @@ +package com.baeldung.repositories; + +import org.springframework.data.repository.CrudRepository; + +import com.baeldung.models.Library; + +public interface LibraryRepository extends CrudRepository { + +} diff --git a/spring-data-rest/src/test/java/com/baeldung/relationships/SpringDataRelationshipsTest.java b/spring-data-rest/src/test/java/com/baeldung/relationships/SpringDataRelationshipsTest.java new file mode 100644 index 0000000000..ea2e70a4e4 --- /dev/null +++ b/spring-data-rest/src/test/java/com/baeldung/relationships/SpringDataRelationshipsTest.java @@ -0,0 +1,103 @@ +package com.baeldung.relationships; + +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; +import org.springframework.boot.test.web.client.TestRestTemplate; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; +import org.springframework.http.ResponseEntity; +import org.springframework.test.context.junit4.SpringRunner; + +import com.baeldung.SpringDataRestApplication; +import com.baeldung.models.Address; +import com.baeldung.models.Author; +import com.baeldung.models.Book; +import com.baeldung.models.Library; + +import org.junit.Test; +import static org.junit.Assert.*; + +import org.json.JSONArray; +import org.json.JSONObject; + +@RunWith(SpringRunner.class) +@SpringBootTest(classes = SpringDataRestApplication.class, webEnvironment = WebEnvironment.DEFINED_PORT) +public class SpringDataRelationshipsTest { + + @Autowired + private TestRestTemplate template; + + private static final String BOOK_ENDPOINT = "http://localhost:8080/books/"; + private static final String AUTHOR_ENDPOINT = "http://localhost:8080/authors/"; + private static final String ADDRESS_ENDPOINT = "http://localhost:8080/addresses/"; + private static final String LIBRARY_ENDPOINT = "http://localhost:8080/libraries/"; + + private static final String LIBRARY_NAME = "My Library"; + private static final String AUTHOR_NAME = "George Orwell"; + + @Test + public void whenSaveOneToOneRelationship_thenCorrect() { + Library library = new Library(LIBRARY_NAME); + template.postForEntity(LIBRARY_ENDPOINT, library, Library.class); + + Address address = new Address("Main street, nr 1"); + template.postForEntity(ADDRESS_ENDPOINT, address, Address.class); + + HttpHeaders requestHeaders = new HttpHeaders(); + requestHeaders.add("Content-type", "text/uri-list"); + HttpEntity httpEntity = new HttpEntity(ADDRESS_ENDPOINT + "/1", requestHeaders); + template.exchange(LIBRARY_ENDPOINT + "/1/libraryAddress", HttpMethod.PUT, httpEntity, String.class); + + ResponseEntity libraryGetResponse = template.getForEntity(ADDRESS_ENDPOINT + "/1/library", Library.class); + assertEquals("library is incorrect", libraryGetResponse.getBody() + .getName(), LIBRARY_NAME); + } + + @Test + public void whenSaveOneToManyRelationship_thenCorrect() { + Library library = new Library(LIBRARY_NAME); + template.postForEntity(LIBRARY_ENDPOINT, library, Library.class); + + Book book1 = new Book("Dune"); + template.postForEntity(BOOK_ENDPOINT, book1, Book.class); + + Book book2 = new Book("1984"); + template.postForEntity(BOOK_ENDPOINT, book2, Book.class); + + HttpHeaders requestHeaders = new HttpHeaders(); + requestHeaders.add("Content-type", "text/uri-list"); + HttpEntity bookHttpEntity = new HttpEntity(LIBRARY_ENDPOINT + "/1", requestHeaders); + template.exchange(BOOK_ENDPOINT + "/1/library", HttpMethod.PUT, bookHttpEntity, String.class); + template.exchange(BOOK_ENDPOINT + "/2/library", HttpMethod.PUT, bookHttpEntity, String.class); + + ResponseEntity libraryGetResponse = template.getForEntity(BOOK_ENDPOINT + "/1/library", Library.class); + assertEquals("library is incorrect", libraryGetResponse.getBody() + .getName(), LIBRARY_NAME); + } + + @Test + public void whenSaveManyToManyRelationship_thenCorrect() { + Author author1 = new Author(AUTHOR_NAME); + template.postForEntity(AUTHOR_ENDPOINT, author1, Author.class); + + Book book1 = new Book("Animal Farm"); + template.postForEntity(BOOK_ENDPOINT, book1, Book.class); + + Book book2 = new Book("1984"); + template.postForEntity(BOOK_ENDPOINT, book2, Book.class); + + HttpHeaders requestHeaders = new HttpHeaders(); + requestHeaders.add("Content-type", "text/uri-list"); + HttpEntity httpEntity = new HttpEntity(BOOK_ENDPOINT + "/1\n" + BOOK_ENDPOINT + "/2", requestHeaders); + template.exchange(AUTHOR_ENDPOINT + "/1/books", HttpMethod.PUT, httpEntity, String.class); + + String jsonResponse = template.getForObject(BOOK_ENDPOINT + "/1/authors", String.class); + JSONObject jsonObj = new JSONObject(jsonResponse).getJSONObject("_embedded"); + JSONArray jsonArray = jsonObj.getJSONArray("authors"); + assertEquals("author is incorrect", jsonArray.getJSONObject(0) + .getString("name"), AUTHOR_NAME); + } +} From f1b4ac4cb9450f927da0eb1ac07883dbf0d4512f Mon Sep 17 00:00:00 2001 From: KevinGilmore Date: Sun, 26 Feb 2017 19:53:56 -0600 Subject: [PATCH 64/68] BAEL-554: README.md files (#1252) * BAEL-278: Updated README.md * BAEL-554: Add and update README.md files --- spring-remoting/README.md | 3 ++- spring-remoting/remoting-hessian-burlap/README.md | 9 +++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) create mode 100644 spring-remoting/remoting-hessian-burlap/README.md diff --git a/spring-remoting/README.md b/spring-remoting/README.md index 1aafdaf6f5..41bbd59f01 100644 --- a/spring-remoting/README.md +++ b/spring-remoting/README.md @@ -1,7 +1,8 @@ -## Spring Remoting Tutorials Project +## Spring Remoting Tutorials ### Relevant Articles - [Intro to Spring Remoting with HTTP Invokers](http://www.baeldung.com/spring-remoting-http-invoker) +- [Spring Remoting with Hessian and Burlap](http://www.baeldung.com/spring-remoting-hessian-burlap) ### Overview This Maven project contains the Java source code for various modules used in the Spring Remoting series of articles. diff --git a/spring-remoting/remoting-hessian-burlap/README.md b/spring-remoting/remoting-hessian-burlap/README.md new file mode 100644 index 0000000000..bf90c9676f --- /dev/null +++ b/spring-remoting/remoting-hessian-burlap/README.md @@ -0,0 +1,9 @@ +## Spring Remoting with Hessian and Burlap Tutorial + +### Relevant Articles +- [Spring Remoting with Hessian and Burlap](http://www.baeldung.com/spring-remoting-hessian-burlap) + +### Overview +This Maven project contains the Java source code for the Hessian and Burlap modules + used in the [Spring Remoting](https://github.com/eugenp/tutorials/tree/master/spring-remoting) + series of articles. From ef610affc9c9d20ef3cb02f6415ebe02b8297ea2 Mon Sep 17 00:00:00 2001 From: pedja4 Date: Mon, 27 Feb 2017 10:18:31 +0100 Subject: [PATCH 65/68] BAEL-343 Renamed one test --- .../src/test/java/com/baeldung/reactor/ReactorTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/reactor-core/src/test/java/com/baeldung/reactor/ReactorTest.java b/reactor-core/src/test/java/com/baeldung/reactor/ReactorTest.java index 46b774c30e..0e534e7d61 100644 --- a/reactor-core/src/test/java/com/baeldung/reactor/ReactorTest.java +++ b/reactor-core/src/test/java/com/baeldung/reactor/ReactorTest.java @@ -104,7 +104,7 @@ public class ReactorTest { } @Test - public void givenConnectableFlux_thenShouldStream_onConnect() { + public void givenConnectableFlux_whenConnected_thenShouldStream() { List elements = new ArrayList<>(); From 8617faf9cf7005eb4014e07fc41364547e3716b0 Mon Sep 17 00:00:00 2001 From: lor6 Date: Mon, 27 Feb 2017 17:04:00 +0200 Subject: [PATCH 66/68] data rest crud, angular fe (#1199) * data rest crud, angular fe * fix formatting, remove extra imports * fix config * change student entity to employee * expose ids * update boot version, removes ids, check null id --- spring-rest-angular/pom.xml | 11 +- .../web/controller/EmployeeController.java | 14 ++ .../web/dao/EmployeeCRUDRepository.java | 13 ++ .../baeldung/web/dao/StudentRepository.java | 3 +- .../org/baeldung/web/entity/Employee.java | 57 +++++++ .../java/org/baeldung/web/main/MvcConfig.java | 36 +++++ .../baeldung/web/main/PersistenceConfig.java | 4 +- .../src/main/resources/db/sql/employees.sql | 16 ++ .../main/webapp/WEB-INF/pages/employee.html | 55 +++++++ .../src/main/webapp/view/app.js | 144 +++++++++++++++++- ...EmployeeCRUDRepositoryIntegrationTest.java | 47 ++++++ 11 files changed, 395 insertions(+), 5 deletions(-) create mode 100644 spring-rest-angular/src/main/java/org/baeldung/web/controller/EmployeeController.java create mode 100644 spring-rest-angular/src/main/java/org/baeldung/web/dao/EmployeeCRUDRepository.java create mode 100644 spring-rest-angular/src/main/java/org/baeldung/web/entity/Employee.java create mode 100644 spring-rest-angular/src/main/java/org/baeldung/web/main/MvcConfig.java create mode 100644 spring-rest-angular/src/main/resources/db/sql/employees.sql create mode 100644 spring-rest-angular/src/main/webapp/WEB-INF/pages/employee.html create mode 100644 spring-rest-angular/src/test/java/org/baeldung/web/service/EmployeeCRUDRepositoryIntegrationTest.java diff --git a/spring-rest-angular/pom.xml b/spring-rest-angular/pom.xml index 099867d19b..62ab03522d 100644 --- a/spring-rest-angular/pom.xml +++ b/spring-rest-angular/pom.xml @@ -11,7 +11,7 @@ org.springframework.boot spring-boot-starter-parent - 1.4.4.RELEASE + 1.5.1.RELEASE @@ -74,6 +74,15 @@ spring-boot-starter-test test + + org.springframework.boot + spring-boot-starter-data-rest + + + javax.servlet + jstl + 1.2 + diff --git a/spring-rest-angular/src/main/java/org/baeldung/web/controller/EmployeeController.java b/spring-rest-angular/src/main/java/org/baeldung/web/controller/EmployeeController.java new file mode 100644 index 0000000000..a8bfc254c3 --- /dev/null +++ b/spring-rest-angular/src/main/java/org/baeldung/web/controller/EmployeeController.java @@ -0,0 +1,14 @@ +package org.baeldung.web.controller; + +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; + +@Controller +public class EmployeeController { + + @RequestMapping(value = "/employeePage") + public String getEmployeePage() { + return "employee"; + } + +} diff --git a/spring-rest-angular/src/main/java/org/baeldung/web/dao/EmployeeCRUDRepository.java b/spring-rest-angular/src/main/java/org/baeldung/web/dao/EmployeeCRUDRepository.java new file mode 100644 index 0000000000..1e5f81ed45 --- /dev/null +++ b/spring-rest-angular/src/main/java/org/baeldung/web/dao/EmployeeCRUDRepository.java @@ -0,0 +1,13 @@ +package org.baeldung.web.dao; + +import java.util.List; + +import org.baeldung.web.entity.Employee; +import org.springframework.data.repository.CrudRepository; +import org.springframework.data.repository.query.Param; +import org.springframework.data.rest.core.annotation.RepositoryRestResource; + +@RepositoryRestResource(collectionResourceRel = "employee", path = "employees") +public interface EmployeeCRUDRepository extends CrudRepository { + List findByName(@Param("name") String name); +} diff --git a/spring-rest-angular/src/main/java/org/baeldung/web/dao/StudentRepository.java b/spring-rest-angular/src/main/java/org/baeldung/web/dao/StudentRepository.java index b1aafb583a..566d95da00 100644 --- a/spring-rest-angular/src/main/java/org/baeldung/web/dao/StudentRepository.java +++ b/spring-rest-angular/src/main/java/org/baeldung/web/dao/StudentRepository.java @@ -3,6 +3,7 @@ package org.baeldung.web.dao; import org.baeldung.web.entity.Student; import org.springframework.data.jpa.repository.JpaRepository; -public interface StudentRepository extends JpaRepository { +public interface StudentRepository extends JpaRepository +{ } diff --git a/spring-rest-angular/src/main/java/org/baeldung/web/entity/Employee.java b/spring-rest-angular/src/main/java/org/baeldung/web/entity/Employee.java new file mode 100644 index 0000000000..8d6831726c --- /dev/null +++ b/spring-rest-angular/src/main/java/org/baeldung/web/entity/Employee.java @@ -0,0 +1,57 @@ +package org.baeldung.web.entity; + +import java.io.Serializable; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; + +@Entity +public class Employee implements Serializable { + + private static final long serialVersionUID = 1L; + + @Id + private long id; + + @Column(nullable = false) + private String name; + + @Column(nullable = false) + private Integer age; + + public Employee() { + } + + public Employee(long id, String name, Integer age) { + super(); + this.id = id; + this.name = name; + this.age = age; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Integer getAge() { + return age; + } + + public void setAge(Integer age) { + this.age = age; + } + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + +} diff --git a/spring-rest-angular/src/main/java/org/baeldung/web/main/MvcConfig.java b/spring-rest-angular/src/main/java/org/baeldung/web/main/MvcConfig.java new file mode 100644 index 0000000000..b24aad1177 --- /dev/null +++ b/spring-rest-angular/src/main/java/org/baeldung/web/main/MvcConfig.java @@ -0,0 +1,36 @@ +package org.baeldung.web.main; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.ViewResolver; +import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer; +import org.springframework.web.servlet.config.annotation.EnableWebMvc; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; +import org.springframework.web.servlet.view.InternalResourceViewResolver; + +@Configuration +@EnableWebMvc +@ComponentScan("org.baeldung.web.controller") +public class MvcConfig extends WebMvcConfigurerAdapter{ + + public MvcConfig(){ + super(); + } + + @Override + public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { + configurer.enable(); + } + + @Bean + public ViewResolver viewResolver() { + final InternalResourceViewResolver bean = new InternalResourceViewResolver(); + + bean.setPrefix("/WEB-INF/pages/"); + bean.setSuffix(".html"); + + return bean; + } + +} diff --git a/spring-rest-angular/src/main/java/org/baeldung/web/main/PersistenceConfig.java b/spring-rest-angular/src/main/java/org/baeldung/web/main/PersistenceConfig.java index df1240f270..8454ce155a 100644 --- a/spring-rest-angular/src/main/java/org/baeldung/web/main/PersistenceConfig.java +++ b/spring-rest-angular/src/main/java/org/baeldung/web/main/PersistenceConfig.java @@ -2,7 +2,7 @@ package org.baeldung.web.main; import javax.sql.DataSource; -import org.springframework.boot.orm.jpa.EntityScan; +import org.springframework.boot.autoconfigure.domain.EntityScan; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; @@ -26,7 +26,7 @@ public class PersistenceConfig { @Bean public DataSource dataSource() { EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder(); - EmbeddedDatabase db = builder.setType(EmbeddedDatabaseType.HSQL).addScript("db/sql/data.sql").build(); + EmbeddedDatabase db = builder.setType(EmbeddedDatabaseType.HSQL).addScript("db/sql/data.sql").addScript("db/sql/employees.sql").build(); return db; } diff --git a/spring-rest-angular/src/main/resources/db/sql/employees.sql b/spring-rest-angular/src/main/resources/db/sql/employees.sql new file mode 100644 index 0000000000..366c0c309a --- /dev/null +++ b/spring-rest-angular/src/main/resources/db/sql/employees.sql @@ -0,0 +1,16 @@ +CREATE TABLE employee ( + id INTEGER PRIMARY KEY, + name VARCHAR(30), + age INTEGER +); + +INSERT INTO employee (id,name,age) +VALUES (1,'Bryan',20); +INSERT INTO employee (id,name,age) +VALUES (2,'Lisa',30); +INSERT INTO employee (id,name,age) +VALUES (3,'Laura',40); +INSERT INTO employee (id,name,age) +VALUES (4,'Alex',35); +INSERT INTO employee (id,name,age) +VALUES (5,'John',47); diff --git a/spring-rest-angular/src/main/webapp/WEB-INF/pages/employee.html b/spring-rest-angular/src/main/webapp/WEB-INF/pages/employee.html new file mode 100644 index 0000000000..510e981f25 --- /dev/null +++ b/spring-rest-angular/src/main/webapp/WEB-INF/pages/employee.html @@ -0,0 +1,55 @@ + + + + +Employee CRUD + + + + + + +
+ + + + + + + + + + + + + +
ID:
Name:
Age:
+

+ Get employee + Update employee + Add employee + Delete employee + +

+

{{message}}

+

{{errorMessage}}

+ +
+
+ Get all Employees

+ Name: + Get employees by name +

+
+ {{emp.name}} {{emp.age}} +
+
+ + \ No newline at end of file diff --git a/spring-rest-angular/src/main/webapp/view/app.js b/spring-rest-angular/src/main/webapp/view/app.js index a41026d2c3..9f78d5adf7 100644 --- a/spring-rest-angular/src/main/webapp/view/app.js +++ b/spring-rest-angular/src/main/webapp/view/app.js @@ -1,5 +1,5 @@ var app = angular.module('app', ['ui.grid','ui.grid.pagination']); - + app.controller('StudentCtrl', ['$scope','StudentService', function ($scope,StudentService) { var paginationOptions = { pageNumber: 1, @@ -53,4 +53,146 @@ app.service('StudentService',['$http', function ($http) { getStudents:getStudents }; +}]); + +app.controller('EmployeeCRUDCtrl', ['$scope','EmployeeCRUDService', function ($scope,EmployeeCRUDService) { + + $scope.updateEmployee = function () { + EmployeeCRUDService.updateEmployee($scope.employee.id,$scope.employee.name,$scope.employee.age) + .then(function success(response){ + $scope.message = 'Employee data updated!'; + $scope.errorMessage = ''; + }, + function error(response){ + $scope.errorMessage = 'Error updating Employee!'; + $scope.message = ''; + }); + } + + $scope.getEmployee = function () { + var id = $scope.employee.id; + EmployeeCRUDService.getEmployee($scope.employee.id) + .then(function success(response){ + $scope.employee = response.data; + $scope.employee.id = id; + $scope.message=''; + $scope.errorMessage = ''; + }, + function error (response ){ + $scope.message = ''; + if (response.status === 404){ + $scope.errorMessage = 'Employee not found!'; + } + else { + $scope.errorMessage = "Error getting Employee!"; + } + }); + } + + $scope.addEmployee = function () { + if ($scope.employee != null && $scope.employee.id) { + EmployeeCRUDService.addEmployee($scope.employee.id, $scope.employee.name, $scope.employee.age) + .then (function success(response){ + $scope.message = 'Employee added!'; + $scope.errorMessage = ''; + }, + function error(response){ + $scope.errorMessage = 'Error adding Employee!'; + $scope.message = ''; + }); + } + else { + $scope.errorMessage = 'Please enter an id!'; + $scope.message = ''; + } + } + + $scope.deleteEmployee = function () { + EmployeeCRUDService.deleteEmployee($scope.employee.id) + .then (function success(response){ + $scope.message = 'Employee deleted!'; + $scope.employee = null; + $scope.errorMessage=''; + }, + function error(response){ + $scope.errorMessage = 'Error deleting Employee!'; + $scope.message=''; + }) + } + + $scope.getAllEmployees = function () { + EmployeeCRUDService.getAllEmployees() + .then(function success(response){ + $scope.employees = response.data._embedded.employee; + $scope.message=''; + $scope.errorMessage = ''; + }, + function error (response ){ + $scope.message=''; + $scope.errorMessage = 'Error getting Employees!'; + }); + } + + $scope.getEmployeesByName = function () { + EmployeeCRUDService.getEmployeesByName($scope.name) + .then(function success(response){ + $scope.employees = response.data._embedded.employee; + $scope.message=''; + $scope.errorMessage = ''; + }, + function error (response ){ + $scope.message=''; + $scope.errorMessage = 'Error getting Employees!'; + }); + } + +}]); + +app.service('EmployeeCRUDService',['$http', function ($http) { + + this.getEmployee = function getEmployee(employeeId){ + return $http({ + method: 'GET', + url:'employees/'+employeeId + }); + } + + this.addEmployee = function addEmployee(id, name, age, gender){ + return $http({ + method: 'POST', + url:'employees', + data: {id:id, name:name, age:age} + }); + } + + this.deleteEmployee = function deleteEmployee(id){ + return $http({ + method: 'DELETE', + url: 'employees/'+id + }) + } + + this.updateEmployee = function updateEmployee(id,name,age){ + return $http({ + method: 'PATCH', + url: 'employees/'+id, + data: {name:name, age:age} + }) + } + + this.getAllEmployees = function getAllEmployees(){ + return $http({ + method: 'GET', + url:'employees' + }); + } + + this.getEmployeesByName = function getEmployeesByName(name){ + return $http({ + method: 'GET', + url:'employees/search/findByName', + params:{name:name} + }); + } + }]); \ No newline at end of file diff --git a/spring-rest-angular/src/test/java/org/baeldung/web/service/EmployeeCRUDRepositoryIntegrationTest.java b/spring-rest-angular/src/test/java/org/baeldung/web/service/EmployeeCRUDRepositoryIntegrationTest.java new file mode 100644 index 0000000000..57fe793596 --- /dev/null +++ b/spring-rest-angular/src/test/java/org/baeldung/web/service/EmployeeCRUDRepositoryIntegrationTest.java @@ -0,0 +1,47 @@ +package org.baeldung.web.service; + +import static org.junit.Assert.*; + +import org.baeldung.web.entity.Employee; +import org.baeldung.web.main.Application; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; +import org.springframework.boot.test.web.client.TestRestTemplate; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@SpringBootTest(classes = Application.class, webEnvironment = WebEnvironment.DEFINED_PORT) +public class EmployeeCRUDRepositoryIntegrationTest { + + @Autowired + private TestRestTemplate template; + + private static final String EMPLOYEE_ENDPOINT = "http://localhost:8080/employees/"; + private static int EMPLOYEE_ID = 1; + private static int EMPLOYEE_AGE = 25; + + @Test + public void whenEmployeeCRUDOperations_thenCorrect() { + Employee Employee = new Employee(EMPLOYEE_ID, "Bryan", 20); + ResponseEntity postResponse = template.postForEntity(EMPLOYEE_ENDPOINT, Employee, Employee.class, ""); + assertEquals("status is not 201", HttpStatus.CREATED, postResponse.getStatusCode()); + + Employee.setAge(EMPLOYEE_AGE); + Employee patchResponse = template.patchForObject(EMPLOYEE_ENDPOINT + "/" + EMPLOYEE_ID, Employee, Employee.class); + assertEquals("age is not 25", Integer.valueOf(EMPLOYEE_AGE), patchResponse.getAge()); + + ResponseEntity getResponse = template.getForEntity(EMPLOYEE_ENDPOINT + "/" + EMPLOYEE_ID, Employee.class, ""); + assertEquals("status is not 200", HttpStatus.OK, getResponse.getStatusCode()); + + template.delete(EMPLOYEE_ENDPOINT + "/" + EMPLOYEE_ID); + + getResponse = template.getForEntity(EMPLOYEE_ENDPOINT + "/" + EMPLOYEE_ID, Employee.class, ""); + assertEquals("status is not 404", HttpStatus.NOT_FOUND, getResponse.getStatusCode()); + + } +} From 5930f9dcbb97005230fdc8181c2663e321d0a73f Mon Sep 17 00:00:00 2001 From: Grzegorz Piwowarek Date: Mon, 27 Feb 2017 19:31:02 +0100 Subject: [PATCH 67/68] Refactor SpringDataRelationshipsTest (#1258) --- .../SpringDataRelationshipsTest.java | 26 +++++++++---------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/spring-data-rest/src/test/java/com/baeldung/relationships/SpringDataRelationshipsTest.java b/spring-data-rest/src/test/java/com/baeldung/relationships/SpringDataRelationshipsTest.java index ea2e70a4e4..21a067a645 100644 --- a/spring-data-rest/src/test/java/com/baeldung/relationships/SpringDataRelationshipsTest.java +++ b/spring-data-rest/src/test/java/com/baeldung/relationships/SpringDataRelationshipsTest.java @@ -1,5 +1,13 @@ package com.baeldung.relationships; +import com.baeldung.SpringDataRestApplication; +import com.baeldung.models.Address; +import com.baeldung.models.Author; +import com.baeldung.models.Book; +import com.baeldung.models.Library; +import org.json.JSONArray; +import org.json.JSONObject; +import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; @@ -11,17 +19,7 @@ import org.springframework.http.HttpMethod; import org.springframework.http.ResponseEntity; import org.springframework.test.context.junit4.SpringRunner; -import com.baeldung.SpringDataRestApplication; -import com.baeldung.models.Address; -import com.baeldung.models.Author; -import com.baeldung.models.Book; -import com.baeldung.models.Library; - -import org.junit.Test; -import static org.junit.Assert.*; - -import org.json.JSONArray; -import org.json.JSONObject; +import static org.junit.Assert.assertEquals; @RunWith(SpringRunner.class) @SpringBootTest(classes = SpringDataRestApplication.class, webEnvironment = WebEnvironment.DEFINED_PORT) @@ -48,7 +46,7 @@ public class SpringDataRelationshipsTest { HttpHeaders requestHeaders = new HttpHeaders(); requestHeaders.add("Content-type", "text/uri-list"); - HttpEntity httpEntity = new HttpEntity(ADDRESS_ENDPOINT + "/1", requestHeaders); + HttpEntity httpEntity = new HttpEntity<>(ADDRESS_ENDPOINT + "/1", requestHeaders); template.exchange(LIBRARY_ENDPOINT + "/1/libraryAddress", HttpMethod.PUT, httpEntity, String.class); ResponseEntity libraryGetResponse = template.getForEntity(ADDRESS_ENDPOINT + "/1/library", Library.class); @@ -69,7 +67,7 @@ public class SpringDataRelationshipsTest { HttpHeaders requestHeaders = new HttpHeaders(); requestHeaders.add("Content-type", "text/uri-list"); - HttpEntity bookHttpEntity = new HttpEntity(LIBRARY_ENDPOINT + "/1", requestHeaders); + HttpEntity bookHttpEntity = new HttpEntity<>(LIBRARY_ENDPOINT + "/1", requestHeaders); template.exchange(BOOK_ENDPOINT + "/1/library", HttpMethod.PUT, bookHttpEntity, String.class); template.exchange(BOOK_ENDPOINT + "/2/library", HttpMethod.PUT, bookHttpEntity, String.class); @@ -91,7 +89,7 @@ public class SpringDataRelationshipsTest { HttpHeaders requestHeaders = new HttpHeaders(); requestHeaders.add("Content-type", "text/uri-list"); - HttpEntity httpEntity = new HttpEntity(BOOK_ENDPOINT + "/1\n" + BOOK_ENDPOINT + "/2", requestHeaders); + HttpEntity httpEntity = new HttpEntity<>(BOOK_ENDPOINT + "/1\n" + BOOK_ENDPOINT + "/2", requestHeaders); template.exchange(AUTHOR_ENDPOINT + "/1/books", HttpMethod.PUT, httpEntity, String.class); String jsonResponse = template.getForObject(BOOK_ENDPOINT + "/1/authors", String.class); From 29882a1f06e02faf90ade160996829ad953a1cf9 Mon Sep 17 00:00:00 2001 From: maibin Date: Mon, 27 Feb 2017 19:59:20 +0100 Subject: [PATCH 68/68] Ant Colony Optimization (#1237) --- .../com/baeldung/algorithms/RunAlgorithm.java | 8 +- .../algorithms/{ => ga}/annealing/City.java | 2 +- .../annealing/SimulatedAnnealing.java | 2 +- .../algorithms/{ => ga}/annealing/Travel.java | 2 +- .../algorithms/ga/ant_colony/Ant.java | 37 +++ .../ga/ant_colony/AntColonyOptimization.java | 212 ++++++++++++++++++ .../algorithms/AntColonyOptimizationTest.java | 22 ++ .../algorithms/SimulatedAnnealingTest.java | 2 +- 8 files changed, 282 insertions(+), 5 deletions(-) rename core-java/src/main/java/com/baeldung/algorithms/{ => ga}/annealing/City.java (90%) rename core-java/src/main/java/com/baeldung/algorithms/{ => ga}/annealing/SimulatedAnnealing.java (96%) rename core-java/src/main/java/com/baeldung/algorithms/{ => ga}/annealing/Travel.java (97%) create mode 100644 core-java/src/main/java/com/baeldung/algorithms/ga/ant_colony/Ant.java create mode 100644 core-java/src/main/java/com/baeldung/algorithms/ga/ant_colony/AntColonyOptimization.java create mode 100644 core-java/src/test/java/com/baeldung/algorithms/AntColonyOptimizationTest.java diff --git a/core-java/src/main/java/com/baeldung/algorithms/RunAlgorithm.java b/core-java/src/main/java/com/baeldung/algorithms/RunAlgorithm.java index 3f7c8bf4b3..22c5776293 100644 --- a/core-java/src/main/java/com/baeldung/algorithms/RunAlgorithm.java +++ b/core-java/src/main/java/com/baeldung/algorithms/RunAlgorithm.java @@ -2,7 +2,8 @@ package com.baeldung.algorithms; import java.util.Scanner; -import com.baeldung.algorithms.annealing.SimulatedAnnealing; +import com.baeldung.algorithms.ga.annealing.SimulatedAnnealing; +import com.baeldung.algorithms.ga.ant_colony.AntColonyOptimization; import com.baeldung.algorithms.ga.binary.SimpleGeneticAlgorithm; import com.baeldung.algorithms.slope_one.SlopeOne; @@ -14,6 +15,7 @@ public class RunAlgorithm { System.out.println("1 - Simulated Annealing"); System.out.println("2 - Slope One"); System.out.println("3 - Simple Genetic Algorithm"); + System.out.println("4 - Ant Colony"); int decision = in.nextInt(); switch (decision) { case 1: @@ -27,6 +29,10 @@ public class RunAlgorithm { SimpleGeneticAlgorithm ga = new SimpleGeneticAlgorithm(); ga.runAlgorithm(50, "1011000100000100010000100000100111001000000100000100000000001111"); break; + case 4: + AntColonyOptimization antColony = new AntColonyOptimization(21); + antColony.startAntOptimization(); + break; default: System.out.println("Unknown option"); break; diff --git a/core-java/src/main/java/com/baeldung/algorithms/annealing/City.java b/core-java/src/main/java/com/baeldung/algorithms/ga/annealing/City.java similarity index 90% rename from core-java/src/main/java/com/baeldung/algorithms/annealing/City.java rename to core-java/src/main/java/com/baeldung/algorithms/ga/annealing/City.java index 77e8652df0..cb5647f4d2 100644 --- a/core-java/src/main/java/com/baeldung/algorithms/annealing/City.java +++ b/core-java/src/main/java/com/baeldung/algorithms/ga/annealing/City.java @@ -1,4 +1,4 @@ -package com.baeldung.algorithms.annealing; +package com.baeldung.algorithms.ga.annealing; import lombok.Data; diff --git a/core-java/src/main/java/com/baeldung/algorithms/annealing/SimulatedAnnealing.java b/core-java/src/main/java/com/baeldung/algorithms/ga/annealing/SimulatedAnnealing.java similarity index 96% rename from core-java/src/main/java/com/baeldung/algorithms/annealing/SimulatedAnnealing.java rename to core-java/src/main/java/com/baeldung/algorithms/ga/annealing/SimulatedAnnealing.java index a7dc974e97..bff64fc239 100644 --- a/core-java/src/main/java/com/baeldung/algorithms/annealing/SimulatedAnnealing.java +++ b/core-java/src/main/java/com/baeldung/algorithms/ga/annealing/SimulatedAnnealing.java @@ -1,4 +1,4 @@ -package com.baeldung.algorithms.annealing; +package com.baeldung.algorithms.ga.annealing; public class SimulatedAnnealing { diff --git a/core-java/src/main/java/com/baeldung/algorithms/annealing/Travel.java b/core-java/src/main/java/com/baeldung/algorithms/ga/annealing/Travel.java similarity index 97% rename from core-java/src/main/java/com/baeldung/algorithms/annealing/Travel.java rename to core-java/src/main/java/com/baeldung/algorithms/ga/annealing/Travel.java index 9bf341fbbe..3139b49586 100644 --- a/core-java/src/main/java/com/baeldung/algorithms/annealing/Travel.java +++ b/core-java/src/main/java/com/baeldung/algorithms/ga/annealing/Travel.java @@ -1,4 +1,4 @@ -package com.baeldung.algorithms.annealing; +package com.baeldung.algorithms.ga.annealing; import java.util.ArrayList; import java.util.Collections; diff --git a/core-java/src/main/java/com/baeldung/algorithms/ga/ant_colony/Ant.java b/core-java/src/main/java/com/baeldung/algorithms/ga/ant_colony/Ant.java new file mode 100644 index 0000000000..4ea23b799f --- /dev/null +++ b/core-java/src/main/java/com/baeldung/algorithms/ga/ant_colony/Ant.java @@ -0,0 +1,37 @@ +package com.baeldung.algorithms.ga.ant_colony; + +public class Ant { + + protected int trailSize; + protected int trail[]; + protected boolean visited[]; + + public Ant(int tourSize) { + this.trailSize = tourSize; + this.trail = new int[tourSize]; + this.visited = new boolean[tourSize]; + } + + protected void visitCity(int currentIndex, int city) { + trail[currentIndex + 1] = city; + visited[city] = true; + } + + protected boolean visited(int i) { + return visited[i]; + } + + protected double trailLength(double graph[][]) { + double length = graph[trail[trailSize - 1]][trail[0]]; + for (int i = 0; i < trailSize - 1; i++) { + length += graph[trail[i]][trail[i + 1]]; + } + return length; + } + + protected void clear() { + for (int i = 0; i < trailSize; i++) + visited[i] = false; + } + +} diff --git a/core-java/src/main/java/com/baeldung/algorithms/ga/ant_colony/AntColonyOptimization.java b/core-java/src/main/java/com/baeldung/algorithms/ga/ant_colony/AntColonyOptimization.java new file mode 100644 index 0000000000..e46ac77e84 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/algorithms/ga/ant_colony/AntColonyOptimization.java @@ -0,0 +1,212 @@ +package com.baeldung.algorithms.ga.ant_colony; + +import java.util.Arrays; +import java.util.Random; + +public class AntColonyOptimization { + + private double c = 1.0; + private double alpha = 1; + private double beta = 5; + private double evaporation = 0.5; + private double Q = 500; + private double antFactor = 0.8; + private double randomFactor = 0.01; + + private int maxIterations = 1000; + + public int numberOfCities; + public int numberOfAnts; + private double graph[][]; + private double trails[][]; + private Ant ants[]; + private Random random = new Random(); + private double probabilities[]; + + private int currentIndex; + + public int[] bestTourOrder; + public double bestTourLength; + + public AntColonyOptimization(int noOfCities) { + graph = generateRandomMatrix(noOfCities); + numberOfCities = graph.length; + numberOfAnts = (int) (numberOfCities * antFactor); + + trails = new double[numberOfCities][numberOfCities]; + probabilities = new double[numberOfCities]; + ants = new Ant[numberOfAnts]; + for (int j = 0; j < numberOfAnts; j++) { + ants[j] = new Ant(numberOfCities); + } + } + + /** + * Generate initial solution + * @param n + * @return + */ + public double[][] generateRandomMatrix(int n) { + double[][] randomMatrix = new double[n][n]; + random.setSeed(System.currentTimeMillis()); + for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) { + Integer r = random.nextInt(100) + 1; + randomMatrix[i][j] = Math.abs(r); + } + } + return randomMatrix; + } + + /** + * Perform ant optimization + * @return + */ + public int[] startAntOptimization() { + int[] finalResult = null; + for (int i = 1; i <= 3; i++) { + System.out.println("Attempt #" + i); + finalResult = solve(); + } + return finalResult; + } + + /** + * Use this method to run the main logic + * @return + */ + private int[] solve() { + setupAnts(); + clearTrails(); + int iteration = 0; + while (iteration < maxIterations) { + moveAnts(); + updateTrails(); + updateBest(); + iteration++; + } + System.out.println("Best tour length: " + (bestTourLength - numberOfCities)); + System.out.println("Best tour order: " + Arrays.toString(bestTourOrder)); + return bestTourOrder.clone(); + } + + /** + * Prepare ants for the simulation + */ + private void setupAnts() { + currentIndex = -1; + for (int i = 0; i < numberOfAnts; i++) { + ants[i].clear(); + ants[i].visitCity(currentIndex, random.nextInt(numberOfCities)); + } + currentIndex++; + } + + /** + * At each iteration, move ants + */ + private void moveAnts() { + while (currentIndex < numberOfCities - 1) { + for (Ant a : ants) + a.visitCity(currentIndex, selectNextCity(a)); + currentIndex++; + } + } + + /** + * Select next city for each ant + * @param ant + * @return + */ + private int selectNextCity(Ant ant) { + if (random.nextDouble() < randomFactor) { + int t = random.nextInt(numberOfCities - currentIndex); + int j = -1; + for (int i = 0; i < numberOfCities; i++) { + if (!ant.visited(i)) { + j++; + } + if (j == t) { + return i; + } + } + } + calculateProbabilities(ant); + double r = random.nextDouble(); + double total = 0; + for (int i = 0; i < numberOfCities; i++) { + total += probabilities[i]; + if (total >= r) { + return i; + } + } + + throw new RuntimeException("There are no other cities"); + } + + /** + * Calculate the next city picks probabilites + * @param ant + */ + private void calculateProbabilities(Ant ant) { + int i = ant.trail[currentIndex]; + double pheromone = 0.0; + for (int l = 0; l < numberOfCities; l++) { + if (!ant.visited(l)) { + pheromone += Math.pow(trails[i][l], alpha) * Math.pow(1.0 / graph[i][l], beta); + } + } + for (int j = 0; j < numberOfCities; j++) { + if (ant.visited(j)) { + probabilities[j] = 0.0; + } else { + double numerator = Math.pow(trails[i][j], alpha) * Math.pow(1.0 / graph[i][j], beta); + probabilities[j] = numerator / pheromone; + } + } + } + + /** + * Update trails that ants used + */ + private void updateTrails() { + for (int i = 0; i < numberOfCities; i++) { + for (int j = 0; j < numberOfCities; j++) { + trails[i][j] *= evaporation; + } + } + for (Ant a : ants) { + double contribution = Q / a.trailLength(graph); + for (int i = 0; i < numberOfCities - 1; i++) { + trails[a.trail[i]][a.trail[i + 1]] += contribution; + } + trails[a.trail[numberOfCities - 1]][a.trail[0]] += contribution; + } + } + + /** + * Update the best solution + */ + private void updateBest() { + if (bestTourOrder == null) { + bestTourOrder = ants[0].trail; + bestTourLength = ants[0].trailLength(graph); + } + for (Ant a : ants) { + if (a.trailLength(graph) < bestTourLength) { + bestTourLength = a.trailLength(graph); + bestTourOrder = a.trail.clone(); + } + } + } + + /** + * Clear trails after simulation + */ + private void clearTrails() { + for (int i = 0; i < numberOfCities; i++) + for (int j = 0; j < numberOfCities; j++) + trails[i][j] = c; + } + +} diff --git a/core-java/src/test/java/com/baeldung/algorithms/AntColonyOptimizationTest.java b/core-java/src/test/java/com/baeldung/algorithms/AntColonyOptimizationTest.java new file mode 100644 index 0000000000..cd8efaa106 --- /dev/null +++ b/core-java/src/test/java/com/baeldung/algorithms/AntColonyOptimizationTest.java @@ -0,0 +1,22 @@ +package com.baeldung.algorithms; + +import org.junit.Assert; +import org.junit.Test; + +import com.baeldung.algorithms.ga.ant_colony.AntColonyOptimization; + +public class AntColonyOptimizationTest { + + @Test + public void testGenerateRandomMatrix() { + AntColonyOptimization antTSP = new AntColonyOptimization(5); + Assert.assertNotNull(antTSP.generateRandomMatrix(5)); + } + + @Test + public void testStartAntOptimization() { + AntColonyOptimization antTSP = new AntColonyOptimization(5); + Assert.assertNotNull(antTSP.startAntOptimization()); + } + +} diff --git a/core-java/src/test/java/com/baeldung/algorithms/SimulatedAnnealingTest.java b/core-java/src/test/java/com/baeldung/algorithms/SimulatedAnnealingTest.java index 2fc7ea9b92..6822bae990 100644 --- a/core-java/src/test/java/com/baeldung/algorithms/SimulatedAnnealingTest.java +++ b/core-java/src/test/java/com/baeldung/algorithms/SimulatedAnnealingTest.java @@ -3,7 +3,7 @@ package com.baeldung.algorithms; import org.junit.Assert; import org.junit.Test; -import com.baeldung.algorithms.annealing.SimulatedAnnealing; +import com.baeldung.algorithms.ga.annealing.SimulatedAnnealing; public class SimulatedAnnealingTest {