From 672be6c7ff62bf3aa0c23c2ab63eaa1c75bbb35b Mon Sep 17 00:00:00 2001 From: Sunil Gulabani Date: Fri, 10 Feb 2017 17:54:00 +0530 Subject: [PATCH 001/112] 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 c1b9675c4317b525a5c9e935b82517ba8391b974 Mon Sep 17 00:00:00 2001 From: tschiman Date: Sat, 11 Feb 2017 11:52:16 -0700 Subject: [PATCH 002/112] 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 c9bb4b9e37ca9d4a45a0d90adbd1059ceb7ae27d Mon Sep 17 00:00:00 2001 From: Sunil Gulabani Date: Sun, 12 Feb 2017 13:44:03 +0530 Subject: [PATCH 003/112] 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 0b793f73980d01c12bfb4723c935d7557a1f67d7 Mon Sep 17 00:00:00 2001 From: tschiman Date: Sun, 12 Feb 2017 15:55:20 -0700 Subject: [PATCH 004/112] BAEL-574 integrating zipkin into the cloud project --- .../application-config/zipkin.properties | 7 ++ .../spring-cloud-bootstrap/gateway/pom.xml | 5 + .../bootstrap/gateway/SecurityConfig.java | 1 + .../src/main/resources/bootstrap.properties | 10 +- spring-cloud/spring-cloud-bootstrap/pom.xml | 1 + .../spring-cloud-bootstrap/svc-book/pom.xml | 5 + .../src/main/resources/bootstrap.properties | 3 + .../spring-cloud-bootstrap/svc-rating/pom.xml | 5 + .../src/main/resources/bootstrap.properties | 3 + .../spring-cloud-bootstrap/zipkin/pom.xml | 101 ++++++++++++++++++ .../bootstrap/zipkin/ZipkinApplication.java | 15 +++ .../src/main/resources/bootstrap.properties | 7 ++ 12 files changed, 162 insertions(+), 1 deletion(-) create mode 100644 spring-cloud/spring-cloud-bootstrap/application-config/zipkin.properties create mode 100644 spring-cloud/spring-cloud-bootstrap/zipkin/pom.xml create mode 100644 spring-cloud/spring-cloud-bootstrap/zipkin/src/main/java/com/baeldung/spring/cloud/bootstrap/zipkin/ZipkinApplication.java create mode 100644 spring-cloud/spring-cloud-bootstrap/zipkin/src/main/resources/bootstrap.properties diff --git a/spring-cloud/spring-cloud-bootstrap/application-config/zipkin.properties b/spring-cloud/spring-cloud-bootstrap/application-config/zipkin.properties new file mode 100644 index 0000000000..ca3aed2263 --- /dev/null +++ b/spring-cloud/spring-cloud-bootstrap/application-config/zipkin.properties @@ -0,0 +1,7 @@ +spring.application.name=zipkin +server.port=9411 + +eureka.client.region = default +eureka.client.registryFetchIntervalSeconds = 5 + +logging.level.org.springframework.web=debug diff --git a/spring-cloud/spring-cloud-bootstrap/gateway/pom.xml b/spring-cloud/spring-cloud-bootstrap/gateway/pom.xml index 044730ba22..97c440c249 100644 --- a/spring-cloud/spring-cloud-bootstrap/gateway/pom.xml +++ b/spring-cloud/spring-cloud-bootstrap/gateway/pom.xml @@ -41,6 +41,11 @@ spring-boot-starter-data-redis + + org.springframework.cloud + spring-cloud-starter-zipkin + + org.springframework.boot spring-boot-starter-test diff --git a/spring-cloud/spring-cloud-bootstrap/gateway/src/main/java/com/baeldung/spring/cloud/bootstrap/gateway/SecurityConfig.java b/spring-cloud/spring-cloud-bootstrap/gateway/src/main/java/com/baeldung/spring/cloud/bootstrap/gateway/SecurityConfig.java index 9e5c424403..935e50ec72 100644 --- a/spring-cloud/spring-cloud-bootstrap/gateway/src/main/java/com/baeldung/spring/cloud/bootstrap/gateway/SecurityConfig.java +++ b/spring-cloud/spring-cloud-bootstrap/gateway/src/main/java/com/baeldung/spring/cloud/bootstrap/gateway/SecurityConfig.java @@ -23,6 +23,7 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter { protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .antMatchers("/book-service/books").permitAll() + .antMatchers("/zipkin/**").permitAll() .antMatchers("/eureka/**").hasRole("ADMIN") .anyRequest().authenticated() .and() diff --git a/spring-cloud/spring-cloud-bootstrap/gateway/src/main/resources/bootstrap.properties b/spring-cloud/spring-cloud-bootstrap/gateway/src/main/resources/bootstrap.properties index 43491ff36b..ca453e1007 100644 --- a/spring-cloud/spring-cloud-bootstrap/gateway/src/main/resources/bootstrap.properties +++ b/spring-cloud/spring-cloud-bootstrap/gateway/src/main/resources/bootstrap.properties @@ -4,4 +4,12 @@ spring.cloud.config.discovery.enabled=true spring.cloud.config.username=configUser spring.cloud.config.password=configPassword -eureka.client.serviceUrl.defaultZone=http://discUser:discPassword@localhost:8082/eureka/ \ No newline at end of file +eureka.client.serviceUrl.defaultZone=http://discUser:discPassword@localhost:8082/eureka/ + +zuul.routes.zipkin.path=/zipkin/** +zuul.routes.zipkin.sensitive-headers=Set-Cookie,Authorization +hystrix.command.zipkin.execution.isolation.thread.timeoutInMilliseconds=600000 + +spring.sleuth.sampler.percentage=1.0 +spring.sleuth.web.skipPattern=(.+?cleanup.*|.+favicon.*) +spring.zipkin.base-url=http://zipkin \ No newline at end of file diff --git a/spring-cloud/spring-cloud-bootstrap/pom.xml b/spring-cloud/spring-cloud-bootstrap/pom.xml index ccfbdb9735..83879cf7d4 100644 --- a/spring-cloud/spring-cloud-bootstrap/pom.xml +++ b/spring-cloud/spring-cloud-bootstrap/pom.xml @@ -16,6 +16,7 @@ gateway svc-book svc-rating + zipkin diff --git a/spring-cloud/spring-cloud-bootstrap/svc-book/pom.xml b/spring-cloud/spring-cloud-bootstrap/svc-book/pom.xml index c351c444f6..cbf7083b83 100644 --- a/spring-cloud/spring-cloud-bootstrap/svc-book/pom.xml +++ b/spring-cloud/spring-cloud-bootstrap/svc-book/pom.xml @@ -53,6 +53,11 @@ runtime + + org.springframework.cloud + spring-cloud-starter-zipkin + + org.springframework.boot spring-boot-starter-test diff --git a/spring-cloud/spring-cloud-bootstrap/svc-book/src/main/resources/bootstrap.properties b/spring-cloud/spring-cloud-bootstrap/svc-book/src/main/resources/bootstrap.properties index 8f3a3261ac..e8360ecb6f 100644 --- a/spring-cloud/spring-cloud-bootstrap/svc-book/src/main/resources/bootstrap.properties +++ b/spring-cloud/spring-cloud-bootstrap/svc-book/src/main/resources/bootstrap.properties @@ -5,3 +5,6 @@ spring.cloud.config.username=configUser spring.cloud.config.password=configPassword eureka.client.serviceUrl.defaultZone=http://discUser:discPassword@localhost:8082/eureka/ + +spring.sleuth.sampler.percentage=1.0 +spring.zipkin.base-url=http://zipkin \ No newline at end of file diff --git a/spring-cloud/spring-cloud-bootstrap/svc-rating/pom.xml b/spring-cloud/spring-cloud-bootstrap/svc-rating/pom.xml index 2285286812..f02ba88f04 100644 --- a/spring-cloud/spring-cloud-bootstrap/svc-rating/pom.xml +++ b/spring-cloud/spring-cloud-bootstrap/svc-rating/pom.xml @@ -53,6 +53,11 @@ runtime + + org.springframework.cloud + spring-cloud-starter-zipkin + + org.springframework.boot spring-boot-starter-test diff --git a/spring-cloud/spring-cloud-bootstrap/svc-rating/src/main/resources/bootstrap.properties b/spring-cloud/spring-cloud-bootstrap/svc-rating/src/main/resources/bootstrap.properties index be5cf7f1e1..a43e7d76a5 100644 --- a/spring-cloud/spring-cloud-bootstrap/svc-rating/src/main/resources/bootstrap.properties +++ b/spring-cloud/spring-cloud-bootstrap/svc-rating/src/main/resources/bootstrap.properties @@ -5,3 +5,6 @@ spring.cloud.config.username=configUser spring.cloud.config.password=configPassword eureka.client.serviceUrl.defaultZone=http://discUser:discPassword@localhost:8082/eureka/ + +spring.sleuth.sampler.percentage=1.0 +spring.zipkin.base-url=http://zipkin diff --git a/spring-cloud/spring-cloud-bootstrap/zipkin/pom.xml b/spring-cloud/spring-cloud-bootstrap/zipkin/pom.xml new file mode 100644 index 0000000000..9d43590366 --- /dev/null +++ b/spring-cloud/spring-cloud-bootstrap/zipkin/pom.xml @@ -0,0 +1,101 @@ + + + 4.0.0 + + zipkin + 1.0.0-SNAPSHOT + + + org.springframework.boot + spring-boot-starter-parent + 1.4.4.RELEASE + + + + + + org.springframework.cloud + spring-cloud-starter-config + + + org.springframework.cloud + spring-cloud-starter-eureka + + + + io.zipkin.java + zipkin-server + + + + io.zipkin.java + zipkin-autoconfigure-ui + runtime + + + + + + + + + + + + + + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + org.springframework.cloud + spring-cloud-dependencies + ${spring-cloud-dependencies.version} + pom + import + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + org.apache.maven.plugins + maven-compiler-plugin + ${maven-compiler-plugin.version} + + 1.8 + 1.8 + + + + org.apache.maven.plugins + maven-surefire-plugin + + + **/*LiveTest.java + + + + + + + + Brixton.SR7 + 3.6.0 + + \ No newline at end of file diff --git a/spring-cloud/spring-cloud-bootstrap/zipkin/src/main/java/com/baeldung/spring/cloud/bootstrap/zipkin/ZipkinApplication.java b/spring-cloud/spring-cloud-bootstrap/zipkin/src/main/java/com/baeldung/spring/cloud/bootstrap/zipkin/ZipkinApplication.java new file mode 100644 index 0000000000..bb1355e258 --- /dev/null +++ b/spring-cloud/spring-cloud-bootstrap/zipkin/src/main/java/com/baeldung/spring/cloud/bootstrap/zipkin/ZipkinApplication.java @@ -0,0 +1,15 @@ +package com.baeldung.spring.cloud.bootstrap.zipkin; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.netflix.eureka.EnableEurekaClient; +import zipkin.server.EnableZipkinServer; + +@SpringBootApplication +@EnableEurekaClient +@EnableZipkinServer +public class ZipkinApplication { + public static void main(String[] args) { + SpringApplication.run(ZipkinApplication.class, args); + } +} diff --git a/spring-cloud/spring-cloud-bootstrap/zipkin/src/main/resources/bootstrap.properties b/spring-cloud/spring-cloud-bootstrap/zipkin/src/main/resources/bootstrap.properties new file mode 100644 index 0000000000..9569179a4f --- /dev/null +++ b/spring-cloud/spring-cloud-bootstrap/zipkin/src/main/resources/bootstrap.properties @@ -0,0 +1,7 @@ +spring.cloud.config.name=zipkin +spring.cloud.config.discovery.service-id=config +spring.cloud.config.discovery.enabled=true +spring.cloud.config.username=configUser +spring.cloud.config.password=configPassword + +eureka.client.serviceUrl.defaultZone=http://discUser:discPassword@localhost:8082/eureka/ \ No newline at end of file From 237eb64079946ad76cc5d0acd03d25f767541680 Mon Sep 17 00:00:00 2001 From: tschiman Date: Sun, 12 Feb 2017 15:59:41 -0700 Subject: [PATCH 005/112] BAEL-574 update the readme --- spring-cloud/spring-cloud-bootstrap/README.MD | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/spring-cloud/spring-cloud-bootstrap/README.MD b/spring-cloud/spring-cloud-bootstrap/README.MD index d6f8faf31e..8afbc945ff 100644 --- a/spring-cloud/spring-cloud-bootstrap/README.MD +++ b/spring-cloud/spring-cloud-bootstrap/README.MD @@ -1,3 +1,8 @@ ### Relevant Articles: - [Spring Cloud – Bootstrapping](http://www.baeldung.com/spring-cloud-bootstrapping) - [Spring Cloud – Securing Services](http://www.baeldung.com/spring-cloud-securing-services) + +- To run the project copy the appliction-config folder to c:\Users\{username}\ on Windows or /Users/{username}/ on *nix. Then open a git bash terminal in application-config and run: + - git init + - git add . + - git commit -m "First commit" From bebef9a230bc5ffe91a3c6980daa9d7ed572db56 Mon Sep 17 00:00:00 2001 From: tschiman Date: Sun, 12 Feb 2017 16:01:28 -0700 Subject: [PATCH 006/112] BAEL-574 update the readme --- spring-cloud/spring-cloud-bootstrap/README.MD | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/spring-cloud/spring-cloud-bootstrap/README.MD b/spring-cloud/spring-cloud-bootstrap/README.MD index 8afbc945ff..251c861830 100644 --- a/spring-cloud/spring-cloud-bootstrap/README.MD +++ b/spring-cloud/spring-cloud-bootstrap/README.MD @@ -2,7 +2,11 @@ - [Spring Cloud – Bootstrapping](http://www.baeldung.com/spring-cloud-bootstrapping) - [Spring Cloud – Securing Services](http://www.baeldung.com/spring-cloud-securing-services) -- To run the project copy the appliction-config folder to c:\Users\{username}\ on Windows or /Users/{username}/ on *nix. Then open a git bash terminal in application-config and run: - - git init - - git add . - - git commit -m "First commit" +- To run the project: + - copy the appliction-config folder to c:\Users\{username}\ on Windows or /Users/{username}/ on *nix. Then open a git bash terminal in application-config and run: + - git init + - git add . + - git commit -m "First commit" + - start the config server + - start the discover server + - start all the other servers in any order (gateway, svc-book, svc-rating, zipkin) From b3303dc0cfebe443c1d997900bd8f8cf89d94764 Mon Sep 17 00:00:00 2001 From: Tomasz Lelek Date: Tue, 14 Feb 2017 16:17:55 +0100 Subject: [PATCH 007/112] 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 c4b693745bf120757ec92e76e3379b02a3e5aa67 Mon Sep 17 00:00:00 2001 From: tschiman Date: Tue, 14 Feb 2017 23:53:23 -0700 Subject: [PATCH 008/112] BAEL-574 adding Zipkin Span Reporters to define the URL from eureka --- .../gateway/EurekaZipkinSpanReporter.java | 32 +++++++++++++++++++ .../src/main/resources/bootstrap.properties | 2 +- .../svcbook/EurekaZipkinSpanReporter.java | 32 +++++++++++++++++++ .../src/main/resources/bootstrap.properties | 2 +- .../svcrating/EurekaZipkinSpanReporter.java | 32 +++++++++++++++++++ .../src/main/resources/bootstrap.properties | 2 +- 6 files changed, 99 insertions(+), 3 deletions(-) create mode 100644 spring-cloud/spring-cloud-bootstrap/gateway/src/main/java/com/baeldung/spring/cloud/bootstrap/gateway/EurekaZipkinSpanReporter.java create mode 100644 spring-cloud/spring-cloud-bootstrap/svc-book/src/main/java/com/baeldung/spring/cloud/bootstrap/svcbook/EurekaZipkinSpanReporter.java create mode 100644 spring-cloud/spring-cloud-bootstrap/svc-rating/src/main/java/com/baeldung/spring/cloud/bootstrap/svcrating/EurekaZipkinSpanReporter.java diff --git a/spring-cloud/spring-cloud-bootstrap/gateway/src/main/java/com/baeldung/spring/cloud/bootstrap/gateway/EurekaZipkinSpanReporter.java b/spring-cloud/spring-cloud-bootstrap/gateway/src/main/java/com/baeldung/spring/cloud/bootstrap/gateway/EurekaZipkinSpanReporter.java new file mode 100644 index 0000000000..87f9c0506c --- /dev/null +++ b/spring-cloud/spring-cloud-bootstrap/gateway/src/main/java/com/baeldung/spring/cloud/bootstrap/gateway/EurekaZipkinSpanReporter.java @@ -0,0 +1,32 @@ +package com.baeldung.spring.cloud.bootstrap.gateway; + +import com.netflix.appinfo.InstanceInfo; +import com.netflix.discovery.EurekaClient; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.cloud.sleuth.metric.SpanMetricReporter; +import org.springframework.cloud.sleuth.zipkin.HttpZipkinSpanReporter; +import org.springframework.cloud.sleuth.zipkin.ZipkinProperties; +import org.springframework.cloud.sleuth.zipkin.ZipkinSpanReporter; +import org.springframework.context.annotation.Configuration; +import zipkin.Span; + +@Configuration +public class EurekaZipkinSpanReporter implements ZipkinSpanReporter { + + @Autowired + private EurekaClient eurekaClient; + @Autowired + private SpanMetricReporter spanMetricReporter; + @Autowired + private ZipkinProperties zipkinProperties; + @Value("${spring.sleuth.web.skipPattern}") + private String skipPattern; + + @Override + public void report(Span span) { + InstanceInfo instance = eurekaClient.getNextServerFromEureka("zipkin", false); + HttpZipkinSpanReporter reporter = new HttpZipkinSpanReporter(instance.getHomePageUrl(), zipkinProperties.getFlushInterval(), zipkinProperties.getCompression().isEnabled(), spanMetricReporter); + if (!span.name.matches(skipPattern)) reporter.report(span); + } +} diff --git a/spring-cloud/spring-cloud-bootstrap/gateway/src/main/resources/bootstrap.properties b/spring-cloud/spring-cloud-bootstrap/gateway/src/main/resources/bootstrap.properties index ca453e1007..5756b8205a 100644 --- a/spring-cloud/spring-cloud-bootstrap/gateway/src/main/resources/bootstrap.properties +++ b/spring-cloud/spring-cloud-bootstrap/gateway/src/main/resources/bootstrap.properties @@ -11,5 +11,5 @@ zuul.routes.zipkin.sensitive-headers=Set-Cookie,Authorization hystrix.command.zipkin.execution.isolation.thread.timeoutInMilliseconds=600000 spring.sleuth.sampler.percentage=1.0 -spring.sleuth.web.skipPattern=(.+?cleanup.*|.+favicon.*) +spring.sleuth.web.skipPattern=(^cleanup.*|.+favicon.*) spring.zipkin.base-url=http://zipkin \ No newline at end of file diff --git a/spring-cloud/spring-cloud-bootstrap/svc-book/src/main/java/com/baeldung/spring/cloud/bootstrap/svcbook/EurekaZipkinSpanReporter.java b/spring-cloud/spring-cloud-bootstrap/svc-book/src/main/java/com/baeldung/spring/cloud/bootstrap/svcbook/EurekaZipkinSpanReporter.java new file mode 100644 index 0000000000..96b514ca3f --- /dev/null +++ b/spring-cloud/spring-cloud-bootstrap/svc-book/src/main/java/com/baeldung/spring/cloud/bootstrap/svcbook/EurekaZipkinSpanReporter.java @@ -0,0 +1,32 @@ +package com.baeldung.spring.cloud.bootstrap.svcbook; + +import com.netflix.appinfo.InstanceInfo; +import com.netflix.discovery.EurekaClient; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.cloud.sleuth.metric.SpanMetricReporter; +import org.springframework.cloud.sleuth.zipkin.HttpZipkinSpanReporter; +import org.springframework.cloud.sleuth.zipkin.ZipkinProperties; +import org.springframework.cloud.sleuth.zipkin.ZipkinSpanReporter; +import org.springframework.context.annotation.Configuration; +import zipkin.Span; + +@Configuration +public class EurekaZipkinSpanReporter implements ZipkinSpanReporter { + + @Autowired + private EurekaClient eurekaClient; + @Autowired + private SpanMetricReporter spanMetricReporter; + @Autowired + private ZipkinProperties zipkinProperties; + @Value("${spring.sleuth.web.skipPattern}") + private String skipPattern; + + @Override + public void report(Span span) { + InstanceInfo instance = eurekaClient.getNextServerFromEureka("zipkin", false); + HttpZipkinSpanReporter reporter = new HttpZipkinSpanReporter(instance.getHomePageUrl(), zipkinProperties.getFlushInterval(), zipkinProperties.getCompression().isEnabled(), spanMetricReporter); + if (!span.name.matches(skipPattern)) reporter.report(span); + } +} diff --git a/spring-cloud/spring-cloud-bootstrap/svc-book/src/main/resources/bootstrap.properties b/spring-cloud/spring-cloud-bootstrap/svc-book/src/main/resources/bootstrap.properties index e8360ecb6f..aa24cb2ab3 100644 --- a/spring-cloud/spring-cloud-bootstrap/svc-book/src/main/resources/bootstrap.properties +++ b/spring-cloud/spring-cloud-bootstrap/svc-book/src/main/resources/bootstrap.properties @@ -7,4 +7,4 @@ spring.cloud.config.password=configPassword eureka.client.serviceUrl.defaultZone=http://discUser:discPassword@localhost:8082/eureka/ spring.sleuth.sampler.percentage=1.0 -spring.zipkin.base-url=http://zipkin \ No newline at end of file +spring.sleuth.web.skipPattern=(^cleanup.*) \ 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/EurekaZipkinSpanReporter.java b/spring-cloud/spring-cloud-bootstrap/svc-rating/src/main/java/com/baeldung/spring/cloud/bootstrap/svcrating/EurekaZipkinSpanReporter.java new file mode 100644 index 0000000000..39f9c7c96e --- /dev/null +++ b/spring-cloud/spring-cloud-bootstrap/svc-rating/src/main/java/com/baeldung/spring/cloud/bootstrap/svcrating/EurekaZipkinSpanReporter.java @@ -0,0 +1,32 @@ +package com.baeldung.spring.cloud.bootstrap.svcrating; + +import com.netflix.appinfo.InstanceInfo; +import com.netflix.discovery.EurekaClient; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.cloud.sleuth.metric.SpanMetricReporter; +import org.springframework.cloud.sleuth.zipkin.HttpZipkinSpanReporter; +import org.springframework.cloud.sleuth.zipkin.ZipkinProperties; +import org.springframework.cloud.sleuth.zipkin.ZipkinSpanReporter; +import org.springframework.context.annotation.Configuration; +import zipkin.Span; + +@Configuration +public class EurekaZipkinSpanReporter implements ZipkinSpanReporter { + + @Autowired + private EurekaClient eurekaClient; + @Autowired + private SpanMetricReporter spanMetricReporter; + @Autowired + private ZipkinProperties zipkinProperties; + @Value("${spring.sleuth.web.skipPattern}") + private String skipPattern; + + @Override + public void report(Span span) { + InstanceInfo instance = eurekaClient.getNextServerFromEureka("zipkin", false); + HttpZipkinSpanReporter reporter = new HttpZipkinSpanReporter(instance.getHomePageUrl(), zipkinProperties.getFlushInterval(), zipkinProperties.getCompression().isEnabled(), spanMetricReporter); + if (!span.name.matches(skipPattern)) reporter.report(span); + } +} diff --git a/spring-cloud/spring-cloud-bootstrap/svc-rating/src/main/resources/bootstrap.properties b/spring-cloud/spring-cloud-bootstrap/svc-rating/src/main/resources/bootstrap.properties index a43e7d76a5..ccbd6eeffe 100644 --- a/spring-cloud/spring-cloud-bootstrap/svc-rating/src/main/resources/bootstrap.properties +++ b/spring-cloud/spring-cloud-bootstrap/svc-rating/src/main/resources/bootstrap.properties @@ -7,4 +7,4 @@ spring.cloud.config.password=configPassword eureka.client.serviceUrl.defaultZone=http://discUser:discPassword@localhost:8082/eureka/ spring.sleuth.sampler.percentage=1.0 -spring.zipkin.base-url=http://zipkin +spring.sleuth.web.skipPattern=(^cleanup.*) From dd0158836921e4170255dddc3da808ebe47577a5 Mon Sep 17 00:00:00 2001 From: Sunil Gulabani Date: Wed, 15 Feb 2017 18:07:14 +0530 Subject: [PATCH 009/112] 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 010/112] 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 551d124e8b463bd50b27c912478c0d820bef69ce Mon Sep 17 00:00:00 2001 From: tschiman Date: Wed, 15 Feb 2017 20:13:27 -0700 Subject: [PATCH 011/112] BAEL-574 adding reporters as beans instead of config classes --- .../gateway/EurekaZipkinSpanReporter.java | 32 --------- .../bootstrap/gateway/GatewayApplication.java | 67 ++++++++++++++----- .../svcbook/BookServiceApplication.java | 39 +++++++++++ .../svcbook/EurekaZipkinSpanReporter.java | 32 --------- .../src/main/resources/bootstrap.properties | 3 +- .../svcrating/EurekaZipkinSpanReporter.java | 32 --------- .../svcrating/RatingServiceApplication.java | 38 +++++++++++ 7 files changed, 130 insertions(+), 113 deletions(-) delete mode 100644 spring-cloud/spring-cloud-bootstrap/gateway/src/main/java/com/baeldung/spring/cloud/bootstrap/gateway/EurekaZipkinSpanReporter.java delete mode 100644 spring-cloud/spring-cloud-bootstrap/svc-book/src/main/java/com/baeldung/spring/cloud/bootstrap/svcbook/EurekaZipkinSpanReporter.java delete mode 100644 spring-cloud/spring-cloud-bootstrap/svc-rating/src/main/java/com/baeldung/spring/cloud/bootstrap/svcrating/EurekaZipkinSpanReporter.java diff --git a/spring-cloud/spring-cloud-bootstrap/gateway/src/main/java/com/baeldung/spring/cloud/bootstrap/gateway/EurekaZipkinSpanReporter.java b/spring-cloud/spring-cloud-bootstrap/gateway/src/main/java/com/baeldung/spring/cloud/bootstrap/gateway/EurekaZipkinSpanReporter.java deleted file mode 100644 index 87f9c0506c..0000000000 --- a/spring-cloud/spring-cloud-bootstrap/gateway/src/main/java/com/baeldung/spring/cloud/bootstrap/gateway/EurekaZipkinSpanReporter.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.baeldung.spring.cloud.bootstrap.gateway; - -import com.netflix.appinfo.InstanceInfo; -import com.netflix.discovery.EurekaClient; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.cloud.sleuth.metric.SpanMetricReporter; -import org.springframework.cloud.sleuth.zipkin.HttpZipkinSpanReporter; -import org.springframework.cloud.sleuth.zipkin.ZipkinProperties; -import org.springframework.cloud.sleuth.zipkin.ZipkinSpanReporter; -import org.springframework.context.annotation.Configuration; -import zipkin.Span; - -@Configuration -public class EurekaZipkinSpanReporter implements ZipkinSpanReporter { - - @Autowired - private EurekaClient eurekaClient; - @Autowired - private SpanMetricReporter spanMetricReporter; - @Autowired - private ZipkinProperties zipkinProperties; - @Value("${spring.sleuth.web.skipPattern}") - private String skipPattern; - - @Override - public void report(Span span) { - InstanceInfo instance = eurekaClient.getNextServerFromEureka("zipkin", false); - HttpZipkinSpanReporter reporter = new HttpZipkinSpanReporter(instance.getHomePageUrl(), zipkinProperties.getFlushInterval(), zipkinProperties.getCompression().isEnabled(), spanMetricReporter); - if (!span.name.matches(skipPattern)) reporter.report(span); - } -} diff --git a/spring-cloud/spring-cloud-bootstrap/gateway/src/main/java/com/baeldung/spring/cloud/bootstrap/gateway/GatewayApplication.java b/spring-cloud/spring-cloud-bootstrap/gateway/src/main/java/com/baeldung/spring/cloud/bootstrap/gateway/GatewayApplication.java index b5ae1e4e7b..16aae66cab 100644 --- a/spring-cloud/spring-cloud-bootstrap/gateway/src/main/java/com/baeldung/spring/cloud/bootstrap/gateway/GatewayApplication.java +++ b/spring-cloud/spring-cloud-bootstrap/gateway/src/main/java/com/baeldung/spring/cloud/bootstrap/gateway/GatewayApplication.java @@ -1,6 +1,9 @@ package com.baeldung.spring.cloud.bootstrap.gateway; +import com.netflix.appinfo.InstanceInfo; +import com.netflix.discovery.EurekaClient; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.loadbalancer.LoadBalanced; @@ -8,8 +11,13 @@ import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.netflix.ribbon.RibbonClientSpecification; import org.springframework.cloud.netflix.ribbon.SpringClientFactory; import org.springframework.cloud.netflix.zuul.EnableZuulProxy; +import org.springframework.cloud.sleuth.metric.SpanMetricReporter; +import org.springframework.cloud.sleuth.zipkin.HttpZipkinSpanReporter; +import org.springframework.cloud.sleuth.zipkin.ZipkinProperties; +import org.springframework.cloud.sleuth.zipkin.ZipkinSpanReporter; import org.springframework.context.annotation.Bean; import org.springframework.web.client.RestTemplate; +import zipkin.Span; import java.util.ArrayList; import java.util.List; @@ -18,23 +26,50 @@ import java.util.List; @EnableZuulProxy @EnableEurekaClient public class GatewayApplication { - public static void main(String[] args) { - SpringApplication.run(GatewayApplication.class, args); - } + public static void main(String[] args) { + SpringApplication.run(GatewayApplication.class, args); + } - @Autowired(required = false) - private List configurations = new ArrayList<>(); + @Autowired(required = false) + private List configurations = new ArrayList<>(); + @Autowired + private EurekaClient eurekaClient; + @Autowired + private SpanMetricReporter spanMetricReporter; + @Autowired + private ZipkinProperties zipkinProperties; + @Value("${spring.sleuth.web.skipPattern}") + private String skipPattern; - @Bean - @LoadBalanced - RestTemplate restTemplate() { - return new RestTemplate(); - } + @Bean + @LoadBalanced + RestTemplate restTemplate() { + return new RestTemplate(); + } - @Bean - public SpringClientFactory springClientFactory() { - SpringClientFactory factory = new SpringClientFactory(); - factory.setConfigurations(this.configurations); - return factory; - } + @Bean + public SpringClientFactory springClientFactory() { + SpringClientFactory factory = new SpringClientFactory(); + factory.setConfigurations(this.configurations); + return factory; + } + + @Bean + public ZipkinSpanReporter makeZipkinSpanReporter() { + return new ZipkinSpanReporter() { + private HttpZipkinSpanReporter delegate; + private String baseUrl; + + @Override + public void report(Span span) { + InstanceInfo instance = eurekaClient.getNextServerFromEureka("zipkin", false); + if (!(baseUrl != null && instance.getHomePageUrl().equals(baseUrl))) { + baseUrl = instance.getHomePageUrl(); + delegate = new HttpZipkinSpanReporter(baseUrl, zipkinProperties.getFlushInterval(), zipkinProperties.getCompression().isEnabled(), spanMetricReporter); + if (!span.name.matches(skipPattern)) delegate.report(span); + } + if (!span.name.matches(skipPattern)) delegate.report(span); + } + }; + } } 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 c5499cd924..63ff0fb134 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 @@ -1,13 +1,52 @@ package com.baeldung.spring.cloud.bootstrap.svcbook; +import com.netflix.appinfo.InstanceInfo; +import com.netflix.discovery.EurekaClient; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; +import org.springframework.cloud.sleuth.metric.SpanMetricReporter; +import org.springframework.cloud.sleuth.zipkin.HttpZipkinSpanReporter; +import org.springframework.cloud.sleuth.zipkin.ZipkinProperties; +import org.springframework.cloud.sleuth.zipkin.ZipkinSpanReporter; +import org.springframework.context.annotation.Bean; +import zipkin.Span; @SpringBootApplication @EnableEurekaClient public class BookServiceApplication { + + @Autowired + private EurekaClient eurekaClient; + @Autowired + private SpanMetricReporter spanMetricReporter; + @Autowired + private ZipkinProperties zipkinProperties; + @Value("${spring.sleuth.web.skipPattern}") + private String skipPattern; + public static void main(String[] args) { SpringApplication.run(BookServiceApplication.class, args); } + + @Bean + public ZipkinSpanReporter makeZipkinSpanReporter() { + return new ZipkinSpanReporter() { + private HttpZipkinSpanReporter delegate; + private String baseUrl; + + @Override + public void report(Span span) { + InstanceInfo instance = eurekaClient.getNextServerFromEureka("zipkin", false); + if (!(baseUrl != null && instance.getHomePageUrl().equals(baseUrl))) { + baseUrl = instance.getHomePageUrl(); + delegate = new HttpZipkinSpanReporter(baseUrl, zipkinProperties.getFlushInterval(), zipkinProperties.getCompression().isEnabled(), spanMetricReporter); + if (!span.name.matches(skipPattern)) delegate.report(span); + } + if (!span.name.matches(skipPattern)) delegate.report(span); + } + }; + } } diff --git a/spring-cloud/spring-cloud-bootstrap/svc-book/src/main/java/com/baeldung/spring/cloud/bootstrap/svcbook/EurekaZipkinSpanReporter.java b/spring-cloud/spring-cloud-bootstrap/svc-book/src/main/java/com/baeldung/spring/cloud/bootstrap/svcbook/EurekaZipkinSpanReporter.java deleted file mode 100644 index 96b514ca3f..0000000000 --- a/spring-cloud/spring-cloud-bootstrap/svc-book/src/main/java/com/baeldung/spring/cloud/bootstrap/svcbook/EurekaZipkinSpanReporter.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.baeldung.spring.cloud.bootstrap.svcbook; - -import com.netflix.appinfo.InstanceInfo; -import com.netflix.discovery.EurekaClient; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.cloud.sleuth.metric.SpanMetricReporter; -import org.springframework.cloud.sleuth.zipkin.HttpZipkinSpanReporter; -import org.springframework.cloud.sleuth.zipkin.ZipkinProperties; -import org.springframework.cloud.sleuth.zipkin.ZipkinSpanReporter; -import org.springframework.context.annotation.Configuration; -import zipkin.Span; - -@Configuration -public class EurekaZipkinSpanReporter implements ZipkinSpanReporter { - - @Autowired - private EurekaClient eurekaClient; - @Autowired - private SpanMetricReporter spanMetricReporter; - @Autowired - private ZipkinProperties zipkinProperties; - @Value("${spring.sleuth.web.skipPattern}") - private String skipPattern; - - @Override - public void report(Span span) { - InstanceInfo instance = eurekaClient.getNextServerFromEureka("zipkin", false); - HttpZipkinSpanReporter reporter = new HttpZipkinSpanReporter(instance.getHomePageUrl(), zipkinProperties.getFlushInterval(), zipkinProperties.getCompression().isEnabled(), spanMetricReporter); - if (!span.name.matches(skipPattern)) reporter.report(span); - } -} diff --git a/spring-cloud/spring-cloud-bootstrap/svc-book/src/main/resources/bootstrap.properties b/spring-cloud/spring-cloud-bootstrap/svc-book/src/main/resources/bootstrap.properties index aa24cb2ab3..f183ee9156 100644 --- a/spring-cloud/spring-cloud-bootstrap/svc-book/src/main/resources/bootstrap.properties +++ b/spring-cloud/spring-cloud-bootstrap/svc-book/src/main/resources/bootstrap.properties @@ -7,4 +7,5 @@ spring.cloud.config.password=configPassword eureka.client.serviceUrl.defaultZone=http://discUser:discPassword@localhost:8082/eureka/ spring.sleuth.sampler.percentage=1.0 -spring.sleuth.web.skipPattern=(^cleanup.*) \ No newline at end of file +spring.sleuth.web.skipPattern=(^cleanup.*) +spring.zipkin.locator.discovery.enabled=true \ 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/EurekaZipkinSpanReporter.java b/spring-cloud/spring-cloud-bootstrap/svc-rating/src/main/java/com/baeldung/spring/cloud/bootstrap/svcrating/EurekaZipkinSpanReporter.java deleted file mode 100644 index 39f9c7c96e..0000000000 --- a/spring-cloud/spring-cloud-bootstrap/svc-rating/src/main/java/com/baeldung/spring/cloud/bootstrap/svcrating/EurekaZipkinSpanReporter.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.baeldung.spring.cloud.bootstrap.svcrating; - -import com.netflix.appinfo.InstanceInfo; -import com.netflix.discovery.EurekaClient; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.cloud.sleuth.metric.SpanMetricReporter; -import org.springframework.cloud.sleuth.zipkin.HttpZipkinSpanReporter; -import org.springframework.cloud.sleuth.zipkin.ZipkinProperties; -import org.springframework.cloud.sleuth.zipkin.ZipkinSpanReporter; -import org.springframework.context.annotation.Configuration; -import zipkin.Span; - -@Configuration -public class EurekaZipkinSpanReporter implements ZipkinSpanReporter { - - @Autowired - private EurekaClient eurekaClient; - @Autowired - private SpanMetricReporter spanMetricReporter; - @Autowired - private ZipkinProperties zipkinProperties; - @Value("${spring.sleuth.web.skipPattern}") - private String skipPattern; - - @Override - public void report(Span span) { - InstanceInfo instance = eurekaClient.getNextServerFromEureka("zipkin", false); - HttpZipkinSpanReporter reporter = new HttpZipkinSpanReporter(instance.getHomePageUrl(), zipkinProperties.getFlushInterval(), zipkinProperties.getCompression().isEnabled(), spanMetricReporter); - if (!span.name.matches(skipPattern)) reporter.report(span); - } -} 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 61074e0bcc..0d85c5c825 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 @@ -1,13 +1,51 @@ package com.baeldung.spring.cloud.bootstrap.svcrating; +import com.netflix.appinfo.InstanceInfo; +import com.netflix.discovery.EurekaClient; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; +import org.springframework.cloud.sleuth.metric.SpanMetricReporter; +import org.springframework.cloud.sleuth.zipkin.HttpZipkinSpanReporter; +import org.springframework.cloud.sleuth.zipkin.ZipkinProperties; +import org.springframework.cloud.sleuth.zipkin.ZipkinSpanReporter; +import org.springframework.context.annotation.Bean; +import zipkin.Span; @SpringBootApplication @EnableEurekaClient public class RatingServiceApplication { + @Autowired + private EurekaClient eurekaClient; + @Autowired + private SpanMetricReporter spanMetricReporter; + @Autowired + private ZipkinProperties zipkinProperties; + @Value("${spring.sleuth.web.skipPattern}") + private String skipPattern; + public static void main(String[] args) { SpringApplication.run(RatingServiceApplication.class, args); } + + @Bean + public ZipkinSpanReporter makeZipkinSpanReporter() { + return new ZipkinSpanReporter() { + private HttpZipkinSpanReporter delegate; + private String baseUrl; + + @Override + public void report(Span span) { + InstanceInfo instance = eurekaClient.getNextServerFromEureka("zipkin", false); + if (!(baseUrl != null && instance.getHomePageUrl().equals(baseUrl))) { + baseUrl = instance.getHomePageUrl(); + delegate = new HttpZipkinSpanReporter(baseUrl, zipkinProperties.getFlushInterval(), zipkinProperties.getCompression().isEnabled(), spanMetricReporter); + if (!span.name.matches(skipPattern)) delegate.report(span); + } + if (!span.name.matches(skipPattern)) delegate.report(span); + } + }; + } } \ No newline at end of file From cb5e2ce8f1553423e768a84199379f360fcf5481 Mon Sep 17 00:00:00 2001 From: tschiman Date: Wed, 15 Feb 2017 20:31:45 -0700 Subject: [PATCH 012/112] BAEL-574 deleting commented out code --- spring-cloud/spring-cloud-bootstrap/zipkin/pom.xml | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/spring-cloud/spring-cloud-bootstrap/zipkin/pom.xml b/spring-cloud/spring-cloud-bootstrap/zipkin/pom.xml index 9d43590366..c2fd7cd2dc 100644 --- a/spring-cloud/spring-cloud-bootstrap/zipkin/pom.xml +++ b/spring-cloud/spring-cloud-bootstrap/zipkin/pom.xml @@ -34,19 +34,6 @@ zipkin-autoconfigure-ui runtime - - - - - - - - - - - - - org.springframework.boot From 6f78b05d03da52b4c66da521500a85beead8f473 Mon Sep 17 00:00:00 2001 From: tschiman Date: Wed, 15 Feb 2017 21:07:43 -0700 Subject: [PATCH 013/112] BAEL-574 moving configuration to config server controlled files --- .../application-config/book-service.properties | 3 +++ .../application-config/gateway.properties | 6 +++++- .../application-config/rating-service.properties | 3 +++ .../gateway/src/main/resources/bootstrap.properties | 10 +--------- .../svc-book/src/main/resources/bootstrap.properties | 6 +----- .../svc-rating/src/main/resources/bootstrap.properties | 3 --- 6 files changed, 13 insertions(+), 18 deletions(-) diff --git a/spring-cloud/spring-cloud-bootstrap/application-config/book-service.properties b/spring-cloud/spring-cloud-bootstrap/application-config/book-service.properties index e1244a0cf0..49f7d1ed91 100644 --- a/spring-cloud/spring-cloud-bootstrap/application-config/book-service.properties +++ b/spring-cloud/spring-cloud-bootstrap/application-config/book-service.properties @@ -15,3 +15,6 @@ logging.level.org.springframework.security=debug spring.redis.host=localhost spring.redis.port=6379 + +spring.sleuth.sampler.percentage=1.0 +spring.sleuth.web.skipPattern=(^cleanup.*) diff --git a/spring-cloud/spring-cloud-bootstrap/application-config/gateway.properties b/spring-cloud/spring-cloud-bootstrap/application-config/gateway.properties index 09f7f3bf4a..e2686ae64c 100644 --- a/spring-cloud/spring-cloud-bootstrap/application-config/gateway.properties +++ b/spring-cloud/spring-cloud-bootstrap/application-config/gateway.properties @@ -24,4 +24,8 @@ logging.level.org.springframework.security=debug logging.level.org.springframework.cloud.netflix.zuul=debug spring.redis.host=localhost -spring.redis.port=6379 \ No newline at end of file +spring.redis.port=6379 + +spring.sleuth.sampler.percentage=1.0 +spring.sleuth.web.skipPattern=(^cleanup.*|.+favicon.*) +spring.zipkin.base-url=http://zipkin \ No newline at end of file diff --git a/spring-cloud/spring-cloud-bootstrap/application-config/rating-service.properties b/spring-cloud/spring-cloud-bootstrap/application-config/rating-service.properties index 4817d12c83..b7cbb6fbd6 100644 --- a/spring-cloud/spring-cloud-bootstrap/application-config/rating-service.properties +++ b/spring-cloud/spring-cloud-bootstrap/application-config/rating-service.properties @@ -15,3 +15,6 @@ logging.level.org.springframework.security=debug spring.redis.host=localhost spring.redis.port=6379 + +spring.sleuth.sampler.percentage=1.0 +spring.sleuth.web.skipPattern=(^cleanup.*) diff --git a/spring-cloud/spring-cloud-bootstrap/gateway/src/main/resources/bootstrap.properties b/spring-cloud/spring-cloud-bootstrap/gateway/src/main/resources/bootstrap.properties index 5756b8205a..43491ff36b 100644 --- a/spring-cloud/spring-cloud-bootstrap/gateway/src/main/resources/bootstrap.properties +++ b/spring-cloud/spring-cloud-bootstrap/gateway/src/main/resources/bootstrap.properties @@ -4,12 +4,4 @@ spring.cloud.config.discovery.enabled=true spring.cloud.config.username=configUser spring.cloud.config.password=configPassword -eureka.client.serviceUrl.defaultZone=http://discUser:discPassword@localhost:8082/eureka/ - -zuul.routes.zipkin.path=/zipkin/** -zuul.routes.zipkin.sensitive-headers=Set-Cookie,Authorization -hystrix.command.zipkin.execution.isolation.thread.timeoutInMilliseconds=600000 - -spring.sleuth.sampler.percentage=1.0 -spring.sleuth.web.skipPattern=(^cleanup.*|.+favicon.*) -spring.zipkin.base-url=http://zipkin \ No newline at end of file +eureka.client.serviceUrl.defaultZone=http://discUser:discPassword@localhost:8082/eureka/ \ No newline at end of file diff --git a/spring-cloud/spring-cloud-bootstrap/svc-book/src/main/resources/bootstrap.properties b/spring-cloud/spring-cloud-bootstrap/svc-book/src/main/resources/bootstrap.properties index f183ee9156..481cdc182c 100644 --- a/spring-cloud/spring-cloud-bootstrap/svc-book/src/main/resources/bootstrap.properties +++ b/spring-cloud/spring-cloud-bootstrap/svc-book/src/main/resources/bootstrap.properties @@ -4,8 +4,4 @@ spring.cloud.config.discovery.enabled=true spring.cloud.config.username=configUser spring.cloud.config.password=configPassword -eureka.client.serviceUrl.defaultZone=http://discUser:discPassword@localhost:8082/eureka/ - -spring.sleuth.sampler.percentage=1.0 -spring.sleuth.web.skipPattern=(^cleanup.*) -spring.zipkin.locator.discovery.enabled=true \ No newline at end of file +eureka.client.serviceUrl.defaultZone=http://discUser:discPassword@localhost:8082/eureka/ \ No newline at end of file diff --git a/spring-cloud/spring-cloud-bootstrap/svc-rating/src/main/resources/bootstrap.properties b/spring-cloud/spring-cloud-bootstrap/svc-rating/src/main/resources/bootstrap.properties index ccbd6eeffe..be5cf7f1e1 100644 --- a/spring-cloud/spring-cloud-bootstrap/svc-rating/src/main/resources/bootstrap.properties +++ b/spring-cloud/spring-cloud-bootstrap/svc-rating/src/main/resources/bootstrap.properties @@ -5,6 +5,3 @@ spring.cloud.config.username=configUser spring.cloud.config.password=configPassword eureka.client.serviceUrl.defaultZone=http://discUser:discPassword@localhost:8082/eureka/ - -spring.sleuth.sampler.percentage=1.0 -spring.sleuth.web.skipPattern=(^cleanup.*) From 895bcc1921eb716d6f7bd08bbef8c1e124753445 Mon Sep 17 00:00:00 2001 From: Tomasz Lelek Date: Fri, 17 Feb 2017 12:19:57 +0100 Subject: [PATCH 014/112] 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 015/112] 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 00d83f90709108e88c16ade58dbeb9899f7067dc Mon Sep 17 00:00:00 2001 From: Tomasz Lelek Date: Fri, 17 Feb 2017 18:26:23 +0100 Subject: [PATCH 016/112] 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 8d4b69671a164f224c1bb7593973a9eca1b2e19c Mon Sep 17 00:00:00 2001 From: Tomasz Lelek Date: Sat, 18 Feb 2017 13:59:08 +0100 Subject: [PATCH 017/112] 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 018/112] 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 e35c452ad1ac8ce4fd6bbf5f182cdf952e6bf02e Mon Sep 17 00:00:00 2001 From: Tomasz Lelek Date: Sat, 18 Feb 2017 22:40:15 +0100 Subject: [PATCH 019/112] 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 e5ecd86f27adedff215716f6916e72caccdfda83 Mon Sep 17 00:00:00 2001 From: mariiakulik Date: Sun, 19 Feb 2017 22:22:43 +0100 Subject: [PATCH 020/112] 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 021/112] 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 022/112] 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 08ced4054783518d34038059f831fe902ca32e20 Mon Sep 17 00:00:00 2001 From: Tomasz Lelek Date: Mon, 20 Feb 2017 10:13:25 +0100 Subject: [PATCH 023/112] 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 024/112] 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 025/112] 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 026/112] 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 027/112] 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 028/112] 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 029/112] 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 030/112] 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 031/112] 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 032/112] 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 033/112] 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 034/112] 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 fc570b9e2ec30c120142cafe0313b369d41eb188 Mon Sep 17 00:00:00 2001 From: root Date: Wed, 22 Feb 2017 11:59:50 -0400 Subject: [PATCH 035/112] JSR-354 Java Money & Currency --- .../.resourceCache/ECBCurrentRateProvider.dat | 42 +++++ .../ECBHistoric90RateProvider.dat | 63 ++++++++ .../IMFHistoricRateProvider.dat | 118 ++++++++++++++ core-java/.resourceCache/IMFRateProvider.dat | 118 ++++++++++++++ core-java/pom.xml | 6 + .../java/com/baeldung/money/JavaMoney.java | 147 ++++++++++++++++++ .../com/baeldung/money/JavaMoneyTest.java | 58 +++++++ 7 files changed, 552 insertions(+) create mode 100644 core-java/.resourceCache/ECBCurrentRateProvider.dat create mode 100644 core-java/.resourceCache/ECBHistoric90RateProvider.dat create mode 100644 core-java/.resourceCache/IMFHistoricRateProvider.dat create mode 100644 core-java/.resourceCache/IMFRateProvider.dat create mode 100644 core-java/src/main/java/com/baeldung/money/JavaMoney.java create mode 100644 core-java/src/test/java/com/baeldung/money/JavaMoneyTest.java diff --git a/core-java/.resourceCache/ECBCurrentRateProvider.dat b/core-java/.resourceCache/ECBCurrentRateProvider.dat new file mode 100644 index 0000000000..37d1d8c8ab --- /dev/null +++ b/core-java/.resourceCache/ECBCurrentRateProvider.dat @@ -0,0 +1,42 @@ + + + Reference rates + + European Central Bank + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/core-java/.resourceCache/ECBHistoric90RateProvider.dat b/core-java/.resourceCache/ECBHistoric90RateProvider.dat new file mode 100644 index 0000000000..16edc6a30a --- /dev/null +++ b/core-java/.resourceCache/ECBHistoric90RateProvider.dat @@ -0,0 +1,63 @@ +Reference ratesEuropean Central Bank + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/core-java/.resourceCache/IMFHistoricRateProvider.dat b/core-java/.resourceCache/IMFHistoricRateProvider.dat new file mode 100644 index 0000000000..1db358b58b --- /dev/null +++ b/core-java/.resourceCache/IMFHistoricRateProvider.dat @@ -0,0 +1,118 @@ +SDRs per Currency unit and Currency units per SDR (1) +last five days +SDRs per Currency unit (2) + +Currency February 22, 2017 February 21, 2017 February 17, 2017 February 16, 2017 February 15, 2017 +Chinese Yuan 0.1077740000 0.1075920000 0.1074250000 0.1075990000 0.1078580000 +Euro 0.7806080000 0.7860480000 0.7864600000 0.7819020000 +Japanese Yen 0.0065267800 0.0065271000 0.0065062900 0.0064850300 0.0064793800 +U.K. Pound Sterling 0.9217470000 0.9201430000 0.9163910000 0.9224580000 0.9204290000 +U.S. Dollar 0.7413120000 0.7408260000 0.7380730000 0.7383210000 0.7407880000 +Algerian Dinar 0.0067122300 0.0067137500 0.0067192300 0.0067098500 +Australian Dollar 0.5680650000 0.5689800000 0.5694670000 0.5682580000 +Bahrain Dinar 1.9702800000 1.9629600000 1.9636200000 1.9701800000 +Botswana Pula 0.0708970000 0.0709288000 0.0712480000 0.0711156000 +Brazilian Real 0.2396560000 0.2419590000 0.2399250000 0.2389790000 +Brunei Dollar 0.5214510000 0.5207230000 0.5201640000 0.5219390000 +Canadian Dollar 0.5634090000 0.5627270000 0.5661790000 +Chilean Peso 0.0011535600 0.0011562200 0.0011537500 +Colombian Peso 0.0002552100 0.0002566600 0.0002567150 0.0002583270 +Czech Koruna 0.0288889000 0.0290901000 0.0291136000 +Danish Krone 0.1050150000 0.1057460000 0.1057900000 0.1051720000 +Hungarian Forint 0.0025387300 0.0025516800 0.0025498900 0.0025358200 +Icelandic Krona 0.0066885700 0.0066860500 0.0066725800 0.0066218600 +Indian Rupee 0.0110083000 0.0110285000 0.0110699000 +Indonesian Rupiah 0.0000554096 0.0000553776 0.0000553921 +Iranian Rial 0.0000228671 0.0000227947 0.0000228695 +Israeli New Sheqel 0.1998450000 0.1986200000 0.1986870000 0.1977020000 +Kazakhstani Tenge 0.0023350800 0.0023165400 0.0023114400 0.0023099100 +Korean Won 0.0006455440 0.0006480580 0.0006467420 0.0006472590 +Kuwaiti Dinar 2.4257600000 2.4171400000 2.4179500000 2.4248400000 +Libyan Dinar 0.5174910000 0.5174910000 0.5174910000 0.5174910000 +Malaysian Ringgit 0.1661230000 0.1655610000 0.1656540000 0.1664690000 +Mauritian Rupee 0.0207521000 0.0207775000 0.0208634000 +Mexican Peso 0.0360870000 0.0364464000 +Nepalese Rupee 0.0069139200 0.0068914400 0.0068937500 0.0069148500 +New Zealand Dollar 0.5306540000 0.5322980000 0.5342490000 0.5300340000 +Norwegian Krone 0.0885668000 0.0886734000 0.0886755000 0.0883278000 +Rial Omani 1.9267300000 1.9195700000 1.9202100000 1.9266300000 +Pakistani Rupee 0.0070658600 0.0070405000 0.0070429300 0.0070671000 +Nuevo Sol 0.2264720000 +Philippine Peso 0.0147628000 0.0147836000 0.0148472000 +Polish Zloty 0.1809450000 0.1813760000 0.1820000000 0.1817800000 +Qatar Riyal 0.2035240000 0.2027670000 0.2028350000 0.2035130000 +Russian Ruble 0.0128040000 0.0128062000 0.0129188000 0.0130485000 +Saudi Arabian Riyal 0.1975540000 0.1968190000 0.1968860000 0.1975430000 +Singapore Dollar 0.5214510000 0.5207230000 0.5201640000 0.5219390000 +South African Rand 0.0562263000 0.0562860000 0.0569367000 0.0568608000 +Sri Lanka Rupee 0.0049020900 0.0048960700 0.0048979200 0.0049141600 +Swedish Krona 0.0824973000 0.0830639000 0.0829146000 0.0828400000 +Swiss Franc 0.7337090000 0.7385900000 0.7376570000 0.7341080000 +Thai Baht 0.0211393000 0.0210914000 0.0210961000 0.0211545000 +Trinidad And Tobago Dollar 0.1095510000 0.1093120000 0.1094000000 0.1097660000 +Tunisian Dinar 0.3234230000 0.3229230000 0.3212750000 0.3239130000 +U.A.E. Dirham 0.2017230000 0.2009730000 0.2010400000 0.2017120000 +Peso Uruguayo 0.0260937000 0.0260969000 +Bolivar Fuerte 0.0740171000 0.0742645000 + +Currency units per SDR(3) + +Currency February 22, 2017 February 21, 2017 February 17, 2017 February 16, 2017 February 15, 2017 +Chinese Yuan 9.278680 9.294370 9.308820 9.293770 9.271450 +Euro 1.281050 1.272190 1.271520 1.278930 +Japanese Yen 153.215000 153.207000 153.697000 154.201000 154.336000 +U.K. Pound Sterling 1.084900 1.086790 1.091240 1.084060 1.086450 +U.S. Dollar 1.348960 1.349840 1.354880 1.354420 1.349910 +Algerian Dinar 148.982000 148.948000 148.827000 149.035000 +Australian Dollar 1.760360 1.757530 1.756030 1.759760 +Bahrain Dinar 0.507542 0.509435 0.509264 0.507568 +Botswana Pula 14.105000 14.098600 14.035500 14.061600 +Brazilian Real 4.172650 4.132930 4.167970 4.184470 +Brunei Dollar 1.917730 1.920410 1.922470 1.915930 +Canadian Dollar 1.774910 1.777060 1.766230 +Chilean Peso 866.882000 864.887000 866.739000 +Colombian Peso 3,918.340000 3,896.210000 3,895.370000 3,871.060000 +Czech Koruna 34.615400 34.376000 34.348200 +Danish Krone 9.522450 9.456620 9.452690 9.508230 +Hungarian Forint 393.898000 391.899000 392.174000 394.350000 +Icelandic Krona 149.509000 149.565000 149.867000 151.015000 +Indian Rupee 90.840500 90.674200 90.335100 +Indonesian Rupiah 18,047.400000 18,057.800000 18,053.100000 +Iranian Rial 43,730.900000 43,869.800000 43,726.400000 +Israeli New Sheqel 5.003880 5.034740 5.033040 5.058120 +Kazakhstani Tenge 428.251000 431.678000 432.631000 432.917000 +Korean Won 1,549.080000 1,543.070000 1,546.210000 1,544.980000 +Kuwaiti Dinar 0.412242 0.413712 0.413573 0.412398 +Libyan Dinar 1.932400 1.932400 1.932400 1.932400 +Malaysian Ringgit 6.019640 6.040070 6.036680 6.007120 +Mauritian Rupee 48.187900 48.129000 47.930800 +Mexican Peso 27.710800 27.437600 +Nepalese Rupee 144.636000 145.108000 145.059000 144.616000 +New Zealand Dollar 1.884470 1.878650 1.871790 1.886670 +Norwegian Krone 11.290900 11.277300 11.277100 11.321500 +Rial Omani 0.519014 0.520950 0.520776 0.519041 +Pakistani Rupee 141.526000 142.035000 141.986000 141.501000 +Nuevo Sol 4.415560 +Philippine Peso 67.737800 67.642500 67.352800 +Polish Zloty 5.526540 5.513410 5.494510 5.501160 +Qatar Riyal 4.913430 4.931770 4.930120 4.913690 +Russian Ruble 78.100600 78.087200 77.406600 76.637200 +Saudi Arabian Riyal 5.061910 5.080810 5.079080 5.062190 +Singapore Dollar 1.917730 1.920410 1.922470 1.915930 +South African Rand 17.785300 17.766400 17.563400 17.586800 +Sri Lanka Rupee 203.995000 204.245000 204.168000 203.494000 +Swedish Krona 12.121600 12.038900 12.060600 12.071500 +Swiss Franc 1.362940 1.353930 1.355640 1.362200 +Thai Baht 47.305300 47.412700 47.402100 47.271300 +Trinidad And Tobago Dollar 9.128170 9.148130 9.140770 9.110290 +Tunisian Dinar 3.091930 3.096710 3.112600 3.087250 +U.A.E. Dirham 4.957290 4.975790 4.974130 4.957560 +Peso Uruguayo 38.323400 38.318700 +Bolivar Fuerte 13.510400 13.465400 + + +(1) Exchange rates are published daily except on IMF holidays or whenever the IMF is closed for business. + +(2) The value of the U.S. dollar in terms of the SDR is the reciprocal of the sum of the dollar values, based on market exchange rates, of specified quantities of the SDR basket currencies. See SDR Valuation.The value in terms of the SDR of each of the other currencies shown above is derived from that currency's representative exchange rate against the U.S. dollar as reported by the issuing central bank and the SDR value of the U.S. dollar, except for the Iranian rial and the Libyan dinar, the values of which are officially expressed directly in terms of domestic currency units per SDR. All figures are rounded to six significant digits. See Representative Exchange Rates for Selected Currencies". + +(3) The value in terms of each national currency of the SDR is the reciprocal of the value in terms of the SDR of each national currency, rounded to six significant digits. \ No newline at end of file diff --git a/core-java/.resourceCache/IMFRateProvider.dat b/core-java/.resourceCache/IMFRateProvider.dat new file mode 100644 index 0000000000..1db358b58b --- /dev/null +++ b/core-java/.resourceCache/IMFRateProvider.dat @@ -0,0 +1,118 @@ +SDRs per Currency unit and Currency units per SDR (1) +last five days +SDRs per Currency unit (2) + +Currency February 22, 2017 February 21, 2017 February 17, 2017 February 16, 2017 February 15, 2017 +Chinese Yuan 0.1077740000 0.1075920000 0.1074250000 0.1075990000 0.1078580000 +Euro 0.7806080000 0.7860480000 0.7864600000 0.7819020000 +Japanese Yen 0.0065267800 0.0065271000 0.0065062900 0.0064850300 0.0064793800 +U.K. Pound Sterling 0.9217470000 0.9201430000 0.9163910000 0.9224580000 0.9204290000 +U.S. Dollar 0.7413120000 0.7408260000 0.7380730000 0.7383210000 0.7407880000 +Algerian Dinar 0.0067122300 0.0067137500 0.0067192300 0.0067098500 +Australian Dollar 0.5680650000 0.5689800000 0.5694670000 0.5682580000 +Bahrain Dinar 1.9702800000 1.9629600000 1.9636200000 1.9701800000 +Botswana Pula 0.0708970000 0.0709288000 0.0712480000 0.0711156000 +Brazilian Real 0.2396560000 0.2419590000 0.2399250000 0.2389790000 +Brunei Dollar 0.5214510000 0.5207230000 0.5201640000 0.5219390000 +Canadian Dollar 0.5634090000 0.5627270000 0.5661790000 +Chilean Peso 0.0011535600 0.0011562200 0.0011537500 +Colombian Peso 0.0002552100 0.0002566600 0.0002567150 0.0002583270 +Czech Koruna 0.0288889000 0.0290901000 0.0291136000 +Danish Krone 0.1050150000 0.1057460000 0.1057900000 0.1051720000 +Hungarian Forint 0.0025387300 0.0025516800 0.0025498900 0.0025358200 +Icelandic Krona 0.0066885700 0.0066860500 0.0066725800 0.0066218600 +Indian Rupee 0.0110083000 0.0110285000 0.0110699000 +Indonesian Rupiah 0.0000554096 0.0000553776 0.0000553921 +Iranian Rial 0.0000228671 0.0000227947 0.0000228695 +Israeli New Sheqel 0.1998450000 0.1986200000 0.1986870000 0.1977020000 +Kazakhstani Tenge 0.0023350800 0.0023165400 0.0023114400 0.0023099100 +Korean Won 0.0006455440 0.0006480580 0.0006467420 0.0006472590 +Kuwaiti Dinar 2.4257600000 2.4171400000 2.4179500000 2.4248400000 +Libyan Dinar 0.5174910000 0.5174910000 0.5174910000 0.5174910000 +Malaysian Ringgit 0.1661230000 0.1655610000 0.1656540000 0.1664690000 +Mauritian Rupee 0.0207521000 0.0207775000 0.0208634000 +Mexican Peso 0.0360870000 0.0364464000 +Nepalese Rupee 0.0069139200 0.0068914400 0.0068937500 0.0069148500 +New Zealand Dollar 0.5306540000 0.5322980000 0.5342490000 0.5300340000 +Norwegian Krone 0.0885668000 0.0886734000 0.0886755000 0.0883278000 +Rial Omani 1.9267300000 1.9195700000 1.9202100000 1.9266300000 +Pakistani Rupee 0.0070658600 0.0070405000 0.0070429300 0.0070671000 +Nuevo Sol 0.2264720000 +Philippine Peso 0.0147628000 0.0147836000 0.0148472000 +Polish Zloty 0.1809450000 0.1813760000 0.1820000000 0.1817800000 +Qatar Riyal 0.2035240000 0.2027670000 0.2028350000 0.2035130000 +Russian Ruble 0.0128040000 0.0128062000 0.0129188000 0.0130485000 +Saudi Arabian Riyal 0.1975540000 0.1968190000 0.1968860000 0.1975430000 +Singapore Dollar 0.5214510000 0.5207230000 0.5201640000 0.5219390000 +South African Rand 0.0562263000 0.0562860000 0.0569367000 0.0568608000 +Sri Lanka Rupee 0.0049020900 0.0048960700 0.0048979200 0.0049141600 +Swedish Krona 0.0824973000 0.0830639000 0.0829146000 0.0828400000 +Swiss Franc 0.7337090000 0.7385900000 0.7376570000 0.7341080000 +Thai Baht 0.0211393000 0.0210914000 0.0210961000 0.0211545000 +Trinidad And Tobago Dollar 0.1095510000 0.1093120000 0.1094000000 0.1097660000 +Tunisian Dinar 0.3234230000 0.3229230000 0.3212750000 0.3239130000 +U.A.E. Dirham 0.2017230000 0.2009730000 0.2010400000 0.2017120000 +Peso Uruguayo 0.0260937000 0.0260969000 +Bolivar Fuerte 0.0740171000 0.0742645000 + +Currency units per SDR(3) + +Currency February 22, 2017 February 21, 2017 February 17, 2017 February 16, 2017 February 15, 2017 +Chinese Yuan 9.278680 9.294370 9.308820 9.293770 9.271450 +Euro 1.281050 1.272190 1.271520 1.278930 +Japanese Yen 153.215000 153.207000 153.697000 154.201000 154.336000 +U.K. Pound Sterling 1.084900 1.086790 1.091240 1.084060 1.086450 +U.S. Dollar 1.348960 1.349840 1.354880 1.354420 1.349910 +Algerian Dinar 148.982000 148.948000 148.827000 149.035000 +Australian Dollar 1.760360 1.757530 1.756030 1.759760 +Bahrain Dinar 0.507542 0.509435 0.509264 0.507568 +Botswana Pula 14.105000 14.098600 14.035500 14.061600 +Brazilian Real 4.172650 4.132930 4.167970 4.184470 +Brunei Dollar 1.917730 1.920410 1.922470 1.915930 +Canadian Dollar 1.774910 1.777060 1.766230 +Chilean Peso 866.882000 864.887000 866.739000 +Colombian Peso 3,918.340000 3,896.210000 3,895.370000 3,871.060000 +Czech Koruna 34.615400 34.376000 34.348200 +Danish Krone 9.522450 9.456620 9.452690 9.508230 +Hungarian Forint 393.898000 391.899000 392.174000 394.350000 +Icelandic Krona 149.509000 149.565000 149.867000 151.015000 +Indian Rupee 90.840500 90.674200 90.335100 +Indonesian Rupiah 18,047.400000 18,057.800000 18,053.100000 +Iranian Rial 43,730.900000 43,869.800000 43,726.400000 +Israeli New Sheqel 5.003880 5.034740 5.033040 5.058120 +Kazakhstani Tenge 428.251000 431.678000 432.631000 432.917000 +Korean Won 1,549.080000 1,543.070000 1,546.210000 1,544.980000 +Kuwaiti Dinar 0.412242 0.413712 0.413573 0.412398 +Libyan Dinar 1.932400 1.932400 1.932400 1.932400 +Malaysian Ringgit 6.019640 6.040070 6.036680 6.007120 +Mauritian Rupee 48.187900 48.129000 47.930800 +Mexican Peso 27.710800 27.437600 +Nepalese Rupee 144.636000 145.108000 145.059000 144.616000 +New Zealand Dollar 1.884470 1.878650 1.871790 1.886670 +Norwegian Krone 11.290900 11.277300 11.277100 11.321500 +Rial Omani 0.519014 0.520950 0.520776 0.519041 +Pakistani Rupee 141.526000 142.035000 141.986000 141.501000 +Nuevo Sol 4.415560 +Philippine Peso 67.737800 67.642500 67.352800 +Polish Zloty 5.526540 5.513410 5.494510 5.501160 +Qatar Riyal 4.913430 4.931770 4.930120 4.913690 +Russian Ruble 78.100600 78.087200 77.406600 76.637200 +Saudi Arabian Riyal 5.061910 5.080810 5.079080 5.062190 +Singapore Dollar 1.917730 1.920410 1.922470 1.915930 +South African Rand 17.785300 17.766400 17.563400 17.586800 +Sri Lanka Rupee 203.995000 204.245000 204.168000 203.494000 +Swedish Krona 12.121600 12.038900 12.060600 12.071500 +Swiss Franc 1.362940 1.353930 1.355640 1.362200 +Thai Baht 47.305300 47.412700 47.402100 47.271300 +Trinidad And Tobago Dollar 9.128170 9.148130 9.140770 9.110290 +Tunisian Dinar 3.091930 3.096710 3.112600 3.087250 +U.A.E. Dirham 4.957290 4.975790 4.974130 4.957560 +Peso Uruguayo 38.323400 38.318700 +Bolivar Fuerte 13.510400 13.465400 + + +(1) Exchange rates are published daily except on IMF holidays or whenever the IMF is closed for business. + +(2) The value of the U.S. dollar in terms of the SDR is the reciprocal of the sum of the dollar values, based on market exchange rates, of specified quantities of the SDR basket currencies. See SDR Valuation.The value in terms of the SDR of each of the other currencies shown above is derived from that currency's representative exchange rate against the U.S. dollar as reported by the issuing central bank and the SDR value of the U.S. dollar, except for the Iranian rial and the Libyan dinar, the values of which are officially expressed directly in terms of domestic currency units per SDR. All figures are rounded to six significant digits. See Representative Exchange Rates for Selected Currencies". + +(3) The value in terms of each national currency of the SDR is the reciprocal of the value in terms of the SDR of each national currency, rounded to six significant digits. \ No newline at end of file diff --git a/core-java/pom.xml b/core-java/pom.xml index b2c59989f1..225eb3fc29 100644 --- a/core-java/pom.xml +++ b/core-java/pom.xml @@ -165,6 +165,12 @@ commons-codec ${commons-codec.version} + + + org.javamoney + moneta + 1.1 + diff --git a/core-java/src/main/java/com/baeldung/money/JavaMoney.java b/core-java/src/main/java/com/baeldung/money/JavaMoney.java new file mode 100644 index 0000000000..f66480bea5 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/money/JavaMoney.java @@ -0,0 +1,147 @@ +package com.baeldung.money; + +import java.util.Locale; +import java.util.logging.Logger; + +import javax.money.CurrencyUnit; +import javax.money.Monetary; +import javax.money.MonetaryAmount; +import javax.money.UnknownCurrencyException; +import javax.money.convert.ConversionQueryBuilder; +import javax.money.convert.CurrencyConversion; +import javax.money.convert.MonetaryConversions; +import javax.money.format.AmountFormatQueryBuilder; +import javax.money.format.MonetaryAmountFormat; +import javax.money.format.MonetaryFormats; + +import org.javamoney.moneta.FastMoney; +import org.javamoney.moneta.Money; +import org.javamoney.moneta.format.CurrencyStyle; + +public class JavaMoney { + final static Logger LOGGER = Logger.getLogger(JavaMoney.class.getName()); + CurrencyUnit USD; + MonetaryAmount fstAmtUSD; + MonetaryAmount fstAmtEUR; + MonetaryAmount oneDolar; + MonetaryAmount moneyof; + MonetaryAmount fastmoneyof; + MonetaryAmount roundEUR; + MonetaryAmount calcAmtUSD; + MonetaryAmount[] monetaryAmounts; + MonetaryAmount sumAmtCHF; + MonetaryAmount calcMoneyFastMoney; + MonetaryAmount convertedAmountEURtoUSD; + MonetaryAmount convertedAmountEURtoUSD2; + MonetaryAmount convertedAmountUSDtoEUR; + MonetaryAmount convertedAmountUSDtoEUR2; + MonetaryAmount multiplyAmount; + MonetaryAmount divideAmount; + MonetaryAmount oneDivThree; + CurrencyConversion convEUR; + CurrencyConversion convUSD; + CurrencyConversion conversionUSD; + CurrencyConversion conversionEUR; + MonetaryAmount oneEuro; + MonetaryAmountFormat formatUSD; + MonetaryAmountFormat customFormat; + String usFormatted; + String customFormatted; + + public JavaMoney() { + USD = Monetary.getCurrency("USD"); + fstAmtUSD = Monetary.getDefaultAmountFactory().setCurrency(USD).setNumber(200.50).create(); + fstAmtEUR = Monetary.getDefaultAmountFactory().setCurrency("EUR").setNumber(1.30473908).create(); + oneDolar = Monetary.getDefaultAmountFactory().setCurrency("USD").setNumber(1).create(); + moneyof = Money.of(12, USD); + fastmoneyof = FastMoney.of(2, USD); + + LOGGER.info("First Amount in USD : " + fstAmtUSD); + LOGGER.info("First Amount in EUR : " + fstAmtEUR); + LOGGER.info("One Dolar : " + oneDolar); + LOGGER.info("MoneyOf : " + moneyof); + LOGGER.info("FastMoneyOf : " + fastmoneyof); + + try{ + @SuppressWarnings("unused") + CurrencyUnit AAA = Monetary.getCurrency("AAA"); + } catch (UnknownCurrencyException e) { + LOGGER.severe("Unknown Currency"); + } + + roundEUR = fstAmtEUR.with(Monetary.getDefaultRounding()); + + LOGGER.info("Rounded EUR : " + roundEUR); + + calcAmtUSD = Money.of(1, "USD").subtract(fstAmtUSD); + + LOGGER.info("Substracting amounts : " + calcAmtUSD); + + calcMoneyFastMoney = moneyof.subtract(fastmoneyof); + + LOGGER.info("Money & FastMoney operations : " + calcMoneyFastMoney); + + monetaryAmounts = new MonetaryAmount[] { Money.of(100, "CHF"), Money.of(10.20, "CHF"), Money.of(1.15, "CHF"), }; + sumAmtCHF = Money.of(0, "CHF"); + for (MonetaryAmount monetaryAmount : monetaryAmounts) { + sumAmtCHF = sumAmtCHF.add(monetaryAmount); + } + + LOGGER.info("Adding amounts : " + sumAmtCHF); + + multiplyAmount = oneDolar.multiply(0.25); + LOGGER.info("Multiply Amount : " + multiplyAmount); + + divideAmount = oneDolar.divide(0.25); + LOGGER.info("Divide Amount : " + divideAmount); + + try{ + oneDivThree = oneDolar.divide(3); + }catch (ArithmeticException e) { + LOGGER.severe("One divide by Three is an infinite number"); + } + + convEUR = MonetaryConversions.getConversion(ConversionQueryBuilder.of().setTermCurrency("EUR").build()); + convUSD = MonetaryConversions.getConversion(ConversionQueryBuilder.of().setTermCurrency(USD).build()); + + conversionUSD = MonetaryConversions.getConversion("USD"); + conversionEUR = MonetaryConversions.getConversion("EUR"); + + convertedAmountEURtoUSD = fstAmtEUR.with(conversionUSD); + convertedAmountEURtoUSD2 = fstAmtEUR.with(convUSD); + convertedAmountUSDtoEUR = oneDolar.with(conversionEUR); + convertedAmountUSDtoEUR2 = oneDolar.with(convEUR); + LOGGER.info("C1 - " + convertedAmountEURtoUSD); + LOGGER.info("C2 - " + convertedAmountEURtoUSD2); + LOGGER.info("One Euro -> " + convertedAmountUSDtoEUR); + LOGGER.info("One Euro2 -> " + convertedAmountUSDtoEUR2); + + oneEuro = Money.of(1, "EUR"); + + if (oneEuro.equals(FastMoney.of(1, "EUR"))) { + LOGGER.info("Money == FastMoney"); + } else { + LOGGER.info("Money != FastMoney"); + } + + if (oneDolar.equals(Money.of(1, "USD"))) { + LOGGER.info("Factory == Money"); + } else { + LOGGER.info("Factory != Money"); + } + + formatUSD = MonetaryFormats.getAmountFormat(Locale.US); + usFormatted = formatUSD.format(oneDolar); + LOGGER.info("One dolar standard formatted : " + usFormatted); + + customFormat = MonetaryFormats.getAmountFormat(AmountFormatQueryBuilder.of(Locale.US).set(CurrencyStyle.NAME).set("pattern", "00000.00 ¤").build()); + customFormatted = customFormat.format(oneDolar); + LOGGER.info("One dolar custom formatted : " + customFormatted); + } + + public static void main(String[] args) { + @SuppressWarnings("unused") + JavaMoney java9Money = new JavaMoney(); + } + +} diff --git a/core-java/src/test/java/com/baeldung/money/JavaMoneyTest.java b/core-java/src/test/java/com/baeldung/money/JavaMoneyTest.java new file mode 100644 index 0000000000..25fcd48f7a --- /dev/null +++ b/core-java/src/test/java/com/baeldung/money/JavaMoneyTest.java @@ -0,0 +1,58 @@ +package com.baeldung.money; + +import javax.money.convert.ConversionQueryBuilder; +import javax.money.convert.MonetaryConversions; + +import com.baeldung.money.JavaMoney; + +import junit.framework.TestCase; + +public class JavaMoneyTest + extends TestCase +{ + JavaMoney j9m; + public JavaMoneyTest( String testName ) + { + super( testName ); + j9m = new JavaMoney(); + } + + public void testAmounts() + { + assertEquals("USD", j9m.USD.toString()); + assertEquals("USD 1", j9m.oneDolar.toString()); + assertEquals("EUR 1", j9m.oneEuro.toString()); + assertEquals("USD 200.5", j9m.fstAmtUSD.toString()); + assertEquals("EUR 1.30473908", j9m.fstAmtEUR.toString()); + assertEquals("USD 12", j9m.moneyof.toString()); + assertEquals("USD 2.00000", j9m.fastmoneyof.toString()); + + } + + public void testArithmetic(){ + assertEquals("USD -199.5", j9m.calcAmtUSD.toString()); + assertEquals("CHF 111.35", j9m.sumAmtCHF.toString()); + assertEquals("USD 10", j9m.calcMoneyFastMoney.toString()); + assertEquals("USD 0.25", j9m.multiplyAmount.toString()); + assertEquals("USD 4", j9m.divideAmount.toString()); + } + + public void testRounding(){ + assertEquals("EUR 1.3", j9m.roundEUR.toString()); + } + + public void testFormatting(){ + assertEquals("USD1.00", j9m.usFormatted); + assertEquals("00001.00 US Dollar", j9m.customFormatted); + } + + public void testConversion(){ + + assertNotNull(MonetaryConversions.getConversion(ConversionQueryBuilder.of().setTermCurrency("EUR").build())); + assertNotNull(MonetaryConversions.getConversion(ConversionQueryBuilder.of().setTermCurrency("USD").build())); + assertNotNull(j9m.convertedAmountEURtoUSD); + assertNotNull(j9m.convertedAmountEURtoUSD2); + assertNotNull(j9m.convertedAmountUSDtoEUR); + assertNotNull(j9m.convertedAmountUSDtoEUR2); + } +} From d267d393210fc2c06272dd491a79f43633e9e70c Mon Sep 17 00:00:00 2001 From: Sunil Gulabani Date: Thu, 23 Feb 2017 11:19:30 +0530 Subject: [PATCH 036/112] 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 037/112] 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 038/112] 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 039/112] 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 040/112] 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 041/112] 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 042/112] 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 043/112] 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 044/112] 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 045/112] 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 046/112] 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 047/112] 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 048/112] 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 049/112] 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 050/112] 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 051/112] 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 052/112] 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 053/112] 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 054/112] 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 055/112] 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 056/112] 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 057/112] 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 058/112] 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 059/112] 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 060/112] 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 6564693ea592bc3faff467a1158765499d20a6ac Mon Sep 17 00:00:00 2001 From: slavisa-baeldung Date: Mon, 27 Feb 2017 06:47:30 +0100 Subject: [PATCH 061/112] BAEL-41 - Renaming tests --- .../log4j2/tests/CustomLoggingTest.java | 34 ++++++++++++------- log4j2/src/test/resources/log4j2.xml | 2 +- 2 files changed, 22 insertions(+), 14 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 1562b67068..333b4f5e78 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 @@ -31,78 +31,85 @@ public class CustomLoggingTest { } @Test - public void givenLoggerWithDefaultConfig_shouldLogToConsole() throws Exception { + public void givenLoggerWithDefaultConfig_whenLogToConsole_thanOK() 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 { + public void givenLoggerWithConsoleConfig_whenLogToConsoleInColors_thanOK() throws Exception { Logger logger = LogManager.getLogger("CONSOLE_PATTERN_APPENDER_MARKER"); + Exception e = new RuntimeException("This is only a test!"); + 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 { + public void givenLoggerWithConsoleConfig_whenFilterByMarker_thanOK() 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.error(appError, "This marker message at ERROR level should be hidden."); logger.trace(connectionTrace, "This is a marker message at TRACE level."); } @Test - public void givenLoggerWithConsoleConfig_shouldFilterByThreadContext() throws Exception { + public void givenLoggerWithConsoleConfig_whenFilterByThreadContext_thanOK() 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 { + public void givenLoggerWithAsyncConfig_whenLogToJsonFile_thanOK() 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 { + public void givenLoggerWithFailoverConfig_whenLog_thanOK() throws Exception { Logger logger = LogManager.getLogger("FAIL_OVER_SYSLOG_APPENDER"); + Exception e = new RuntimeException("This is only a test!"); + 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 { + public void givenLoggerWithJdbcConfig_whenLogToDataSource_thanOK() 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"); + .executeQuery("SELECT COUNT(*) AS ROW_COUNT FROM logs"); + int logCount = 0; if (resultSet.next()) { logCount = resultSet.getInt("ROW_COUNT"); @@ -111,8 +118,9 @@ public class CustomLoggingTest { } @Test - public void givenLoggerWithRollingFileConfig_shouldLogToXMLFile() throws Exception { + public void givenLoggerWithRollingFileConfig_whenLogToXMLFile_thanOK() 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/resources/log4j2.xml b/log4j2/src/test/resources/log4j2.xml index 83c1184f1f..b0640b16eb 100644 --- a/log4j2/src/test/resources/log4j2.xml +++ b/log4j2/src/test/resources/log4j2.xml @@ -19,7 +19,7 @@ - + From e6c1562f83427c857cb0e4136330de46edb41bd7 Mon Sep 17 00:00:00 2001 From: slavisa-baeldung Date: Mon, 27 Feb 2017 07:10:30 +0100 Subject: [PATCH 062/112] BAEL-41 - Commenting out failover configuration --- log4j2/src/test/resources/log4j2.xml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/log4j2/src/test/resources/log4j2.xml b/log4j2/src/test/resources/log4j2.xml index b0640b16eb..21fd1da446 100644 --- a/log4j2/src/test/resources/log4j2.xml +++ b/log4j2/src/test/resources/log4j2.xml @@ -19,12 +19,14 @@ + @@ -53,9 +55,11 @@ + From ef610affc9c9d20ef3cb02f6415ebe02b8297ea2 Mon Sep 17 00:00:00 2001 From: pedja4 Date: Mon, 27 Feb 2017 10:18:31 +0100 Subject: [PATCH 063/112] 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 064/112] 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 d16ab250b16ad77e23cd9e85af36cb6e571d0e83 Mon Sep 17 00:00:00 2001 From: pivovarit Date: Mon, 27 Feb 2017 18:31:59 +0100 Subject: [PATCH 065/112] Refactor SpringDataRelationshipsTest --- .../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 a23eaf33235ee70476b8ea96c4b57028d2723615 Mon Sep 17 00:00:00 2001 From: Tomasz Lelek Date: Mon, 27 Feb 2017 19:06:34 +0100 Subject: [PATCH 066/112] BAEL-311 add jasyp module --- jasypt/pom.xml | 34 +++++++++++++++++++ .../java/org/baeldung/jasypt/JasyptTest.java | 27 +++++++++++++++ pom.xml | 1 + 3 files changed, 62 insertions(+) create mode 100644 jasypt/pom.xml create mode 100644 jasypt/src/test/java/org/baeldung/jasypt/JasyptTest.java diff --git a/jasypt/pom.xml b/jasypt/pom.xml new file mode 100644 index 0000000000..7e0c51f0b9 --- /dev/null +++ b/jasypt/pom.xml @@ -0,0 +1,34 @@ + + + + parent-modules + com.baeldung + 1.0.0-SNAPSHOT + + 4.0.0 + + jasypt + + + + org.jasypt + jasypt + ${jasypt.version} + + + junit + junit + ${junit.version} + test + + + + + 1.9.2 + 4.12 + + + + \ No newline at end of file diff --git a/jasypt/src/test/java/org/baeldung/jasypt/JasyptTest.java b/jasypt/src/test/java/org/baeldung/jasypt/JasyptTest.java new file mode 100644 index 0000000000..c81d605e50 --- /dev/null +++ b/jasypt/src/test/java/org/baeldung/jasypt/JasyptTest.java @@ -0,0 +1,27 @@ +package org.baeldung.jasypt; + + +import org.jasypt.util.text.BasicTextEncryptor; +import org.junit.Test; + +import static junit.framework.Assert.assertNotSame; +import static junit.framework.TestCase.assertEquals; + +public class JasyptTest { + + @Test + public void givenTextPassword_whenDecrypt_shouldCompareToEncrypted() { + //given + BasicTextEncryptor textEncryptor = new BasicTextEncryptor(); + String password = "secret-pass"; + textEncryptor.setPasswordCharArray(password.toCharArray()); + + //when + String myEncryptedText = textEncryptor.encrypt("secret-pass"); + assertNotSame("secret-pass", myEncryptedText); //myEncryptedText can be save in db + + //then + String plainText = textEncryptor.decrypt(myEncryptedText); + assertEquals(plainText, "secret-pass"); + } +} diff --git a/pom.xml b/pom.xml index 014e4016c5..d42c184993 100644 --- a/pom.xml +++ b/pom.xml @@ -61,6 +61,7 @@ javaslang javax-servlets javaxval + jasypt jaxb jee7 jjwt From 29fb2b339bdade9174c4b6bedd3427bb3369a888 Mon Sep 17 00:00:00 2001 From: Tomasz Lelek Date: Mon, 27 Feb 2017 19:29:20 +0100 Subject: [PATCH 067/112] BAEL-311 two tests that works when installed legal_policy.jar --- .../java/org/baeldung/jasypt/JasyptTest.java | 47 +++++++++++++++++-- 1 file changed, 44 insertions(+), 3 deletions(-) diff --git a/jasypt/src/test/java/org/baeldung/jasypt/JasyptTest.java b/jasypt/src/test/java/org/baeldung/jasypt/JasyptTest.java index c81d605e50..7d1265c887 100644 --- a/jasypt/src/test/java/org/baeldung/jasypt/JasyptTest.java +++ b/jasypt/src/test/java/org/baeldung/jasypt/JasyptTest.java @@ -1,7 +1,10 @@ package org.baeldung.jasypt; +import org.jasypt.encryption.pbe.PooledPBEStringEncryptor; +import org.jasypt.encryption.pbe.StandardPBEStringEncryptor; import org.jasypt.util.text.BasicTextEncryptor; +import org.junit.Ignore; import org.junit.Test; import static junit.framework.Assert.assertNotSame; @@ -17,11 +20,49 @@ public class JasyptTest { textEncryptor.setPasswordCharArray(password.toCharArray()); //when - String myEncryptedText = textEncryptor.encrypt("secret-pass"); - assertNotSame("secret-pass", myEncryptedText); //myEncryptedText can be save in db + String myEncryptedText = textEncryptor.encrypt(password); + assertNotSame(password, myEncryptedText); //myEncryptedText can be save in db //then String plainText = textEncryptor.decrypt(myEncryptedText); - assertEquals(plainText, "secret-pass"); + assertEquals(plainText, password); + } + + + @Test + @Ignore + public void givenTextPassword_whenDecrypt_shouldCompareToEncryptedWithCustomAlgorithm() { + //given + StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor(); + String password = "secret-pass"; + encryptor.setPassword("secret-pass"); + encryptor.setAlgorithm("PBEWithMD5AndTripleDES"); + + //when + String encryptedText = encryptor.encrypt("secret-pass"); + assertNotSame(password, encryptedText); + + //then + String plainText = encryptor.decrypt(encryptedText); + assertEquals(plainText, password); + } + + @Test + @Ignore + public void givenTextPassword_whenDecryptOnHighPerformance_shouldDecrypt(){ + //given + String password = "secret-pass"; + PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor(); + encryptor.setPoolSize(4); + encryptor.setPassword(password); + encryptor.setAlgorithm("PBEWithMD5AndTripleDES"); + + //when + String encryptedText = encryptor.encrypt(password); + assertNotSame(password, encryptedText); + + //then + String plainText = encryptor.decrypt(encryptedText); + assertEquals(plainText, password); } } From 5930f9dcbb97005230fdc8181c2663e321d0a73f Mon Sep 17 00:00:00 2001 From: Grzegorz Piwowarek Date: Mon, 27 Feb 2017 19:31:02 +0100 Subject: [PATCH 068/112] 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 64efdec3ef1669e3391a54e0520ccb92e1eeea2e Mon Sep 17 00:00:00 2001 From: Tomasz Lelek Date: Mon, 27 Feb 2017 19:31:19 +0100 Subject: [PATCH 069/112] BAEL-311 msg in ignore --- jasypt/src/test/java/org/baeldung/jasypt/JasyptTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/jasypt/src/test/java/org/baeldung/jasypt/JasyptTest.java b/jasypt/src/test/java/org/baeldung/jasypt/JasyptTest.java index 7d1265c887..d05b18171d 100644 --- a/jasypt/src/test/java/org/baeldung/jasypt/JasyptTest.java +++ b/jasypt/src/test/java/org/baeldung/jasypt/JasyptTest.java @@ -30,7 +30,7 @@ public class JasyptTest { @Test - @Ignore + @Ignore("should have installed local_policy.jar") public void givenTextPassword_whenDecrypt_shouldCompareToEncryptedWithCustomAlgorithm() { //given StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor(); @@ -48,7 +48,7 @@ public class JasyptTest { } @Test - @Ignore + @Ignore("should have installed local_policy.jar") public void givenTextPassword_whenDecryptOnHighPerformance_shouldDecrypt(){ //given String password = "secret-pass"; From 29882a1f06e02faf90ade160996829ad953a1cf9 Mon Sep 17 00:00:00 2001 From: maibin Date: Mon, 27 Feb 2017 19:59:20 +0100 Subject: [PATCH 070/112] 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 { From bb1b9c4ca43f5f3f2bb0acb2d7d28336de97f061 Mon Sep 17 00:00:00 2001 From: root Date: Mon, 27 Feb 2017 15:13:19 -0400 Subject: [PATCH 071/112] Java Money & Currency --- .../.resourceCache/ECBCurrentRateProvider.dat | 60 +++--- .../ECBHistoric90RateProvider.dat | 10 +- .../IMFHistoricRateProvider.dat | 204 +++++++++--------- core-java/.resourceCache/IMFRateProvider.dat | 204 +++++++++--------- .../java/com/baeldung/money/JavaMoney.java | 8 +- .../com/baeldung/money/JavaMoneyTest.java | 37 ++-- 6 files changed, 269 insertions(+), 254 deletions(-) diff --git a/core-java/.resourceCache/ECBCurrentRateProvider.dat b/core-java/.resourceCache/ECBCurrentRateProvider.dat index 37d1d8c8ab..45cf516ac8 100644 --- a/core-java/.resourceCache/ECBCurrentRateProvider.dat +++ b/core-java/.resourceCache/ECBCurrentRateProvider.dat @@ -5,38 +5,38 @@ European Central Bank - - - + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/core-java/.resourceCache/ECBHistoric90RateProvider.dat b/core-java/.resourceCache/ECBHistoric90RateProvider.dat index 16edc6a30a..f51c2eacfa 100644 --- a/core-java/.resourceCache/ECBHistoric90RateProvider.dat +++ b/core-java/.resourceCache/ECBHistoric90RateProvider.dat @@ -1,4 +1,7 @@ -Reference ratesEuropean Central Bank +Reference ratesEuropean Central Bank + + + @@ -57,7 +60,4 @@ - - - - \ No newline at end of file + \ No newline at end of file diff --git a/core-java/.resourceCache/IMFHistoricRateProvider.dat b/core-java/.resourceCache/IMFHistoricRateProvider.dat index 1db358b58b..4c98b3b04e 100644 --- a/core-java/.resourceCache/IMFHistoricRateProvider.dat +++ b/core-java/.resourceCache/IMFHistoricRateProvider.dat @@ -2,113 +2,113 @@ SDRs per Currency unit and Currency units per SDR (1) last five days SDRs per Currency unit (2) -Currency February 22, 2017 February 21, 2017 February 17, 2017 February 16, 2017 February 15, 2017 -Chinese Yuan 0.1077740000 0.1075920000 0.1074250000 0.1075990000 0.1078580000 -Euro 0.7806080000 0.7860480000 0.7864600000 0.7819020000 -Japanese Yen 0.0065267800 0.0065271000 0.0065062900 0.0064850300 0.0064793800 -U.K. Pound Sterling 0.9217470000 0.9201430000 0.9163910000 0.9224580000 0.9204290000 -U.S. Dollar 0.7413120000 0.7408260000 0.7380730000 0.7383210000 0.7407880000 -Algerian Dinar 0.0067122300 0.0067137500 0.0067192300 0.0067098500 -Australian Dollar 0.5680650000 0.5689800000 0.5694670000 0.5682580000 -Bahrain Dinar 1.9702800000 1.9629600000 1.9636200000 1.9701800000 -Botswana Pula 0.0708970000 0.0709288000 0.0712480000 0.0711156000 -Brazilian Real 0.2396560000 0.2419590000 0.2399250000 0.2389790000 -Brunei Dollar 0.5214510000 0.5207230000 0.5201640000 0.5219390000 -Canadian Dollar 0.5634090000 0.5627270000 0.5661790000 -Chilean Peso 0.0011535600 0.0011562200 0.0011537500 -Colombian Peso 0.0002552100 0.0002566600 0.0002567150 0.0002583270 -Czech Koruna 0.0288889000 0.0290901000 0.0291136000 -Danish Krone 0.1050150000 0.1057460000 0.1057900000 0.1051720000 -Hungarian Forint 0.0025387300 0.0025516800 0.0025498900 0.0025358200 -Icelandic Krona 0.0066885700 0.0066860500 0.0066725800 0.0066218600 -Indian Rupee 0.0110083000 0.0110285000 0.0110699000 -Indonesian Rupiah 0.0000554096 0.0000553776 0.0000553921 -Iranian Rial 0.0000228671 0.0000227947 0.0000228695 -Israeli New Sheqel 0.1998450000 0.1986200000 0.1986870000 0.1977020000 -Kazakhstani Tenge 0.0023350800 0.0023165400 0.0023114400 0.0023099100 -Korean Won 0.0006455440 0.0006480580 0.0006467420 0.0006472590 -Kuwaiti Dinar 2.4257600000 2.4171400000 2.4179500000 2.4248400000 +Currency February 27, 2017 February 24, 2017 February 23, 2017 February 22, 2017 February 21, 2017 +Chinese Yuan 0.1075530000 0.1074200000 0.1076250000 0.1077740000 0.1075920000 +Euro 0.7826360000 0.7829640000 0.7824120000 0.7793410000 0.7806080000 +Japanese Yen 0.0065915500 0.0065398200 0.0065279600 0.0065267800 0.0065271000 +U.K. Pound Sterling 0.9178430000 0.9263250000 0.9233090000 0.9217470000 0.9201430000 +U.S. Dollar 0.7392420000 0.7380190000 0.7400090000 0.7413120000 0.7408260000 +Algerian Dinar 0.0067056800 0.0067170500 0.0067108200 0.0067122300 +Australian Dollar 0.5695290000 0.5688450000 0.5701430000 0.5680650000 +Bahrain Dinar 1.9628200000 1.9681100000 1.9715700000 1.9702800000 +Botswana Pula 0.0712926000 0.0714849000 0.0710918000 0.0708970000 +Brazilian Real 0.2409310000 0.2401220000 0.2394110000 0.2396560000 +Brunei Dollar 0.5253930000 0.5230480000 0.5224920000 0.5214510000 +Canadian Dollar 0.5632010000 0.5645050000 0.5621540000 0.5634090000 +Chilean Peso 0.0011525100 0.0011535400 0.0011517700 0.0011535600 +Colombian Peso 0.0002570000 0.0002557440 0.0002553890 0.0002552100 +Czech Koruna 0.0289783000 0.0289564000 0.0288448000 0.0288889000 +Danish Krone 0.1053170000 0.1052550000 0.1048460000 0.1050150000 +Hungarian Forint 0.0025337100 0.0025343600 0.0025357900 0.0025387300 +Icelandic Krona 0.0068082900 0.0067593100 0.0067227000 0.0066885700 +Indian Rupee 0.0110722000 0.0110702000 +Indonesian Rupiah 0.0000553404 0.0000553899 0.0000555040 0.0000554096 +Iranian Rial 0.0000228786 0.0000228671 +Israeli New Sheqel 0.1995720000 0.1995710000 0.1998150000 0.1998450000 +Kazakhstani Tenge 0.0023693200 0.0023668900 0.0023601900 0.0023350800 +Korean Won 0.0006480670 0.0006486190 0.0006464180 0.0006455440 +Kuwaiti Dinar 2.4165700000 2.4230800000 2.4273500000 2.4257600000 Libyan Dinar 0.5174910000 0.5174910000 0.5174910000 0.5174910000 -Malaysian Ringgit 0.1661230000 0.1655610000 0.1656540000 0.1664690000 -Mauritian Rupee 0.0207521000 0.0207775000 0.0208634000 -Mexican Peso 0.0360870000 0.0364464000 -Nepalese Rupee 0.0069139200 0.0068914400 0.0068937500 0.0069148500 -New Zealand Dollar 0.5306540000 0.5322980000 0.5342490000 0.5300340000 -Norwegian Krone 0.0885668000 0.0886734000 0.0886755000 0.0883278000 -Rial Omani 1.9267300000 1.9195700000 1.9202100000 1.9266300000 -Pakistani Rupee 0.0070658600 0.0070405000 0.0070429300 0.0070671000 -Nuevo Sol 0.2264720000 -Philippine Peso 0.0147628000 0.0147836000 0.0148472000 -Polish Zloty 0.1809450000 0.1813760000 0.1820000000 0.1817800000 -Qatar Riyal 0.2035240000 0.2027670000 0.2028350000 0.2035130000 -Russian Ruble 0.0128040000 0.0128062000 0.0129188000 0.0130485000 -Saudi Arabian Riyal 0.1975540000 0.1968190000 0.1968860000 0.1975430000 -Singapore Dollar 0.5214510000 0.5207230000 0.5201640000 0.5219390000 -South African Rand 0.0562263000 0.0562860000 0.0569367000 0.0568608000 -Sri Lanka Rupee 0.0049020900 0.0048960700 0.0048979200 0.0049141600 -Swedish Krona 0.0824973000 0.0830639000 0.0829146000 0.0828400000 -Swiss Franc 0.7337090000 0.7385900000 0.7376570000 0.7341080000 -Thai Baht 0.0211393000 0.0210914000 0.0210961000 0.0211545000 -Trinidad And Tobago Dollar 0.1095510000 0.1093120000 0.1094000000 0.1097660000 -Tunisian Dinar 0.3234230000 0.3229230000 0.3212750000 0.3239130000 -U.A.E. Dirham 0.2017230000 0.2009730000 0.2010400000 0.2017120000 -Peso Uruguayo 0.0260937000 0.0260969000 -Bolivar Fuerte 0.0740171000 0.0742645000 +Malaysian Ringgit 0.1659960000 0.1662940000 0.1663250000 0.1661230000 +Mauritian Rupee 0.0208418000 +Mexican Peso 0.0372107000 0.0375618000 0.0372281000 0.0362282000 +Nepalese Rupee 0.0069012400 0.0069030700 0.0069229700 0.0069139200 +New Zealand Dollar 0.5331450000 0.5322880000 0.5307790000 0.5306540000 +Norwegian Krone 0.0886062000 0.0888398000 0.0884082000 0.0885668000 +Rial Omani 1.9194300000 1.9246000000 1.9279900000 1.9267300000 +Pakistani Rupee 0.0070384300 0.0070575400 0.0070700400 0.0070658600 +Nuevo Sol 0.2283980000 0.2287290000 0.2281570000 +Philippine Peso 0.0146922000 0.0147295000 0.0147363000 0.0147628000 +Polish Zloty 0.1814160000 0.1811970000 0.1812540000 0.1809450000 +Qatar Riyal 0.2027520000 0.2032990000 0.2036570000 0.2035240000 +Russian Ruble 0.0128977000 0.0128040000 +Saudi Arabian Riyal 0.1968050000 0.1973360000 0.1976830000 0.1975540000 +Singapore Dollar 0.5253930000 0.5230480000 0.5224920000 0.5214510000 +South African Rand 0.0569347000 0.0570889000 0.0566127000 0.0562263000 +Sri Lanka Rupee 0.0048931100 0.0049014800 0.0049020900 +Swedish Krona 0.0822544000 0.0824642000 0.0823543000 0.0824973000 +Swiss Franc 0.7345670000 0.7331180000 0.7312210000 0.7337090000 +Thai Baht 0.0211098000 0.0211467000 0.0211707000 0.0211393000 +Trinidad And Tobago Dollar 0.1092030000 0.1091300000 0.1093820000 0.1095510000 +Tunisian Dinar 0.3206630000 0.3204540000 0.3225790000 0.3234230000 +U.A.E. Dirham 0.2009580000 0.2015000000 0.2018550000 0.2017230000 +Peso Uruguayo 0.0259816000 0.0261108000 0.0261517000 +Bolivar Fuerte 0.0739869000 0.0741864000 Currency units per SDR(3) -Currency February 22, 2017 February 21, 2017 February 17, 2017 February 16, 2017 February 15, 2017 -Chinese Yuan 9.278680 9.294370 9.308820 9.293770 9.271450 -Euro 1.281050 1.272190 1.271520 1.278930 -Japanese Yen 153.215000 153.207000 153.697000 154.201000 154.336000 -U.K. Pound Sterling 1.084900 1.086790 1.091240 1.084060 1.086450 -U.S. Dollar 1.348960 1.349840 1.354880 1.354420 1.349910 -Algerian Dinar 148.982000 148.948000 148.827000 149.035000 -Australian Dollar 1.760360 1.757530 1.756030 1.759760 -Bahrain Dinar 0.507542 0.509435 0.509264 0.507568 -Botswana Pula 14.105000 14.098600 14.035500 14.061600 -Brazilian Real 4.172650 4.132930 4.167970 4.184470 -Brunei Dollar 1.917730 1.920410 1.922470 1.915930 -Canadian Dollar 1.774910 1.777060 1.766230 -Chilean Peso 866.882000 864.887000 866.739000 -Colombian Peso 3,918.340000 3,896.210000 3,895.370000 3,871.060000 -Czech Koruna 34.615400 34.376000 34.348200 -Danish Krone 9.522450 9.456620 9.452690 9.508230 -Hungarian Forint 393.898000 391.899000 392.174000 394.350000 -Icelandic Krona 149.509000 149.565000 149.867000 151.015000 -Indian Rupee 90.840500 90.674200 90.335100 -Indonesian Rupiah 18,047.400000 18,057.800000 18,053.100000 -Iranian Rial 43,730.900000 43,869.800000 43,726.400000 -Israeli New Sheqel 5.003880 5.034740 5.033040 5.058120 -Kazakhstani Tenge 428.251000 431.678000 432.631000 432.917000 -Korean Won 1,549.080000 1,543.070000 1,546.210000 1,544.980000 -Kuwaiti Dinar 0.412242 0.413712 0.413573 0.412398 +Currency February 27, 2017 February 24, 2017 February 23, 2017 February 22, 2017 February 21, 2017 +Chinese Yuan 9.297740 9.309250 9.291520 9.278680 9.294370 +Euro 1.277730 1.277200 1.278100 1.283140 1.281050 +Japanese Yen 151.709000 152.909000 153.187000 153.215000 153.207000 +U.K. Pound Sterling 1.089510 1.079530 1.083060 1.084900 1.086790 +U.S. Dollar 1.352740 1.354980 1.351330 1.348960 1.349840 +Algerian Dinar 149.127000 148.875000 149.013000 148.982000 +Australian Dollar 1.755840 1.757950 1.753950 1.760360 +Bahrain Dinar 0.509471 0.508102 0.507210 0.507542 +Botswana Pula 14.026700 13.989000 14.066300 14.105000 +Brazilian Real 4.150570 4.164550 4.176920 4.172650 +Brunei Dollar 1.903340 1.911870 1.913900 1.917730 +Canadian Dollar 1.775570 1.771460 1.778870 1.774910 +Chilean Peso 867.671000 866.897000 868.229000 866.882000 +Colombian Peso 3,891.050000 3,910.160000 3,915.600000 3,918.340000 +Czech Koruna 34.508600 34.534700 34.668300 34.615400 +Danish Krone 9.495140 9.500740 9.537800 9.522450 +Hungarian Forint 394.678000 394.577000 394.354000 393.898000 +Icelandic Krona 146.880000 147.944000 148.750000 149.509000 +Indian Rupee 90.316300 90.332600 +Indonesian Rupiah 18,070.000000 18,053.800000 18,016.700000 18,047.400000 +Iranian Rial 43,709.000000 43,730.900000 +Israeli New Sheqel 5.010720 5.010750 5.004630 5.003880 +Kazakhstani Tenge 422.062000 422.495000 423.695000 428.251000 +Korean Won 1,543.050000 1,541.740000 1,546.990000 1,549.080000 +Kuwaiti Dinar 0.413810 0.412698 0.411972 0.412242 Libyan Dinar 1.932400 1.932400 1.932400 1.932400 -Malaysian Ringgit 6.019640 6.040070 6.036680 6.007120 -Mauritian Rupee 48.187900 48.129000 47.930800 -Mexican Peso 27.710800 27.437600 -Nepalese Rupee 144.636000 145.108000 145.059000 144.616000 -New Zealand Dollar 1.884470 1.878650 1.871790 1.886670 -Norwegian Krone 11.290900 11.277300 11.277100 11.321500 -Rial Omani 0.519014 0.520950 0.520776 0.519041 -Pakistani Rupee 141.526000 142.035000 141.986000 141.501000 -Nuevo Sol 4.415560 -Philippine Peso 67.737800 67.642500 67.352800 -Polish Zloty 5.526540 5.513410 5.494510 5.501160 -Qatar Riyal 4.913430 4.931770 4.930120 4.913690 -Russian Ruble 78.100600 78.087200 77.406600 76.637200 -Saudi Arabian Riyal 5.061910 5.080810 5.079080 5.062190 -Singapore Dollar 1.917730 1.920410 1.922470 1.915930 -South African Rand 17.785300 17.766400 17.563400 17.586800 -Sri Lanka Rupee 203.995000 204.245000 204.168000 203.494000 -Swedish Krona 12.121600 12.038900 12.060600 12.071500 -Swiss Franc 1.362940 1.353930 1.355640 1.362200 -Thai Baht 47.305300 47.412700 47.402100 47.271300 -Trinidad And Tobago Dollar 9.128170 9.148130 9.140770 9.110290 -Tunisian Dinar 3.091930 3.096710 3.112600 3.087250 -U.A.E. Dirham 4.957290 4.975790 4.974130 4.957560 -Peso Uruguayo 38.323400 38.318700 -Bolivar Fuerte 13.510400 13.465400 +Malaysian Ringgit 6.024240 6.013450 6.012330 6.019640 +Mauritian Rupee 47.980500 +Mexican Peso 26.874000 26.622800 26.861400 27.602800 +Nepalese Rupee 144.901000 144.863000 144.447000 144.636000 +New Zealand Dollar 1.875660 1.878680 1.884020 1.884470 +Norwegian Krone 11.285900 11.256200 11.311200 11.290900 +Rial Omani 0.520988 0.519588 0.518675 0.519014 +Pakistani Rupee 142.077000 141.692000 141.442000 141.526000 +Nuevo Sol 4.378320 4.371990 4.382950 +Philippine Peso 68.063300 67.891000 67.859600 67.737800 +Polish Zloty 5.512190 5.518860 5.517120 5.526540 +Qatar Riyal 4.932130 4.918860 4.910220 4.913430 +Russian Ruble 77.533200 78.100600 +Saudi Arabian Riyal 5.081170 5.067500 5.058600 5.061910 +Singapore Dollar 1.903340 1.911870 1.913900 1.917730 +South African Rand 17.564000 17.516500 17.663900 17.785300 +Sri Lanka Rupee 204.369000 204.020000 203.995000 +Swedish Krona 12.157400 12.126500 12.142700 12.121600 +Swiss Franc 1.361350 1.364040 1.367580 1.362940 +Thai Baht 47.371400 47.288700 47.235100 47.305300 +Trinidad And Tobago Dollar 9.157260 9.163380 9.142270 9.128170 +Tunisian Dinar 3.118540 3.120570 3.100020 3.091930 +U.A.E. Dirham 4.976160 4.962780 4.954050 4.957290 +Peso Uruguayo 38.488800 38.298300 38.238400 +Bolivar Fuerte 13.515900 13.479600 (1) Exchange rates are published daily except on IMF holidays or whenever the IMF is closed for business. diff --git a/core-java/.resourceCache/IMFRateProvider.dat b/core-java/.resourceCache/IMFRateProvider.dat index 1db358b58b..4c98b3b04e 100644 --- a/core-java/.resourceCache/IMFRateProvider.dat +++ b/core-java/.resourceCache/IMFRateProvider.dat @@ -2,113 +2,113 @@ SDRs per Currency unit and Currency units per SDR (1) last five days SDRs per Currency unit (2) -Currency February 22, 2017 February 21, 2017 February 17, 2017 February 16, 2017 February 15, 2017 -Chinese Yuan 0.1077740000 0.1075920000 0.1074250000 0.1075990000 0.1078580000 -Euro 0.7806080000 0.7860480000 0.7864600000 0.7819020000 -Japanese Yen 0.0065267800 0.0065271000 0.0065062900 0.0064850300 0.0064793800 -U.K. Pound Sterling 0.9217470000 0.9201430000 0.9163910000 0.9224580000 0.9204290000 -U.S. Dollar 0.7413120000 0.7408260000 0.7380730000 0.7383210000 0.7407880000 -Algerian Dinar 0.0067122300 0.0067137500 0.0067192300 0.0067098500 -Australian Dollar 0.5680650000 0.5689800000 0.5694670000 0.5682580000 -Bahrain Dinar 1.9702800000 1.9629600000 1.9636200000 1.9701800000 -Botswana Pula 0.0708970000 0.0709288000 0.0712480000 0.0711156000 -Brazilian Real 0.2396560000 0.2419590000 0.2399250000 0.2389790000 -Brunei Dollar 0.5214510000 0.5207230000 0.5201640000 0.5219390000 -Canadian Dollar 0.5634090000 0.5627270000 0.5661790000 -Chilean Peso 0.0011535600 0.0011562200 0.0011537500 -Colombian Peso 0.0002552100 0.0002566600 0.0002567150 0.0002583270 -Czech Koruna 0.0288889000 0.0290901000 0.0291136000 -Danish Krone 0.1050150000 0.1057460000 0.1057900000 0.1051720000 -Hungarian Forint 0.0025387300 0.0025516800 0.0025498900 0.0025358200 -Icelandic Krona 0.0066885700 0.0066860500 0.0066725800 0.0066218600 -Indian Rupee 0.0110083000 0.0110285000 0.0110699000 -Indonesian Rupiah 0.0000554096 0.0000553776 0.0000553921 -Iranian Rial 0.0000228671 0.0000227947 0.0000228695 -Israeli New Sheqel 0.1998450000 0.1986200000 0.1986870000 0.1977020000 -Kazakhstani Tenge 0.0023350800 0.0023165400 0.0023114400 0.0023099100 -Korean Won 0.0006455440 0.0006480580 0.0006467420 0.0006472590 -Kuwaiti Dinar 2.4257600000 2.4171400000 2.4179500000 2.4248400000 +Currency February 27, 2017 February 24, 2017 February 23, 2017 February 22, 2017 February 21, 2017 +Chinese Yuan 0.1075530000 0.1074200000 0.1076250000 0.1077740000 0.1075920000 +Euro 0.7826360000 0.7829640000 0.7824120000 0.7793410000 0.7806080000 +Japanese Yen 0.0065915500 0.0065398200 0.0065279600 0.0065267800 0.0065271000 +U.K. Pound Sterling 0.9178430000 0.9263250000 0.9233090000 0.9217470000 0.9201430000 +U.S. Dollar 0.7392420000 0.7380190000 0.7400090000 0.7413120000 0.7408260000 +Algerian Dinar 0.0067056800 0.0067170500 0.0067108200 0.0067122300 +Australian Dollar 0.5695290000 0.5688450000 0.5701430000 0.5680650000 +Bahrain Dinar 1.9628200000 1.9681100000 1.9715700000 1.9702800000 +Botswana Pula 0.0712926000 0.0714849000 0.0710918000 0.0708970000 +Brazilian Real 0.2409310000 0.2401220000 0.2394110000 0.2396560000 +Brunei Dollar 0.5253930000 0.5230480000 0.5224920000 0.5214510000 +Canadian Dollar 0.5632010000 0.5645050000 0.5621540000 0.5634090000 +Chilean Peso 0.0011525100 0.0011535400 0.0011517700 0.0011535600 +Colombian Peso 0.0002570000 0.0002557440 0.0002553890 0.0002552100 +Czech Koruna 0.0289783000 0.0289564000 0.0288448000 0.0288889000 +Danish Krone 0.1053170000 0.1052550000 0.1048460000 0.1050150000 +Hungarian Forint 0.0025337100 0.0025343600 0.0025357900 0.0025387300 +Icelandic Krona 0.0068082900 0.0067593100 0.0067227000 0.0066885700 +Indian Rupee 0.0110722000 0.0110702000 +Indonesian Rupiah 0.0000553404 0.0000553899 0.0000555040 0.0000554096 +Iranian Rial 0.0000228786 0.0000228671 +Israeli New Sheqel 0.1995720000 0.1995710000 0.1998150000 0.1998450000 +Kazakhstani Tenge 0.0023693200 0.0023668900 0.0023601900 0.0023350800 +Korean Won 0.0006480670 0.0006486190 0.0006464180 0.0006455440 +Kuwaiti Dinar 2.4165700000 2.4230800000 2.4273500000 2.4257600000 Libyan Dinar 0.5174910000 0.5174910000 0.5174910000 0.5174910000 -Malaysian Ringgit 0.1661230000 0.1655610000 0.1656540000 0.1664690000 -Mauritian Rupee 0.0207521000 0.0207775000 0.0208634000 -Mexican Peso 0.0360870000 0.0364464000 -Nepalese Rupee 0.0069139200 0.0068914400 0.0068937500 0.0069148500 -New Zealand Dollar 0.5306540000 0.5322980000 0.5342490000 0.5300340000 -Norwegian Krone 0.0885668000 0.0886734000 0.0886755000 0.0883278000 -Rial Omani 1.9267300000 1.9195700000 1.9202100000 1.9266300000 -Pakistani Rupee 0.0070658600 0.0070405000 0.0070429300 0.0070671000 -Nuevo Sol 0.2264720000 -Philippine Peso 0.0147628000 0.0147836000 0.0148472000 -Polish Zloty 0.1809450000 0.1813760000 0.1820000000 0.1817800000 -Qatar Riyal 0.2035240000 0.2027670000 0.2028350000 0.2035130000 -Russian Ruble 0.0128040000 0.0128062000 0.0129188000 0.0130485000 -Saudi Arabian Riyal 0.1975540000 0.1968190000 0.1968860000 0.1975430000 -Singapore Dollar 0.5214510000 0.5207230000 0.5201640000 0.5219390000 -South African Rand 0.0562263000 0.0562860000 0.0569367000 0.0568608000 -Sri Lanka Rupee 0.0049020900 0.0048960700 0.0048979200 0.0049141600 -Swedish Krona 0.0824973000 0.0830639000 0.0829146000 0.0828400000 -Swiss Franc 0.7337090000 0.7385900000 0.7376570000 0.7341080000 -Thai Baht 0.0211393000 0.0210914000 0.0210961000 0.0211545000 -Trinidad And Tobago Dollar 0.1095510000 0.1093120000 0.1094000000 0.1097660000 -Tunisian Dinar 0.3234230000 0.3229230000 0.3212750000 0.3239130000 -U.A.E. Dirham 0.2017230000 0.2009730000 0.2010400000 0.2017120000 -Peso Uruguayo 0.0260937000 0.0260969000 -Bolivar Fuerte 0.0740171000 0.0742645000 +Malaysian Ringgit 0.1659960000 0.1662940000 0.1663250000 0.1661230000 +Mauritian Rupee 0.0208418000 +Mexican Peso 0.0372107000 0.0375618000 0.0372281000 0.0362282000 +Nepalese Rupee 0.0069012400 0.0069030700 0.0069229700 0.0069139200 +New Zealand Dollar 0.5331450000 0.5322880000 0.5307790000 0.5306540000 +Norwegian Krone 0.0886062000 0.0888398000 0.0884082000 0.0885668000 +Rial Omani 1.9194300000 1.9246000000 1.9279900000 1.9267300000 +Pakistani Rupee 0.0070384300 0.0070575400 0.0070700400 0.0070658600 +Nuevo Sol 0.2283980000 0.2287290000 0.2281570000 +Philippine Peso 0.0146922000 0.0147295000 0.0147363000 0.0147628000 +Polish Zloty 0.1814160000 0.1811970000 0.1812540000 0.1809450000 +Qatar Riyal 0.2027520000 0.2032990000 0.2036570000 0.2035240000 +Russian Ruble 0.0128977000 0.0128040000 +Saudi Arabian Riyal 0.1968050000 0.1973360000 0.1976830000 0.1975540000 +Singapore Dollar 0.5253930000 0.5230480000 0.5224920000 0.5214510000 +South African Rand 0.0569347000 0.0570889000 0.0566127000 0.0562263000 +Sri Lanka Rupee 0.0048931100 0.0049014800 0.0049020900 +Swedish Krona 0.0822544000 0.0824642000 0.0823543000 0.0824973000 +Swiss Franc 0.7345670000 0.7331180000 0.7312210000 0.7337090000 +Thai Baht 0.0211098000 0.0211467000 0.0211707000 0.0211393000 +Trinidad And Tobago Dollar 0.1092030000 0.1091300000 0.1093820000 0.1095510000 +Tunisian Dinar 0.3206630000 0.3204540000 0.3225790000 0.3234230000 +U.A.E. Dirham 0.2009580000 0.2015000000 0.2018550000 0.2017230000 +Peso Uruguayo 0.0259816000 0.0261108000 0.0261517000 +Bolivar Fuerte 0.0739869000 0.0741864000 Currency units per SDR(3) -Currency February 22, 2017 February 21, 2017 February 17, 2017 February 16, 2017 February 15, 2017 -Chinese Yuan 9.278680 9.294370 9.308820 9.293770 9.271450 -Euro 1.281050 1.272190 1.271520 1.278930 -Japanese Yen 153.215000 153.207000 153.697000 154.201000 154.336000 -U.K. Pound Sterling 1.084900 1.086790 1.091240 1.084060 1.086450 -U.S. Dollar 1.348960 1.349840 1.354880 1.354420 1.349910 -Algerian Dinar 148.982000 148.948000 148.827000 149.035000 -Australian Dollar 1.760360 1.757530 1.756030 1.759760 -Bahrain Dinar 0.507542 0.509435 0.509264 0.507568 -Botswana Pula 14.105000 14.098600 14.035500 14.061600 -Brazilian Real 4.172650 4.132930 4.167970 4.184470 -Brunei Dollar 1.917730 1.920410 1.922470 1.915930 -Canadian Dollar 1.774910 1.777060 1.766230 -Chilean Peso 866.882000 864.887000 866.739000 -Colombian Peso 3,918.340000 3,896.210000 3,895.370000 3,871.060000 -Czech Koruna 34.615400 34.376000 34.348200 -Danish Krone 9.522450 9.456620 9.452690 9.508230 -Hungarian Forint 393.898000 391.899000 392.174000 394.350000 -Icelandic Krona 149.509000 149.565000 149.867000 151.015000 -Indian Rupee 90.840500 90.674200 90.335100 -Indonesian Rupiah 18,047.400000 18,057.800000 18,053.100000 -Iranian Rial 43,730.900000 43,869.800000 43,726.400000 -Israeli New Sheqel 5.003880 5.034740 5.033040 5.058120 -Kazakhstani Tenge 428.251000 431.678000 432.631000 432.917000 -Korean Won 1,549.080000 1,543.070000 1,546.210000 1,544.980000 -Kuwaiti Dinar 0.412242 0.413712 0.413573 0.412398 +Currency February 27, 2017 February 24, 2017 February 23, 2017 February 22, 2017 February 21, 2017 +Chinese Yuan 9.297740 9.309250 9.291520 9.278680 9.294370 +Euro 1.277730 1.277200 1.278100 1.283140 1.281050 +Japanese Yen 151.709000 152.909000 153.187000 153.215000 153.207000 +U.K. Pound Sterling 1.089510 1.079530 1.083060 1.084900 1.086790 +U.S. Dollar 1.352740 1.354980 1.351330 1.348960 1.349840 +Algerian Dinar 149.127000 148.875000 149.013000 148.982000 +Australian Dollar 1.755840 1.757950 1.753950 1.760360 +Bahrain Dinar 0.509471 0.508102 0.507210 0.507542 +Botswana Pula 14.026700 13.989000 14.066300 14.105000 +Brazilian Real 4.150570 4.164550 4.176920 4.172650 +Brunei Dollar 1.903340 1.911870 1.913900 1.917730 +Canadian Dollar 1.775570 1.771460 1.778870 1.774910 +Chilean Peso 867.671000 866.897000 868.229000 866.882000 +Colombian Peso 3,891.050000 3,910.160000 3,915.600000 3,918.340000 +Czech Koruna 34.508600 34.534700 34.668300 34.615400 +Danish Krone 9.495140 9.500740 9.537800 9.522450 +Hungarian Forint 394.678000 394.577000 394.354000 393.898000 +Icelandic Krona 146.880000 147.944000 148.750000 149.509000 +Indian Rupee 90.316300 90.332600 +Indonesian Rupiah 18,070.000000 18,053.800000 18,016.700000 18,047.400000 +Iranian Rial 43,709.000000 43,730.900000 +Israeli New Sheqel 5.010720 5.010750 5.004630 5.003880 +Kazakhstani Tenge 422.062000 422.495000 423.695000 428.251000 +Korean Won 1,543.050000 1,541.740000 1,546.990000 1,549.080000 +Kuwaiti Dinar 0.413810 0.412698 0.411972 0.412242 Libyan Dinar 1.932400 1.932400 1.932400 1.932400 -Malaysian Ringgit 6.019640 6.040070 6.036680 6.007120 -Mauritian Rupee 48.187900 48.129000 47.930800 -Mexican Peso 27.710800 27.437600 -Nepalese Rupee 144.636000 145.108000 145.059000 144.616000 -New Zealand Dollar 1.884470 1.878650 1.871790 1.886670 -Norwegian Krone 11.290900 11.277300 11.277100 11.321500 -Rial Omani 0.519014 0.520950 0.520776 0.519041 -Pakistani Rupee 141.526000 142.035000 141.986000 141.501000 -Nuevo Sol 4.415560 -Philippine Peso 67.737800 67.642500 67.352800 -Polish Zloty 5.526540 5.513410 5.494510 5.501160 -Qatar Riyal 4.913430 4.931770 4.930120 4.913690 -Russian Ruble 78.100600 78.087200 77.406600 76.637200 -Saudi Arabian Riyal 5.061910 5.080810 5.079080 5.062190 -Singapore Dollar 1.917730 1.920410 1.922470 1.915930 -South African Rand 17.785300 17.766400 17.563400 17.586800 -Sri Lanka Rupee 203.995000 204.245000 204.168000 203.494000 -Swedish Krona 12.121600 12.038900 12.060600 12.071500 -Swiss Franc 1.362940 1.353930 1.355640 1.362200 -Thai Baht 47.305300 47.412700 47.402100 47.271300 -Trinidad And Tobago Dollar 9.128170 9.148130 9.140770 9.110290 -Tunisian Dinar 3.091930 3.096710 3.112600 3.087250 -U.A.E. Dirham 4.957290 4.975790 4.974130 4.957560 -Peso Uruguayo 38.323400 38.318700 -Bolivar Fuerte 13.510400 13.465400 +Malaysian Ringgit 6.024240 6.013450 6.012330 6.019640 +Mauritian Rupee 47.980500 +Mexican Peso 26.874000 26.622800 26.861400 27.602800 +Nepalese Rupee 144.901000 144.863000 144.447000 144.636000 +New Zealand Dollar 1.875660 1.878680 1.884020 1.884470 +Norwegian Krone 11.285900 11.256200 11.311200 11.290900 +Rial Omani 0.520988 0.519588 0.518675 0.519014 +Pakistani Rupee 142.077000 141.692000 141.442000 141.526000 +Nuevo Sol 4.378320 4.371990 4.382950 +Philippine Peso 68.063300 67.891000 67.859600 67.737800 +Polish Zloty 5.512190 5.518860 5.517120 5.526540 +Qatar Riyal 4.932130 4.918860 4.910220 4.913430 +Russian Ruble 77.533200 78.100600 +Saudi Arabian Riyal 5.081170 5.067500 5.058600 5.061910 +Singapore Dollar 1.903340 1.911870 1.913900 1.917730 +South African Rand 17.564000 17.516500 17.663900 17.785300 +Sri Lanka Rupee 204.369000 204.020000 203.995000 +Swedish Krona 12.157400 12.126500 12.142700 12.121600 +Swiss Franc 1.361350 1.364040 1.367580 1.362940 +Thai Baht 47.371400 47.288700 47.235100 47.305300 +Trinidad And Tobago Dollar 9.157260 9.163380 9.142270 9.128170 +Tunisian Dinar 3.118540 3.120570 3.100020 3.091930 +U.A.E. Dirham 4.976160 4.962780 4.954050 4.957290 +Peso Uruguayo 38.488800 38.298300 38.238400 +Bolivar Fuerte 13.515900 13.479600 (1) Exchange rates are published daily except on IMF holidays or whenever the IMF is closed for business. diff --git a/core-java/src/main/java/com/baeldung/money/JavaMoney.java b/core-java/src/main/java/com/baeldung/money/JavaMoney.java index f66480bea5..3171d226ed 100644 --- a/core-java/src/main/java/com/baeldung/money/JavaMoney.java +++ b/core-java/src/main/java/com/baeldung/money/JavaMoney.java @@ -81,7 +81,11 @@ public class JavaMoney { LOGGER.info("Money & FastMoney operations : " + calcMoneyFastMoney); - monetaryAmounts = new MonetaryAmount[] { Money.of(100, "CHF"), Money.of(10.20, "CHF"), Money.of(1.15, "CHF"), }; + monetaryAmounts = + new MonetaryAmount[] { + Money.of(100, "CHF"), + Money.of(10.20, "CHF"), + Money.of(1.15, "CHF"), }; sumAmtCHF = Money.of(0, "CHF"); for (MonetaryAmount monetaryAmount : monetaryAmounts) { sumAmtCHF = sumAmtCHF.add(monetaryAmount); @@ -134,7 +138,7 @@ public class JavaMoney { usFormatted = formatUSD.format(oneDolar); LOGGER.info("One dolar standard formatted : " + usFormatted); - customFormat = MonetaryFormats.getAmountFormat(AmountFormatQueryBuilder.of(Locale.US).set(CurrencyStyle.NAME).set("pattern", "00000.00 ¤").build()); + customFormat = MonetaryFormats.getAmountFormat(AmountFormatQueryBuilder.of(Locale.US).set(CurrencyStyle.NAME).set("pattern", "00000.00 ¤").build()); customFormatted = customFormat.format(oneDolar); LOGGER.info("One dolar custom formatted : " + customFormatted); } diff --git a/core-java/src/test/java/com/baeldung/money/JavaMoneyTest.java b/core-java/src/test/java/com/baeldung/money/JavaMoneyTest.java index 25fcd48f7a..5aa12095ff 100644 --- a/core-java/src/test/java/com/baeldung/money/JavaMoneyTest.java +++ b/core-java/src/test/java/com/baeldung/money/JavaMoneyTest.java @@ -1,24 +1,24 @@ package com.baeldung.money; +import static org.junit.Assert.*; + import javax.money.convert.ConversionQueryBuilder; import javax.money.convert.MonetaryConversions; +import org.junit.Test; + import com.baeldung.money.JavaMoney; import junit.framework.TestCase; public class JavaMoneyTest - extends TestCase { - JavaMoney j9m; - public JavaMoneyTest( String testName ) - { - super( testName ); - j9m = new JavaMoney(); - } - public void testAmounts() + @Test + public void givenAmountsAreCorrect() { + JavaMoney j9m; + j9m = new JavaMoney(); assertEquals("USD", j9m.USD.toString()); assertEquals("USD 1", j9m.oneDolar.toString()); assertEquals("EUR 1", j9m.oneEuro.toString()); @@ -29,7 +29,10 @@ public class JavaMoneyTest } - public void testArithmetic(){ + @Test + public void givenArithmeticIsCorrect(){ + JavaMoney j9m; + j9m = new JavaMoney(); assertEquals("USD -199.5", j9m.calcAmtUSD.toString()); assertEquals("CHF 111.35", j9m.sumAmtCHF.toString()); assertEquals("USD 10", j9m.calcMoneyFastMoney.toString()); @@ -37,17 +40,25 @@ public class JavaMoneyTest assertEquals("USD 4", j9m.divideAmount.toString()); } - public void testRounding(){ + @Test + public void givenRoundingIsCorrect(){ + JavaMoney j9m; + j9m = new JavaMoney(); assertEquals("EUR 1.3", j9m.roundEUR.toString()); } - public void testFormatting(){ + @Test + public void givenFormatIsCorrect(){ + JavaMoney j9m; + j9m = new JavaMoney(); assertEquals("USD1.00", j9m.usFormatted); assertEquals("00001.00 US Dollar", j9m.customFormatted); } - public void testConversion(){ - + @Test + public void givenConversionIsNotNull(){ + JavaMoney j9m; + j9m = new JavaMoney(); assertNotNull(MonetaryConversions.getConversion(ConversionQueryBuilder.of().setTermCurrency("EUR").build())); assertNotNull(MonetaryConversions.getConversion(ConversionQueryBuilder.of().setTermCurrency("USD").build())); assertNotNull(j9m.convertedAmountEURtoUSD); From 0d31cab067788ec1d2926de7b0beb2bf06f2fb38 Mon Sep 17 00:00:00 2001 From: tschiman Date: Mon, 27 Feb 2017 20:52:22 -0700 Subject: [PATCH 072/112] BAEL-574 removing zipkin URL since we are setting it in our custom configuration --- .../application-config/gateway.properties | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/spring-cloud/spring-cloud-bootstrap/application-config/gateway.properties b/spring-cloud/spring-cloud-bootstrap/application-config/gateway.properties index e2686ae64c..e9e593284c 100644 --- a/spring-cloud/spring-cloud-bootstrap/application-config/gateway.properties +++ b/spring-cloud/spring-cloud-bootstrap/application-config/gateway.properties @@ -27,5 +27,4 @@ spring.redis.host=localhost spring.redis.port=6379 spring.sleuth.sampler.percentage=1.0 -spring.sleuth.web.skipPattern=(^cleanup.*|.+favicon.*) -spring.zipkin.base-url=http://zipkin \ No newline at end of file +spring.sleuth.web.skipPattern=(^cleanup.*|.+favicon.*) \ No newline at end of file From 1bfc944c5a5db228a6bd648794360df6aeb58225 Mon Sep 17 00:00:00 2001 From: Nancy Bosecker Date: Mon, 27 Feb 2017 20:26:30 -0800 Subject: [PATCH 073/112] Added more indexing/delete/query examples to code (#1251) * 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 * More examples for indexing,delete,and query for solrj * More examples for indexing,delete,and query for solrj --- .../com/baeldung/solrjava/ProductBean.java | 44 +++++++++++++ .../solrjava/SolrJavaIntegration.java | 15 ++++- .../solrjava/SolrJavaIntegrationTest.java | 62 +++++++++++++++++-- 3 files changed, 114 insertions(+), 7 deletions(-) create mode 100644 apache-solrj/src/main/java/com/baeldung/solrjava/ProductBean.java diff --git a/apache-solrj/src/main/java/com/baeldung/solrjava/ProductBean.java b/apache-solrj/src/main/java/com/baeldung/solrjava/ProductBean.java new file mode 100644 index 0000000000..14eea8f2f9 --- /dev/null +++ b/apache-solrj/src/main/java/com/baeldung/solrjava/ProductBean.java @@ -0,0 +1,44 @@ +package com.baeldung.solrjava; + +import org.apache.solr.client.solrj.beans.Field; + +public class ProductBean { + + String id; + String name; + String price; + + public ProductBean(String id, String name, String price) { + super(); + this.id = id; + this.name = name; + this.price = price; + } + + public String getId() { + return id; + } + + @Field("id") + protected void setId(String id) { + this.id = id; + } + + public String getName() { + return name; + } + + @Field("name") + protected void setName(String name) { + this.name = name; + } + + public String getPrice() { + return price; + } + + @Field("price") + protected void setPrice(String price) { + this.price = price; + } +} diff --git a/apache-solrj/src/main/java/com/baeldung/solrjava/SolrJavaIntegration.java b/apache-solrj/src/main/java/com/baeldung/solrjava/SolrJavaIntegration.java index f2d21f0993..c55e1c9ada 100644 --- a/apache-solrj/src/main/java/com/baeldung/solrjava/SolrJavaIntegration.java +++ b/apache-solrj/src/main/java/com/baeldung/solrjava/SolrJavaIntegration.java @@ -17,6 +17,12 @@ public class SolrJavaIntegration { solrClient.setParser(new XMLResponseParser()); } + public void addProductBean(ProductBean pBean) throws IOException, SolrServerException { + + solrClient.addBean(pBean); + solrClient.commit(); + } + public void addSolrDocument(String documentId, String itemName, String itemPrice) throws SolrServerException, IOException { SolrInputDocument document = new SolrInputDocument(); @@ -27,12 +33,18 @@ public class SolrJavaIntegration { solrClient.commit(); } - public void deleteSolrDocument(String documentId) throws SolrServerException, IOException { + public void deleteSolrDocumentById(String documentId) throws SolrServerException, IOException { solrClient.deleteById(documentId); solrClient.commit(); } + public void deleteSolrDocumentByQuery(String query) throws SolrServerException, IOException { + + solrClient.deleteByQuery(query); + solrClient.commit(); + } + protected HttpSolrClient getSolrClient() { return solrClient; } @@ -40,4 +52,5 @@ public class SolrJavaIntegration { protected void setSolrClient(HttpSolrClient solrClient) { this.solrClient = solrClient; } + } diff --git a/apache-solrj/src/test/java/com/baeldung/solrjava/SolrJavaIntegrationTest.java b/apache-solrj/src/test/java/com/baeldung/solrjava/SolrJavaIntegrationTest.java index 22f9eae8ee..7f4599a91d 100644 --- a/apache-solrj/src/test/java/com/baeldung/solrjava/SolrJavaIntegrationTest.java +++ b/apache-solrj/src/test/java/com/baeldung/solrjava/SolrJavaIntegrationTest.java @@ -24,7 +24,7 @@ public class SolrJavaIntegrationTest { } @Test - public void whenAdd_thenVerifyAdded() throws SolrServerException, IOException { + public void whenAdd_thenVerifyAddedByQueryOnId() throws SolrServerException, IOException { SolrQuery query = new SolrQuery(); query.set("q", "id:123456"); @@ -36,15 +36,65 @@ public class SolrJavaIntegrationTest { assertEquals(docList.getNumFound(), 1); for (SolrDocument doc : docList) { - assertEquals((String) doc.getFieldValue("id"), "123456"); - assertEquals((Double) doc.getFieldValue("price"), (Double) 599.99); + assertEquals("Kenmore Dishwasher", (String) doc.getFieldValue("name")); + assertEquals((Double) 599.99, (Double) doc.getFieldValue("price")); } } @Test - public void whenDelete_thenVerifyDeleted() throws SolrServerException, IOException { + public void whenAdd_thenVerifyAddedByQueryOnPrice() throws SolrServerException, IOException { - solrJavaIntegration.deleteSolrDocument("123456"); + SolrQuery query = new SolrQuery(); + query.set("q", "price:599.99"); + QueryResponse response = null; + + response = solrJavaIntegration.getSolrClient().query(query); + + SolrDocumentList docList = response.getResults(); + assertEquals(1, docList.getNumFound()); + + for (SolrDocument doc : docList) { + assertEquals("123456", (String) doc.getFieldValue("id")); + assertEquals((Double) 599.99, (Double) doc.getFieldValue("price")); + } + } + + @Test + public void whenAdd_thenVerifyAddedByQuery() throws SolrServerException, IOException { + + SolrDocument doc = solrJavaIntegration.getSolrClient().getById("123456"); + assertEquals("Kenmore Dishwasher", (String) doc.getFieldValue("name")); + assertEquals((Double) 599.99, (Double) doc.getFieldValue("price")); + } + + @Test + public void whenAddBean_thenVerifyAddedByQuery() throws SolrServerException, IOException { + + ProductBean pBean = new ProductBean("888", "Apple iPhone 6s", "299.99"); + solrJavaIntegration.addProductBean(pBean); + + SolrDocument doc = solrJavaIntegration.getSolrClient().getById("888"); + assertEquals("Apple iPhone 6s", (String) doc.getFieldValue("name")); + assertEquals((Double) 299.99, (Double) doc.getFieldValue("price")); + } + + @Test + public void whenDeleteById_thenVerifyDeleted() throws SolrServerException, IOException { + + solrJavaIntegration.deleteSolrDocumentById("123456"); + + SolrQuery query = new SolrQuery(); + query.set("q", "id:123456"); + QueryResponse response = solrJavaIntegration.getSolrClient().query(query); + + SolrDocumentList docList = response.getResults(); + assertEquals(0, docList.getNumFound()); + } + + @Test + public void whenDeleteByQuery_thenVerifyDeleted() throws SolrServerException, IOException { + + solrJavaIntegration.deleteSolrDocumentByQuery("name:Kenmore Dishwasher"); SolrQuery query = new SolrQuery(); query.set("q", "id:123456"); @@ -53,6 +103,6 @@ public class SolrJavaIntegrationTest { response = solrJavaIntegration.getSolrClient().query(query); SolrDocumentList docList = response.getResults(); - assertEquals(docList.getNumFound(), 0); + assertEquals(0, docList.getNumFound()); } } From 442c00545c4fe7109c971d2dd6414dc619df301e Mon Sep 17 00:00:00 2001 From: mujahid Date: Wed, 1 Mar 2017 02:00:13 +0800 Subject: [PATCH 074/112] BAEL-347-Full-text search with SOLR (#1254) * solr-fulltext-search module created * solr-fulltext-search modue created * solr-fulltext-search change s * pom changes merged from upstream --- solr-fulltext-search/pom.xml | 2 +- .../solr/fulltext/search/model/Item.java | 51 +++ .../search/service/ItemSearchService.java | 15 + .../search/service/ItemSearchServiceImpl.java | 34 ++ .../ItemSearchServiceIntegrationTest.java | 374 ++++++++++++++++++ 5 files changed, 475 insertions(+), 1 deletion(-) create mode 100644 solr-fulltext-search/src/main/java/com/baeldung/solr/fulltext/search/model/Item.java create mode 100644 solr-fulltext-search/src/main/java/com/baeldung/solr/fulltext/search/service/ItemSearchService.java create mode 100644 solr-fulltext-search/src/main/java/com/baeldung/solr/fulltext/search/service/ItemSearchServiceImpl.java create mode 100644 solr-fulltext-search/src/test/java/com/baeldung/solr/fulltext/search/service/ItemSearchServiceIntegrationTest.java diff --git a/solr-fulltext-search/pom.xml b/solr-fulltext-search/pom.xml index 4afcb5838a..bed6afd48f 100644 --- a/solr-fulltext-search/pom.xml +++ b/solr-fulltext-search/pom.xml @@ -18,7 +18,7 @@ org.apache.solr solr-solrj - 6.1.0 + 6.4.1 log4j diff --git a/solr-fulltext-search/src/main/java/com/baeldung/solr/fulltext/search/model/Item.java b/solr-fulltext-search/src/main/java/com/baeldung/solr/fulltext/search/model/Item.java new file mode 100644 index 0000000000..d7a0524ca9 --- /dev/null +++ b/solr-fulltext-search/src/main/java/com/baeldung/solr/fulltext/search/model/Item.java @@ -0,0 +1,51 @@ +package com.baeldung.solr.fulltext.search.model; + +import org.apache.solr.client.solrj.beans.Field; + +public class Item { + + @Field + private String id; + + @Field + private String description; + + @Field + private String category; + + @Field + private float price; + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public String getCategory() { + return category; + } + + public void setCategory(String category) { + this.category = category; + } + + public float getPrice() { + return price; + } + + public void setPrice(float price) { + this.price = price; + } + +} diff --git a/solr-fulltext-search/src/main/java/com/baeldung/solr/fulltext/search/service/ItemSearchService.java b/solr-fulltext-search/src/main/java/com/baeldung/solr/fulltext/search/service/ItemSearchService.java new file mode 100644 index 0000000000..fa906fc975 --- /dev/null +++ b/solr-fulltext-search/src/main/java/com/baeldung/solr/fulltext/search/service/ItemSearchService.java @@ -0,0 +1,15 @@ +package com.baeldung.solr.fulltext.search.service; + +import java.io.IOException; + +import org.apache.solr.client.solrj.SolrServerException; + +import com.baeldung.solr.fulltext.search.model.Item; + +public interface ItemSearchService { + + public void index(String id, String description, String category, float price) throws SolrServerException, IOException; + + public void indexBean(Item item) throws IOException, SolrServerException; + +} diff --git a/solr-fulltext-search/src/main/java/com/baeldung/solr/fulltext/search/service/ItemSearchServiceImpl.java b/solr-fulltext-search/src/main/java/com/baeldung/solr/fulltext/search/service/ItemSearchServiceImpl.java new file mode 100644 index 0000000000..573cc58bc0 --- /dev/null +++ b/solr-fulltext-search/src/main/java/com/baeldung/solr/fulltext/search/service/ItemSearchServiceImpl.java @@ -0,0 +1,34 @@ +package com.baeldung.solr.fulltext.search.service; + +import java.io.IOException; + +import org.apache.solr.client.solrj.SolrClient; +import org.apache.solr.client.solrj.SolrServerException; +import org.apache.solr.common.SolrInputDocument; + +import com.baeldung.solr.fulltext.search.model.Item; + +public class ItemSearchServiceImpl implements ItemSearchService { + + private final SolrClient solrClient; + + public ItemSearchServiceImpl(SolrClient solrClient) { + this.solrClient = solrClient; + } + + public void index(String id, String description, String category, float price) throws SolrServerException, IOException { + SolrInputDocument doc = new SolrInputDocument(); + doc.addField("id", id); + doc.addField("description", description); + doc.addField("category", category); + doc.addField("price", price); + solrClient.add(doc); + solrClient.commit(); + } + + public void indexBean(Item item) throws IOException, SolrServerException { + solrClient.addBean(item); + solrClient.commit(); + } + +} diff --git a/solr-fulltext-search/src/test/java/com/baeldung/solr/fulltext/search/service/ItemSearchServiceIntegrationTest.java b/solr-fulltext-search/src/test/java/com/baeldung/solr/fulltext/search/service/ItemSearchServiceIntegrationTest.java new file mode 100644 index 0000000000..94661ffc2e --- /dev/null +++ b/solr-fulltext-search/src/test/java/com/baeldung/solr/fulltext/search/service/ItemSearchServiceIntegrationTest.java @@ -0,0 +1,374 @@ +package com.baeldung.solr.fulltext.search.service; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + +import java.util.List; +import java.util.Map; + +import org.apache.solr.client.solrj.SolrClient; +import org.apache.solr.client.solrj.SolrQuery; +import org.apache.solr.client.solrj.impl.HttpSolrClient; +import org.apache.solr.client.solrj.response.FacetField.Count; +import org.apache.solr.client.solrj.response.QueryResponse; +import org.apache.solr.client.solrj.response.RangeFacet; +import org.apache.solr.client.solrj.response.SpellCheckResponse; +import org.apache.solr.client.solrj.response.SpellCheckResponse.Suggestion; +import org.apache.solr.client.solrj.response.SuggesterResponse; +import org.apache.solr.common.SolrDocument; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +import com.baeldung.solr.fulltext.search.model.Item; + +public class ItemSearchServiceIntegrationTest { + + private static SolrClient solrClient; + private static ItemSearchService itemSearchService; + private static final String solrUrl = "http://localhost:8987/solr/item"; + + @BeforeClass + public static void initBeans() throws Exception { + solrClient = new HttpSolrClient.Builder(solrUrl).build(); + itemSearchService = new ItemSearchServiceImpl(solrClient); + + solrClient.commit(); + } + + @Before + public void clearSolrData() throws Exception { + solrClient.deleteByQuery("*:*"); + } + + @Test + public void whenIndexing_thenAvailableOnRetrieval() throws Exception { + itemSearchService.index("hm0001", "Washing Machine", "Home Appliances", 450f); + final SolrDocument indexedDoc = solrClient.getById("hm0001"); + assertEquals("hm0001", indexedDoc.get("id")); + } + + @Test + public void whenIndexingBean_thenAvailableOnRetrieval() throws Exception { + Item item = new Item(); + item.setId("hm0002"); + item.setCategory("Televisions"); + item.setDescription("LED TV 32"); + item.setPrice(500); + itemSearchService.indexBean(item); + } + + @Test + public void whenSearchingByBasicQuery_thenAllMatchingItemsShouldAvialble() throws Exception { + itemSearchService.index("hm0001", "Brand1 Washing Machine", "Home Appliances", 450f); + itemSearchService.index("hm0002", "Brand1 Refrigerator", "Home Appliances", 450f); + itemSearchService.index("hm0003", "LED TV 32", "Brand1 Home Appliances", 450f); + + SolrQuery query = new SolrQuery(); + query.setQuery("brand1"); + query.setStart(0); + query.setRows(10); + + QueryResponse response = solrClient.query(query); + List items = response.getBeans(Item.class); + + assertEquals(3, items.size()); + + } + + @Test + public void whenSearchingWithWildCard_thenAllMatchingItemsShouldAvialble() throws Exception { + itemSearchService.index("hm0001", "Brand1 Washing Machine", "Home Appliances", 450f); + itemSearchService.index("hm0002", "Brand1 Refrigerator", "Home Appliances", 450f); + itemSearchService.index("hm0003", "LED TV 32", "Brand1 Home Appliances", 450f); + + SolrQuery query = new SolrQuery(); + query.setQuery("*rand?"); + + QueryResponse response = solrClient.query(query); + List items = response.getBeans(Item.class); + + assertEquals(3, items.size()); + } + + @Test + public void whenSearchingWithLogicalOperators_thenAllMatchingItemsShouldAvialble() throws Exception { + itemSearchService.index("hm0001", "Brand1 Washing Machine", "Home Appliances", 450f); + itemSearchService.index("hm0002", "Brand1 Refrigerator", "Home Appliances", 450f); + itemSearchService.index("hm0003", "Brand2 LED TV 32", "Washing Appliances", 450f); + + SolrQuery query = new SolrQuery(); + query.setQuery("brand1 AND (Washing OR Refrigerator)"); + + QueryResponse response = solrClient.query(query); + List items = response.getBeans(Item.class); + + assertEquals(2, items.size()); + } + + @Test + public void whenSearchingWithFields_thenAllMatchingItemsShouldAvialble() throws Exception { + itemSearchService.index("0001", "Brand1 Washing Machine", "Home Appliances", 450f); + itemSearchService.index("0002", "Brand1 Refrigerator", "Home Appliances", 450f); + itemSearchService.index("0003", "Brand2 LED TV 32", "Brand1 Washing Home Appliances", 450f); + + SolrQuery query = new SolrQuery(); + query.setQuery("description:Brand* AND category:*Washing*"); + + QueryResponse response = solrClient.query(query); + List items = response.getBeans(Item.class); + + assertEquals(1, items.size()); + } + + @Test + public void whenSearchingWithPhrase_thenAllMatchingItemsShouldAvialble() throws Exception { + itemSearchService.index("hm0001", "Brand1 Washing Machine", "Home Appliances", 450f); + itemSearchService.index("hm0002", "Brand1 Refrigerator", "Home Appliances", 450f); + itemSearchService.index("hm0003", "Brand2 Dishwasher", "Washing tools and equipment ", 450f); + + SolrQuery query = new SolrQuery(); + query.setQuery("washing MachIne"); + + QueryResponse response = solrClient.query(query); + List items = response.getBeans(Item.class); + + assertEquals(2, items.size()); + } + + @Test + public void whenSearchingWithRealPhrase_thenAllMatchingItemsShouldAvialble() throws Exception { + itemSearchService.index("hm0001", "Brand1 Washing Machine", "Home Appliances", 450f); + itemSearchService.index("hm0002", "Brand1 Refrigerator", "Home Appliances", 450f); + itemSearchService.index("hm0003", "Brand2 Dishwasher", "Washing tools and equipment ", 450f); + + SolrQuery query = new SolrQuery(); + query.setQuery("\"washing machine\""); + + QueryResponse response = solrClient.query(query); + List items = response.getBeans(Item.class); + + assertEquals(1, items.size()); + } + + @Test + public void whenSearchingPhraseWithProximity_thenAllMatchingItemsShouldAvialble() throws Exception { + itemSearchService.index("hm0001", "Brand1 Washing Machine", "Home Appliances", 450f); + itemSearchService.index("hm0002", "Brand1 Refrigerator", "Home Appliances", 450f); + itemSearchService.index("hm0003", "Brand2 Dishwasher", "Washing tools and equipment ", 450f); + + SolrQuery query = new SolrQuery(); + query.setQuery("\"Washing equipment\"~2"); + + QueryResponse response = solrClient.query(query); + List items = response.getBeans(Item.class); + + assertEquals(1, items.size()); + } + + @Test + public void whenSearchingWithPriceRange_thenAllMatchingItemsShouldAvialble() throws Exception { + itemSearchService.index("hm0001", "Brand1 Washing Machine", "Home Appliances", 100f); + itemSearchService.index("hm0002", "Brand1 Refrigerator", "Home Appliances", 300f); + itemSearchService.index("hm0003", "Brand2 Dishwasher", "Home Appliances", 200f); + itemSearchService.index("hm0004", "Brand2 Dishwasher", "Washing tools and equipment ", 450f); + + SolrQuery query = new SolrQuery(); + query.setQuery("price:[100 TO 300]"); + + QueryResponse response = solrClient.query(query); + List items = response.getBeans(Item.class); + + assertEquals(3, items.size()); + } + + @Test + public void whenSearchingWithPriceRangeInclusiveExclusive_thenAllMatchingItemsShouldAvialble() throws Exception { + itemSearchService.index("hm0001", "Brand1 Washing Machine", "Home Appliances", 100f); + itemSearchService.index("hm0002", "Brand1 Refrigerator", "Home Appliances", 300f); + itemSearchService.index("hm0003", "Brand2 Dishwasher", "Home Appliances", 200f); + itemSearchService.index("hm0004", "Brand2 Dishwasher", "Washing tools and equipment ", 450f); + + SolrQuery query = new SolrQuery(); + query.setQuery("price:{100 TO 300]"); + + QueryResponse response = solrClient.query(query); + List items = response.getBeans(Item.class); + + assertEquals(2, items.size()); + } + + @Test + public void whenSearchingWithFilterQuery_thenAllMatchingItemsShouldAvialble() throws Exception { + itemSearchService.index("hm0001", "Brand1 Washing Machine", "Home Appliances", 100f); + itemSearchService.index("hm0002", "Brand1 Refrigerator", "Home Appliances", 300f); + itemSearchService.index("hm0003", "Brand2 Ceiling Fan", "Home Appliances", 200f); + itemSearchService.index("hm0004", "Brand2 Dishwasher", "Washing tools and equipment ", 250f); + + SolrQuery query = new SolrQuery(); + query.setQuery("price:[100 TO 300]"); + query.addFilterQuery("description:Brand1", "category:Home Appliances"); + + QueryResponse response = solrClient.query(query); + List items = response.getBeans(Item.class); + + assertEquals(2, items.size()); + } + + @Test + public void whenSearchingWithFacetFields_thenAllMatchingFacetsShouldAvialble() throws Exception { + itemSearchService.index("hm0001", "Brand1 Washing Machine", "CategoryA", 100f); + itemSearchService.index("hm0002", "Brand1 Refrigerator", "CategoryA", 300f); + itemSearchService.index("hm0003", "Brand2 Ceiling Fan", "CategoryB", 200f); + itemSearchService.index("hm0004", "Brand2 Dishwasher", "CategoryB", 250f); + + SolrQuery query = new SolrQuery(); + query.setQuery("*:*"); + query.addFacetField("category"); + + QueryResponse response = solrClient.query(query); + List facetResults = response.getFacetField("category").getValues(); + + assertEquals(2, facetResults.size()); + + for (Count count : facetResults) { + if ("categorya".equalsIgnoreCase(count.getName())) { + assertEquals(2, count.getCount()); + } else if ("categoryb".equalsIgnoreCase(count.getName())) { + assertEquals(2, count.getCount()); + } else { + fail("unexpected category"); + } + } + } + + @Test + public void whenSearchingWithFacetQuery_thenAllMatchingFacetsShouldAvialble() throws Exception { + itemSearchService.index("hm0001", "Brand1 Washing Machine", "CategoryA", 100f); + itemSearchService.index("hm0002", "Brand1 Refrigerator", "CategoryA", 300f); + itemSearchService.index("hm0003", "Brand2 Ceiling Fan", "CategoryB", 200f); + itemSearchService.index("hm0004", "Brand2 Dishwasher", "CategoryB", 250f); + + SolrQuery query = new SolrQuery(); + query.setQuery("*:*"); + + query.addFacetQuery("Washing OR Refrigerator"); + query.addFacetQuery("Brand2"); + + QueryResponse response = solrClient.query(query); + Map facetQueryMap = response.getFacetQuery(); + + assertEquals(2, facetQueryMap.size()); + + for (Map.Entry entry : facetQueryMap.entrySet()) { + String facetQuery = entry.getKey(); + + if ("Washing OR Refrigerator".equals(facetQuery)) { + assertEquals(Integer.valueOf(2), entry.getValue()); + } else if ("Brand2".equals(facetQuery)) { + assertEquals(Integer.valueOf(2), entry.getValue()); + } else { + fail("unexpected query"); + } + + } + } + + @Test + public void whenSearchingWithFacetRange_thenAllMatchingFacetsShouldAvialble() throws Exception { + itemSearchService.index("hm0001", "Brand1 Washing Machine", "CategoryA", 100f); + itemSearchService.index("hm0002", "Brand1 Refrigerator", "CategoryA", 125f); + itemSearchService.index("hm0003", "Brand2 Ceiling Fan", "CategoryB", 150f); + itemSearchService.index("hm0004", "Brand2 Dishwasher", "CategoryB", 250f); + + SolrQuery query = new SolrQuery(); + query.setQuery("*:*"); + + query.addNumericRangeFacet("price", 100, 275, 25); + + QueryResponse response = solrClient.query(query); + List rangeFacets = response.getFacetRanges().get(0).getCounts(); + + assertEquals(7, rangeFacets.size()); + } + + @Test + public void whenSearchingWithHitHighlighting_thenKeywordsShouldBeHighlighted() throws Exception { + itemSearchService.index("hm0001", "Brand1 Washing Machine", "Home Appliances", 100f); + itemSearchService.index("hm0002", "Brand1 Refrigerator", "Home Appliances", 300f); + itemSearchService.index("hm0003", "Brand2 Ceiling Fan", "Home Appliances", 200f); + itemSearchService.index("hm0004", "Brand2 Dishwasher", "Washing equipments", 250f); + + SolrQuery query = new SolrQuery(); + query.setQuery("Appliances"); + query.setHighlight(true); + query.addHighlightField("category"); + query.setHighlightSimplePre(""); + query.setHighlightSimplePost(""); + QueryResponse response = solrClient.query(query); + + Map>> hitHighlightedMap = response.getHighlighting(); + Map> highlightedFieldMap = hitHighlightedMap.get("hm0001"); + List highlightedList = highlightedFieldMap.get("category"); + String highLightedText = highlightedList.get(0); + + assertEquals("Home Appliances", highLightedText); + + } + + @Test + public void whenSearchingWithKeywordWithMistake_thenSpellingSuggestionsShouldBeReturned() throws Exception { + itemSearchService.index("hm0001", "Brand1 Washing Machine", "Home Appliances", 100f); + itemSearchService.index("hm0002", "Brand1 Refrigerator", "Home Appliances", 300f); + itemSearchService.index("hm0003", "Brand2 Ceiling Fan", "Home Appliances", 200f); + itemSearchService.index("hm0004", "Brand2 Dishwasher", "Washing equipments", 250f); + + SolrQuery query = new SolrQuery(); + query.setQuery("hme"); + query.set("spellcheck", "on"); + QueryResponse response = solrClient.query(query); + + SpellCheckResponse spellCheckResponse = response.getSpellCheckResponse(); + + assertEquals(false, spellCheckResponse.isCorrectlySpelled()); + + Suggestion suggestion = spellCheckResponse.getSuggestions().get(0); + + assertEquals("hme", suggestion.getToken()); + + List alternatives = suggestion.getAlternatives(); + String alternative = alternatives.get(0); + + assertEquals("home", alternative); + } + + @Test + public void whenSearchingWithIncompleteKeyword_thenKeywordSuggestionsShouldBeReturned() throws Exception { + itemSearchService.index("hm0001", "Brand1 Washing Machine", "Home Appliances", 100f); + itemSearchService.index("hm0002", "Brand1 Refrigerator", "Home Appliances", 300f); + itemSearchService.index("hm0003", "Brand2 Ceiling Fan", "Home Appliances", 200f); + itemSearchService.index("hm0004", "Brand2 Dishwasher", "Home washing equipments", 250f); + + SolrQuery query = new SolrQuery(); + query.setRequestHandler("/suggest"); + query.set("suggest", "true"); + query.set("suggest.build", "true"); + query.set("suggest.dictionary", "mySuggester"); + query.set("suggest.q", "Hom"); + QueryResponse response = solrClient.query(query); + + SuggesterResponse suggesterResponse = response.getSuggesterResponse(); + Map> suggestedTerms = suggesterResponse.getSuggestedTerms(); + List suggestions = suggestedTerms.get("mySuggester"); + + assertEquals(2, suggestions.size()); + + for (String term : suggestions) { + if (!"Home Appliances".equals(term) && !"Home washing equipments".equals(term)) { + fail("Unexpected suggestions"); + } + } + + } + +} From 52a3565f39a36487393230d6e0ffc780dfe602c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Muhammet=20O=C4=9Fuz=20=C3=96ZCAN?= Date: Wed, 1 Mar 2017 22:41:28 +0200 Subject: [PATCH 075/112] Seems no conflict (#1274) * 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. * Most popular use case of grouping elements together added Most popular use case of grouping elements together added --- .../list/listoflist/ListOfListsTest.java | 28 ++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) 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 index ce24ff24bc..674a2f89bc 100644 --- a/core-java/src/test/java/com/baeldung/list/listoflist/ListOfListsTest.java +++ b/core-java/src/test/java/com/baeldung/list/listoflist/ListOfListsTest.java @@ -39,7 +39,8 @@ public class ListOfListsTest { @SuppressWarnings("unchecked") @Test public void givenListOfLists_whenRemovingElements_thenCheckNames() { - ((ArrayList) listOfLists.get(1)).remove(0); + + ((ArrayList) listOfLists.get(1)).remove(0); listOfLists.remove(1); assertEquals("Rubber 1", ((Rubber) listOfLists.get(1) .get(0)).getName()); @@ -47,4 +48,29 @@ public class ListOfListsTest { assertEquals("Rubber 1", ((Rubber) listOfLists.get(0) .get(0)).getName()); } + + @Test + public void givenThreeList_whenCombineIntoOneList_thenCheckList() { + ArrayList pens = new ArrayList<>(); + pens.add(new Pen("Pen 1")); + pens.add(new Pen("Pen 2")); + ArrayList pencils = new ArrayList<>(); + pencils.add(new Pencil("Pencil 1")); + pencils.add(new Pencil("Pencil 2")); + ArrayList rubbers = new ArrayList<>(); + rubbers.add(new Rubber("Rubber 1")); + rubbers.add(new Rubber("Rubber 2")); + + List> list = new ArrayList>(); + list.add(pens); + list.add(pencils); + list.add(rubbers); + + assertEquals("Pen 1", ((Pen) list.get(0) + .get(0)).getName()); + assertEquals("Pencil 1", ((Pencil) list.get(1) + .get(0)).getName()); + assertEquals("Rubber 1", ((Rubber) list.get(2) + .get(0)).getName()); + } } From efdd45259f30ea2794a47a4e252831e4f1337210 Mon Sep 17 00:00:00 2001 From: slavisa-baeldung Date: Wed, 1 Mar 2017 22:37:00 +0100 Subject: [PATCH 076/112] BAEL-701 - TestNG reformatting --- testng/pom.xml | 196 +++++++++--------- .../java/baeldung/com/MultiThreadedTests.java | 6 +- .../src/test/java/baeldung/com/TestGroup.java | 56 ++--- .../baeldung/reports/CustomisedListener.java | 24 +-- .../baeldung/reports/CustomisedReports.java | 54 ++--- 5 files changed, 168 insertions(+), 168 deletions(-) diff --git a/testng/pom.xml b/testng/pom.xml index 5cf8ab06f3..83b32bb84d 100644 --- a/testng/pom.xml +++ b/testng/pom.xml @@ -1,121 +1,121 @@ - 4.0.0 - com.baeldung - testng - 0.1.0-SNAPSHOT - jar - testng + 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 + testng + 0.1.0-SNAPSHOT + jar + testng - + - + - - org.slf4j - slf4j-api - ${org.slf4j.version} - - - ch.qos.logback - logback-classic - ${logback.version} - + + org.slf4j + slf4j-api + ${org.slf4j.version} + + + ch.qos.logback + logback-classic + ${logback.version} + - + - - org.testng - testng - ${testng.version} - test - + + org.testng + testng + ${testng.version} + test + - + - - testng - - - src/main/resources - true - - - - - src/main/resources - true - - + + testng + + + src/main/resources + true + + + + + src/main/resources + true + + - + - - org.apache.maven.plugins - maven-compiler-plugin - ${maven-compiler-plugin.version} - - 1.8 - 1.8 - - + + 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 - - - src\test\resources\parametrized_testng.xml - src\test\resources\test_group.xml - src\test\resources\test_setup.xml - src\test\resources\test_suite.xml - + + org.apache.maven.plugins + maven-surefire-plugin + ${maven-surefire-plugin.version} + + + **/*IntegrationTest.java + **/*LongRunningUnitTest.java + **/*ManualTest.java + + + src\test\resources\parametrized_testng.xml + src\test\resources\test_group.xml + src\test\resources\test_setup.xml + src\test\resources\test_suite.xml + - true - - + true + + - - org.apache.maven.plugins - maven-dependency-plugin - - - copy-dependencies - prepare-package - - copy-dependencies - - - ${project.build.directory}/libs - - - - - + + org.apache.maven.plugins + maven-dependency-plugin + + + copy-dependencies + prepare-package + + copy-dependencies + + + ${project.build.directory}/libs + + + + + - + - + - - 1.7.21 - 1.1.7 + + 1.7.21 + 1.1.7 - - 6.10 + + 6.10 - - 3.6.0 - 2.19.1 + + 3.6.0 + 2.19.1 - + \ No newline at end of file diff --git a/testng/src/test/java/baeldung/com/MultiThreadedTests.java b/testng/src/test/java/baeldung/com/MultiThreadedTests.java index 48096b8c20..4476eaa7a0 100644 --- a/testng/src/test/java/baeldung/com/MultiThreadedTests.java +++ b/testng/src/test/java/baeldung/com/MultiThreadedTests.java @@ -4,11 +4,11 @@ import org.testng.Assert; import org.testng.annotations.Test; public class MultiThreadedTests { - + @Test(threadPoolSize = 5, invocationCount = 10, timeOut = 1000) - public void givenMethod_whenRunInThreads_thenCorrect(){ + public void givenMethod_whenRunInThreads_thenCorrect() { int count = Thread.activeCount(); - Assert.assertTrue(count>1); + Assert.assertTrue(count > 1); } } diff --git a/testng/src/test/java/baeldung/com/TestGroup.java b/testng/src/test/java/baeldung/com/TestGroup.java index 014fad7f8b..a592000bed 100644 --- a/testng/src/test/java/baeldung/com/TestGroup.java +++ b/testng/src/test/java/baeldung/com/TestGroup.java @@ -6,39 +6,39 @@ import org.testng.annotations.Test; public class TestGroup { - @BeforeGroups("database") - public void setupDB() { - System.out.println("setupDB()"); - } + @BeforeGroups("database") + public void setupDB() { + System.out.println("setupDB()"); + } - @AfterGroups("database") - public void cleanDB() { - System.out.println("cleanDB()"); - } + @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 runSelenium() { + System.out.println("runSelenium()"); + } - @Test(groups= "selenium-test") - public void runSelenium1() { - System.out.println("runSelenium()1"); - } + @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 testConnectOracle() { + System.out.println("testConnectOracle()"); + } - @Test(groups = "database") - public void testConnectMsSQL() { - System.out.println("testConnectMsSQL"); - } + @Test(groups = "database") + public void testConnectMsSQL() { + System.out.println("testConnectMsSQL"); + } - @Test(dependsOnGroups = {"database","selenium-test"}) - public void runFinal() { - System.out.println("runFinal"); - } + @Test(dependsOnGroups = {"database", "selenium-test"}) + public void runFinal() { + System.out.println("runFinal"); + } } \ No newline at end of file diff --git a/testng/src/test/java/com/baeldung/reports/CustomisedListener.java b/testng/src/test/java/com/baeldung/reports/CustomisedListener.java index 179ca5f736..e8338c941d 100644 --- a/testng/src/test/java/com/baeldung/reports/CustomisedListener.java +++ b/testng/src/test/java/com/baeldung/reports/CustomisedListener.java @@ -13,25 +13,25 @@ public class CustomisedListener implements ITestListener { public void onFinish(ITestContext arg0) { LOGGER.info("PASSED TEST CASES"); arg0.getPassedTests() - .getAllResults() - .stream() - .forEach(result -> { - LOGGER.info(result.getName()); - }); + .getAllResults() + .stream() + .forEach(result -> { + LOGGER.info(result.getName()); + }); LOGGER.info("FAILED TEST CASES"); arg0.getFailedTests() - .getAllResults() - .stream() - .forEach(result -> { - LOGGER.info(result.getName()); - }); - LOGGER.info("Test completed on: "+arg0.getEndDate().toString()); + .getAllResults() + .stream() + .forEach(result -> { + LOGGER.info(result.getName()); + }); + LOGGER.info("Test completed on: " + arg0.getEndDate().toString()); } @Override public void onStart(ITestContext arg0) { LOGGER.info("Started testing on: " + arg0.getStartDate() - .toString()); + .toString()); } @Override diff --git a/testng/src/test/java/com/baeldung/reports/CustomisedReports.java b/testng/src/test/java/com/baeldung/reports/CustomisedReports.java index e59ecbd79b..07ba2162cc 100644 --- a/testng/src/test/java/com/baeldung/reports/CustomisedReports.java +++ b/testng/src/test/java/com/baeldung/reports/CustomisedReports.java @@ -1,8 +1,5 @@ package com.baeldung.reports; -import java.util.List; -import java.util.Map; - import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.testng.IReporter; @@ -11,6 +8,9 @@ import org.testng.ISuiteResult; import org.testng.ITestContext; import org.testng.xml.XmlSuite; +import java.util.List; +import java.util.Map; + public class CustomisedReports implements IReporter { private static final Logger LOGGER = LoggerFactory.getLogger("TEST_REPORT"); @@ -18,32 +18,32 @@ public class CustomisedReports implements IReporter { public void generateReport(List xmlSuites, List suites, String outputDirectory) { suites.stream() - .forEach(suite -> { - String suiteName = suite.getName(); - Map suiteResults = suite.getResults(); - suiteResults.values() - .stream() - .forEach(result -> { - ITestContext context - = ((ISuiteResult) result).getTestContext(); + .forEach(suite -> { + String suiteName = suite.getName(); + Map suiteResults = suite.getResults(); + suiteResults.values() + .stream() + .forEach(result -> { + ITestContext context + = ((ISuiteResult) result).getTestContext(); - LOGGER.info("Passed tests for suite '" - + suiteName + "' is:" - + context.getPassedTests() - .getAllResults() - .size()); - LOGGER.info("Failed tests for suite '" - + suiteName + "' is:" - + context.getFailedTests() - .getAllResults() - .size()); - LOGGER.info("Skipped tests for suite '" - + suiteName + "' is:" - + context.getSkippedTests() - .getAllResults() - .size()); + LOGGER.info("Passed tests for suite '" + + suiteName + "' is:" + + context.getPassedTests() + .getAllResults() + .size()); + LOGGER.info("Failed tests for suite '" + + suiteName + "' is:" + + context.getFailedTests() + .getAllResults() + .size()); + LOGGER.info("Skipped tests for suite '" + + suiteName + "' is:" + + context.getSkippedTests() + .getAllResults() + .size()); + }); }); - }); } From aedbf7874c0b8dfeaa8a08839db8da2482c0acab Mon Sep 17 00:00:00 2001 From: Felipe Reis Date: Wed, 1 Mar 2017 19:23:26 -0300 Subject: [PATCH 077/112] feat: Create new module 'mockito2' Relates to: BAEL-632 --- mockito2/.gitignore | 14 +++++++ mockito2/README.md | 5 +++ mockito2/pom.xml | 98 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 117 insertions(+) create mode 100644 mockito2/.gitignore create mode 100644 mockito2/README.md create mode 100644 mockito2/pom.xml diff --git a/mockito2/.gitignore b/mockito2/.gitignore new file mode 100644 index 0000000000..7f300600e6 --- /dev/null +++ b/mockito2/.gitignore @@ -0,0 +1,14 @@ +*.class + +.settings +.project + +#folders# +/target +/src/main/webapp/WEB-INF/classes +*/META-INF/* + +# Packaged files # +*.jar +*.war +*.ear diff --git a/mockito2/README.md b/mockito2/README.md new file mode 100644 index 0000000000..587f1341bb --- /dev/null +++ b/mockito2/README.md @@ -0,0 +1,5 @@ +========= + +## Mockito 2 and Java 8 Tips + +Examples on how to leverage Java 8 new features with Mockito version 2 diff --git a/mockito2/pom.xml b/mockito2/pom.xml new file mode 100644 index 0000000000..e116647009 --- /dev/null +++ b/mockito2/pom.xml @@ -0,0 +1,98 @@ + + + 4.0.0 + com.baeldung + mockito2 + 0.0.1-SNAPSHOT + jar + mockito-2-with-java8 + + + + + junit + junit + ${junit.version} + test + + + + org.mockito + mockito-core + ${mockito.version} + test + + + + + + + maven-compiler-plugin + ${maven-compiler-plugin.version} + + 1.8 + 1.8 + + + + org.apache.maven.plugins + maven-surefire-plugin + ${maven-surefire-plugin.version} + + + **/*IntegrationTest.java + **/*LiveTest.java + + + + + + + + + integration + + + + org.apache.maven.plugins + maven-surefire-plugin + + + integration-test + + test + + + + **/*LiveTest.java + + + **/*IntegrationTest.java + + + + + + + json + + + + + + + + + + UTF-8 + + + 4.12 + 2.7.5 + + + 3.6.0 + 2.19.1 + + From 6b6b7c79b4396e6b0be4ef6d29f848924f6af981 Mon Sep 17 00:00:00 2001 From: Felipe Reis Date: Wed, 1 Mar 2017 19:28:56 -0300 Subject: [PATCH 078/112] feat: Create Job Position Model and Services Relates to: BAEL-632 --- .../baeldung/mockito/java8/JobPosition.java | 20 +++++++++++++ .../baeldung/mockito/java8/JobService.java | 20 +++++++++++++ .../com/baeldung/mockito/java8/Person.java | 28 +++++++++++++++++++ .../mockito/java8/UnemploymentService.java | 11 ++++++++ .../java8/UnemploymentServiceImpl.java | 26 +++++++++++++++++ 5 files changed, 105 insertions(+) create mode 100644 mockito2/src/main/java/com/baeldung/mockito/java8/JobPosition.java create mode 100644 mockito2/src/main/java/com/baeldung/mockito/java8/JobService.java create mode 100644 mockito2/src/main/java/com/baeldung/mockito/java8/Person.java create mode 100644 mockito2/src/main/java/com/baeldung/mockito/java8/UnemploymentService.java create mode 100644 mockito2/src/main/java/com/baeldung/mockito/java8/UnemploymentServiceImpl.java diff --git a/mockito2/src/main/java/com/baeldung/mockito/java8/JobPosition.java b/mockito2/src/main/java/com/baeldung/mockito/java8/JobPosition.java new file mode 100644 index 0000000000..c7712fa47e --- /dev/null +++ b/mockito2/src/main/java/com/baeldung/mockito/java8/JobPosition.java @@ -0,0 +1,20 @@ +package com.baeldung.mockito.java8; + +public class JobPosition { + private String title; + + public JobPosition() {} + + public JobPosition(String title) { + super(); + this.title = title; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } +} diff --git a/mockito2/src/main/java/com/baeldung/mockito/java8/JobService.java b/mockito2/src/main/java/com/baeldung/mockito/java8/JobService.java new file mode 100644 index 0000000000..85a59b18a4 --- /dev/null +++ b/mockito2/src/main/java/com/baeldung/mockito/java8/JobService.java @@ -0,0 +1,20 @@ +package com.baeldung.mockito.java8; + +import java.util.Optional; +import java.util.stream.Stream; + +public interface JobService { + Optional findCurrentJobPosition(Person person); + + default boolean assignJobPosition(Person person, JobPosition jobPosition) { + if (!findCurrentJobPosition(person).isPresent()) { + person.setCurrentJobPosition(jobPosition); + + return true; + } else { + return false; + } + } + + Stream listJobs(Person person); +} diff --git a/mockito2/src/main/java/com/baeldung/mockito/java8/Person.java b/mockito2/src/main/java/com/baeldung/mockito/java8/Person.java new file mode 100644 index 0000000000..4f7af49f9d --- /dev/null +++ b/mockito2/src/main/java/com/baeldung/mockito/java8/Person.java @@ -0,0 +1,28 @@ +package com.baeldung.mockito.java8; + +public class Person { + private String name; + private JobPosition currentJobPosition; + + public Person() {} + + public Person(String name) { + this.name = name; + } + + public JobPosition getCurrentJobPosition() { + return currentJobPosition; + } + + public void setCurrentJobPosition(JobPosition currentJobPosition) { + this.currentJobPosition = currentJobPosition; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/mockito2/src/main/java/com/baeldung/mockito/java8/UnemploymentService.java b/mockito2/src/main/java/com/baeldung/mockito/java8/UnemploymentService.java new file mode 100644 index 0000000000..5859ab31b0 --- /dev/null +++ b/mockito2/src/main/java/com/baeldung/mockito/java8/UnemploymentService.java @@ -0,0 +1,11 @@ +package com.baeldung.mockito.java8; + +import java.util.Optional; + +public interface UnemploymentService { + + boolean personIsEntitledToUnemploymentSupport(Person person); + + Optional searchJob(Person person, String searchString); + +} diff --git a/mockito2/src/main/java/com/baeldung/mockito/java8/UnemploymentServiceImpl.java b/mockito2/src/main/java/com/baeldung/mockito/java8/UnemploymentServiceImpl.java new file mode 100644 index 0000000000..6f189b46ea --- /dev/null +++ b/mockito2/src/main/java/com/baeldung/mockito/java8/UnemploymentServiceImpl.java @@ -0,0 +1,26 @@ +package com.baeldung.mockito.java8; + +import java.util.Optional; +import java.util.stream.Stream; + +public class UnemploymentServiceImpl implements UnemploymentService { + private final JobService jobService; + + public UnemploymentServiceImpl(JobService jobService) { + this.jobService = jobService; + } + + @Override + public boolean personIsEntitledToUnemploymentSupport(Person person) { + Optional optional = jobService.findCurrentJobPosition(person); + + return !optional.isPresent(); + } + + @Override + public Optional searchJob(Person person, String searchString) { + Stream stream = jobService.listJobs(person); + + return stream.filter((j) -> j.getTitle().contains(searchString)).findFirst(); + } +} From 4b70b1d5ce7089703287739f7f6b84a4e715b8f8 Mon Sep 17 00:00:00 2001 From: Felipe Reis Date: Wed, 1 Mar 2017 19:29:34 -0300 Subject: [PATCH 079/112] feat: Implement example tests for all scenarios Relates to: BAEL-632 --- .../ArgumentMatcherWithLambdaUnitTest.java | 44 +++++++++++++ .../ArgumentMatcherWithoutLambdaUnitTest.java | 55 ++++++++++++++++ .../java8/CustomAnswerWithLambdaUnitTest.java | 45 ++++++++++++++ .../CustomAnswerWithoutLambdaUnitTest.java | 59 ++++++++++++++++++ .../mockito/java8/JobServiceUnitTest.java | 44 +++++++++++++ .../UnemploymentServiceImplUnitTest.java | 62 +++++++++++++++++++ 6 files changed, 309 insertions(+) create mode 100644 mockito2/src/test/java/com/baeldung/mockito/java8/ArgumentMatcherWithLambdaUnitTest.java create mode 100644 mockito2/src/test/java/com/baeldung/mockito/java8/ArgumentMatcherWithoutLambdaUnitTest.java create mode 100644 mockito2/src/test/java/com/baeldung/mockito/java8/CustomAnswerWithLambdaUnitTest.java create mode 100644 mockito2/src/test/java/com/baeldung/mockito/java8/CustomAnswerWithoutLambdaUnitTest.java create mode 100644 mockito2/src/test/java/com/baeldung/mockito/java8/JobServiceUnitTest.java create mode 100644 mockito2/src/test/java/com/baeldung/mockito/java8/UnemploymentServiceImplUnitTest.java diff --git a/mockito2/src/test/java/com/baeldung/mockito/java8/ArgumentMatcherWithLambdaUnitTest.java b/mockito2/src/test/java/com/baeldung/mockito/java8/ArgumentMatcherWithLambdaUnitTest.java new file mode 100644 index 0000000000..2efac513b7 --- /dev/null +++ b/mockito2/src/test/java/com/baeldung/mockito/java8/ArgumentMatcherWithLambdaUnitTest.java @@ -0,0 +1,44 @@ +package com.baeldung.mockito.java8; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.when; + +import java.util.Optional; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.ArgumentMatchers; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + + +public class ArgumentMatcherWithLambdaUnitTest { + + @InjectMocks + private UnemploymentServiceImpl unemploymentService; + + @Mock + private JobService jobService; + + @Test + public void whenPersonWithJob_thenIsNotEntitled() { + Person peter = new Person("Peter"); + Person linda = new Person("Linda"); + + JobPosition teacher = new JobPosition("Teacher"); + + when(jobService.findCurrentJobPosition( + ArgumentMatchers.argThat((p) -> p.getName().equals("Peter"))) + ).thenReturn(Optional.of(teacher)); + + assertTrue(unemploymentService.personIsEntitledToUnemploymentSupport(linda)); + assertFalse(unemploymentService.personIsEntitledToUnemploymentSupport(peter)); + } + + @Before + public void init() { + MockitoAnnotations.initMocks(this); + } +} diff --git a/mockito2/src/test/java/com/baeldung/mockito/java8/ArgumentMatcherWithoutLambdaUnitTest.java b/mockito2/src/test/java/com/baeldung/mockito/java8/ArgumentMatcherWithoutLambdaUnitTest.java new file mode 100644 index 0000000000..f4e8fb4cf5 --- /dev/null +++ b/mockito2/src/test/java/com/baeldung/mockito/java8/ArgumentMatcherWithoutLambdaUnitTest.java @@ -0,0 +1,55 @@ +package com.baeldung.mockito.java8; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.when; + +import java.util.Optional; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.ArgumentMatchers; +import org.mockito.InjectMocks; +import org.mockito.ArgumentMatcher; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + + +public class ArgumentMatcherWithoutLambdaUnitTest { + @InjectMocks + private UnemploymentServiceImpl unemploymentService; + + @Mock + private JobService jobService; + + @Test + public void whenPersonWithJob_thenIsNotEntitled() { + Person peter = new Person("Peter"); + Person linda = new Person("Linda"); + + JobPosition teacher = new JobPosition("Teacher"); + + when(jobService.findCurrentJobPosition( + ArgumentMatchers.argThat(new PeterArgumentMatcher())) + ).thenReturn(Optional.of(teacher)); + + assertTrue(unemploymentService.personIsEntitledToUnemploymentSupport(linda)); + assertFalse(unemploymentService.personIsEntitledToUnemploymentSupport(peter)); + } + + private class PeterArgumentMatcher implements ArgumentMatcher { + @Override + public boolean matches(Person p) { + + if (p.getName().equals("Peter")) { + return true; + } + return false; + } + } + + @Before + public void init() { + MockitoAnnotations.initMocks(this); + } +} diff --git a/mockito2/src/test/java/com/baeldung/mockito/java8/CustomAnswerWithLambdaUnitTest.java b/mockito2/src/test/java/com/baeldung/mockito/java8/CustomAnswerWithLambdaUnitTest.java new file mode 100644 index 0000000000..eebb86e239 --- /dev/null +++ b/mockito2/src/test/java/com/baeldung/mockito/java8/CustomAnswerWithLambdaUnitTest.java @@ -0,0 +1,45 @@ +package com.baeldung.mockito.java8; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.when; + +import java.util.stream.Stream; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +public class CustomAnswerWithLambdaUnitTest { + @InjectMocks + private UnemploymentServiceImpl unemploymentService; + + @Mock + private JobService jobService; + + @Test + public void whenPersonWithJobHistory_thenSearchReturnsValue() { + Person peter = new Person("Peter"); + + assertEquals("Teacher", unemploymentService.searchJob(peter, "").get().getTitle()); + } + + @Test + public void whenPersonWithNoJobHistory_thenSearchReturnsEmpty() { + Person linda = new Person("Linda"); + + assertFalse(unemploymentService.searchJob(linda, "").isPresent()); + } + + @Before + public void init() { + MockitoAnnotations.initMocks(this); + + when(jobService.listJobs(any(Person.class))).then((i) -> { + return ((Person) i.getArgument(0)).getName().equals("Peter") ? Stream. builder().add(new JobPosition("Teacher")).build() : Stream.empty(); + }); + } +} diff --git a/mockito2/src/test/java/com/baeldung/mockito/java8/CustomAnswerWithoutLambdaUnitTest.java b/mockito2/src/test/java/com/baeldung/mockito/java8/CustomAnswerWithoutLambdaUnitTest.java new file mode 100644 index 0000000000..28922bb303 --- /dev/null +++ b/mockito2/src/test/java/com/baeldung/mockito/java8/CustomAnswerWithoutLambdaUnitTest.java @@ -0,0 +1,59 @@ +package com.baeldung.mockito.java8; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.when; + +import java.util.stream.Stream; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; + + +public class CustomAnswerWithoutLambdaUnitTest { + @InjectMocks + private UnemploymentServiceImpl unemploymentService; + + @Mock + private JobService jobService; + + @Test + public void whenPersonWithJobHistory_thenSearchReturnsValue() { + Person peter = new Person("Peter"); + + assertEquals("Teacher", unemploymentService.searchJob(peter, "").get().getTitle()); + } + + @Test + public void whenPersonWithNoJobHistory_thenSearchReturnsEmpty() { + Person linda = new Person("Linda"); + + assertFalse(unemploymentService.searchJob(linda, "").isPresent()); + } + + private class PersonAnswer implements Answer> { + @Override + public Stream answer(InvocationOnMock invocation) throws Throwable { + Person person = invocation.getArgument(0); + + if(person.getName().equals("Peter")) { + return Stream.builder().add(new JobPosition("Teacher")).build(); + } + + return Stream.empty(); + } + } + + @Before + public void init() { + MockitoAnnotations.initMocks(this); + + when(jobService.listJobs(any(Person.class))).then(new PersonAnswer()); + } +} diff --git a/mockito2/src/test/java/com/baeldung/mockito/java8/JobServiceUnitTest.java b/mockito2/src/test/java/com/baeldung/mockito/java8/JobServiceUnitTest.java new file mode 100644 index 0000000000..9ea5c1db47 --- /dev/null +++ b/mockito2/src/test/java/com/baeldung/mockito/java8/JobServiceUnitTest.java @@ -0,0 +1,44 @@ +package com.baeldung.mockito.java8; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.doCallRealMethod; +import static org.mockito.Mockito.when; + +import java.util.Optional; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; + +public class JobServiceUnitTest { + @Mock + private JobService jobService; + + @Test + public void givenDefaultMethod_whenCallRealMethod_thenNoExceptionIsRaised() { + Person person = new Person(); + + when(jobService.findCurrentJobPosition(person)).thenReturn(Optional.of(new JobPosition())); + doCallRealMethod().when(jobService).assignJobPosition(Mockito.any(Person.class), Mockito.any(JobPosition.class)); + + assertFalse(jobService.assignJobPosition(person, new JobPosition())); + } + + @Test + public void givenReturnIsOfTypeOptional_whenDefaultValueIsReturned_thenValueIsEmpty() { + Person person = new Person(); + + when(jobService.findCurrentJobPosition(person)).thenReturn(Optional.empty()); + doCallRealMethod().when(jobService).assignJobPosition(Mockito.any(Person.class), Mockito.any(JobPosition.class)); + + assertTrue(jobService.assignJobPosition(person, new JobPosition())); + } + + @Before + public void init() { + MockitoAnnotations.initMocks(this); + } +} diff --git a/mockito2/src/test/java/com/baeldung/mockito/java8/UnemploymentServiceImplUnitTest.java b/mockito2/src/test/java/com/baeldung/mockito/java8/UnemploymentServiceImplUnitTest.java new file mode 100644 index 0000000000..b3b71e5bf9 --- /dev/null +++ b/mockito2/src/test/java/com/baeldung/mockito/java8/UnemploymentServiceImplUnitTest.java @@ -0,0 +1,62 @@ +package com.baeldung.mockito.java8; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.when; + +import java.util.Optional; +import java.util.stream.Stream; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +public class UnemploymentServiceImplUnitTest { + @Mock + private JobService jobService; + + @InjectMocks + private UnemploymentServiceImpl unemploymentService; + + @Test + public void givenReturnIsOfTypeOptional_whenMocked_thenValueIsEmpty() { + Person person = new Person(); + + when(jobService.findCurrentJobPosition(any(Person.class))).thenReturn(Optional.empty()); + + assertTrue(unemploymentService.personIsEntitledToUnemploymentSupport(person)); + } + + @Test + public void givenReturnIsOfTypeOptional_whenDefaultValueIsReturned_thenValueIsEmpty() { + Person person = new Person(); + + // This will fail when Mockito 1 is used + assertTrue(unemploymentService.personIsEntitledToUnemploymentSupport(person)); + } + + @Test + public void givenReturnIsOfTypeStream_whenMocked_thenValueIsEmpty() { + Person person = new Person(); + + when(jobService.listJobs(any(Person.class))).thenReturn(Stream.empty()); + + assertFalse(unemploymentService.searchJob(person, "").isPresent()); + } + + @Test + public void givenReturnIsOfTypeStream_whenDefaultValueIsReturned_thenValueIsEmpty() { + Person person = new Person(); + + // This will fail when Mockito 1 is used + assertFalse(unemploymentService.searchJob(person, "").isPresent()); + } + + @Before + public void init() { + MockitoAnnotations.initMocks(this); + } +} From 30a692421703b0cb0282b8da287f12f1cf24b44a Mon Sep 17 00:00:00 2001 From: Felipe Reis Date: Wed, 1 Mar 2017 20:18:20 -0300 Subject: [PATCH 080/112] feat: Add new module, mockito2, to root pom.xml Resolves: BAEL-632 --- pom.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/pom.xml b/pom.xml index 014e4016c5..dfd08926b0 100644 --- a/pom.xml +++ b/pom.xml @@ -84,6 +84,7 @@ metrics mesos-marathon mockito + mockito2 mocks orika From 5421a352abcb081b682e7c62b99e21aaf3fe199b Mon Sep 17 00:00:00 2001 From: KevinGilmore Date: Wed, 1 Mar 2017 22:20:07 -0600 Subject: [PATCH 081/112] BAEL-345: SolrJ (#1263) * BAEL-278: Updated README.md * BAEL-554: Add and update README.md files * BAEL-345: fixed assertion * BAEL-109: Updated README.md --- .../java/com/baeldung/solrjava/SolrJavaIntegrationTest.java | 2 +- spring-data-rest/README.md | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/apache-solrj/src/test/java/com/baeldung/solrjava/SolrJavaIntegrationTest.java b/apache-solrj/src/test/java/com/baeldung/solrjava/SolrJavaIntegrationTest.java index 7f4599a91d..8b5fe77c6f 100644 --- a/apache-solrj/src/test/java/com/baeldung/solrjava/SolrJavaIntegrationTest.java +++ b/apache-solrj/src/test/java/com/baeldung/solrjava/SolrJavaIntegrationTest.java @@ -33,7 +33,7 @@ public class SolrJavaIntegrationTest { response = solrJavaIntegration.getSolrClient().query(query); SolrDocumentList docList = response.getResults(); - assertEquals(docList.getNumFound(), 1); + assertEquals(1, docList.getNumFound()); for (SolrDocument doc : docList) { assertEquals("Kenmore Dishwasher", (String) doc.getFieldValue("name")); diff --git a/spring-data-rest/README.md b/spring-data-rest/README.md index 4e8828a688..f77c4dc202 100644 --- a/spring-data-rest/README.md +++ b/spring-data-rest/README.md @@ -15,3 +15,4 @@ To view the running application, visit [http://localhost:8080](http://localhost: ###Relevant Articles: - [Guide to Spring Data REST Validators](http://www.baeldung.com/spring-data-rest-validators) +- [Working with Relationships in Spring Data REST](http://www.baeldung.com/spring-data-rest-relationships) From 41bf9ddd6d30cc1c767e6cd42be509df0b500b4f Mon Sep 17 00:00:00 2001 From: Adam InTae Gerard Date: Wed, 1 Mar 2017 22:29:55 -0600 Subject: [PATCH 082/112] BAEL-9 Final (#1262) * BAEL-9 #3 * pom.xml fix * Final --- pom.xml | 2 - 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 - .../src/main/resources/application.properties | 10 ---- spring-boot/README.MD | 1 + spring-boot/pom.xml | 18 ++++++ .../baeldung/servlets}/ApplicationMain.java | 4 +- .../configuration/WebAppInitializer.java | 4 +- .../configuration/WebMvcConfigure.java | 3 +- .../baeldung/servlets}/props/Constants.java | 2 +- .../servlets}/props/PropertyLoader.java | 2 +- .../props/PropertySourcesLoader.java | 2 +- .../servlets/GenericCustomServlet.java | 2 +- .../servlets/javaee/AnnotationServlet.java | 2 +- .../servlets/javaee/EEWebXmlServlet.java | 4 +- .../SpringRegistrationBeanServlet.java | 6 +- .../embedded/EmbeddedTomcatExample.java | 2 +- .../src/main/resources/application.properties | 6 +- .../src/main/resources/custom.properties | 0 .../src/main/webapp/WEB-INF/context.xml | 0 .../src/main/webapp/WEB-INF/dispatcher.xml | 0 .../src/main/webapp/WEB-INF/web.xml | 0 .../src/main/webapp/annotationservlet.jsp | 0 .../src/main/webapp/index.jsp | 0 26 files changed, 39 insertions(+), 94 deletions(-) delete mode 100644 spring-boot-servlet/.gitignore delete mode 100644 spring-boot-servlet/README.md delete mode 100644 spring-boot-servlet/pom.xml delete mode 100644 spring-boot-servlet/src/main/java/META-INF/MANIFEST.MF delete mode 100644 spring-boot-servlet/src/main/resources/application.properties rename {spring-boot-servlet/src/main/java/com/baeldung => spring-boot/src/main/java/com/baeldung/servlets}/ApplicationMain.java (95%) rename {spring-boot-servlet/src/main/java/com/baeldung => spring-boot/src/main/java/com/baeldung/servlets}/configuration/WebAppInitializer.java (96%) rename {spring-boot-servlet/src/main/java/com/baeldung => spring-boot/src/main/java/com/baeldung/servlets}/configuration/WebMvcConfigure.java (97%) rename {spring-boot-servlet/src/main/java/com/baeldung => spring-boot/src/main/java/com/baeldung/servlets}/props/Constants.java (95%) rename {spring-boot-servlet/src/main/java/com/baeldung => spring-boot/src/main/java/com/baeldung/servlets}/props/PropertyLoader.java (94%) rename {spring-boot-servlet/src/main/java/com/baeldung => spring-boot/src/main/java/com/baeldung/servlets}/props/PropertySourcesLoader.java (95%) rename {spring-boot-servlet/src/main/java/com/baeldung => spring-boot/src/main/java/com/baeldung/servlets}/servlets/GenericCustomServlet.java (93%) rename {spring-boot-servlet/src/main/java/com/baeldung => spring-boot/src/main/java/com/baeldung/servlets}/servlets/javaee/AnnotationServlet.java (93%) rename {spring-boot-servlet/src/main/java/com/baeldung => spring-boot/src/main/java/com/baeldung/servlets}/servlets/javaee/EEWebXmlServlet.java (92%) rename {spring-boot-servlet/src/main/java/com/baeldung => spring-boot/src/main/java/com/baeldung/servlets}/servlets/springboot/SpringRegistrationBeanServlet.java (82%) rename {spring-boot-servlet/src/main/java/com/baeldung => spring-boot/src/main/java/com/baeldung/servlets}/servlets/springboot/embedded/EmbeddedTomcatExample.java (90%) rename {spring-boot-servlet => spring-boot}/src/main/resources/custom.properties (100%) rename {spring-boot-servlet => spring-boot}/src/main/webapp/WEB-INF/context.xml (100%) rename {spring-boot-servlet => spring-boot}/src/main/webapp/WEB-INF/dispatcher.xml (100%) rename {spring-boot-servlet => spring-boot}/src/main/webapp/WEB-INF/web.xml (100%) rename {spring-boot-servlet => spring-boot}/src/main/webapp/annotationservlet.jsp (100%) rename {spring-boot-servlet => spring-boot}/src/main/webapp/index.jsp (100%) diff --git a/pom.xml b/pom.xml index 014e4016c5..a392c142d0 100644 --- a/pom.xml +++ b/pom.xml @@ -94,7 +94,6 @@ querydsl - reactor-core redis rest-assured rest-testing @@ -111,7 +110,6 @@ 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 deleted file mode 100644 index 60be5b80aa..0000000000 --- a/spring-boot-servlet/.gitignore +++ /dev/null @@ -1,4 +0,0 @@ -/target/ -.settings/ -.classpath -.project diff --git a/spring-boot-servlet/README.md b/spring-boot-servlet/README.md deleted file mode 100644 index 262a11fc36..0000000000 --- a/spring-boot-servlet/README.md +++ /dev/null @@ -1,2 +0,0 @@ -###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 deleted file mode 100644 index 3818e3468f..0000000000 --- a/spring-boot-servlet/pom.xml +++ /dev/null @@ -1,55 +0,0 @@ - - 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 deleted file mode 100644 index 69ebae1751..0000000000 --- a/spring-boot-servlet/src/main/java/META-INF/MANIFEST.MF +++ /dev/null @@ -1,2 +0,0 @@ -Manifest-Version: 1.0 -Main-Class: com.baeldung.ApplicationMain diff --git a/spring-boot-servlet/src/main/resources/application.properties b/spring-boot-servlet/src/main/resources/application.properties deleted file mode 100644 index 4e9e2b4cf1..0000000000 --- a/spring-boot-servlet/src/main/resources/application.properties +++ /dev/null @@ -1,10 +0,0 @@ -#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/README.MD b/spring-boot/README.MD index d0a02c69fc..78ef3c843c 100644 --- a/spring-boot/README.MD +++ b/spring-boot/README.MD @@ -10,3 +10,4 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring - [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) +- [How to Register a Servlet in a Java Web Application](http://www.baeldung.com/how-to-register-a-servlet-in-a-java-web-application/) diff --git a/spring-boot/pom.xml b/spring-boot/pom.xml index e77ab10aff..65b0f247f8 100644 --- a/spring-boot/pom.xml +++ b/spring-boot/pom.xml @@ -41,6 +41,24 @@ spring-boot-starter-security + + 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} + + io.dropwizard.metrics metrics-core diff --git a/spring-boot-servlet/src/main/java/com/baeldung/ApplicationMain.java b/spring-boot/src/main/java/com/baeldung/servlets/ApplicationMain.java similarity index 95% rename from spring-boot-servlet/src/main/java/com/baeldung/ApplicationMain.java rename to spring-boot/src/main/java/com/baeldung/servlets/ApplicationMain.java index 66f2e85999..a6ea3757fe 100644 --- a/spring-boot-servlet/src/main/java/com/baeldung/ApplicationMain.java +++ b/spring-boot/src/main/java/com/baeldung/servlets/ApplicationMain.java @@ -1,4 +1,4 @@ -package com.baeldung; +package com.baeldung.servlets; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @@ -16,4 +16,4 @@ public class ApplicationMain extends SpringBootServletInitializer { 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/src/main/java/com/baeldung/servlets/configuration/WebAppInitializer.java similarity index 96% rename from spring-boot-servlet/src/main/java/com/baeldung/configuration/WebAppInitializer.java rename to spring-boot/src/main/java/com/baeldung/servlets/configuration/WebAppInitializer.java index b7e22500f4..eadd40355a 100644 --- a/spring-boot-servlet/src/main/java/com/baeldung/configuration/WebAppInitializer.java +++ b/spring-boot/src/main/java/com/baeldung/servlets/configuration/WebAppInitializer.java @@ -1,4 +1,4 @@ -package com.baeldung.configuration; +package com.baeldung.servlets.configuration; import org.springframework.web.WebApplicationInitializer; import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; @@ -29,4 +29,4 @@ public class WebAppInitializer implements WebApplicationInitializer { 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/src/main/java/com/baeldung/servlets/configuration/WebMvcConfigure.java similarity index 97% rename from spring-boot-servlet/src/main/java/com/baeldung/configuration/WebMvcConfigure.java rename to spring-boot/src/main/java/com/baeldung/servlets/configuration/WebMvcConfigure.java index de9067de6e..3d6a10c2ac 100644 --- a/spring-boot-servlet/src/main/java/com/baeldung/configuration/WebMvcConfigure.java +++ b/spring-boot/src/main/java/com/baeldung/servlets/configuration/WebMvcConfigure.java @@ -1,4 +1,4 @@ -package com.baeldung.configuration; +package com.baeldung.servlets.configuration; import org.springframework.boot.web.support.ErrorPageFilter; import org.springframework.context.annotation.Bean; @@ -37,4 +37,3 @@ public class WebMvcConfigure extends WebMvcConfigurerAdapter { return new ErrorPageFilter(); } } - diff --git a/spring-boot-servlet/src/main/java/com/baeldung/props/Constants.java b/spring-boot/src/main/java/com/baeldung/servlets/props/Constants.java similarity index 95% rename from spring-boot-servlet/src/main/java/com/baeldung/props/Constants.java rename to spring-boot/src/main/java/com/baeldung/servlets/props/Constants.java index 421401eec7..6345d1f969 100644 --- a/spring-boot-servlet/src/main/java/com/baeldung/props/Constants.java +++ b/spring-boot/src/main/java/com/baeldung/servlets/props/Constants.java @@ -1,4 +1,4 @@ -package com.baeldung.props; +package com.baeldung.servlets.props; import org.springframework.beans.factory.annotation.Autowired; diff --git a/spring-boot-servlet/src/main/java/com/baeldung/props/PropertyLoader.java b/spring-boot/src/main/java/com/baeldung/servlets/props/PropertyLoader.java similarity index 94% rename from spring-boot-servlet/src/main/java/com/baeldung/props/PropertyLoader.java rename to spring-boot/src/main/java/com/baeldung/servlets/props/PropertyLoader.java index 5d890d96fa..c29da45929 100644 --- a/spring-boot-servlet/src/main/java/com/baeldung/props/PropertyLoader.java +++ b/spring-boot/src/main/java/com/baeldung/servlets/props/PropertyLoader.java @@ -1,4 +1,4 @@ -package com.baeldung.props; +package com.baeldung.servlets.props; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/spring-boot-servlet/src/main/java/com/baeldung/props/PropertySourcesLoader.java b/spring-boot/src/main/java/com/baeldung/servlets/props/PropertySourcesLoader.java similarity index 95% rename from spring-boot-servlet/src/main/java/com/baeldung/props/PropertySourcesLoader.java rename to spring-boot/src/main/java/com/baeldung/servlets/props/PropertySourcesLoader.java index 8c7b3a4af5..56a6751326 100644 --- a/spring-boot-servlet/src/main/java/com/baeldung/props/PropertySourcesLoader.java +++ b/spring-boot/src/main/java/com/baeldung/servlets/props/PropertySourcesLoader.java @@ -1,4 +1,4 @@ -package com.baeldung.props; +package com.baeldung.servlets.props; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/spring-boot-servlet/src/main/java/com/baeldung/servlets/GenericCustomServlet.java b/spring-boot/src/main/java/com/baeldung/servlets/servlets/GenericCustomServlet.java similarity index 93% rename from spring-boot-servlet/src/main/java/com/baeldung/servlets/GenericCustomServlet.java rename to spring-boot/src/main/java/com/baeldung/servlets/servlets/GenericCustomServlet.java index c6543c9eef..49dd9404b7 100644 --- a/spring-boot-servlet/src/main/java/com/baeldung/servlets/GenericCustomServlet.java +++ b/spring-boot/src/main/java/com/baeldung/servlets/servlets/GenericCustomServlet.java @@ -1,4 +1,4 @@ -package com.baeldung.servlets; +package com.baeldung.servlets.servlets; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; diff --git a/spring-boot-servlet/src/main/java/com/baeldung/servlets/javaee/AnnotationServlet.java b/spring-boot/src/main/java/com/baeldung/servlets/servlets/javaee/AnnotationServlet.java similarity index 93% rename from spring-boot-servlet/src/main/java/com/baeldung/servlets/javaee/AnnotationServlet.java rename to spring-boot/src/main/java/com/baeldung/servlets/servlets/javaee/AnnotationServlet.java index d971e68cfa..b50a7d5454 100644 --- a/spring-boot-servlet/src/main/java/com/baeldung/servlets/javaee/AnnotationServlet.java +++ b/spring-boot/src/main/java/com/baeldung/servlets/servlets/javaee/AnnotationServlet.java @@ -1,4 +1,4 @@ -package com.baeldung.servlets.javaee; +package com.baeldung.servlets.servlets.javaee; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; diff --git a/spring-boot-servlet/src/main/java/com/baeldung/servlets/javaee/EEWebXmlServlet.java b/spring-boot/src/main/java/com/baeldung/servlets/servlets/javaee/EEWebXmlServlet.java similarity index 92% rename from spring-boot-servlet/src/main/java/com/baeldung/servlets/javaee/EEWebXmlServlet.java rename to spring-boot/src/main/java/com/baeldung/servlets/servlets/javaee/EEWebXmlServlet.java index 4209e815cd..c7b373064f 100644 --- a/spring-boot-servlet/src/main/java/com/baeldung/servlets/javaee/EEWebXmlServlet.java +++ b/spring-boot/src/main/java/com/baeldung/servlets/servlets/javaee/EEWebXmlServlet.java @@ -1,4 +1,4 @@ -package com.baeldung.servlets.javaee; +package com.baeldung.servlets.servlets.javaee; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; @@ -17,4 +17,4 @@ public class EEWebXmlServlet extends HttpServlet { 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/src/main/java/com/baeldung/servlets/servlets/springboot/SpringRegistrationBeanServlet.java similarity index 82% rename from spring-boot-servlet/src/main/java/com/baeldung/servlets/springboot/SpringRegistrationBeanServlet.java rename to spring-boot/src/main/java/com/baeldung/servlets/servlets/springboot/SpringRegistrationBeanServlet.java index 4a34465894..e3c225d429 100644 --- a/spring-boot-servlet/src/main/java/com/baeldung/servlets/springboot/SpringRegistrationBeanServlet.java +++ b/spring-boot/src/main/java/com/baeldung/servlets/servlets/springboot/SpringRegistrationBeanServlet.java @@ -1,6 +1,6 @@ -package com.baeldung.servlets.springboot; +package com.baeldung.servlets.servlets.springboot; -import com.baeldung.servlets.GenericCustomServlet; +import com.baeldung.servlets.servlets.GenericCustomServlet; import org.springframework.boot.web.servlet.ServletRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -15,5 +15,3 @@ public class SpringRegistrationBeanServlet { return bean; } } - - diff --git a/spring-boot-servlet/src/main/java/com/baeldung/servlets/springboot/embedded/EmbeddedTomcatExample.java b/spring-boot/src/main/java/com/baeldung/servlets/servlets/springboot/embedded/EmbeddedTomcatExample.java similarity index 90% rename from spring-boot-servlet/src/main/java/com/baeldung/servlets/springboot/embedded/EmbeddedTomcatExample.java rename to spring-boot/src/main/java/com/baeldung/servlets/servlets/springboot/embedded/EmbeddedTomcatExample.java index b2458f33c7..9e460d03a8 100644 --- a/spring-boot-servlet/src/main/java/com/baeldung/servlets/springboot/embedded/EmbeddedTomcatExample.java +++ b/spring-boot/src/main/java/com/baeldung/servlets/servlets/springboot/embedded/EmbeddedTomcatExample.java @@ -1,4 +1,4 @@ -package com.baeldung.servlets.springboot.embedded; +package com.baeldung.servlets.servlets.springboot.embedded; import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory; import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory; diff --git a/spring-boot/src/main/resources/application.properties b/spring-boot/src/main/resources/application.properties index d30045d1dc..8c6549f53d 100644 --- a/spring-boot/src/main/resources/application.properties +++ b/spring-boot/src/main/resources/application.properties @@ -28,4 +28,8 @@ security.user.name=admin1 security.user.password=secret1 management.security.role=SUPERUSER -logging.level.org.springframework=INFO \ No newline at end of file +logging.level.org.springframework=INFO + +#Servlet Configuration +servlet.name=dispatcherExample +servlet.mapping=/dispatcherExampleURL diff --git a/spring-boot-servlet/src/main/resources/custom.properties b/spring-boot/src/main/resources/custom.properties similarity index 100% rename from spring-boot-servlet/src/main/resources/custom.properties rename to spring-boot/src/main/resources/custom.properties diff --git a/spring-boot-servlet/src/main/webapp/WEB-INF/context.xml b/spring-boot/src/main/webapp/WEB-INF/context.xml similarity index 100% rename from spring-boot-servlet/src/main/webapp/WEB-INF/context.xml rename to spring-boot/src/main/webapp/WEB-INF/context.xml diff --git a/spring-boot-servlet/src/main/webapp/WEB-INF/dispatcher.xml b/spring-boot/src/main/webapp/WEB-INF/dispatcher.xml similarity index 100% rename from spring-boot-servlet/src/main/webapp/WEB-INF/dispatcher.xml rename to spring-boot/src/main/webapp/WEB-INF/dispatcher.xml diff --git a/spring-boot-servlet/src/main/webapp/WEB-INF/web.xml b/spring-boot/src/main/webapp/WEB-INF/web.xml similarity index 100% rename from spring-boot-servlet/src/main/webapp/WEB-INF/web.xml rename to spring-boot/src/main/webapp/WEB-INF/web.xml diff --git a/spring-boot-servlet/src/main/webapp/annotationservlet.jsp b/spring-boot/src/main/webapp/annotationservlet.jsp similarity index 100% rename from spring-boot-servlet/src/main/webapp/annotationservlet.jsp rename to spring-boot/src/main/webapp/annotationservlet.jsp diff --git a/spring-boot-servlet/src/main/webapp/index.jsp b/spring-boot/src/main/webapp/index.jsp similarity index 100% rename from spring-boot-servlet/src/main/webapp/index.jsp rename to spring-boot/src/main/webapp/index.jsp From 73e195b190a58899a24fbbf699a67ac1081e0524 Mon Sep 17 00:00:00 2001 From: slavisa-baeldung Date: Thu, 2 Mar 2017 05:49:21 +0100 Subject: [PATCH 083/112] BAEL-574 - Removing obsolete test --- .../bootstrap/gateway/IntegrationTest.java | 201 ------------------ 1 file changed, 201 deletions(-) delete mode 100644 spring-cloud/spring-cloud-bootstrap/gateway/src/test/java/com/baeldung/spring/cloud/bootstrap/gateway/IntegrationTest.java 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 deleted file mode 100644 index 16057edc48..0000000000 --- a/spring-cloud/spring-cloud-bootstrap/gateway/src/test/java/com/baeldung/spring/cloud/bootstrap/gateway/IntegrationTest.java +++ /dev/null @@ -1,201 +0,0 @@ -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 From d99c87777f1e9d8cfa61e70894c84e5e80678cca Mon Sep 17 00:00:00 2001 From: Tomasz Lelek Date: Thu, 2 Mar 2017 10:53:24 +0100 Subject: [PATCH 084/112] BAEL-311 add exampe of one way encryption --- .../java/org/baeldung/jasypt/JasyptTest.java | 38 +++++++++++++++++-- 1 file changed, 34 insertions(+), 4 deletions(-) diff --git a/jasypt/src/test/java/org/baeldung/jasypt/JasyptTest.java b/jasypt/src/test/java/org/baeldung/jasypt/JasyptTest.java index d05b18171d..c4bed5de83 100644 --- a/jasypt/src/test/java/org/baeldung/jasypt/JasyptTest.java +++ b/jasypt/src/test/java/org/baeldung/jasypt/JasyptTest.java @@ -3,21 +3,24 @@ package org.baeldung.jasypt; import org.jasypt.encryption.pbe.PooledPBEStringEncryptor; import org.jasypt.encryption.pbe.StandardPBEStringEncryptor; +import org.jasypt.util.password.BasicPasswordEncryptor; import org.jasypt.util.text.BasicTextEncryptor; import org.junit.Ignore; import org.junit.Test; +import static junit.framework.Assert.assertFalse; import static junit.framework.Assert.assertNotSame; +import static junit.framework.Assert.assertTrue; import static junit.framework.TestCase.assertEquals; public class JasyptTest { @Test - public void givenTextPassword_whenDecrypt_shouldCompareToEncrypted() { + public void givenTextPassword_whenDecrypt_thenCompareToEncrypted() { //given BasicTextEncryptor textEncryptor = new BasicTextEncryptor(); String password = "secret-pass"; - textEncryptor.setPasswordCharArray(password.toCharArray()); + textEncryptor.setPasswordCharArray("some-random-password".toCharArray()); //when String myEncryptedText = textEncryptor.encrypt(password); @@ -28,10 +31,37 @@ public class JasyptTest { assertEquals(plainText, password); } + @Test + public void givenTextPassword_whenOneWayEncryption_thenCompareEncryptedPasswordsShouldBeSame(){ + String password = "secret-pass"; + BasicPasswordEncryptor passwordEncryptor = new BasicPasswordEncryptor(); + String encryptedPassword = passwordEncryptor.encryptPassword(password); + + //when + boolean result = passwordEncryptor.checkPassword("secret-pass", encryptedPassword); + + //then + assertTrue(result); + } + + @Test + public void givenTextPassword_whenOneWayEncryption_thenCompareEncryptedPasswordsShouldNotBeSame(){ + String password = "secret-pass"; + BasicPasswordEncryptor passwordEncryptor = new BasicPasswordEncryptor(); + String encryptedPassword = passwordEncryptor.encryptPassword(password); + + //when + boolean result = passwordEncryptor.checkPassword("secret-pass-not-same", encryptedPassword); + + //then + assertFalse(result); + } + + @Test @Ignore("should have installed local_policy.jar") - public void givenTextPassword_whenDecrypt_shouldCompareToEncryptedWithCustomAlgorithm() { + public void givenTextPassword_whenDecrypt_thenCompareToEncryptedWithCustomAlgorithm() { //given StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor(); String password = "secret-pass"; @@ -49,7 +79,7 @@ public class JasyptTest { @Test @Ignore("should have installed local_policy.jar") - public void givenTextPassword_whenDecryptOnHighPerformance_shouldDecrypt(){ + public void givenTextPassword_whenDecryptOnHighPerformance_thenDecrypt(){ //given String password = "secret-pass"; PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor(); From 08c63209efce2ebed5355cfa7c55d3af68c4203b Mon Sep 17 00:00:00 2001 From: KevinGilmore Date: Thu, 2 Mar 2017 09:11:46 -0600 Subject: [PATCH 085/112] BAEL-345: README.md (#1280) * BAEL-278: Updated README.md * BAEL-554: Add and update README.md files * BAEL-345: fixed assertion * BAEL-109: Updated README.md * BAEL-345: Added README.md --- apache-solrj/README.md | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 apache-solrj/README.md diff --git a/apache-solrj/README.md b/apache-solrj/README.md new file mode 100644 index 0000000000..7a32becb64 --- /dev/null +++ b/apache-solrj/README.md @@ -0,0 +1,4 @@ +## Apache Solrj Tutorials Project + +### Relevant Articles +- [Guide to Solr in Java with Apache Solrj](http://www.baeldung.com/apache-solrj) From f1322a34a6da51b0a93b90637ee790b5dcf08d47 Mon Sep 17 00:00:00 2001 From: KevinGilmore Date: Thu, 2 Mar 2017 09:49:46 -0600 Subject: [PATCH 086/112] Reinstate module reactor-core in main pom (#1283) * BAEL-278: Updated README.md * BAEL-554: Add and update README.md files * BAEL-345: fixed assertion * BAEL-109: Updated README.md * BAEL-345: Added README.md * Reinstating reactor-core module in root-level pom --- pom.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/pom.xml b/pom.xml index 16dc0a50d0..b0156a4289 100644 --- a/pom.xml +++ b/pom.xml @@ -95,6 +95,7 @@ querydsl + reactor-core redis rest-assured rest-testing From 1df0503b202f6615f0870bbfe843597ec9596519 Mon Sep 17 00:00:00 2001 From: root Date: Thu, 2 Mar 2017 12:06:22 -0400 Subject: [PATCH 087/112] Java Money & Currency --- .../.resourceCache/ECBCurrentRateProvider.dat | 60 ++--- .../ECBHistoric90RateProvider.dat | 8 +- .../IMFHistoricRateProvider.dat | 204 ++++++++--------- core-java/.resourceCache/IMFRateProvider.dat | 204 ++++++++--------- .../com/baeldung/money/JavaMoneyTest.java | 208 +++++++++++++----- 5 files changed, 396 insertions(+), 288 deletions(-) diff --git a/core-java/.resourceCache/ECBCurrentRateProvider.dat b/core-java/.resourceCache/ECBCurrentRateProvider.dat index 45cf516ac8..69eed9a0d8 100644 --- a/core-java/.resourceCache/ECBCurrentRateProvider.dat +++ b/core-java/.resourceCache/ECBCurrentRateProvider.dat @@ -5,38 +5,38 @@ European Central Bank - - - + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/core-java/.resourceCache/ECBHistoric90RateProvider.dat b/core-java/.resourceCache/ECBHistoric90RateProvider.dat index f51c2eacfa..157cac1a6a 100644 --- a/core-java/.resourceCache/ECBHistoric90RateProvider.dat +++ b/core-java/.resourceCache/ECBHistoric90RateProvider.dat @@ -1,4 +1,6 @@ -Reference ratesEuropean Central Bank +Reference ratesEuropean Central Bank + + @@ -58,6 +60,4 @@ - - - \ No newline at end of file + \ No newline at end of file diff --git a/core-java/.resourceCache/IMFHistoricRateProvider.dat b/core-java/.resourceCache/IMFHistoricRateProvider.dat index 4c98b3b04e..12809d35af 100644 --- a/core-java/.resourceCache/IMFHistoricRateProvider.dat +++ b/core-java/.resourceCache/IMFHistoricRateProvider.dat @@ -2,113 +2,113 @@ SDRs per Currency unit and Currency units per SDR (1) last five days SDRs per Currency unit (2) -Currency February 27, 2017 February 24, 2017 February 23, 2017 February 22, 2017 February 21, 2017 -Chinese Yuan 0.1075530000 0.1074200000 0.1076250000 0.1077740000 0.1075920000 -Euro 0.7826360000 0.7829640000 0.7824120000 0.7793410000 0.7806080000 -Japanese Yen 0.0065915500 0.0065398200 0.0065279600 0.0065267800 0.0065271000 -U.K. Pound Sterling 0.9178430000 0.9263250000 0.9233090000 0.9217470000 0.9201430000 -U.S. Dollar 0.7392420000 0.7380190000 0.7400090000 0.7413120000 0.7408260000 -Algerian Dinar 0.0067056800 0.0067170500 0.0067108200 0.0067122300 -Australian Dollar 0.5695290000 0.5688450000 0.5701430000 0.5680650000 -Bahrain Dinar 1.9628200000 1.9681100000 1.9715700000 1.9702800000 -Botswana Pula 0.0712926000 0.0714849000 0.0710918000 0.0708970000 -Brazilian Real 0.2409310000 0.2401220000 0.2394110000 0.2396560000 -Brunei Dollar 0.5253930000 0.5230480000 0.5224920000 0.5214510000 -Canadian Dollar 0.5632010000 0.5645050000 0.5621540000 0.5634090000 -Chilean Peso 0.0011525100 0.0011535400 0.0011517700 0.0011535600 -Colombian Peso 0.0002570000 0.0002557440 0.0002553890 0.0002552100 -Czech Koruna 0.0289783000 0.0289564000 0.0288448000 0.0288889000 -Danish Krone 0.1053170000 0.1052550000 0.1048460000 0.1050150000 -Hungarian Forint 0.0025337100 0.0025343600 0.0025357900 0.0025387300 -Icelandic Krona 0.0068082900 0.0067593100 0.0067227000 0.0066885700 -Indian Rupee 0.0110722000 0.0110702000 -Indonesian Rupiah 0.0000553404 0.0000553899 0.0000555040 0.0000554096 -Iranian Rial 0.0000228786 0.0000228671 -Israeli New Sheqel 0.1995720000 0.1995710000 0.1998150000 0.1998450000 -Kazakhstani Tenge 0.0023693200 0.0023668900 0.0023601900 0.0023350800 -Korean Won 0.0006480670 0.0006486190 0.0006464180 0.0006455440 -Kuwaiti Dinar 2.4165700000 2.4230800000 2.4273500000 2.4257600000 +Currency March 02, 2017 March 01, 2017 February 28, 2017 February 27, 2017 February 24, 2017 +Chinese Yuan 0.1078010000 0.1075250000 0.1075530000 0.1074200000 +Euro 0.7811130000 0.7827060000 0.7826360000 0.7829640000 +Japanese Yen 0.0065627100 0.0065625100 0.0065915500 0.0065398200 +U.K. Pound Sterling 0.9132630000 0.9188320000 0.9178430000 0.9263250000 +U.S. Dollar 0.7415860000 0.7386110000 0.7392420000 0.7380190000 +Algerian Dinar 0.0067127200 0.0067093100 0.0067141700 0.0067056800 +Australian Dollar 0.5677580000 0.5678440000 0.5688470000 0.5695290000 +Bahrain Dinar 1.9723000000 1.9643900000 1.9660700000 1.9628200000 +Botswana Pula 0.0711181000 0.0711282000 0.0712629000 0.0712926000 +Brazilian Real 0.2393220000 0.2383620000 0.2385650000 0.2409310000 +Brunei Dollar 0.5269940000 0.5260760000 0.5261880000 0.5253930000 +Canadian Dollar 0.5575260000 0.5642640000 0.5632010000 +Chilean Peso 0.0011428700 0.0011448000 0.0011449400 0.0011525100 +Colombian Peso 0.0002540400 0.0002550210 0.0002561010 0.0002570000 +Czech Koruna 0.0289072000 0.0289685000 0.0289649000 0.0289783000 +Danish Krone 0.1050820000 0.1052990000 0.1052900000 0.1053170000 +Hungarian Forint 0.0025403700 0.0025419400 0.0025407000 0.0025337100 +Icelandic Krona 0.0069632500 0.0069197200 0.0068709200 0.0068082900 +Indian Rupee 0.0110936000 0.0110674000 0.0110790000 +Indonesian Rupiah 0.0000555038 0.0000553391 0.0000554196 0.0000553404 +Iranian Rial 0.0000228835 0.0000227945 0.0000228133 +Israeli New Sheqel 0.2041810000 0.2018610000 0.2009360000 0.1995720000 +Kazakhstani Tenge 0.0023615900 0.0023658000 0.0023693200 +Korean Won 0.0006524260 0.0006536180 0.0006480670 +Kuwaiti Dinar 2.4274500000 2.4193000000 2.4213600000 2.4165700000 Libyan Dinar 0.5174910000 0.5174910000 0.5174910000 0.5174910000 -Malaysian Ringgit 0.1659960000 0.1662940000 0.1663250000 0.1661230000 -Mauritian Rupee 0.0208418000 -Mexican Peso 0.0372107000 0.0375618000 0.0372281000 0.0362282000 -Nepalese Rupee 0.0069012400 0.0069030700 0.0069229700 0.0069139200 -New Zealand Dollar 0.5331450000 0.5322880000 0.5307790000 0.5306540000 -Norwegian Krone 0.0886062000 0.0888398000 0.0884082000 0.0885668000 -Rial Omani 1.9194300000 1.9246000000 1.9279900000 1.9267300000 -Pakistani Rupee 0.0070384300 0.0070575400 0.0070700400 0.0070658600 -Nuevo Sol 0.2283980000 0.2287290000 0.2281570000 -Philippine Peso 0.0146922000 0.0147295000 0.0147363000 0.0147628000 -Polish Zloty 0.1814160000 0.1811970000 0.1812540000 0.1809450000 -Qatar Riyal 0.2027520000 0.2032990000 0.2036570000 0.2035240000 -Russian Ruble 0.0128977000 0.0128040000 -Saudi Arabian Riyal 0.1968050000 0.1973360000 0.1976830000 0.1975540000 -Singapore Dollar 0.5253930000 0.5230480000 0.5224920000 0.5214510000 -South African Rand 0.0569347000 0.0570889000 0.0566127000 0.0562263000 -Sri Lanka Rupee 0.0048931100 0.0049014800 0.0049020900 -Swedish Krona 0.0822544000 0.0824642000 0.0823543000 0.0824973000 -Swiss Franc 0.7345670000 0.7331180000 0.7312210000 0.7337090000 -Thai Baht 0.0211098000 0.0211467000 0.0211707000 0.0211393000 -Trinidad And Tobago Dollar 0.1092030000 0.1091300000 0.1093820000 0.1095510000 -Tunisian Dinar 0.3206630000 0.3204540000 0.3225790000 0.3234230000 -U.A.E. Dirham 0.2009580000 0.2015000000 0.2018550000 0.2017230000 -Peso Uruguayo 0.0259816000 0.0261108000 0.0261517000 -Bolivar Fuerte 0.0739869000 0.0741864000 +Malaysian Ringgit 0.1667420000 0.1661670000 0.1663840000 0.1659960000 +Mauritian Rupee 0.0208473000 0.0207651000 0.0207673000 +Mexican Peso 0.0369385000 0.0372748000 0.0372107000 +Nepalese Rupee 0.0069436900 0.0069210200 0.0069133300 0.0069012400 +New Zealand Dollar 0.5305310000 0.5304700000 0.5318850000 0.5331450000 +Norwegian Krone 0.0881434000 0.0882493000 0.0885500000 0.0886062000 +Rial Omani 1.9287000000 1.9209600000 1.9226100000 1.9194300000 +Pakistani Rupee 0.0070732900 0.0070443600 0.0070502500 0.0070384300 +Nuevo Sol 0.2266370000 0.2274590000 0.2271530000 +Philippine Peso 0.0147565000 0.0146938000 0.0147268000 0.0146922000 +Polish Zloty 0.1819580000 0.1811650000 0.1814310000 0.1814160000 +Qatar Riyal 0.2037320000 0.2029150000 0.2030880000 0.2027520000 +Russian Ruble 0.0127033000 0.0127429000 0.0127594000 +Saudi Arabian Riyal 0.1977560000 0.1969630000 0.1971310000 0.1968050000 +Singapore Dollar 0.5269940000 0.5260760000 0.5261880000 0.5253930000 +South African Rand 0.0567982000 0.0567078000 0.0569908000 0.0569347000 +Sri Lanka Rupee 0.0049030500 0.0048833800 0.0048875500 +Swedish Krona 0.0818510000 0.0817029000 0.0818978000 0.0822544000 +Swiss Franc 0.7340980000 0.7348630000 0.7341760000 0.7345670000 +Thai Baht 0.0211936000 0.0211661000 0.0212042000 0.0211098000 +Trinidad And Tobago Dollar 0.1102220000 0.1092030000 +Tunisian Dinar 0.3225800000 0.3211100000 0.3215340000 0.3206630000 +U.A.E. Dirham 0.2019290000 0.2011190000 0.2012910000 0.2009580000 +Peso Uruguayo 0.0258745000 +Bolivar Fuerte 0.0743445000 0.0740462000 0.0741095000 0.0739869000 Currency units per SDR(3) -Currency February 27, 2017 February 24, 2017 February 23, 2017 February 22, 2017 February 21, 2017 -Chinese Yuan 9.297740 9.309250 9.291520 9.278680 9.294370 -Euro 1.277730 1.277200 1.278100 1.283140 1.281050 -Japanese Yen 151.709000 152.909000 153.187000 153.215000 153.207000 -U.K. Pound Sterling 1.089510 1.079530 1.083060 1.084900 1.086790 -U.S. Dollar 1.352740 1.354980 1.351330 1.348960 1.349840 -Algerian Dinar 149.127000 148.875000 149.013000 148.982000 -Australian Dollar 1.755840 1.757950 1.753950 1.760360 -Bahrain Dinar 0.509471 0.508102 0.507210 0.507542 -Botswana Pula 14.026700 13.989000 14.066300 14.105000 -Brazilian Real 4.150570 4.164550 4.176920 4.172650 -Brunei Dollar 1.903340 1.911870 1.913900 1.917730 -Canadian Dollar 1.775570 1.771460 1.778870 1.774910 -Chilean Peso 867.671000 866.897000 868.229000 866.882000 -Colombian Peso 3,891.050000 3,910.160000 3,915.600000 3,918.340000 -Czech Koruna 34.508600 34.534700 34.668300 34.615400 -Danish Krone 9.495140 9.500740 9.537800 9.522450 -Hungarian Forint 394.678000 394.577000 394.354000 393.898000 -Icelandic Krona 146.880000 147.944000 148.750000 149.509000 -Indian Rupee 90.316300 90.332600 -Indonesian Rupiah 18,070.000000 18,053.800000 18,016.700000 18,047.400000 -Iranian Rial 43,709.000000 43,730.900000 -Israeli New Sheqel 5.010720 5.010750 5.004630 5.003880 -Kazakhstani Tenge 422.062000 422.495000 423.695000 428.251000 -Korean Won 1,543.050000 1,541.740000 1,546.990000 1,549.080000 -Kuwaiti Dinar 0.413810 0.412698 0.411972 0.412242 +Currency March 02, 2017 March 01, 2017 February 28, 2017 February 27, 2017 February 24, 2017 +Chinese Yuan 9.276350 9.300160 9.297740 9.309250 +Euro 1.280220 1.277620 1.277730 1.277200 +Japanese Yen 152.376000 152.381000 151.709000 152.909000 +U.K. Pound Sterling 1.094970 1.088340 1.089510 1.079530 +U.S. Dollar 1.348460 1.353890 1.352740 1.354980 +Algerian Dinar 148.971000 149.047000 148.939000 149.127000 +Australian Dollar 1.761310 1.761050 1.757940 1.755840 +Bahrain Dinar 0.507022 0.509064 0.508629 0.509471 +Botswana Pula 14.061100 14.059100 14.032500 14.026700 +Brazilian Real 4.178470 4.195300 4.191730 4.150570 +Brunei Dollar 1.897550 1.900870 1.900460 1.903340 +Canadian Dollar 1.793640 1.772220 1.775570 +Chilean Peso 874.990000 873.515000 873.408000 867.671000 +Colombian Peso 3,936.390000 3,921.250000 3,904.710000 3,891.050000 +Czech Koruna 34.593500 34.520300 34.524500 34.508600 +Danish Krone 9.516380 9.496770 9.497580 9.495140 +Hungarian Forint 393.643000 393.400000 393.592000 394.678000 +Icelandic Krona 143.611000 144.515000 145.541000 146.880000 +Indian Rupee 90.142100 90.355500 90.260900 +Indonesian Rupiah 18,016.800000 18,070.400000 18,044.200000 18,070.000000 +Iranian Rial 43,699.600000 43,870.200000 43,834.100000 +Israeli New Sheqel 4.897620 4.953900 4.976710 5.010720 +Kazakhstani Tenge 423.444000 422.690000 422.062000 +Korean Won 1,532.740000 1,529.950000 1,543.050000 +Kuwaiti Dinar 0.411955 0.413343 0.412991 0.413810 Libyan Dinar 1.932400 1.932400 1.932400 1.932400 -Malaysian Ringgit 6.024240 6.013450 6.012330 6.019640 -Mauritian Rupee 47.980500 -Mexican Peso 26.874000 26.622800 26.861400 27.602800 -Nepalese Rupee 144.901000 144.863000 144.447000 144.636000 -New Zealand Dollar 1.875660 1.878680 1.884020 1.884470 -Norwegian Krone 11.285900 11.256200 11.311200 11.290900 -Rial Omani 0.520988 0.519588 0.518675 0.519014 -Pakistani Rupee 142.077000 141.692000 141.442000 141.526000 -Nuevo Sol 4.378320 4.371990 4.382950 -Philippine Peso 68.063300 67.891000 67.859600 67.737800 -Polish Zloty 5.512190 5.518860 5.517120 5.526540 -Qatar Riyal 4.932130 4.918860 4.910220 4.913430 -Russian Ruble 77.533200 78.100600 -Saudi Arabian Riyal 5.081170 5.067500 5.058600 5.061910 -Singapore Dollar 1.903340 1.911870 1.913900 1.917730 -South African Rand 17.564000 17.516500 17.663900 17.785300 -Sri Lanka Rupee 204.369000 204.020000 203.995000 -Swedish Krona 12.157400 12.126500 12.142700 12.121600 -Swiss Franc 1.361350 1.364040 1.367580 1.362940 -Thai Baht 47.371400 47.288700 47.235100 47.305300 -Trinidad And Tobago Dollar 9.157260 9.163380 9.142270 9.128170 -Tunisian Dinar 3.118540 3.120570 3.100020 3.091930 -U.A.E. Dirham 4.976160 4.962780 4.954050 4.957290 -Peso Uruguayo 38.488800 38.298300 38.238400 -Bolivar Fuerte 13.515900 13.479600 +Malaysian Ringgit 5.997290 6.018040 6.010190 6.024240 +Mauritian Rupee 47.967800 48.157700 48.152600 +Mexican Peso 27.072000 26.827800 26.874000 +Nepalese Rupee 144.016000 144.487000 144.648000 144.901000 +New Zealand Dollar 1.884900 1.885120 1.880110 1.875660 +Norwegian Krone 11.345100 11.331500 11.293100 11.285900 +Rial Omani 0.518484 0.520573 0.520126 0.520988 +Pakistani Rupee 141.377000 141.958000 141.839000 142.077000 +Nuevo Sol 4.412340 4.396400 4.402320 +Philippine Peso 67.766700 68.055900 67.903400 68.063300 +Polish Zloty 5.495770 5.519830 5.511740 5.512190 +Qatar Riyal 4.908410 4.928170 4.923970 4.932130 +Russian Ruble 78.719700 78.475100 78.373600 +Saudi Arabian Riyal 5.056740 5.077100 5.072770 5.081170 +Singapore Dollar 1.897550 1.900870 1.900460 1.903340 +South African Rand 17.606200 17.634300 17.546700 17.564000 +Sri Lanka Rupee 203.955000 204.776000 204.601000 +Swedish Krona 12.217300 12.239500 12.210300 12.157400 +Swiss Franc 1.362220 1.360800 1.362070 1.361350 +Thai Baht 47.184100 47.245400 47.160500 47.371400 +Trinidad And Tobago Dollar 9.072600 9.157260 +Tunisian Dinar 3.100010 3.114200 3.110090 3.118540 +U.A.E. Dirham 4.952240 4.972180 4.967930 4.976160 +Peso Uruguayo 38.648100 +Bolivar Fuerte 13.450900 13.505100 13.493500 13.515900 (1) Exchange rates are published daily except on IMF holidays or whenever the IMF is closed for business. diff --git a/core-java/.resourceCache/IMFRateProvider.dat b/core-java/.resourceCache/IMFRateProvider.dat index 4c98b3b04e..12809d35af 100644 --- a/core-java/.resourceCache/IMFRateProvider.dat +++ b/core-java/.resourceCache/IMFRateProvider.dat @@ -2,113 +2,113 @@ SDRs per Currency unit and Currency units per SDR (1) last five days SDRs per Currency unit (2) -Currency February 27, 2017 February 24, 2017 February 23, 2017 February 22, 2017 February 21, 2017 -Chinese Yuan 0.1075530000 0.1074200000 0.1076250000 0.1077740000 0.1075920000 -Euro 0.7826360000 0.7829640000 0.7824120000 0.7793410000 0.7806080000 -Japanese Yen 0.0065915500 0.0065398200 0.0065279600 0.0065267800 0.0065271000 -U.K. Pound Sterling 0.9178430000 0.9263250000 0.9233090000 0.9217470000 0.9201430000 -U.S. Dollar 0.7392420000 0.7380190000 0.7400090000 0.7413120000 0.7408260000 -Algerian Dinar 0.0067056800 0.0067170500 0.0067108200 0.0067122300 -Australian Dollar 0.5695290000 0.5688450000 0.5701430000 0.5680650000 -Bahrain Dinar 1.9628200000 1.9681100000 1.9715700000 1.9702800000 -Botswana Pula 0.0712926000 0.0714849000 0.0710918000 0.0708970000 -Brazilian Real 0.2409310000 0.2401220000 0.2394110000 0.2396560000 -Brunei Dollar 0.5253930000 0.5230480000 0.5224920000 0.5214510000 -Canadian Dollar 0.5632010000 0.5645050000 0.5621540000 0.5634090000 -Chilean Peso 0.0011525100 0.0011535400 0.0011517700 0.0011535600 -Colombian Peso 0.0002570000 0.0002557440 0.0002553890 0.0002552100 -Czech Koruna 0.0289783000 0.0289564000 0.0288448000 0.0288889000 -Danish Krone 0.1053170000 0.1052550000 0.1048460000 0.1050150000 -Hungarian Forint 0.0025337100 0.0025343600 0.0025357900 0.0025387300 -Icelandic Krona 0.0068082900 0.0067593100 0.0067227000 0.0066885700 -Indian Rupee 0.0110722000 0.0110702000 -Indonesian Rupiah 0.0000553404 0.0000553899 0.0000555040 0.0000554096 -Iranian Rial 0.0000228786 0.0000228671 -Israeli New Sheqel 0.1995720000 0.1995710000 0.1998150000 0.1998450000 -Kazakhstani Tenge 0.0023693200 0.0023668900 0.0023601900 0.0023350800 -Korean Won 0.0006480670 0.0006486190 0.0006464180 0.0006455440 -Kuwaiti Dinar 2.4165700000 2.4230800000 2.4273500000 2.4257600000 +Currency March 02, 2017 March 01, 2017 February 28, 2017 February 27, 2017 February 24, 2017 +Chinese Yuan 0.1078010000 0.1075250000 0.1075530000 0.1074200000 +Euro 0.7811130000 0.7827060000 0.7826360000 0.7829640000 +Japanese Yen 0.0065627100 0.0065625100 0.0065915500 0.0065398200 +U.K. Pound Sterling 0.9132630000 0.9188320000 0.9178430000 0.9263250000 +U.S. Dollar 0.7415860000 0.7386110000 0.7392420000 0.7380190000 +Algerian Dinar 0.0067127200 0.0067093100 0.0067141700 0.0067056800 +Australian Dollar 0.5677580000 0.5678440000 0.5688470000 0.5695290000 +Bahrain Dinar 1.9723000000 1.9643900000 1.9660700000 1.9628200000 +Botswana Pula 0.0711181000 0.0711282000 0.0712629000 0.0712926000 +Brazilian Real 0.2393220000 0.2383620000 0.2385650000 0.2409310000 +Brunei Dollar 0.5269940000 0.5260760000 0.5261880000 0.5253930000 +Canadian Dollar 0.5575260000 0.5642640000 0.5632010000 +Chilean Peso 0.0011428700 0.0011448000 0.0011449400 0.0011525100 +Colombian Peso 0.0002540400 0.0002550210 0.0002561010 0.0002570000 +Czech Koruna 0.0289072000 0.0289685000 0.0289649000 0.0289783000 +Danish Krone 0.1050820000 0.1052990000 0.1052900000 0.1053170000 +Hungarian Forint 0.0025403700 0.0025419400 0.0025407000 0.0025337100 +Icelandic Krona 0.0069632500 0.0069197200 0.0068709200 0.0068082900 +Indian Rupee 0.0110936000 0.0110674000 0.0110790000 +Indonesian Rupiah 0.0000555038 0.0000553391 0.0000554196 0.0000553404 +Iranian Rial 0.0000228835 0.0000227945 0.0000228133 +Israeli New Sheqel 0.2041810000 0.2018610000 0.2009360000 0.1995720000 +Kazakhstani Tenge 0.0023615900 0.0023658000 0.0023693200 +Korean Won 0.0006524260 0.0006536180 0.0006480670 +Kuwaiti Dinar 2.4274500000 2.4193000000 2.4213600000 2.4165700000 Libyan Dinar 0.5174910000 0.5174910000 0.5174910000 0.5174910000 -Malaysian Ringgit 0.1659960000 0.1662940000 0.1663250000 0.1661230000 -Mauritian Rupee 0.0208418000 -Mexican Peso 0.0372107000 0.0375618000 0.0372281000 0.0362282000 -Nepalese Rupee 0.0069012400 0.0069030700 0.0069229700 0.0069139200 -New Zealand Dollar 0.5331450000 0.5322880000 0.5307790000 0.5306540000 -Norwegian Krone 0.0886062000 0.0888398000 0.0884082000 0.0885668000 -Rial Omani 1.9194300000 1.9246000000 1.9279900000 1.9267300000 -Pakistani Rupee 0.0070384300 0.0070575400 0.0070700400 0.0070658600 -Nuevo Sol 0.2283980000 0.2287290000 0.2281570000 -Philippine Peso 0.0146922000 0.0147295000 0.0147363000 0.0147628000 -Polish Zloty 0.1814160000 0.1811970000 0.1812540000 0.1809450000 -Qatar Riyal 0.2027520000 0.2032990000 0.2036570000 0.2035240000 -Russian Ruble 0.0128977000 0.0128040000 -Saudi Arabian Riyal 0.1968050000 0.1973360000 0.1976830000 0.1975540000 -Singapore Dollar 0.5253930000 0.5230480000 0.5224920000 0.5214510000 -South African Rand 0.0569347000 0.0570889000 0.0566127000 0.0562263000 -Sri Lanka Rupee 0.0048931100 0.0049014800 0.0049020900 -Swedish Krona 0.0822544000 0.0824642000 0.0823543000 0.0824973000 -Swiss Franc 0.7345670000 0.7331180000 0.7312210000 0.7337090000 -Thai Baht 0.0211098000 0.0211467000 0.0211707000 0.0211393000 -Trinidad And Tobago Dollar 0.1092030000 0.1091300000 0.1093820000 0.1095510000 -Tunisian Dinar 0.3206630000 0.3204540000 0.3225790000 0.3234230000 -U.A.E. Dirham 0.2009580000 0.2015000000 0.2018550000 0.2017230000 -Peso Uruguayo 0.0259816000 0.0261108000 0.0261517000 -Bolivar Fuerte 0.0739869000 0.0741864000 +Malaysian Ringgit 0.1667420000 0.1661670000 0.1663840000 0.1659960000 +Mauritian Rupee 0.0208473000 0.0207651000 0.0207673000 +Mexican Peso 0.0369385000 0.0372748000 0.0372107000 +Nepalese Rupee 0.0069436900 0.0069210200 0.0069133300 0.0069012400 +New Zealand Dollar 0.5305310000 0.5304700000 0.5318850000 0.5331450000 +Norwegian Krone 0.0881434000 0.0882493000 0.0885500000 0.0886062000 +Rial Omani 1.9287000000 1.9209600000 1.9226100000 1.9194300000 +Pakistani Rupee 0.0070732900 0.0070443600 0.0070502500 0.0070384300 +Nuevo Sol 0.2266370000 0.2274590000 0.2271530000 +Philippine Peso 0.0147565000 0.0146938000 0.0147268000 0.0146922000 +Polish Zloty 0.1819580000 0.1811650000 0.1814310000 0.1814160000 +Qatar Riyal 0.2037320000 0.2029150000 0.2030880000 0.2027520000 +Russian Ruble 0.0127033000 0.0127429000 0.0127594000 +Saudi Arabian Riyal 0.1977560000 0.1969630000 0.1971310000 0.1968050000 +Singapore Dollar 0.5269940000 0.5260760000 0.5261880000 0.5253930000 +South African Rand 0.0567982000 0.0567078000 0.0569908000 0.0569347000 +Sri Lanka Rupee 0.0049030500 0.0048833800 0.0048875500 +Swedish Krona 0.0818510000 0.0817029000 0.0818978000 0.0822544000 +Swiss Franc 0.7340980000 0.7348630000 0.7341760000 0.7345670000 +Thai Baht 0.0211936000 0.0211661000 0.0212042000 0.0211098000 +Trinidad And Tobago Dollar 0.1102220000 0.1092030000 +Tunisian Dinar 0.3225800000 0.3211100000 0.3215340000 0.3206630000 +U.A.E. Dirham 0.2019290000 0.2011190000 0.2012910000 0.2009580000 +Peso Uruguayo 0.0258745000 +Bolivar Fuerte 0.0743445000 0.0740462000 0.0741095000 0.0739869000 Currency units per SDR(3) -Currency February 27, 2017 February 24, 2017 February 23, 2017 February 22, 2017 February 21, 2017 -Chinese Yuan 9.297740 9.309250 9.291520 9.278680 9.294370 -Euro 1.277730 1.277200 1.278100 1.283140 1.281050 -Japanese Yen 151.709000 152.909000 153.187000 153.215000 153.207000 -U.K. Pound Sterling 1.089510 1.079530 1.083060 1.084900 1.086790 -U.S. Dollar 1.352740 1.354980 1.351330 1.348960 1.349840 -Algerian Dinar 149.127000 148.875000 149.013000 148.982000 -Australian Dollar 1.755840 1.757950 1.753950 1.760360 -Bahrain Dinar 0.509471 0.508102 0.507210 0.507542 -Botswana Pula 14.026700 13.989000 14.066300 14.105000 -Brazilian Real 4.150570 4.164550 4.176920 4.172650 -Brunei Dollar 1.903340 1.911870 1.913900 1.917730 -Canadian Dollar 1.775570 1.771460 1.778870 1.774910 -Chilean Peso 867.671000 866.897000 868.229000 866.882000 -Colombian Peso 3,891.050000 3,910.160000 3,915.600000 3,918.340000 -Czech Koruna 34.508600 34.534700 34.668300 34.615400 -Danish Krone 9.495140 9.500740 9.537800 9.522450 -Hungarian Forint 394.678000 394.577000 394.354000 393.898000 -Icelandic Krona 146.880000 147.944000 148.750000 149.509000 -Indian Rupee 90.316300 90.332600 -Indonesian Rupiah 18,070.000000 18,053.800000 18,016.700000 18,047.400000 -Iranian Rial 43,709.000000 43,730.900000 -Israeli New Sheqel 5.010720 5.010750 5.004630 5.003880 -Kazakhstani Tenge 422.062000 422.495000 423.695000 428.251000 -Korean Won 1,543.050000 1,541.740000 1,546.990000 1,549.080000 -Kuwaiti Dinar 0.413810 0.412698 0.411972 0.412242 +Currency March 02, 2017 March 01, 2017 February 28, 2017 February 27, 2017 February 24, 2017 +Chinese Yuan 9.276350 9.300160 9.297740 9.309250 +Euro 1.280220 1.277620 1.277730 1.277200 +Japanese Yen 152.376000 152.381000 151.709000 152.909000 +U.K. Pound Sterling 1.094970 1.088340 1.089510 1.079530 +U.S. Dollar 1.348460 1.353890 1.352740 1.354980 +Algerian Dinar 148.971000 149.047000 148.939000 149.127000 +Australian Dollar 1.761310 1.761050 1.757940 1.755840 +Bahrain Dinar 0.507022 0.509064 0.508629 0.509471 +Botswana Pula 14.061100 14.059100 14.032500 14.026700 +Brazilian Real 4.178470 4.195300 4.191730 4.150570 +Brunei Dollar 1.897550 1.900870 1.900460 1.903340 +Canadian Dollar 1.793640 1.772220 1.775570 +Chilean Peso 874.990000 873.515000 873.408000 867.671000 +Colombian Peso 3,936.390000 3,921.250000 3,904.710000 3,891.050000 +Czech Koruna 34.593500 34.520300 34.524500 34.508600 +Danish Krone 9.516380 9.496770 9.497580 9.495140 +Hungarian Forint 393.643000 393.400000 393.592000 394.678000 +Icelandic Krona 143.611000 144.515000 145.541000 146.880000 +Indian Rupee 90.142100 90.355500 90.260900 +Indonesian Rupiah 18,016.800000 18,070.400000 18,044.200000 18,070.000000 +Iranian Rial 43,699.600000 43,870.200000 43,834.100000 +Israeli New Sheqel 4.897620 4.953900 4.976710 5.010720 +Kazakhstani Tenge 423.444000 422.690000 422.062000 +Korean Won 1,532.740000 1,529.950000 1,543.050000 +Kuwaiti Dinar 0.411955 0.413343 0.412991 0.413810 Libyan Dinar 1.932400 1.932400 1.932400 1.932400 -Malaysian Ringgit 6.024240 6.013450 6.012330 6.019640 -Mauritian Rupee 47.980500 -Mexican Peso 26.874000 26.622800 26.861400 27.602800 -Nepalese Rupee 144.901000 144.863000 144.447000 144.636000 -New Zealand Dollar 1.875660 1.878680 1.884020 1.884470 -Norwegian Krone 11.285900 11.256200 11.311200 11.290900 -Rial Omani 0.520988 0.519588 0.518675 0.519014 -Pakistani Rupee 142.077000 141.692000 141.442000 141.526000 -Nuevo Sol 4.378320 4.371990 4.382950 -Philippine Peso 68.063300 67.891000 67.859600 67.737800 -Polish Zloty 5.512190 5.518860 5.517120 5.526540 -Qatar Riyal 4.932130 4.918860 4.910220 4.913430 -Russian Ruble 77.533200 78.100600 -Saudi Arabian Riyal 5.081170 5.067500 5.058600 5.061910 -Singapore Dollar 1.903340 1.911870 1.913900 1.917730 -South African Rand 17.564000 17.516500 17.663900 17.785300 -Sri Lanka Rupee 204.369000 204.020000 203.995000 -Swedish Krona 12.157400 12.126500 12.142700 12.121600 -Swiss Franc 1.361350 1.364040 1.367580 1.362940 -Thai Baht 47.371400 47.288700 47.235100 47.305300 -Trinidad And Tobago Dollar 9.157260 9.163380 9.142270 9.128170 -Tunisian Dinar 3.118540 3.120570 3.100020 3.091930 -U.A.E. Dirham 4.976160 4.962780 4.954050 4.957290 -Peso Uruguayo 38.488800 38.298300 38.238400 -Bolivar Fuerte 13.515900 13.479600 +Malaysian Ringgit 5.997290 6.018040 6.010190 6.024240 +Mauritian Rupee 47.967800 48.157700 48.152600 +Mexican Peso 27.072000 26.827800 26.874000 +Nepalese Rupee 144.016000 144.487000 144.648000 144.901000 +New Zealand Dollar 1.884900 1.885120 1.880110 1.875660 +Norwegian Krone 11.345100 11.331500 11.293100 11.285900 +Rial Omani 0.518484 0.520573 0.520126 0.520988 +Pakistani Rupee 141.377000 141.958000 141.839000 142.077000 +Nuevo Sol 4.412340 4.396400 4.402320 +Philippine Peso 67.766700 68.055900 67.903400 68.063300 +Polish Zloty 5.495770 5.519830 5.511740 5.512190 +Qatar Riyal 4.908410 4.928170 4.923970 4.932130 +Russian Ruble 78.719700 78.475100 78.373600 +Saudi Arabian Riyal 5.056740 5.077100 5.072770 5.081170 +Singapore Dollar 1.897550 1.900870 1.900460 1.903340 +South African Rand 17.606200 17.634300 17.546700 17.564000 +Sri Lanka Rupee 203.955000 204.776000 204.601000 +Swedish Krona 12.217300 12.239500 12.210300 12.157400 +Swiss Franc 1.362220 1.360800 1.362070 1.361350 +Thai Baht 47.184100 47.245400 47.160500 47.371400 +Trinidad And Tobago Dollar 9.072600 9.157260 +Tunisian Dinar 3.100010 3.114200 3.110090 3.118540 +U.A.E. Dirham 4.952240 4.972180 4.967930 4.976160 +Peso Uruguayo 38.648100 +Bolivar Fuerte 13.450900 13.505100 13.493500 13.515900 (1) Exchange rates are published daily except on IMF holidays or whenever the IMF is closed for business. diff --git a/core-java/src/test/java/com/baeldung/money/JavaMoneyTest.java b/core-java/src/test/java/com/baeldung/money/JavaMoneyTest.java index 5aa12095ff..ed737c6184 100644 --- a/core-java/src/test/java/com/baeldung/money/JavaMoneyTest.java +++ b/core-java/src/test/java/com/baeldung/money/JavaMoneyTest.java @@ -1,69 +1,177 @@ package com.baeldung.money; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import java.math.BigDecimal; +import java.util.Locale; + +import javax.money.CurrencyUnit; +import javax.money.Monetary; +import javax.money.MonetaryAmount; +import javax.money.UnknownCurrencyException; import javax.money.convert.ConversionQueryBuilder; +import javax.money.convert.CurrencyConversion; import javax.money.convert.MonetaryConversions; +import javax.money.format.AmountFormatQueryBuilder; +import javax.money.format.MonetaryAmountFormat; +import javax.money.format.MonetaryFormats; +import org.javamoney.moneta.FastMoney; +import org.javamoney.moneta.Money; +import org.javamoney.moneta.format.CurrencyStyle; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; -import com.baeldung.money.JavaMoney; - -import junit.framework.TestCase; - -public class JavaMoneyTest -{ +public class JavaMoneyTest { + @Rule + public ExpectedException thrown = ExpectedException.none(); @Test - public void givenAmountsAreCorrect() - { - JavaMoney j9m; - j9m = new JavaMoney(); - assertEquals("USD", j9m.USD.toString()); - assertEquals("USD 1", j9m.oneDolar.toString()); - assertEquals("EUR 1", j9m.oneEuro.toString()); - assertEquals("USD 200.5", j9m.fstAmtUSD.toString()); - assertEquals("EUR 1.30473908", j9m.fstAmtEUR.toString()); - assertEquals("USD 12", j9m.moneyof.toString()); - assertEquals("USD 2.00000", j9m.fastmoneyof.toString()); + public void givenCurrencyCode_whenString_thanExist() { + CurrencyUnit USD = Monetary.getCurrency("USD"); + assertNotNull(USD); + assertEquals(USD.getCurrencyCode(), "USD"); + assertEquals(USD.getNumericCode(), 840); + assertEquals(USD.getDefaultFractionDigits(), 2); + assertFalse(Monetary.isCurrencyAvailable("AAA")); + + } + + @Test + public void givenCurrencyCode_whenNoExist_thanThrowsError() { + thrown.expect(UnknownCurrencyException.class); + CurrencyUnit AAA = Monetary.getCurrency("AAA"); + assertNull(AAA); + throw new UnknownCurrencyException("AAA"); + } + + @Test + public void givenAmounts_whenStrinfied_thanEquals() { + CurrencyUnit USD = Monetary.getCurrency("USD"); + MonetaryAmount fstAmtUSD = Monetary.getDefaultAmountFactory().setCurrency(USD).setNumber(200.50).create(); + MonetaryAmount fstAmtEUR = Monetary.getDefaultAmountFactory().setCurrency("EUR").setNumber(1.30473908).create(); + MonetaryAmount oneDolar = Monetary.getDefaultAmountFactory().setCurrency("USD").setNumber(1).create(); + Money moneyof = Money.of(12, USD); + FastMoney fastmoneyof = FastMoney.of(2, USD); + Money oneEuro = Money.of(1, "EUR"); + + assertEquals("USD", USD.toString()); + assertEquals("USD 1", oneDolar.toString()); + assertEquals("EUR 1", oneEuro.toString()); + assertEquals("USD 200.5", fstAmtUSD.toString()); + assertEquals("EUR 1.30473908", fstAmtEUR.toString()); + assertEquals("USD 12", moneyof.toString()); + assertEquals("USD 2.00000", fastmoneyof.toString()); + + } + + @Test + public void givenCurrencies_whenCompared_thanNotequal() { + MonetaryAmount oneDolar = Monetary.getDefaultAmountFactory().setCurrency("USD").setNumber(1).create(); + Money oneEuro = Money.of(1, "EUR"); + assertFalse(oneEuro.equals(FastMoney.of(1, "EUR"))); + assertTrue(oneDolar.equals(Money.of(1, "USD"))); + + } + + @Test + public void givenAmount_whenDivided_thanThrowsException() { + MonetaryAmount oneDolar = Monetary.getDefaultAmountFactory().setCurrency("USD").setNumber(1).create(); + thrown.expect(ArithmeticException.class); + MonetaryAmount oneDivThree = oneDolar.divide(3); + assertNull(oneDivThree); + throw new ArithmeticException(); + + } + + @Test + public void givenArithmetic_whenStrinfied_thanEqualsAmount() { + CurrencyUnit USD = Monetary.getCurrency("USD"); + Money moneyof = Money.of(12, USD); + MonetaryAmount fstAmtUSD = Monetary.getDefaultAmountFactory().setCurrency(USD).setNumber(200.50).create(); + Money calcAmtUSD = Money.of(1, "USD").subtract(fstAmtUSD); + FastMoney fastmoneyof = FastMoney.of(2, USD); + MonetaryAmount calcMoneyFastMoney = moneyof.subtract(fastmoneyof); + MonetaryAmount oneDolar = Monetary.getDefaultAmountFactory().setCurrency("USD").setNumber(1).create(); + MonetaryAmount multiplyAmount = oneDolar.multiply(0.25); + MonetaryAmount divideAmount = oneDolar.divide(0.25); + MonetaryAmount[] monetaryAmounts = new MonetaryAmount[] { + Money.of(100, "CHF"), + Money.of(10.20, "CHF"), + Money.of(1.15, "CHF"), }; + + Money sumAmtCHF = Money.of(0, "CHF"); + + for (MonetaryAmount monetaryAmount : monetaryAmounts) { + sumAmtCHF = sumAmtCHF.add(monetaryAmount); + } + assertEquals("USD", USD.toString()); + assertEquals("USD 1", oneDolar.toString()); + assertEquals("USD 200.5", fstAmtUSD.toString()); + assertEquals("USD 12", moneyof.toString()); + assertEquals("USD 2.00000", fastmoneyof.toString()); + assertEquals("USD -199.5", calcAmtUSD.toString()); + assertEquals("CHF 111.35", sumAmtCHF.toString()); + assertEquals("USD 10", calcMoneyFastMoney.toString()); + assertEquals("USD 0.25", multiplyAmount.toString()); + assertEquals("USD 4", divideAmount.toString()); } - + @Test - public void givenArithmeticIsCorrect(){ - JavaMoney j9m; - j9m = new JavaMoney(); - assertEquals("USD -199.5", j9m.calcAmtUSD.toString()); - assertEquals("CHF 111.35", j9m.sumAmtCHF.toString()); - assertEquals("USD 10", j9m.calcMoneyFastMoney.toString()); - assertEquals("USD 0.25", j9m.multiplyAmount.toString()); - assertEquals("USD 4", j9m.divideAmount.toString()); + public void givenAmount_whenRounded_thanEquals() { + MonetaryAmount fstAmtEUR = Monetary.getDefaultAmountFactory().setCurrency("EUR").setNumber(1.30473908).create(); + MonetaryAmount roundEUR = fstAmtEUR.with(Monetary.getDefaultRounding()); + assertEquals("EUR 1.30473908", fstAmtEUR.toString()); + assertEquals("EUR 1.3", roundEUR.toString()); } - + @Test - public void givenRoundingIsCorrect(){ - JavaMoney j9m; - j9m = new JavaMoney(); - assertEquals("EUR 1.3", j9m.roundEUR.toString()); + public void givenAmount_whenCustomFormat_thanEquals() { + MonetaryAmount oneDolar = Monetary.getDefaultAmountFactory().setCurrency("USD").setNumber(1).create(); + MonetaryAmountFormat formatUSD = MonetaryFormats.getAmountFormat(Locale.US); + String usFormatted = formatUSD.format(oneDolar); + + MonetaryAmountFormat customFormat = MonetaryFormats.getAmountFormat(AmountFormatQueryBuilder.of(Locale.US).set(CurrencyStyle.NAME).set("pattern", "00000.00 ¤").build()); + String customFormatted = customFormat.format(oneDolar); + + assertEquals("USD 1", oneDolar.toString()); + assertNotNull(formatUSD); + assertNotNull(customFormat); + assertEquals("USD1.00", usFormatted); + assertEquals("00001.00 US Dollar", customFormatted); } - + @Test - public void givenFormatIsCorrect(){ - JavaMoney j9m; - j9m = new JavaMoney(); - assertEquals("USD1.00", j9m.usFormatted); - assertEquals("00001.00 US Dollar", j9m.customFormatted); - } - - @Test - public void givenConversionIsNotNull(){ - JavaMoney j9m; - j9m = new JavaMoney(); - assertNotNull(MonetaryConversions.getConversion(ConversionQueryBuilder.of().setTermCurrency("EUR").build())); - assertNotNull(MonetaryConversions.getConversion(ConversionQueryBuilder.of().setTermCurrency("USD").build())); - assertNotNull(j9m.convertedAmountEURtoUSD); - assertNotNull(j9m.convertedAmountEURtoUSD2); - assertNotNull(j9m.convertedAmountUSDtoEUR); - assertNotNull(j9m.convertedAmountUSDtoEUR2); + public void givenAmount_whenConversion_thenNotNull() { + CurrencyUnit USD = Monetary.getCurrency("USD"); + MonetaryAmount oneDolar = Monetary.getDefaultAmountFactory().setCurrency("USD").setNumber(1).create(); + MonetaryAmount fstAmtEUR = Monetary.getDefaultAmountFactory().setCurrency("EUR").setNumber(1.30473908).create(); + + CurrencyConversion convEUR = MonetaryConversions.getConversion(ConversionQueryBuilder.of().setTermCurrency("EUR").build()); + CurrencyConversion convUSD = MonetaryConversions.getConversion(ConversionQueryBuilder.of().setTermCurrency(USD).build()); + + CurrencyConversion conversionUSD = MonetaryConversions.getConversion("USD"); + CurrencyConversion conversionEUR = MonetaryConversions.getConversion("EUR"); + + MonetaryAmount convertedAmountEURtoUSD = fstAmtEUR.with(conversionUSD); + MonetaryAmount convertedAmountEURtoUSD2 = fstAmtEUR.with(convUSD); + MonetaryAmount convertedAmountUSDtoEUR = oneDolar.with(conversionEUR); + MonetaryAmount convertedAmountUSDtoEUR2 = oneDolar.with(convEUR); + + assertEquals("USD", USD.toString()); + assertEquals("USD 1", oneDolar.toString()); + assertEquals("EUR 1.30473908", fstAmtEUR.toString()); + assertNotNull(convEUR); + assertNotNull(convUSD); + assertNotNull(convertedAmountEURtoUSD); + assertNotNull(convertedAmountEURtoUSD2); + assertNotNull(convertedAmountUSDtoEUR); + assertNotNull(convertedAmountUSDtoEUR2); } } From 351af60bcea9dd7e54abf75c3a54fc222bf5d613 Mon Sep 17 00:00:00 2001 From: Felipe Reis Date: Thu, 2 Mar 2017 18:16:06 -0300 Subject: [PATCH 088/112] refactor: Use 'of' instead of Stream 'builder' Simplify the construction of a Stream by using the 'of()' method instead of a 'builder' Resolves: BAEL-632 --- .../baeldung/mockito/java8/CustomAnswerWithLambdaUnitTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mockito2/src/test/java/com/baeldung/mockito/java8/CustomAnswerWithLambdaUnitTest.java b/mockito2/src/test/java/com/baeldung/mockito/java8/CustomAnswerWithLambdaUnitTest.java index eebb86e239..b4e5abdc6c 100644 --- a/mockito2/src/test/java/com/baeldung/mockito/java8/CustomAnswerWithLambdaUnitTest.java +++ b/mockito2/src/test/java/com/baeldung/mockito/java8/CustomAnswerWithLambdaUnitTest.java @@ -39,7 +39,7 @@ public class CustomAnswerWithLambdaUnitTest { MockitoAnnotations.initMocks(this); when(jobService.listJobs(any(Person.class))).then((i) -> { - return ((Person) i.getArgument(0)).getName().equals("Peter") ? Stream. builder().add(new JobPosition("Teacher")).build() : Stream.empty(); + return ((Person) i.getArgument(0)).getName().equals("Peter") ? Stream.of(new JobPosition("Teacher")) : Stream.empty(); }); } } From 23489a082bf1776addc0cee74bec94341934f7ce Mon Sep 17 00:00:00 2001 From: Wim Deblauwe Date: Fri, 3 Mar 2017 00:21:28 +0100 Subject: [PATCH 089/112] BEAL-75 - Spring Boot Audit Support (#1240) * BEAL-75 - Spring Boot Audit Support Source code for http://inprogress.baeldung.com/wp-admin/post.php?post=35337&action=edit * BEAL-75 - Spring Boot Audit Support Update to use SLF4J logger instead of System.out.println --- spring-boot-auditing/.gitignore | 6 + spring-boot-auditing/pom.xml | 198 ++++++++++++++++++ .../main/java/org/baeldung/Application.java | 13 ++ .../src/main/java/org/baeldung/MvcConfig.java | 18 ++ .../java/org/baeldung/WebSecurityConfig.java | 34 +++ ...temptedPathAuthorizationAuditListener.java | 36 ++++ .../auditing/LoginAttemptsLogger.java | 25 +++ .../src/main/resources/application.properties | 1 + .../src/main/resources/logback.xml | 14 ++ .../src/main/resources/templates/hello.html | 13 ++ .../src/main/resources/templates/home.html | 11 + .../src/main/resources/templates/login.html | 20 ++ 12 files changed, 389 insertions(+) create mode 100644 spring-boot-auditing/.gitignore create mode 100644 spring-boot-auditing/pom.xml create mode 100755 spring-boot-auditing/src/main/java/org/baeldung/Application.java create mode 100755 spring-boot-auditing/src/main/java/org/baeldung/MvcConfig.java create mode 100755 spring-boot-auditing/src/main/java/org/baeldung/WebSecurityConfig.java create mode 100644 spring-boot-auditing/src/main/java/org/baeldung/auditing/ExposeAttemptedPathAuthorizationAuditListener.java create mode 100644 spring-boot-auditing/src/main/java/org/baeldung/auditing/LoginAttemptsLogger.java create mode 100644 spring-boot-auditing/src/main/resources/application.properties create mode 100644 spring-boot-auditing/src/main/resources/logback.xml create mode 100755 spring-boot-auditing/src/main/resources/templates/hello.html create mode 100755 spring-boot-auditing/src/main/resources/templates/home.html create mode 100755 spring-boot-auditing/src/main/resources/templates/login.html diff --git a/spring-boot-auditing/.gitignore b/spring-boot-auditing/.gitignore new file mode 100644 index 0000000000..31ce405488 --- /dev/null +++ b/spring-boot-auditing/.gitignore @@ -0,0 +1,6 @@ +/target/ +.settings/ +.classpath +.project +*.iml +.idea \ No newline at end of file diff --git a/spring-boot-auditing/pom.xml b/spring-boot-auditing/pom.xml new file mode 100644 index 0000000000..9307db39ee --- /dev/null +++ b/spring-boot-auditing/pom.xml @@ -0,0 +1,198 @@ + + 4.0.0 + com.baeldung + spring-boot-auditing + 0.0.1-SNAPSHOT + war + spring-boot-auditing + This is simple boot application for Spring boot auditing test + + + + org.springframework.boot + spring-boot-starter-parent + 1.5.1.RELEASE + + + + + + org.springframework.boot + spring-boot-starter-thymeleaf + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-data-jpa + + + + org.springframework.boot + spring-boot-starter-actuator + + + + org.springframework.boot + spring-boot-starter-security + + + + io.dropwizard.metrics + metrics-core + + + + com.h2database + h2 + + + + org.springframework.boot + spring-boot-starter-test + test + + + + org.springframework.boot + spring-boot-starter + + + com.jayway.jsonpath + json-path + test + + + org.springframework.boot + spring-boot-starter-mail + + + org.subethamail + subethasmtp + ${subethasmtp.version} + test + + + + org.webjars + bootstrap + ${bootstrap.version} + + + org.webjars + jquery + ${jquery.version} + + + + org.apache.tomcat + tomcat-servlet-api + ${tomee-servlet-api.version} + provided + + + + + + spring-boot + + + src/main/resources + true + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.8 + 1.8 + + + + + org.apache.maven.plugins + maven-war-plugin + + + + pl.project13.maven + git-commit-id-plugin + ${git-commit-id-plugin.version} + + + + org.apache.maven.plugins + maven-surefire-plugin + + + **/*IntegrationTest.java + **/*LiveTest.java + + + + + + + + + + + integration + + + + org.apache.maven.plugins + maven-surefire-plugin + + + integration-test + + test + + + + **/*LiveTest.java + + + **/*IntegrationTest.java + + + + + + + json + + + + + + + + + + + UTF-8 + 1.8 + 4.3.4.RELEASE + 2.2.1 + 3.1.1 + 3.3.7-1 + 3.1.7 + 8.5.11 + + + diff --git a/spring-boot-auditing/src/main/java/org/baeldung/Application.java b/spring-boot-auditing/src/main/java/org/baeldung/Application.java new file mode 100755 index 0000000000..bf7b7bd1a6 --- /dev/null +++ b/spring-boot-auditing/src/main/java/org/baeldung/Application.java @@ -0,0 +1,13 @@ +package org.baeldung; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class Application { + + public static void main(String[] args) throws Throwable { + SpringApplication.run(Application.class, args); + } + +} diff --git a/spring-boot-auditing/src/main/java/org/baeldung/MvcConfig.java b/spring-boot-auditing/src/main/java/org/baeldung/MvcConfig.java new file mode 100755 index 0000000000..fecb8c5c0b --- /dev/null +++ b/spring-boot-auditing/src/main/java/org/baeldung/MvcConfig.java @@ -0,0 +1,18 @@ +package org.baeldung; + +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; + +@Configuration +public class MvcConfig extends WebMvcConfigurerAdapter { + + @Override + public void addViewControllers(ViewControllerRegistry registry) { + registry.addViewController("/home").setViewName("home"); + registry.addViewController("/").setViewName("home"); + registry.addViewController("/hello").setViewName("hello"); + registry.addViewController("/login").setViewName("login"); + } + +} diff --git a/spring-boot-auditing/src/main/java/org/baeldung/WebSecurityConfig.java b/spring-boot-auditing/src/main/java/org/baeldung/WebSecurityConfig.java new file mode 100755 index 0000000000..199edce0bc --- /dev/null +++ b/spring-boot-auditing/src/main/java/org/baeldung/WebSecurityConfig.java @@ -0,0 +1,34 @@ +package org.baeldung; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Configuration; +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.WebSecurityConfigurerAdapter; +import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; + +@Configuration +@EnableWebSecurity +public class WebSecurityConfig extends WebSecurityConfigurerAdapter { + @Override + protected void configure(HttpSecurity http) throws Exception { + http + .authorizeRequests() + .antMatchers("/", "/home").permitAll() + .anyRequest().authenticated() + .and() + .formLogin() + .loginPage("/login") + .permitAll() + .and() + .logout() + .permitAll(); + } + + @Autowired + public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { + auth + .inMemoryAuthentication() + .withUser("user").password("password").roles("USER"); + } +} diff --git a/spring-boot-auditing/src/main/java/org/baeldung/auditing/ExposeAttemptedPathAuthorizationAuditListener.java b/spring-boot-auditing/src/main/java/org/baeldung/auditing/ExposeAttemptedPathAuthorizationAuditListener.java new file mode 100644 index 0000000000..bc36ac08b3 --- /dev/null +++ b/spring-boot-auditing/src/main/java/org/baeldung/auditing/ExposeAttemptedPathAuthorizationAuditListener.java @@ -0,0 +1,36 @@ +package org.baeldung.auditing; + +import org.springframework.boot.actuate.audit.AuditEvent; +import org.springframework.boot.actuate.security.AbstractAuthorizationAuditListener; +import org.springframework.security.access.event.AbstractAuthorizationEvent; +import org.springframework.security.access.event.AuthorizationFailureEvent; +import org.springframework.security.web.FilterInvocation; +import org.springframework.stereotype.Component; + +import java.util.HashMap; +import java.util.Map; + +@Component +public class ExposeAttemptedPathAuthorizationAuditListener extends AbstractAuthorizationAuditListener { + + public static final String AUTHORIZATION_FAILURE = "AUTHORIZATION_FAILURE"; + + @Override + public void onApplicationEvent(AbstractAuthorizationEvent event) { + if (event instanceof AuthorizationFailureEvent) { + onAuthorizationFailureEvent((AuthorizationFailureEvent) event); + } + } + + private void onAuthorizationFailureEvent(AuthorizationFailureEvent event) { + Map data = new HashMap<>(); + data.put("type", event.getAccessDeniedException().getClass().getName()); + data.put("message", event.getAccessDeniedException().getMessage()); + data.put("requestUrl", ((FilterInvocation)event.getSource()).getRequestUrl() ); + if (event.getAuthentication().getDetails() != null) { + data.put("details", event.getAuthentication().getDetails()); + } + publish(new AuditEvent(event.getAuthentication().getName(), AUTHORIZATION_FAILURE, + data)); + } +} diff --git a/spring-boot-auditing/src/main/java/org/baeldung/auditing/LoginAttemptsLogger.java b/spring-boot-auditing/src/main/java/org/baeldung/auditing/LoginAttemptsLogger.java new file mode 100644 index 0000000000..5be8cebfd3 --- /dev/null +++ b/spring-boot-auditing/src/main/java/org/baeldung/auditing/LoginAttemptsLogger.java @@ -0,0 +1,25 @@ +package org.baeldung.auditing; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.boot.actuate.audit.AuditEvent; +import org.springframework.boot.actuate.audit.listener.AuditApplicationEvent; +import org.springframework.context.event.EventListener; +import org.springframework.security.web.authentication.WebAuthenticationDetails; +import org.springframework.stereotype.Component; + +@Component +public class LoginAttemptsLogger { + private static final Logger LOGGER = LoggerFactory.getLogger(LoginAttemptsLogger.class); + + @EventListener + public void auditEventHappened(AuditApplicationEvent auditApplicationEvent) { + AuditEvent auditEvent = auditApplicationEvent.getAuditEvent(); + LOGGER.debug("Principal " + auditEvent.getPrincipal() + " - " + auditEvent.getType()); + + WebAuthenticationDetails details = (WebAuthenticationDetails) auditEvent.getData().get("details"); + LOGGER.debug(" Remote IP address: " + details.getRemoteAddress()); + LOGGER.debug(" Session Id: " + details.getSessionId()); + LOGGER.debug(" Request URL: " + auditEvent.getData().get("requestUrl")); + } +} diff --git a/spring-boot-auditing/src/main/resources/application.properties b/spring-boot-auditing/src/main/resources/application.properties new file mode 100644 index 0000000000..cf09473b60 --- /dev/null +++ b/spring-boot-auditing/src/main/resources/application.properties @@ -0,0 +1 @@ +logging.level.org.springframework=INFO \ No newline at end of file diff --git a/spring-boot-auditing/src/main/resources/logback.xml b/spring-boot-auditing/src/main/resources/logback.xml new file mode 100644 index 0000000000..78913ee76f --- /dev/null +++ b/spring-boot-auditing/src/main/resources/logback.xml @@ -0,0 +1,14 @@ + + + + + web - %date [%thread] %-5level %logger{36} - %message%n + + + + + + + + + \ No newline at end of file diff --git a/spring-boot-auditing/src/main/resources/templates/hello.html b/spring-boot-auditing/src/main/resources/templates/hello.html new file mode 100755 index 0000000000..46feef7e2c --- /dev/null +++ b/spring-boot-auditing/src/main/resources/templates/hello.html @@ -0,0 +1,13 @@ + + + + Hello World! + + +

Hello [[${#httpServletRequest.remoteUser}]]!

+
+ +
+ + \ No newline at end of file diff --git a/spring-boot-auditing/src/main/resources/templates/home.html b/spring-boot-auditing/src/main/resources/templates/home.html new file mode 100755 index 0000000000..fe4e8b337e --- /dev/null +++ b/spring-boot-auditing/src/main/resources/templates/home.html @@ -0,0 +1,11 @@ + + + + Spring Security Example + + +

Welcome!

+ +

Click here to see a greeting.

+ + \ No newline at end of file diff --git a/spring-boot-auditing/src/main/resources/templates/login.html b/spring-boot-auditing/src/main/resources/templates/login.html new file mode 100755 index 0000000000..a1785313f5 --- /dev/null +++ b/spring-boot-auditing/src/main/resources/templates/login.html @@ -0,0 +1,20 @@ + + + + Spring Security Example + + +
+ Invalid username and password. +
+
+ You have been logged out. +
+
+
+
+
+
+ + \ No newline at end of file From 6356fb6a26c68b6efb0ecb0e33de263183e23bd3 Mon Sep 17 00:00:00 2001 From: k0l0ssus Date: Thu, 2 Mar 2017 23:20:59 -0500 Subject: [PATCH 090/112] Guice Intro (#1177) * Add files via upload * Update pom.xml * Update RunGuice.java * Update Communication.java * Update CommunicationMode.java * Update DefaultCommunicator.java * Update EmailCommunicationMode.java * Update IMCommunicationMode.java * Update SMSCommunicationMode.java * Update MessageLogger.java * Update MessageSentLoggable.java * Update AOPModule.java * Update BasicModule.java * Update CommunicationModel.java * Update Communicator.java * Update BasicModule.java * Update RunGuice.java * Update MessageLogger.java * Update Communicator.java * Update pom.xml * Update pom.xml * Update pom.xml * Update pom.xml --- guice-intro/pom.xml | 34 +++++++++++ .../java/com/baeldung/examples/RunGuice.java | 36 ++++++++++++ .../examples/guice/Communication.java | 57 +++++++++++++++++++ .../examples/guice/CommunicationMode.java | 12 ++++ .../examples/guice/DefaultCommunicator.java | 48 ++++++++++++++++ .../guice/EmailCommunicationMode.java | 23 ++++++++ .../examples/guice/IMCommunicationMode.java | 30 ++++++++++ .../examples/guice/SMSCommunicationMode.java | 29 ++++++++++ .../examples/guice/aop/MessageLogger.java | 22 +++++++ .../guice/aop/MessageSentLoggable.java | 16 ++++++ .../examples/guice/binding/AOPModule.java | 22 +++++++ .../examples/guice/binding/BasicModule.java | 36 ++++++++++++ .../guice/constant/CommunicationModel.java | 16 ++++++ .../examples/guice/marker/Communicator.java | 11 ++++ .../examples/guice/modules/BasicModule.java | 37 ++++++++++++ 15 files changed, 429 insertions(+) create mode 100644 guice-intro/pom.xml create mode 100644 guice-intro/src/main/java/com/baeldung/examples/RunGuice.java create mode 100644 guice-intro/src/main/java/com/baeldung/examples/guice/Communication.java create mode 100644 guice-intro/src/main/java/com/baeldung/examples/guice/CommunicationMode.java create mode 100644 guice-intro/src/main/java/com/baeldung/examples/guice/DefaultCommunicator.java create mode 100644 guice-intro/src/main/java/com/baeldung/examples/guice/EmailCommunicationMode.java create mode 100644 guice-intro/src/main/java/com/baeldung/examples/guice/IMCommunicationMode.java create mode 100644 guice-intro/src/main/java/com/baeldung/examples/guice/SMSCommunicationMode.java create mode 100644 guice-intro/src/main/java/com/baeldung/examples/guice/aop/MessageLogger.java create mode 100644 guice-intro/src/main/java/com/baeldung/examples/guice/aop/MessageSentLoggable.java create mode 100644 guice-intro/src/main/java/com/baeldung/examples/guice/binding/AOPModule.java create mode 100644 guice-intro/src/main/java/com/baeldung/examples/guice/binding/BasicModule.java create mode 100644 guice-intro/src/main/java/com/baeldung/examples/guice/constant/CommunicationModel.java create mode 100644 guice-intro/src/main/java/com/baeldung/examples/guice/marker/Communicator.java create mode 100644 guice-intro/src/main/java/com/baeldung/examples/guice/modules/BasicModule.java diff --git a/guice-intro/pom.xml b/guice-intro/pom.xml new file mode 100644 index 0000000000..1f0d7679b7 --- /dev/null +++ b/guice-intro/pom.xml @@ -0,0 +1,34 @@ + + + 4.0.0 + com.baeldung.examples.guice + guice-intro + 1.0-SNAPSHOT + jar + + + com.google.inject + guice + ${guice.version} + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.8 + 1.8 + + + + + + UTF-8 + 1.8 + 1.8 + 4.1.0 + + guice-intro + diff --git a/guice-intro/src/main/java/com/baeldung/examples/RunGuice.java b/guice-intro/src/main/java/com/baeldung/examples/RunGuice.java new file mode 100644 index 0000000000..b4b3e8571e --- /dev/null +++ b/guice-intro/src/main/java/com/baeldung/examples/RunGuice.java @@ -0,0 +1,36 @@ + +package com.baeldung.examples; + +import com.baeldung.examples.guice.Communication; +import com.baeldung.examples.guice.binding.AOPModule; +import com.baeldung.examples.guice.modules.BasicModule; +import com.google.inject.Guice; +import com.google.inject.Injector; +import java.util.Scanner; + +/** + * + * @author Baeldung + */ +public class RunGuice { + + public static void main(String[] args) { + Injector injector = Guice.createInjector(new BasicModule(), new AOPModule()); + Communication comms = injector.getInstance(Communication.class); + Scanner scanner = new Scanner(System.in); + System.out.println("Enter your message to be sent; press Q to quit and P to print the message log"); + while (true) { + String input = scanner.nextLine(); + if (input.equalsIgnoreCase("q")) { + System.exit(0); + } + if (input.equalsIgnoreCase("p")) { + comms.print(); + } else { + comms.sendMessage(input); + } + + } + + } +} diff --git a/guice-intro/src/main/java/com/baeldung/examples/guice/Communication.java b/guice-intro/src/main/java/com/baeldung/examples/guice/Communication.java new file mode 100644 index 0000000000..c4b17b57d2 --- /dev/null +++ b/guice-intro/src/main/java/com/baeldung/examples/guice/Communication.java @@ -0,0 +1,57 @@ + +package com.baeldung.examples.guice; + +import com.google.inject.Inject; +import com.google.inject.name.Named; +import java.util.Date; +import java.util.LinkedList; +import java.util.Queue; +import java.util.logging.Logger; + +/** + * + * @author Baeldung + */ +public class Communication { + + final Date start = new Date(); + + @Inject + private Logger logger; + + private Queue messageLog; + + @Named("CommsUUID") + private String commsID; + + @Inject + private DefaultCommunicator communicator; + + public Communication(Boolean keepRecords) { + if (keepRecords) { + messageLog = new LinkedList(); + } + } + + public boolean sendMessage(String message) { + if (!message.isEmpty() && messageLog != null) { + messageLog.add(message); + } + return communicator.sendMessage(message); + } + + public void print() { + if (messageLog != null) { + for (String message : messageLog) { + logger.info(message); + } + } else { + logger.info("Message logging wasn't enabled"); + } + } + + public DefaultCommunicator getCommunicator() { + return this.communicator; + } + +} diff --git a/guice-intro/src/main/java/com/baeldung/examples/guice/CommunicationMode.java b/guice-intro/src/main/java/com/baeldung/examples/guice/CommunicationMode.java new file mode 100644 index 0000000000..444b775478 --- /dev/null +++ b/guice-intro/src/main/java/com/baeldung/examples/guice/CommunicationMode.java @@ -0,0 +1,12 @@ + +package com.baeldung.examples.guice; + +import com.baeldung.examples.guice.constant.CommunicationModel; + +public interface CommunicationMode { + + public CommunicationModel getMode(); + + public boolean sendMessage(String message); + +} diff --git a/guice-intro/src/main/java/com/baeldung/examples/guice/DefaultCommunicator.java b/guice-intro/src/main/java/com/baeldung/examples/guice/DefaultCommunicator.java new file mode 100644 index 0000000000..423c24f789 --- /dev/null +++ b/guice-intro/src/main/java/com/baeldung/examples/guice/DefaultCommunicator.java @@ -0,0 +1,48 @@ + +package com.baeldung.examples.guice; + +import com.baeldung.examples.guice.marker.Communicator; +import com.google.inject.Inject; +import com.google.inject.name.Named; + + +public class DefaultCommunicator implements Communicator { + + private CommunicationMode defaultCommsMode; + @Inject + @Named("SMSComms") + CommunicationMode smsCommsMode; + @Inject + @Named("EmailComms") + CommunicationMode emailCommsMode; + @Inject + @Named("IMComms") + CommunicationMode imCommsMode; + + protected DefaultCommunicator(CommunicationMode defaultComms) { + this.defaultCommsMode = defaultComms; + } + + public DefaultCommunicator() { + + } + + public boolean sendMessage(String message) { + boolean sent = false; + if (defaultCommsMode != null) { + sent = sendMessageByDefault(message); + } else { + sent = smsCommsMode.sendMessage(message); + } + return sent; + } + + private boolean sendMessageByDefault(String message) { + boolean sent = false; + if (message != null && !message.trim().equals("")) { + return defaultCommsMode.sendMessage(message); + } + return sent; + } + +} diff --git a/guice-intro/src/main/java/com/baeldung/examples/guice/EmailCommunicationMode.java b/guice-intro/src/main/java/com/baeldung/examples/guice/EmailCommunicationMode.java new file mode 100644 index 0000000000..642ee7ace0 --- /dev/null +++ b/guice-intro/src/main/java/com/baeldung/examples/guice/EmailCommunicationMode.java @@ -0,0 +1,23 @@ +package com.baeldung.examples.guice; + +import com.baeldung.examples.guice.aop.MessageSentLoggable; +import com.baeldung.examples.guice.constant.CommunicationModel; + +/** + * + * @author Baekdung + */ +public class EmailCommunicationMode implements CommunicationMode { + + @Override + public CommunicationModel getMode() { + return CommunicationModel.EMAIL; + } + + @Override + @MessageSentLoggable + public boolean sendMessage(String Message) { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + +} diff --git a/guice-intro/src/main/java/com/baeldung/examples/guice/IMCommunicationMode.java b/guice-intro/src/main/java/com/baeldung/examples/guice/IMCommunicationMode.java new file mode 100644 index 0000000000..9f34e9a241 --- /dev/null +++ b/guice-intro/src/main/java/com/baeldung/examples/guice/IMCommunicationMode.java @@ -0,0 +1,30 @@ + +package com.baeldung.examples.guice; + +import com.baeldung.examples.guice.aop.MessageSentLoggable; +import com.baeldung.examples.guice.constant.CommunicationModel; +import com.google.inject.Inject; +import java.util.logging.Logger; + +/** + * + * @author Baeldung + */ +public class IMCommunicationMode implements CommunicationMode { + + @Inject + private Logger logger; + + @Override + public CommunicationModel getMode() { + return CommunicationModel.IM; + } + + @Override + @MessageSentLoggable + public boolean sendMessage(String message) { + logger.info("IM Message Sent"); + return true; + } + +} diff --git a/guice-intro/src/main/java/com/baeldung/examples/guice/SMSCommunicationMode.java b/guice-intro/src/main/java/com/baeldung/examples/guice/SMSCommunicationMode.java new file mode 100644 index 0000000000..251e249971 --- /dev/null +++ b/guice-intro/src/main/java/com/baeldung/examples/guice/SMSCommunicationMode.java @@ -0,0 +1,29 @@ +package com.baeldung.examples.guice; + +import com.baeldung.examples.guice.aop.MessageSentLoggable; +import com.baeldung.examples.guice.constant.CommunicationModel; +import com.google.inject.Inject; +import java.util.logging.Logger; + +/** + * + * @author Baeldung + */ +public class SMSCommunicationMode implements CommunicationMode { + + @Inject + private Logger logger; + + @Override + public CommunicationModel getMode() { + return CommunicationModel.SMS; + } + + @Override + @MessageSentLoggable + public boolean sendMessage(String message) { + logger.info("SMS message sent"); + return true; + } + +} diff --git a/guice-intro/src/main/java/com/baeldung/examples/guice/aop/MessageLogger.java b/guice-intro/src/main/java/com/baeldung/examples/guice/aop/MessageLogger.java new file mode 100644 index 0000000000..8926dfa714 --- /dev/null +++ b/guice-intro/src/main/java/com/baeldung/examples/guice/aop/MessageLogger.java @@ -0,0 +1,22 @@ +package com.baeldung.examples.guice.aop; + +import java.util.logging.Logger; +import org.aopalliance.intercept.MethodInterceptor; +import org.aopalliance.intercept.MethodInvocation; + +/** + * + * @author Baeldung + */ +public class MessageLogger implements MethodInterceptor { + + @Override + public Object invoke(MethodInvocation invocation) throws Throwable { + Object[] objectArray = invocation.getArguments(); + int i = 0; + for (Object object : objectArray) { + Logger.getAnonymousLogger().info("Sending message: " + object.toString()); + } + return invocation.proceed(); + } +} diff --git a/guice-intro/src/main/java/com/baeldung/examples/guice/aop/MessageSentLoggable.java b/guice-intro/src/main/java/com/baeldung/examples/guice/aop/MessageSentLoggable.java new file mode 100644 index 0000000000..cacf3bde7c --- /dev/null +++ b/guice-intro/src/main/java/com/baeldung/examples/guice/aop/MessageSentLoggable.java @@ -0,0 +1,16 @@ +package com.baeldung.examples.guice.aop; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * + * @author Baeldung + */ +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.METHOD) +public @interface MessageSentLoggable { + +} diff --git a/guice-intro/src/main/java/com/baeldung/examples/guice/binding/AOPModule.java b/guice-intro/src/main/java/com/baeldung/examples/guice/binding/AOPModule.java new file mode 100644 index 0000000000..dc9d258efa --- /dev/null +++ b/guice-intro/src/main/java/com/baeldung/examples/guice/binding/AOPModule.java @@ -0,0 +1,22 @@ +package com.baeldung.examples.guice.binding; + +import com.baeldung.examples.guice.aop.MessageLogger; +import com.baeldung.examples.guice.aop.MessageSentLoggable; +import com.google.inject.AbstractModule; +import com.google.inject.matcher.Matchers; + +/** + * + * @author Baeldung + */ +public class AOPModule extends AbstractModule { + + @Override + protected void configure() { + bindInterceptor(Matchers.any(), + Matchers.annotatedWith(MessageSentLoggable.class), + new MessageLogger() + ); + } + +} diff --git a/guice-intro/src/main/java/com/baeldung/examples/guice/binding/BasicModule.java b/guice-intro/src/main/java/com/baeldung/examples/guice/binding/BasicModule.java new file mode 100644 index 0000000000..9168195130 --- /dev/null +++ b/guice-intro/src/main/java/com/baeldung/examples/guice/binding/BasicModule.java @@ -0,0 +1,36 @@ +package com.baeldung.examples.guice.binding; + +import com.baeldung.examples.guice.Communication; +import com.baeldung.examples.guice.CommunicationMode; +import com.baeldung.examples.guice.DefaultCommunicator; +import com.baeldung.examples.guice.EmailCommunicationMode; +import com.baeldung.examples.guice.IMCommunicationMode; +import com.baeldung.examples.guice.SMSCommunicationMode; +import com.google.inject.AbstractModule; +import com.google.inject.name.Names; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * + * @author Baeldung + */ +public class BasicModule extends AbstractModule { + + @Override + protected void configure() { + try { + bind(Communication.class).toConstructor(Communication.class.getConstructor(Boolean.TYPE)); + } catch (NoSuchMethodException ex) { + Logger.getLogger(BasicModule.class.getName()).log(Level.SEVERE, null, ex); + } catch (SecurityException ex) { + Logger.getLogger(BasicModule.class.getName()).log(Level.SEVERE, null, ex); + } + bind(DefaultCommunicator.class).annotatedWith(Names.named("AnotherCommunicator")).to(DefaultCommunicator.class).asEagerSingleton(); + + bind(CommunicationMode.class).annotatedWith(Names.named("IMComms")).to(IMCommunicationMode.class); + bind(CommunicationMode.class).annotatedWith(Names.named("EmailComms")).to(EmailCommunicationMode.class); + bind(CommunicationMode.class).annotatedWith(Names.named("SMSComms")).to(SMSCommunicationMode.class); + } + +} diff --git a/guice-intro/src/main/java/com/baeldung/examples/guice/constant/CommunicationModel.java b/guice-intro/src/main/java/com/baeldung/examples/guice/constant/CommunicationModel.java new file mode 100644 index 0000000000..b9fa604a32 --- /dev/null +++ b/guice-intro/src/main/java/com/baeldung/examples/guice/constant/CommunicationModel.java @@ -0,0 +1,16 @@ +package com.baeldung.examples.guice.constant; + +/** + * + * @author Baeldung + */ +public enum CommunicationModel { + + EMAIL("Email"), SMS("SMS"), IM("IM"), PHONE("Phone"); + + final String name; + + CommunicationModel(String name) { + this.name = name; + } +} diff --git a/guice-intro/src/main/java/com/baeldung/examples/guice/marker/Communicator.java b/guice-intro/src/main/java/com/baeldung/examples/guice/marker/Communicator.java new file mode 100644 index 0000000000..239666b6ab --- /dev/null +++ b/guice-intro/src/main/java/com/baeldung/examples/guice/marker/Communicator.java @@ -0,0 +1,11 @@ +package com.baeldung.examples.guice.marker; + +/** + * + * @author Baeldung + */ +public interface Communicator { + + public boolean sendMessage(String message); + +} diff --git a/guice-intro/src/main/java/com/baeldung/examples/guice/modules/BasicModule.java b/guice-intro/src/main/java/com/baeldung/examples/guice/modules/BasicModule.java new file mode 100644 index 0000000000..47b3e2e573 --- /dev/null +++ b/guice-intro/src/main/java/com/baeldung/examples/guice/modules/BasicModule.java @@ -0,0 +1,37 @@ +package com.baeldung.examples.guice.modules; + +import com.baeldung.examples.guice.Communication; +import com.baeldung.examples.guice.CommunicationMode; +import com.baeldung.examples.guice.DefaultCommunicator; +import com.baeldung.examples.guice.EmailCommunicationMode; +import com.baeldung.examples.guice.IMCommunicationMode; +import com.baeldung.examples.guice.SMSCommunicationMode; +import com.google.inject.AbstractModule; +import com.google.inject.name.Names; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * + * @author Baeldung + */ +public class BasicModule extends AbstractModule { + + @Override + protected void configure() { + try { + bind(Communication.class).toConstructor(Communication.class.getConstructor(Boolean.class)); + bind(Boolean.class).toInstance(true); + } catch (NoSuchMethodException ex) { + Logger.getLogger(com.baeldung.examples.guice.binding.BasicModule.class.getName()).log(Level.SEVERE, null, ex); + } catch (SecurityException ex) { + Logger.getLogger(com.baeldung.examples.guice.binding.BasicModule.class.getName()).log(Level.SEVERE, null, ex); + } + bind(DefaultCommunicator.class).annotatedWith(Names.named("AnotherCommunicator")).to(DefaultCommunicator.class).asEagerSingleton(); + + bind(CommunicationMode.class).annotatedWith(Names.named("IMComms")).to(IMCommunicationMode.class); + bind(CommunicationMode.class).annotatedWith(Names.named("EmailComms")).to(EmailCommunicationMode.class); + bind(CommunicationMode.class).annotatedWith(Names.named("SMSComms")).to(SMSCommunicationMode.class); + } + +} From bca902362a131c0e00480cb1ddb0eb5dca526f03 Mon Sep 17 00:00:00 2001 From: KevinGilmore Date: Thu, 2 Mar 2017 22:52:52 -0600 Subject: [PATCH 091/112] BAEL-393: adding guice-intro module to main pom (#1287) * BAEL-278: Updated README.md * BAEL-554: Add and update README.md files * BAEL-345: fixed assertion * BAEL-109: Updated README.md * BAEL-345: Added README.md * Reinstating reactor-core module in root-level pom * BAEL-393: Adding guide-intro module to root pom --- pom.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/pom.xml b/pom.xml index b0156a4289..d859f529da 100644 --- a/pom.xml +++ b/pom.xml @@ -46,6 +46,7 @@ guava guava18 guava19 + guice-intro disruptor handling-spring-static-resources From f57a7ddaed01142142c791f3dca9314081baf028 Mon Sep 17 00:00:00 2001 From: Tomasz Lelek Date: Fri, 3 Mar 2017 06:15:34 +0100 Subject: [PATCH 092/112] Bael 641 (#1282) * BEEL-641 guava reflection utils code * BEEL-641 fix formatting * BEEL-641 add assertion on subttype number -> integer * BEEL-641 rename class * BEEL-641 formatting * BEEL-641 add comparison using standard java reflecetion api and guava one --- .../guava/GuavaReflectionUtilsTest.java | 150 ++++++++++++++++++ 1 file changed, 150 insertions(+) create mode 100644 guava/src/test/java/org/baeldung/guava/GuavaReflectionUtilsTest.java diff --git a/guava/src/test/java/org/baeldung/guava/GuavaReflectionUtilsTest.java b/guava/src/test/java/org/baeldung/guava/GuavaReflectionUtilsTest.java new file mode 100644 index 0000000000..e59a682e08 --- /dev/null +++ b/guava/src/test/java/org/baeldung/guava/GuavaReflectionUtilsTest.java @@ -0,0 +1,150 @@ +package org.baeldung.guava; + + +import com.google.common.collect.Lists; +import com.google.common.reflect.Invokable; +import com.google.common.reflect.TypeToken; +import org.junit.Test; + +import java.lang.reflect.Method; +import java.lang.reflect.Modifier; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.function.Function; + +import static junit.framework.TestCase.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +public class GuavaReflectionUtilsTest { + + @Test + public void givenTwoGenericList_whenCheckIsAssignableFrom_thenReturnTrueDueToTypeErasure() { + //given + ArrayList stringList = Lists.newArrayList(); + ArrayList intList = Lists.newArrayList(); + + //when + boolean result = stringList.getClass().isAssignableFrom(intList.getClass()); + + //then + assertTrue(result); + } + + @Test + public void givenTypeToken_whenResolveType_thenShouldResolveProperType() { + //given + TypeToken> stringListToken = new TypeToken>() { + }; + TypeToken> integerListToken = new TypeToken>() { + }; + TypeToken> numberTypeToken = new TypeToken>() { + }; + + //then + assertFalse(stringListToken.isSubtypeOf(integerListToken)); + assertFalse(numberTypeToken.isSubtypeOf(integerListToken)); + assertTrue(integerListToken.isSubtypeOf(numberTypeToken)); + } + + @Test + public void givenCustomClass_whenCaptureGeneric_thenReturnTypeAtRuntime() { + //given + ParametrizedClass parametrizedClass = new ParametrizedClass() { + }; + + //then + assertEquals(parametrizedClass.type, TypeToken.of(String.class)); + } + + @Test + public void givenComplexType_whenGetTypeArgument_thenShouldReturnTypeAtRuntime() { + //given + TypeToken> funToken = new TypeToken>() { + }; + + //when + TypeToken funResultToken = funToken.resolveType(Function.class.getTypeParameters()[1]); + + //then + assertEquals(funResultToken, TypeToken.of(String.class)); + } + + + @Test + public void givenMapType_whenGetTypeArgumentOfEntry_thenShouldReturnTypeAtRuntime() throws NoSuchMethodException { + //given + TypeToken> mapToken = new TypeToken>() { + }; + + //when + TypeToken entrySetToken = mapToken.resolveType(Map.class.getMethod("entrySet").getGenericReturnType()); + + //then + assertEquals(entrySetToken, new TypeToken>>() { + }); + } + + @Test + public void givenInvokable_whenCheckPublicMethod_shouldReturnTrue() throws NoSuchMethodException { + //given + Method method = CustomClass.class.getMethod("somePublicMethod"); + Invokable invokable = new TypeToken() { + }.method(method); + + //when + boolean isPublicStandradJava = Modifier.isPublic(method.getModifiers()); + boolean isPublicGuava = invokable.isPublic(); + //then + assertTrue(isPublicStandradJava); + assertTrue(isPublicGuava); + } + + @Test + public void givenInvokable_whenCheckFinalMethod_shouldReturnFalseForIsOverridable() throws NoSuchMethodException { + //given + Method method = CustomClass.class.getMethod("notOverridablePublicMethod"); + Invokable invokable = new TypeToken() { + }.method(method); + + //when + boolean isOverridableStandardJava = (!(Modifier.isFinal(method.getModifiers()) || Modifier.isPrivate(method.getModifiers()) + || Modifier.isStatic(method.getModifiers()) + || Modifier.isFinal(method.getDeclaringClass().getModifiers()))); + boolean isOverridableFinalGauava = invokable.isOverridable(); + + //then + assertFalse(isOverridableStandardJava); + assertFalse(isOverridableFinalGauava); + } + + @Test + public void givenListOfType_whenGetReturnRype_shouldCaptureTypeAtRuntime() throws NoSuchMethodException { + //given + Method getMethod = List.class.getMethod("get", int.class); + + //when + Invokable, ?> invokable = new TypeToken>() { + }.method(getMethod); + + //then + assertEquals(TypeToken.of(Integer.class), invokable.getReturnType()); // Not Object.class! + } + + + abstract class ParametrizedClass { + TypeToken type = new TypeToken(getClass()) { + }; + } + + class CustomClass { + public void somePublicMethod() { + } + + public final void notOverridablePublicMethod() { + + } + } +} From 735b377a1a078f774b26f475954669ede2f203ac Mon Sep 17 00:00:00 2001 From: pedja4 Date: Fri, 3 Mar 2017 06:51:23 +0100 Subject: [PATCH 093/112] Update pom.xml --- reactor-core/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/reactor-core/pom.xml b/reactor-core/pom.xml index 2be8892983..3aeb4af3d5 100644 --- a/reactor-core/pom.xml +++ b/reactor-core/pom.xml @@ -50,7 +50,7 @@ - 3.0.4.RELEASE + 3.0.5.RELEASE 4.12 3.6.1 1.1.3 From 8ffc21e8cf42bcbd4e9dfaa7111d955159cfad1d Mon Sep 17 00:00:00 2001 From: slavisa-baeldung Date: Fri, 3 Mar 2017 06:57:03 +0100 Subject: [PATCH 094/112] BAEL - 606 - Tests for JSR 354 Money implementation --- .../com/baeldung/money/JavaMoneyTest.java | 69 ++++++++----------- 1 file changed, 27 insertions(+), 42 deletions(-) diff --git a/core-java/src/test/java/com/baeldung/money/JavaMoneyTest.java b/core-java/src/test/java/com/baeldung/money/JavaMoneyTest.java index ed737c6184..7ef4dceccb 100644 --- a/core-java/src/test/java/com/baeldung/money/JavaMoneyTest.java +++ b/core-java/src/test/java/com/baeldung/money/JavaMoneyTest.java @@ -1,13 +1,9 @@ package com.baeldung.money; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; - -import java.math.BigDecimal; -import java.util.Locale; +import org.javamoney.moneta.FastMoney; +import org.javamoney.moneta.Money; +import org.javamoney.moneta.format.CurrencyStyle; +import org.junit.Test; import javax.money.CurrencyUnit; import javax.money.Monetary; @@ -19,43 +15,36 @@ import javax.money.convert.MonetaryConversions; import javax.money.format.AmountFormatQueryBuilder; import javax.money.format.MonetaryAmountFormat; import javax.money.format.MonetaryFormats; +import java.util.Locale; -import org.javamoney.moneta.FastMoney; -import org.javamoney.moneta.Money; -import org.javamoney.moneta.format.CurrencyStyle; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.ExpectedException; +import static org.junit.Assert.*; public class JavaMoneyTest { - @Rule - public ExpectedException thrown = ExpectedException.none(); @Test public void givenCurrencyCode_whenString_thanExist() { - CurrencyUnit USD = Monetary.getCurrency("USD"); - assertNotNull(USD); - assertEquals(USD.getCurrencyCode(), "USD"); - assertEquals(USD.getNumericCode(), 840); - assertEquals(USD.getDefaultFractionDigits(), 2); + CurrencyUnit usd = Monetary.getCurrency("USD"); + + assertNotNull(usd); + assertEquals(usd.getCurrencyCode(), "USD"); + assertEquals(usd.getNumericCode(), 840); + assertEquals(usd.getDefaultFractionDigits(), 2); assertFalse(Monetary.isCurrencyAvailable("AAA")); - } - @Test + @Test(expected = UnknownCurrencyException.class) public void givenCurrencyCode_whenNoExist_thanThrowsError() { - thrown.expect(UnknownCurrencyException.class); - CurrencyUnit AAA = Monetary.getCurrency("AAA"); - assertNull(AAA); - throw new UnknownCurrencyException("AAA"); + CurrencyUnit aaa = Monetary.getCurrency("AAA"); + fail(); // if no exception } @Test - public void givenAmounts_whenStrinfied_thanEquals() { + public void givenAmounts_whenStringified_thanEquals() { CurrencyUnit USD = Monetary.getCurrency("USD"); MonetaryAmount fstAmtUSD = Monetary.getDefaultAmountFactory().setCurrency(USD).setNumber(200.50).create(); MonetaryAmount fstAmtEUR = Monetary.getDefaultAmountFactory().setCurrency("EUR").setNumber(1.30473908).create(); MonetaryAmount oneDolar = Monetary.getDefaultAmountFactory().setCurrency("USD").setNumber(1).create(); + Money moneyof = Money.of(12, USD); FastMoney fastmoneyof = FastMoney.of(2, USD); Money oneEuro = Money.of(1, "EUR"); @@ -67,30 +56,26 @@ public class JavaMoneyTest { assertEquals("EUR 1.30473908", fstAmtEUR.toString()); assertEquals("USD 12", moneyof.toString()); assertEquals("USD 2.00000", fastmoneyof.toString()); - } @Test public void givenCurrencies_whenCompared_thanNotequal() { MonetaryAmount oneDolar = Monetary.getDefaultAmountFactory().setCurrency("USD").setNumber(1).create(); Money oneEuro = Money.of(1, "EUR"); + assertFalse(oneEuro.equals(FastMoney.of(1, "EUR"))); assertTrue(oneDolar.equals(Money.of(1, "USD"))); - } - @Test + @Test(expected = ArithmeticException.class) public void givenAmount_whenDivided_thanThrowsException() { MonetaryAmount oneDolar = Monetary.getDefaultAmountFactory().setCurrency("USD").setNumber(1).create(); - thrown.expect(ArithmeticException.class); - MonetaryAmount oneDivThree = oneDolar.divide(3); - assertNull(oneDivThree); - throw new ArithmeticException(); - + oneDolar.divide(3); + fail(); // if no exception } @Test - public void givenArithmetic_whenStrinfied_thanEqualsAmount() { + public void givenArithmetic_whenStringified_thanEqualsAmount() { CurrencyUnit USD = Monetary.getCurrency("USD"); Money moneyof = Money.of(12, USD); MonetaryAmount fstAmtUSD = Monetary.getDefaultAmountFactory().setCurrency(USD).setNumber(200.50).create(); @@ -100,11 +85,11 @@ public class JavaMoneyTest { MonetaryAmount oneDolar = Monetary.getDefaultAmountFactory().setCurrency("USD").setNumber(1).create(); MonetaryAmount multiplyAmount = oneDolar.multiply(0.25); MonetaryAmount divideAmount = oneDolar.divide(0.25); - - MonetaryAmount[] monetaryAmounts = new MonetaryAmount[] { + + MonetaryAmount[] monetaryAmounts = new MonetaryAmount[]{ Money.of(100, "CHF"), Money.of(10.20, "CHF"), - Money.of(1.15, "CHF"), }; + Money.of(1.15, "CHF"),}; Money sumAmtCHF = Money.of(0, "CHF"); @@ -158,12 +143,12 @@ public class JavaMoneyTest { CurrencyConversion conversionUSD = MonetaryConversions.getConversion("USD"); CurrencyConversion conversionEUR = MonetaryConversions.getConversion("EUR"); - + MonetaryAmount convertedAmountEURtoUSD = fstAmtEUR.with(conversionUSD); MonetaryAmount convertedAmountEURtoUSD2 = fstAmtEUR.with(convUSD); MonetaryAmount convertedAmountUSDtoEUR = oneDolar.with(conversionEUR); MonetaryAmount convertedAmountUSDtoEUR2 = oneDolar.with(convEUR); - + assertEquals("USD", USD.toString()); assertEquals("USD 1", oneDolar.toString()); assertEquals("EUR 1.30473908", fstAmtEUR.toString()); From 2538f131768f3ceb0978e8cd24aa59537610cfe6 Mon Sep 17 00:00:00 2001 From: pivovarit Date: Fri, 3 Mar 2017 08:32:46 +0100 Subject: [PATCH 095/112] Mockito2 Java8 refactor --- .../ArgumentMatcherWithoutLambdaUnitTest.java | 26 ++++++++----------- .../java8/CustomAnswerWithLambdaUnitTest.java | 21 ++++++++------- .../CustomAnswerWithoutLambdaUnitTest.java | 23 ++++++++-------- 3 files changed, 33 insertions(+), 37 deletions(-) diff --git a/mockito2/src/test/java/com/baeldung/mockito/java8/ArgumentMatcherWithoutLambdaUnitTest.java b/mockito2/src/test/java/com/baeldung/mockito/java8/ArgumentMatcherWithoutLambdaUnitTest.java index f4e8fb4cf5..786062ee57 100644 --- a/mockito2/src/test/java/com/baeldung/mockito/java8/ArgumentMatcherWithoutLambdaUnitTest.java +++ b/mockito2/src/test/java/com/baeldung/mockito/java8/ArgumentMatcherWithoutLambdaUnitTest.java @@ -1,21 +1,18 @@ package com.baeldung.mockito.java8; +import org.junit.Before; +import org.junit.Test; +import org.mockito.*; + +import java.util.Optional; + import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import static org.mockito.Mockito.when; -import java.util.Optional; - -import org.junit.Before; -import org.junit.Test; -import org.mockito.ArgumentMatchers; -import org.mockito.InjectMocks; -import org.mockito.ArgumentMatcher; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; - public class ArgumentMatcherWithoutLambdaUnitTest { + @InjectMocks private UnemploymentServiceImpl unemploymentService; @@ -38,13 +35,12 @@ public class ArgumentMatcherWithoutLambdaUnitTest { } private class PeterArgumentMatcher implements ArgumentMatcher { + @Override public boolean matches(Person p) { - - if (p.getName().equals("Peter")) { - return true; - } - return false; + return p + .getName() + .equals("Peter"); } } diff --git a/mockito2/src/test/java/com/baeldung/mockito/java8/CustomAnswerWithLambdaUnitTest.java b/mockito2/src/test/java/com/baeldung/mockito/java8/CustomAnswerWithLambdaUnitTest.java index b4e5abdc6c..06e9bca6d3 100644 --- a/mockito2/src/test/java/com/baeldung/mockito/java8/CustomAnswerWithLambdaUnitTest.java +++ b/mockito2/src/test/java/com/baeldung/mockito/java8/CustomAnswerWithLambdaUnitTest.java @@ -1,19 +1,20 @@ package com.baeldung.mockito.java8; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.when; - -import java.util.stream.Stream; - import org.junit.Before; import org.junit.Test; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.MockitoAnnotations; +import java.util.stream.Stream; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.when; + public class CustomAnswerWithLambdaUnitTest { + @InjectMocks private UnemploymentServiceImpl unemploymentService; @@ -38,8 +39,8 @@ public class CustomAnswerWithLambdaUnitTest { public void init() { MockitoAnnotations.initMocks(this); - when(jobService.listJobs(any(Person.class))).then((i) -> { - return ((Person) i.getArgument(0)).getName().equals("Peter") ? Stream.of(new JobPosition("Teacher")) : Stream.empty(); - }); + when(jobService.listJobs(any(Person.class))).then((i) -> + Stream.of(new JobPosition("Teacher")) + .filter(p -> ((Person) i.getArgument(0)).getName().equals("Peter"))); } } diff --git a/mockito2/src/test/java/com/baeldung/mockito/java8/CustomAnswerWithoutLambdaUnitTest.java b/mockito2/src/test/java/com/baeldung/mockito/java8/CustomAnswerWithoutLambdaUnitTest.java index 28922bb303..9d1aa3a3c0 100644 --- a/mockito2/src/test/java/com/baeldung/mockito/java8/CustomAnswerWithoutLambdaUnitTest.java +++ b/mockito2/src/test/java/com/baeldung/mockito/java8/CustomAnswerWithoutLambdaUnitTest.java @@ -1,12 +1,5 @@ package com.baeldung.mockito.java8; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.when; - -import java.util.stream.Stream; - import org.junit.Before; import org.junit.Test; import org.mockito.InjectMocks; @@ -15,8 +8,16 @@ import org.mockito.MockitoAnnotations; import org.mockito.invocation.InvocationOnMock; import org.mockito.stubbing.Answer; +import java.util.stream.Stream; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.when; + public class CustomAnswerWithoutLambdaUnitTest { + @InjectMocks private UnemploymentServiceImpl unemploymentService; @@ -38,15 +39,13 @@ public class CustomAnswerWithoutLambdaUnitTest { } private class PersonAnswer implements Answer> { + @Override public Stream answer(InvocationOnMock invocation) throws Throwable { Person person = invocation.getArgument(0); - if(person.getName().equals("Peter")) { - return Stream.builder().add(new JobPosition("Teacher")).build(); - } - - return Stream.empty(); + return Stream.of(new JobPosition("Teacher")) + .filter(p -> person.getName().equals("Peter")); } } From 005ac52b68ffb3ecdc02b907e9d0541dd7527d7d Mon Sep 17 00:00:00 2001 From: mariiakulik Date: Fri, 3 Mar 2017 20:19:26 +0100 Subject: [PATCH 096/112] Create README.md --- spring-security-cache-control/README.md | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 spring-security-cache-control/README.md diff --git a/spring-security-cache-control/README.md b/spring-security-cache-control/README.md new file mode 100644 index 0000000000..4b0bab72cb --- /dev/null +++ b/spring-security-cache-control/README.md @@ -0,0 +1,6 @@ +========= + +## Spring Security Cache Control + +### Relevant Articles: +- [Spring Security – Cache Control Headers](http://www.baeldung.com/spring-security-cache-control-headers) From 10a648dfbcbe6ab1ee107cad4eb617039c991bd3 Mon Sep 17 00:00:00 2001 From: slavisa-baeldung Date: Sat, 4 Mar 2017 11:05:43 +0100 Subject: [PATCH 097/112] BAEL - 606 - Test improvements --- .../com/baeldung/money/JavaMoneyTest.java | 61 ++++++++----------- 1 file changed, 26 insertions(+), 35 deletions(-) diff --git a/core-java/src/test/java/com/baeldung/money/JavaMoneyTest.java b/core-java/src/test/java/com/baeldung/money/JavaMoneyTest.java index 7ef4dceccb..3d2f986c30 100644 --- a/core-java/src/test/java/com/baeldung/money/JavaMoneyTest.java +++ b/core-java/src/test/java/com/baeldung/money/JavaMoneyTest.java @@ -29,31 +29,23 @@ public class JavaMoneyTest { assertEquals(usd.getCurrencyCode(), "USD"); assertEquals(usd.getNumericCode(), 840); assertEquals(usd.getDefaultFractionDigits(), 2); - assertFalse(Monetary.isCurrencyAvailable("AAA")); } @Test(expected = UnknownCurrencyException.class) public void givenCurrencyCode_whenNoExist_thanThrowsError() { - CurrencyUnit aaa = Monetary.getCurrency("AAA"); + Monetary.getCurrency("AAA"); fail(); // if no exception } @Test public void givenAmounts_whenStringified_thanEquals() { - CurrencyUnit USD = Monetary.getCurrency("USD"); - MonetaryAmount fstAmtUSD = Monetary.getDefaultAmountFactory().setCurrency(USD).setNumber(200.50).create(); - MonetaryAmount fstAmtEUR = Monetary.getDefaultAmountFactory().setCurrency("EUR").setNumber(1.30473908).create(); - MonetaryAmount oneDolar = Monetary.getDefaultAmountFactory().setCurrency("USD").setNumber(1).create(); + CurrencyUnit usd = Monetary.getCurrency("USD"); + MonetaryAmount fstAmtUSD = Monetary.getDefaultAmountFactory().setCurrency(usd).setNumber(200).create(); + Money moneyof = Money.of(12, usd); + FastMoney fastmoneyof = FastMoney.of(2, usd); - Money moneyof = Money.of(12, USD); - FastMoney fastmoneyof = FastMoney.of(2, USD); - Money oneEuro = Money.of(1, "EUR"); - - assertEquals("USD", USD.toString()); - assertEquals("USD 1", oneDolar.toString()); - assertEquals("EUR 1", oneEuro.toString()); - assertEquals("USD 200.5", fstAmtUSD.toString()); - assertEquals("EUR 1.30473908", fstAmtEUR.toString()); + assertEquals("USD", usd.toString()); + assertEquals("USD 200", fstAmtUSD.toString()); assertEquals("USD 12", moneyof.toString()); assertEquals("USD 2.00000", fastmoneyof.toString()); } @@ -75,35 +67,34 @@ public class JavaMoneyTest { } @Test - public void givenArithmetic_whenStringified_thanEqualsAmount() { - CurrencyUnit USD = Monetary.getCurrency("USD"); - Money moneyof = Money.of(12, USD); - MonetaryAmount fstAmtUSD = Monetary.getDefaultAmountFactory().setCurrency(USD).setNumber(200.50).create(); - Money calcAmtUSD = Money.of(1, "USD").subtract(fstAmtUSD); - FastMoney fastmoneyof = FastMoney.of(2, USD); - MonetaryAmount calcMoneyFastMoney = moneyof.subtract(fastmoneyof); - MonetaryAmount oneDolar = Monetary.getDefaultAmountFactory().setCurrency("USD").setNumber(1).create(); - MonetaryAmount multiplyAmount = oneDolar.multiply(0.25); - MonetaryAmount divideAmount = oneDolar.divide(0.25); - + public void givenAmounts_whenSummed_thanCorrect() { MonetaryAmount[] monetaryAmounts = new MonetaryAmount[]{ - Money.of(100, "CHF"), - Money.of(10.20, "CHF"), - Money.of(1.15, "CHF"),}; + Money.of(100, "CHF"), Money.of(10.20, "CHF"), Money.of(1.15, "CHF")}; Money sumAmtCHF = Money.of(0, "CHF"); - for (MonetaryAmount monetaryAmount : monetaryAmounts) { sumAmtCHF = sumAmtCHF.add(monetaryAmount); } - assertEquals("USD", USD.toString()); + + assertEquals("CHF 111.35", sumAmtCHF.toString()); + } + + @Test + public void givenArithmetic_whenStringified_thanEqualsAmount() { + CurrencyUnit usd = Monetary.getCurrency("USD"); + + Money moneyof = Money.of(12, usd); + MonetaryAmount fstAmtUSD = Monetary.getDefaultAmountFactory().setCurrency(usd).setNumber(200.50).create(); + MonetaryAmount oneDolar = Monetary.getDefaultAmountFactory().setCurrency("USD").setNumber(1).create(); + Money subtractedAmount = Money.of(1, "USD").subtract(fstAmtUSD); + MonetaryAmount multiplyAmount = oneDolar.multiply(0.25); + MonetaryAmount divideAmount = oneDolar.divide(0.25); + + assertEquals("USD", usd.toString()); assertEquals("USD 1", oneDolar.toString()); assertEquals("USD 200.5", fstAmtUSD.toString()); assertEquals("USD 12", moneyof.toString()); - assertEquals("USD 2.00000", fastmoneyof.toString()); - assertEquals("USD -199.5", calcAmtUSD.toString()); - assertEquals("CHF 111.35", sumAmtCHF.toString()); - assertEquals("USD 10", calcMoneyFastMoney.toString()); + assertEquals("USD -199.5", subtractedAmount.toString()); assertEquals("USD 0.25", multiplyAmount.toString()); assertEquals("USD 4", divideAmount.toString()); } From e9d847186dd08b04b55553d23336c4d2c67cfa17 Mon Sep 17 00:00:00 2001 From: Doha2012 Date: Sat, 4 Mar 2017 16:57:28 +0200 Subject: [PATCH 098/112] add rest-assured test (#1292) --- .../spring-cloud-bootstrap/gateway/pom.xml | 7 + .../cloud/bootstrap/gateway/LiveTest.java | 198 ++++++++++++++++++ 2 files changed, 205 insertions(+) create mode 100644 spring-cloud/spring-cloud-bootstrap/gateway/src/test/java/com/baeldung/spring/cloud/bootstrap/gateway/LiveTest.java diff --git a/spring-cloud/spring-cloud-bootstrap/gateway/pom.xml b/spring-cloud/spring-cloud-bootstrap/gateway/pom.xml index 97c440c249..13ae6b4050 100644 --- a/spring-cloud/spring-cloud-bootstrap/gateway/pom.xml +++ b/spring-cloud/spring-cloud-bootstrap/gateway/pom.xml @@ -51,6 +51,13 @@ spring-boot-starter-test test
+ + + io.rest-assured + rest-assured + test + 3.0.2 + diff --git a/spring-cloud/spring-cloud-bootstrap/gateway/src/test/java/com/baeldung/spring/cloud/bootstrap/gateway/LiveTest.java b/spring-cloud/spring-cloud-bootstrap/gateway/src/test/java/com/baeldung/spring/cloud/bootstrap/gateway/LiveTest.java new file mode 100644 index 0000000000..71ed2cd709 --- /dev/null +++ b/spring-cloud/spring-cloud-bootstrap/gateway/src/test/java/com/baeldung/spring/cloud/bootstrap/gateway/LiveTest.java @@ -0,0 +1,198 @@ +package com.baeldung.spring.cloud.bootstrap.gateway; + +import static io.restassured.RestAssured.config; +import io.restassured.RestAssured; +import io.restassured.authentication.FormAuthConfig; +import io.restassured.config.RedirectConfig; +import io.restassured.http.ContentType; +import io.restassured.response.Response; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.springframework.http.HttpStatus; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; + +public class LiveTest { + + private final String ROOT_URI = "http://localhost:8080"; + private final FormAuthConfig formConfig = new FormAuthConfig("/login", "username", "password"); + + @Before + public void setup() { + RestAssured.config = config().redirect(RedirectConfig.redirectConfig() + .followRedirects(false)); + } + + @Test + public void whenGetAllBooks_thenSuccess() { + final Response response = RestAssured.get(ROOT_URI + "/book-service/books"); + Assert.assertEquals(HttpStatus.OK.value(), response.getStatusCode()); + Assert.assertNotNull(response.getBody()); + } + + @Test + public void whenAccessProtectedResourceWithoutLogin_thenRedirectToLogin() { + final Response response = RestAssured.get(ROOT_URI + "/book-service/books/1"); + Assert.assertEquals(HttpStatus.FOUND.value(), response.getStatusCode()); + Assert.assertEquals("http://localhost:8080/login", response.getHeader("Location")); + } + + @Test + public void whenAccessProtectedResourceAfterLogin_thenSuccess() { + final Response response = RestAssured.given() + .auth() + .form("user", "password", formConfig) + .get(ROOT_URI + "/book-service/books/1"); + Assert.assertEquals(HttpStatus.OK.value(), response.getStatusCode()); + Assert.assertNotNull(response.getBody()); + } + + @Test + public void whenAccessAdminProtectedResource_thenForbidden() { + final Response response = RestAssured.given() + .auth() + .form("user", "password", formConfig) + .get(ROOT_URI + "/rating-service/ratings"); + Assert.assertEquals(HttpStatus.FORBIDDEN.value(), response.getStatusCode()); + + } + + @Test + public void whenAdminAccessProtectedResource_thenSuccess() { + final Response response = RestAssured.given() + .auth() + .form("admin", "admin", formConfig) + .get(ROOT_URI + "/rating-service/ratings"); + Assert.assertEquals(HttpStatus.OK.value(), response.getStatusCode()); + Assert.assertNotNull(response.getBody()); + } + + @Test + public void whenAdminAccessDiscoveryResource_thenSuccess() { + final Response response = RestAssured.given() + .auth() + .form("admin", "admin", formConfig) + .get(ROOT_URI + "/discovery"); + Assert.assertEquals(HttpStatus.OK.value(), response.getStatusCode()); + } + + @Test + public void whenAddnewRating_thenSuccess() { + + final Rating rating = new Rating(1L, 4); + + // request the protected resource + final Response ratingResponse = RestAssured.given() + .auth() + .form("admin", "admin", formConfig) + .and() + .contentType(ContentType.JSON) + .body(rating) + .post(ROOT_URI + "/rating-service/ratings"); + final Rating result = ratingResponse.as(Rating.class); + Assert.assertEquals(HttpStatus.OK.value(), ratingResponse.getStatusCode()); + Assert.assertEquals(rating.getBookId(), result.getBookId()); + Assert.assertEquals(rating.getStars(), result.getStars()); + } + + @Test + public void whenAddnewBook_thenSuccess() { + final Book book = new Book("Baeldung", "How to spring cloud"); + + // request the protected resource + final Response bookResponse = RestAssured.given() + .auth() + .form("admin", "admin", formConfig) + .and() + .contentType(ContentType.JSON) + .body(book) + .post(ROOT_URI + "/book-service/books"); + final Book result = bookResponse.as(Book.class); + Assert.assertEquals(HttpStatus.OK.value(), bookResponse.getStatusCode()); + Assert.assertEquals(book.getAuthor(), result.getAuthor()); + Assert.assertEquals(book.getTitle(), result.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 From 15eb03d21241e07b58de487a8dac2a4e20bb10d1 Mon Sep 17 00:00:00 2001 From: eugenp Date: Sat, 4 Mar 2017 17:52:50 +0200 Subject: [PATCH 099/112] minor cleanup --- core-java/0.004102810554955205 | 0 core-java/0.04832801936270381 | 0 core-java/0.5633433244738808 | 0 core-java/0.5967303215007616 | 0 core-java/0.6256429734439612 | 0 core-java/0.9252611327674576 | 0 core-java/0.9799201796740292 | 0 7 files changed, 0 insertions(+), 0 deletions(-) delete mode 100644 core-java/0.004102810554955205 delete mode 100644 core-java/0.04832801936270381 delete mode 100644 core-java/0.5633433244738808 delete mode 100644 core-java/0.5967303215007616 delete mode 100644 core-java/0.6256429734439612 delete mode 100644 core-java/0.9252611327674576 delete mode 100644 core-java/0.9799201796740292 diff --git a/core-java/0.004102810554955205 b/core-java/0.004102810554955205 deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/core-java/0.04832801936270381 b/core-java/0.04832801936270381 deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/core-java/0.5633433244738808 b/core-java/0.5633433244738808 deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/core-java/0.5967303215007616 b/core-java/0.5967303215007616 deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/core-java/0.6256429734439612 b/core-java/0.6256429734439612 deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/core-java/0.9252611327674576 b/core-java/0.9252611327674576 deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/core-java/0.9799201796740292 b/core-java/0.9799201796740292 deleted file mode 100644 index e69de29bb2..0000000000 From 348adc878bf1a4ef4e99f16fbfb111ce75c5b4d0 Mon Sep 17 00:00:00 2001 From: Yasin Date: Sat, 4 Mar 2017 22:28:03 +0530 Subject: [PATCH 100/112] Fixed compilation error and removed unused import (#1298) * yasin.bhojawala@gmail.com Evaluation article on Different Types of Bean Injection in Spring * Revert "yasin.bhojawala@gmail.com" This reverts commit 963cc51a7a15b75b550108fe4e198cd65a274032. * Fixing compilation error and removing unused import --- .../test/java/com/baeldung/java9/Java9OptionalsStreamTest.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/core-java-9/src/test/java/com/baeldung/java9/Java9OptionalsStreamTest.java b/core-java-9/src/test/java/com/baeldung/java9/Java9OptionalsStreamTest.java index 121c17a860..a6060b1a9d 100644 --- a/core-java-9/src/test/java/com/baeldung/java9/Java9OptionalsStreamTest.java +++ b/core-java-9/src/test/java/com/baeldung/java9/Java9OptionalsStreamTest.java @@ -1,4 +1,4 @@ -package com.baeldung.java8; +package com.baeldung.java9; import static org.junit.Assert.assertEquals; @@ -8,7 +8,6 @@ import java.util.Optional; import java.util.stream.Collectors; import java.util.stream.Stream; -import org.junit.Before; import org.junit.Test; public class Java9OptionalsStreamTest { From 28fe826e3b6495475eaf1c2c27f6b15d2c1f58e4 Mon Sep 17 00:00:00 2001 From: slavisa-baeldung Date: Sun, 5 Mar 2017 07:29:47 +0100 Subject: [PATCH 101/112] BAEL-574 - Removing redundant pattern check --- .../cloud/bootstrap/svcbook/BookServiceApplication.java | 1 - .../spring/cloud/bootstrap/zipkin/ZipkinApplication.java | 4 ++-- 2 files changed, 2 insertions(+), 3 deletions(-) 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 63ff0fb134..3d55a59dbc 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 @@ -45,7 +45,6 @@ public class BookServiceApplication { delegate = new HttpZipkinSpanReporter(baseUrl, zipkinProperties.getFlushInterval(), zipkinProperties.getCompression().isEnabled(), spanMetricReporter); if (!span.name.matches(skipPattern)) delegate.report(span); } - if (!span.name.matches(skipPattern)) delegate.report(span); } }; } diff --git a/spring-cloud/spring-cloud-bootstrap/zipkin/src/main/java/com/baeldung/spring/cloud/bootstrap/zipkin/ZipkinApplication.java b/spring-cloud/spring-cloud-bootstrap/zipkin/src/main/java/com/baeldung/spring/cloud/bootstrap/zipkin/ZipkinApplication.java index bb1355e258..d757567156 100644 --- a/spring-cloud/spring-cloud-bootstrap/zipkin/src/main/java/com/baeldung/spring/cloud/bootstrap/zipkin/ZipkinApplication.java +++ b/spring-cloud/spring-cloud-bootstrap/zipkin/src/main/java/com/baeldung/spring/cloud/bootstrap/zipkin/ZipkinApplication.java @@ -10,6 +10,6 @@ import zipkin.server.EnableZipkinServer; @EnableZipkinServer public class ZipkinApplication { public static void main(String[] args) { - SpringApplication.run(ZipkinApplication.class, args); - } + SpringApplication.run(ZipkinApplication.class, args); + } } From c83c449fa5a7ac2462fabf0ed26969f1b037aa12 Mon Sep 17 00:00:00 2001 From: Alex Vargas Date: Sun, 5 Mar 2017 02:09:37 -0800 Subject: [PATCH 102/112] Bael 389 - Chat-like app using the Java API for WebSocket (#1265) * Project for " A Guide to the Java API for WebSocket" article * Setting dependencies correctly * Formatting adjustments * Removing tomcat7 maven plugin * Applying formatt - No spaces --- java-websocket/pom.xml | 41 ++++++ .../main/java/com/baeldung/model/Message.java | 36 +++++ .../com/baeldung/websocket/ChatEndpoint.java | 71 +++++++++ .../baeldung/websocket/MessageDecoder.java | 32 +++++ .../baeldung/websocket/MessageEncoder.java | 27 ++++ .../src/main/webapp/WEB-INF/beans.xml | 5 + .../src/main/webapp/WEB-INF/web.xml | 7 + java-websocket/src/main/webapp/index.html | 30 ++++ java-websocket/src/main/webapp/style.css | 136 ++++++++++++++++++ java-websocket/src/main/webapp/websocket.js | 23 +++ 10 files changed, 408 insertions(+) create mode 100644 java-websocket/pom.xml create mode 100644 java-websocket/src/main/java/com/baeldung/model/Message.java create mode 100644 java-websocket/src/main/java/com/baeldung/websocket/ChatEndpoint.java create mode 100644 java-websocket/src/main/java/com/baeldung/websocket/MessageDecoder.java create mode 100644 java-websocket/src/main/java/com/baeldung/websocket/MessageEncoder.java create mode 100644 java-websocket/src/main/webapp/WEB-INF/beans.xml create mode 100644 java-websocket/src/main/webapp/WEB-INF/web.xml create mode 100644 java-websocket/src/main/webapp/index.html create mode 100644 java-websocket/src/main/webapp/style.css create mode 100644 java-websocket/src/main/webapp/websocket.js diff --git a/java-websocket/pom.xml b/java-websocket/pom.xml new file mode 100644 index 0000000000..929e6491fd --- /dev/null +++ b/java-websocket/pom.xml @@ -0,0 +1,41 @@ + + 4.0.0 + com.baeldung + java-websocket + war + 0.0.1-SNAPSHOT + java-websocket Maven Webapp + http://maven.apache.org + + + UTF-8 + + + + + javax.websocket + javax.websocket-api + 1.1 + + + com.google.code.gson + gson + 2.8.0 + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.5.1 + + 1.8 + 1.8 + + + + + diff --git a/java-websocket/src/main/java/com/baeldung/model/Message.java b/java-websocket/src/main/java/com/baeldung/model/Message.java new file mode 100644 index 0000000000..3d5bbcbc5d --- /dev/null +++ b/java-websocket/src/main/java/com/baeldung/model/Message.java @@ -0,0 +1,36 @@ +package com.baeldung.model; + +public class Message { + private String from; + private String to; + private String content; + + @Override + public String toString() { + return super.toString(); + } + + public String getFrom() { + return from; + } + + public void setFrom(String from) { + this.from = from; + } + + public String getTo() { + return to; + } + + public void setTo(String to) { + this.to = to; + } + + public String getContent() { + return content; + } + + public void setContent(String content) { + this.content = content; + } +} diff --git a/java-websocket/src/main/java/com/baeldung/websocket/ChatEndpoint.java b/java-websocket/src/main/java/com/baeldung/websocket/ChatEndpoint.java new file mode 100644 index 0000000000..c4e20f4b27 --- /dev/null +++ b/java-websocket/src/main/java/com/baeldung/websocket/ChatEndpoint.java @@ -0,0 +1,71 @@ +package com.baeldung.websocket; + +import java.io.IOException; +import java.util.HashMap; +import java.util.Set; +import java.util.concurrent.CopyOnWriteArraySet; + +import javax.websocket.EncodeException; +import javax.websocket.OnClose; +import javax.websocket.OnError; +import javax.websocket.OnMessage; +import javax.websocket.OnOpen; +import javax.websocket.Session; +import javax.websocket.server.PathParam; +import javax.websocket.server.ServerEndpoint; + +import com.baeldung.model.Message; + +@ServerEndpoint(value = "/chat/{username}", decoders = MessageDecoder.class, encoders = MessageEncoder.class) +public class ChatEndpoint { + private Session session; + private static final Set chatEndpoints = new CopyOnWriteArraySet<>(); + private static HashMap users = new HashMap<>(); + + @OnOpen + public void onOpen(Session session, @PathParam("username") String username) throws IOException, EncodeException { + + this.session = session; + chatEndpoints.add(this); + users.put(session.getId(), username); + + Message message = new Message(); + message.setFrom(username); + message.setContent("Connected!"); + broadcast(message); + } + + @OnMessage + public void onMessage(Session session, Message message) throws IOException, EncodeException { + message.setFrom(users.get(session.getId())); + broadcast(message); + } + + @OnClose + public void onClose(Session session) throws IOException, EncodeException { + chatEndpoints.remove(this); + Message message = new Message(); + message.setFrom(users.get(session.getId())); + message.setContent("Disconnected!"); + broadcast(message); + } + + @OnError + public void onError(Session session, Throwable throwable) { + // Do error handling here + } + + private static void broadcast(Message message) throws IOException, EncodeException { + chatEndpoints.forEach(endpoint -> { + synchronized (endpoint) { + try { + endpoint.session.getBasicRemote() + .sendObject(message); + } catch (IOException | EncodeException e) { + e.printStackTrace(); + } + } + }); + } + +} diff --git a/java-websocket/src/main/java/com/baeldung/websocket/MessageDecoder.java b/java-websocket/src/main/java/com/baeldung/websocket/MessageDecoder.java new file mode 100644 index 0000000000..29ae5b93e6 --- /dev/null +++ b/java-websocket/src/main/java/com/baeldung/websocket/MessageDecoder.java @@ -0,0 +1,32 @@ +package com.baeldung.websocket; + +import javax.websocket.DecodeException; +import javax.websocket.Decoder; +import javax.websocket.EndpointConfig; + +import com.baeldung.model.Message; +import com.google.gson.Gson; + +public class MessageDecoder implements Decoder.Text { + @Override + public Message decode(String s) throws DecodeException { + Gson gson = new Gson(); + Message message = gson.fromJson(s, Message.class); + return message; + } + + @Override + public boolean willDecode(String s) { + return (s != null); + } + + @Override + public void init(EndpointConfig endpointConfig) { + // Custom initialization logic + } + + @Override + public void destroy() { + // Close resources + } +} diff --git a/java-websocket/src/main/java/com/baeldung/websocket/MessageEncoder.java b/java-websocket/src/main/java/com/baeldung/websocket/MessageEncoder.java new file mode 100644 index 0000000000..bfecc87a96 --- /dev/null +++ b/java-websocket/src/main/java/com/baeldung/websocket/MessageEncoder.java @@ -0,0 +1,27 @@ +package com.baeldung.websocket; + +import javax.websocket.EncodeException; +import javax.websocket.Encoder; +import javax.websocket.EndpointConfig; + +import com.baeldung.model.Message; +import com.google.gson.Gson; + +public class MessageEncoder implements Encoder.Text { + @Override + public String encode(Message message) throws EncodeException { + Gson gson = new Gson(); + String json = gson.toJson(message); + return json; + } + + @Override + public void init(EndpointConfig endpointConfig) { + // Custom initialization logic + } + + @Override + public void destroy() { + // Close resources + } +} diff --git a/java-websocket/src/main/webapp/WEB-INF/beans.xml b/java-websocket/src/main/webapp/WEB-INF/beans.xml new file mode 100644 index 0000000000..73cd0d8e10 --- /dev/null +++ b/java-websocket/src/main/webapp/WEB-INF/beans.xml @@ -0,0 +1,5 @@ + + + \ No newline at end of file diff --git a/java-websocket/src/main/webapp/WEB-INF/web.xml b/java-websocket/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000000..9f88c1f963 --- /dev/null +++ b/java-websocket/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,7 @@ + + + + Archetype Created Web Application + diff --git a/java-websocket/src/main/webapp/index.html b/java-websocket/src/main/webapp/index.html new file mode 100644 index 0000000000..2b76fb1e49 --- /dev/null +++ b/java-websocket/src/main/webapp/index.html @@ -0,0 +1,30 @@ + + + Chat + + + + + + + + + + + + + +
+ + +
+ +
+ + +
+ + + + + \ No newline at end of file diff --git a/java-websocket/src/main/webapp/style.css b/java-websocket/src/main/webapp/style.css new file mode 100644 index 0000000000..ec0982005a --- /dev/null +++ b/java-websocket/src/main/webapp/style.css @@ -0,0 +1,136 @@ +body { + font-family: Arial, Helvetica, sans-serif; + font-size: 80%; + background-color: #1f1f1f; +} + +#wrapper { + width: 960px; + margin: auto; + text-align: left; + color: #d9d9d9; +} + +p { + text-align: left; +} + +.button { + display: inline; + color: #fff; + background-color: #f2791d; + padding: 8px; + margin: auto; + border-radius: 8px; + -moz-border-radius: 8px; + -webkit-border-radius: 8px; + box-shadow: none; + border: none; +} + +.button:hover { + background-color: #ffb15e; +} +.button a, a:visited, a:hover, a:active { + color: #fff; + text-decoration: none; +} + +#addDevice { + text-align: center; + width: 960px; + margin: auto; + margin-bottom: 10px; +} + +#addDeviceForm { + text-align: left; + width: 400px; + margin: auto; + padding: 10px; +} + +#addDeviceForm span { + display: block; +} + +#content { + margin: auto; + width: 960px; +} + +.device { + width: 180px; + height: 110px; + margin: 10px; + padding: 16px; + color: #fff; + vertical-align: top; + border-radius: 8px; + -moz-border-radius: 8px; + -webkit-border-radius: 8px; + display: inline-block; +} + +.device.off { + background-color: #c8cccf; +} + +.device span { + display: block; +} + +.deviceName { + text-align: center; + font-weight: bold; + margin-bottom: 12px; +} + +.removeDevice { + margin-top: 12px; + text-align: center; +} + +.device.Appliance { + background-color: #5eb85e; +} + +.device.Appliance a:hover { + color: #a1ed82; +} + +.device.Electronics { + background-color: #0f90d1; +} + +.device.Electronics a:hover { + color: #4badd1; +} + +.device.Lights { + background-color: #c2a00c; +} + +.device.Lights a:hover { + color: #fad232; +} + +.device.Other { + background-color: #db524d; +} + +.device.Other a:hover { + color: #ff907d; +} + +.device a { + text-decoration: none; +} + +.device a:visited, a:active, a:hover { + color: #fff; +} + +.device a:hover { + text-decoration: underline; +} \ No newline at end of file diff --git a/java-websocket/src/main/webapp/websocket.js b/java-websocket/src/main/webapp/websocket.js new file mode 100644 index 0000000000..39e5687f0c --- /dev/null +++ b/java-websocket/src/main/webapp/websocket.js @@ -0,0 +1,23 @@ +var ws; + +function connect() { + var username = document.getElementById("username").value; + ws = new WebSocket("ws://" + document.location.host + "/java-websocket/chat/" + username); + + + ws.onmessage = function(event) { + var log = document.getElementById("log"); + console.log(event.data); + var message = JSON.parse(event.data); + log.innerHTML += message.from + " : " + message.content + "\n"; + }; +} + +function send() { + var content = document.getElementById("msg").value; + var json = JSON.stringify({ + "content":content + }); + + ws.send(json); +} \ No newline at end of file From 31b7ced8c4c96bbae3c1cabb12a9e0bfa821e40c Mon Sep 17 00:00:00 2001 From: nguyennamthai Date: Sun, 5 Mar 2017 17:19:42 +0700 Subject: [PATCH 103/112] Introduction to Apache Commons Lang 3 (#1301) --- apache-commons/pom.xml | 36 +++++ .../commons/lang3/ArrayUtilsTest.java | 137 ++++++++++++++++++ .../commons/lang3/StringUtilsTest.java | 99 +++++++++++++ 3 files changed, 272 insertions(+) create mode 100644 apache-commons/pom.xml create mode 100644 apache-commons/src/test/java/com/baeldung/commons/lang3/ArrayUtilsTest.java create mode 100644 apache-commons/src/test/java/com/baeldung/commons/lang3/StringUtilsTest.java diff --git a/apache-commons/pom.xml b/apache-commons/pom.xml new file mode 100644 index 0000000000..6e5c2065db --- /dev/null +++ b/apache-commons/pom.xml @@ -0,0 +1,36 @@ + + 4.0.0 + com.baeldung + apache-commons + 0.0.1-SNAPSHOT + + 4.12 + 3.6.0 + + + + + maven-compiler-plugin + ${compiler.version} + + 1.8 + 1.8 + + + + + + + org.apache.commons + commons-lang3 + 3.5 + + + junit + junit + ${junit.version} + test + + + \ No newline at end of file diff --git a/apache-commons/src/test/java/com/baeldung/commons/lang3/ArrayUtilsTest.java b/apache-commons/src/test/java/com/baeldung/commons/lang3/ArrayUtilsTest.java new file mode 100644 index 0000000000..b579458730 --- /dev/null +++ b/apache-commons/src/test/java/com/baeldung/commons/lang3/ArrayUtilsTest.java @@ -0,0 +1,137 @@ +package com.baeldung.commons.lang3; + +import org.apache.commons.lang3.ArrayUtils; +import org.junit.Test; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertArrayEquals; + +public class ArrayUtilsTest { + @Test + public void givenArray_whenAddingElementAtSpecifiedPosition_thenCorrect() { + int[] oldArray = { 2, 3, 4, 5 }; + int[] newArray = ArrayUtils.add(oldArray, 0, 1); + int[] expectedArray = { 1, 2, 3, 4, 5 }; + assertArrayEquals(expectedArray, newArray); + } + + @Test + public void givenArray_whenAddingElementAtTheEnd_thenCorrect() { + int[] oldArray = { 2, 3, 4, 5 }; + int[] newArray = ArrayUtils.add(oldArray, 1); + int[] expectedArray = { 2, 3, 4, 5, 1 }; + assertArrayEquals(expectedArray, newArray); + } + + @Test + public void givenArray_whenAddingAllElementsAtTheEnd_thenCorrect() { + int[] oldArray = { 0, 1, 2 }; + int[] newArray = ArrayUtils.addAll(oldArray, 3, 4, 5); + int[] expectedArray = { 0, 1, 2, 3, 4, 5 }; + assertArrayEquals(expectedArray, newArray); + } + + @Test + public void givenArray_whenRemovingElementAtSpecifiedPosition_thenCorrect() { + int[] oldArray = { 1, 2, 3, 4, 5 }; + int[] newArray = ArrayUtils.remove(oldArray, 1); + int[] expectedArray = { 1, 3, 4, 5 }; + assertArrayEquals(expectedArray, newArray); + } + + @Test + public void givenArray_whenRemovingAllElementsAtSpecifiedPositions_thenCorrect() { + int[] oldArray = { 1, 2, 3, 4, 5 }; + int[] newArray = ArrayUtils.removeAll(oldArray, 1, 3); + int[] expectedArray = { 1, 3, 5 }; + assertArrayEquals(expectedArray, newArray); + } + + @Test + public void givenArray_whenRemovingAnElement_thenCorrect() { + int[] oldArray = { 1, 2, 3, 3, 4 }; + int[] newArray = ArrayUtils.removeElement(oldArray, 3); + int[] expectedArray = { 1, 2, 3, 4 }; + assertArrayEquals(expectedArray, newArray); + } + + @Test + public void givenArray_whenRemovingElements_thenCorrect() { + int[] oldArray = { 1, 2, 3, 3, 4 }; + int[] newArray = ArrayUtils.removeElements(oldArray, 2, 3, 5); + int[] expectedArray = { 1, 3, 4 }; + assertArrayEquals(expectedArray, newArray); + } + + @Test + public void givenArray_whenRemovingAllElementOccurences_thenCorrect() { + int[] oldArray = { 1, 2, 2, 2, 3 }; + int[] newArray = ArrayUtils.removeAllOccurences(oldArray, 2); + int[] expectedArray = { 1, 3 }; + assertArrayEquals(expectedArray, newArray); + } + + @Test + public void givenArray_whenCheckingExistingElement_thenCorrect() { + int[] array = { 1, 3, 5, 7, 9 }; + boolean evenContained = ArrayUtils.contains(array, 2); + boolean oddContained = ArrayUtils.contains(array, 7); + assertEquals(false, evenContained); + assertEquals(true, oddContained); + } + + @Test + public void givenArray_whenReversingElementsWithinARange_thenCorrect() { + int[] originalArray = { 1, 2, 3, 4, 5 }; + ArrayUtils.reverse(originalArray, 1, 4); + int[] expectedArray = { 1, 4, 3, 2, 5 }; + assertArrayEquals(expectedArray, originalArray); + } + + @Test + public void givenArray_whenReversingAllElements_thenCorrect() { + int[] originalArray = { 1, 2, 3, 4, 5 }; + ArrayUtils.reverse(originalArray); + int[] expectedArray = { 5, 4, 3, 2, 1 }; + assertArrayEquals(expectedArray, originalArray); + } + + @Test + public void givenArray_whenShiftingElementsWithinARange_thenCorrect() { + int[] originalArray = { 1, 2, 3, 4, 5 }; + ArrayUtils.shift(originalArray, 1, 4, 1); + int[] expectedArray = { 1, 4, 2, 3, 5 }; + assertArrayEquals(expectedArray, originalArray); + } + + @Test + public void givenArray_whenShiftingAllElements_thenCorrect() { + int[] originalArray = { 1, 2, 3, 4, 5 }; + ArrayUtils.shift(originalArray, 1); + int[] expectedArray = { 5, 1, 2, 3, 4 }; + assertArrayEquals(expectedArray, originalArray); + } + + @Test + public void givenArray_whenExtractingElements_thenCorrect() { + int[] oldArray = { 1, 2, 3, 4, 5 }; + int[] newArray = ArrayUtils.subarray(oldArray, 2, 7); + int[] expectedArray = { 3, 4, 5 }; + assertArrayEquals(expectedArray, newArray); + } + + @Test + public void givenArray_whenSwapingElementsWithinARange_thenCorrect() { + int[] originalArray = { 1, 2, 3, 4, 5 }; + ArrayUtils.swap(originalArray, 0, 3, 2); + int[] expectedArray = { 4, 5, 3, 1, 2 }; + assertArrayEquals(expectedArray, originalArray); + } + + @Test + public void givenArray_whenSwapingElementsAtSpecifiedPositions_thenCorrect() { + int[] originalArray = { 1, 2, 3, 4, 5 }; + ArrayUtils.swap(originalArray, 0, 3); + int[] expectedArray = { 4, 2, 3, 1, 5 }; + assertArrayEquals(expectedArray, originalArray); + } +} diff --git a/apache-commons/src/test/java/com/baeldung/commons/lang3/StringUtilsTest.java b/apache-commons/src/test/java/com/baeldung/commons/lang3/StringUtilsTest.java new file mode 100644 index 0000000000..a17a8ea60d --- /dev/null +++ b/apache-commons/src/test/java/com/baeldung/commons/lang3/StringUtilsTest.java @@ -0,0 +1,99 @@ +package com.baeldung.commons.lang3; + +import org.apache.commons.lang3.StringUtils; +import org.junit.Test; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.assertFalse; + +public class StringUtilsTest { + @Test + public void givenString_whenCheckingContainsAny_thenCorrect() { + String string = "baeldung.com"; + boolean contained1 = StringUtils.containsAny(string, 'a', 'b', 'c'); + boolean contained2 = StringUtils.containsAny(string, 'x', 'y', 'z'); + boolean contained3 = StringUtils.containsAny(string, "abc"); + boolean contained4 = StringUtils.containsAny(string, "xyz"); + assertTrue(contained1); + assertFalse(contained2); + assertTrue(contained3); + assertFalse(contained4); + } + + @Test + public void givenString_whenCheckingContainsIgnoreCase_thenCorrect() { + String string = "baeldung.com"; + boolean contained = StringUtils.containsIgnoreCase(string, "BAELDUNG"); + assertTrue(contained); + } + + @Test + public void givenString_whenCountingMatches_thenCorrect() { + String string = "welcome to www.baeldung.com"; + int charNum = StringUtils.countMatches(string, 'w'); + int stringNum = StringUtils.countMatches(string, "com"); + assertEquals(4, charNum); + assertEquals(2, stringNum); + } + + @Test + public void givenString_whenAppendingAndPrependingIfMissing_thenCorrect() { + String string = "baeldung.com"; + String stringWithSuffix = StringUtils.appendIfMissing(string, ".com"); + String stringWithPrefix = StringUtils.prependIfMissing(string, "www."); + assertEquals("baeldung.com", stringWithSuffix); + assertEquals("www.baeldung.com", stringWithPrefix); + } + + @Test + public void givenString_whenSwappingCase_thenCorrect() { + String originalString = "baeldung.COM"; + String swappedString = StringUtils.swapCase(originalString); + assertEquals("BAELDUNG.com", swappedString); + } + + @Test + public void givenString_whenCapitalizing_thenCorrect() { + String originalString = "baeldung"; + String capitalizedString = StringUtils.capitalize(originalString); + assertEquals("Baeldung", capitalizedString); + } + + @Test + public void givenString_whenUncapitalizing_thenCorrect() { + String originalString = "Baeldung"; + String uncapitalizedString = StringUtils.uncapitalize(originalString); + assertEquals("baeldung", uncapitalizedString); + } + + @Test + public void givenString_whenReversingCharacters_thenCorrect() { + String originalString = "baeldung"; + String reversedString = StringUtils.reverse(originalString); + assertEquals("gnudleab", reversedString); + } + + @Test + public void givenString_whenReversingWithDelimiter_thenCorrect() { + String originalString = "www.baeldung.com"; + String reversedString = StringUtils.reverseDelimited(originalString, '.'); + assertEquals("com.baeldung.www", reversedString); + } + + @Test + public void givenString_whenRotatingTwoPositions_thenCorrect() { + String originalString = "baeldung"; + String rotatedString = StringUtils.rotate(originalString, 4); + assertEquals("dungbael", rotatedString); + } + + @Test + public void givenTwoStrings_whenComparing_thenCorrect() { + String tutorials = "Baeldung Tutorials"; + String courses = "Baeldung Courses"; + String diff1 = StringUtils.difference(tutorials, courses); + String diff2 = StringUtils.difference(courses, tutorials); + assertEquals("Courses", diff1); + assertEquals("Tutorials", diff2); + } +} From b9314b142c4adebefafec463fc0850e892816899 Mon Sep 17 00:00:00 2001 From: Paulo Motta Date: Sun, 5 Mar 2017 08:48:22 -0300 Subject: [PATCH 104/112] Create PrimitiveConversionsJUnitTest.java Tests for the possible conversions for primitive data types --- .../PrimitiveConversionsJUnitTest.java | 125 ++++++++++++++++++ 1 file changed, 125 insertions(+) create mode 100644 core-java/src/test/java/com/baeldung/PrimitiveConversionsJUnitTest.java diff --git a/core-java/src/test/java/com/baeldung/PrimitiveConversionsJUnitTest.java b/core-java/src/test/java/com/baeldung/PrimitiveConversionsJUnitTest.java new file mode 100644 index 0000000000..4137b5af00 --- /dev/null +++ b/core-java/src/test/java/com/baeldung/PrimitiveConversionsJUnitTest.java @@ -0,0 +1,125 @@ +package com.baeldung.primitiveconversions; + +import org.junit.Test; +import static org.junit.Assert.*; + +/** + * + * @author paulo.motta + */ +public class PrimitiveConversionsJUnitTest { + + @Test + public void givenDataWithLessBits_whenAttributingToLargerSizeVariable_thenNoSpecialNotation() { + int myInt = 127; + + long myLong = myInt; + assertEquals(127L, myLong); + + float myFloat = myLong; + assertEquals(127.0f, myFloat, 0.00001f); + + double myDouble = myLong; + assertEquals(127.0, myDouble,0.00001); + } + + @Test + public void givenDataWithMoreBits_whenAttributingToSmallerSizeVariable_thenCastOperatorNeeded() { + + long myLong = 127L; + double myDouble = 127.0; + + float myFloat = (float) myDouble; + assertEquals(127.0f, myFloat, 0.00001f); + + int myInt = (int) myLong; + assertEquals(127, myInt); + + byte myByte = (byte) myInt; + assertEquals( ((byte)127), myByte); + } + + @Test + public void givenPrimitiveData_whenAssiginingToWrapper_thenAutomaticBoxingHappens(){ + int myInt = 127; + + Integer myIntegerReference = myInt; + assertEquals(new Integer("127"), myIntegerReference); + + } + + @Test + public void givenWrapperObjectData_whenAssiginingToPrimitive_thenAutomaticUnboxingHappens(){ + Integer myIntegerReference = new Integer("127"); + + int myOtherInt = myIntegerReference; + assertEquals(127, myOtherInt); + + } + + @Test + public void givenByteValue_whenConvertingToChar_thenWidenAndNarrowTakesPlace(){ + byte myLargeValueByte = (byte) 130; //0b10000010 + System.out.println(myLargeValueByte); //0b10000010 -126 + assertEquals( -126, myLargeValueByte); + + int myLargeValueInt = myLargeValueByte; + System.out.println(myLargeValueInt); //0b11111111 11111111 11111111 10000010 -126 + assertEquals( -126, myLargeValueInt); + + char myLargeValueChar = (char) myLargeValueByte; + System.out.println(myLargeValueChar);//0b11111111 10000010 unsigned 0xFF82 + assertEquals(0xFF82, myLargeValueChar); + + myLargeValueInt = myLargeValueChar; + System.out.println(myLargeValueInt); //0b11111111 10000010 65410 + assertEquals(65410, myLargeValueInt); + + byte myOtherByte = (byte) myLargeValueInt; + System.out.println(myOtherByte); //0b10000010 -126 + assertEquals( -126, myOtherByte); + + + char myLargeValueChar2 = 130; //This is an int not a byte! + System.out.println(myLargeValueChar2);//0b00000000 10000010 unsigned 0x0082 + assertEquals(0x0082, myLargeValueChar2); + + int myLargeValueInt2 = myLargeValueChar2; + System.out.println(myLargeValueInt2); //0b00000000 10000010 130 + assertEquals(130, myLargeValueInt2); + + byte myOtherByte2 = (byte) myLargeValueInt2; + System.out.println(myOtherByte2); //0b10000010 -126 + assertEquals( -126, myOtherByte2); + } + + @Test + public void givenString_whenParsingWithWrappers_thenValuesAreReturned(){ + String myString = "127"; + + byte myNewByte = Byte.parseByte(myString); + assertEquals( ((byte)127), myNewByte); + + short myNewShort = Short.parseShort(myString); + assertEquals( ((short)127), myNewShort); + + int myNewInt = Integer.parseInt(myString); + assertEquals( 127, myNewInt); + + long myNewLong = Long.parseLong(myString); + assertEquals( 127L, myNewLong); + + float myNewFloat = Float.parseFloat(myString); + assertEquals( 127.0f, myNewFloat, 0.00001f); + + double myNewDouble = Double.parseDouble(myString); + assertEquals( 127.0, myNewDouble, 0.00001f); + + boolean myNewBoolean = Boolean.parseBoolean(myString); + assertEquals( false, myNewBoolean); //numbers are not true! + + char myNewChar = myString.charAt(0); + assertEquals( 49, myNewChar); //the value of '1' + } + +} From f0c4486cb126f6cbd4e2d51e8aa00e1878bba79d Mon Sep 17 00:00:00 2001 From: maibin Date: Sun, 5 Mar 2017 19:21:35 +0100 Subject: [PATCH 105/112] Ant Colony Optimization updates (#1306) * Ant Colony Optimization * Updated code for Ant Colony --- .../ga/ant_colony/AntColonyOptimization.java | 97 +++++++++---------- .../algorithms/AntColonyOptimizationTest.java | 2 +- 2 files changed, 47 insertions(+), 52 deletions(-) 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 index e46ac77e84..27a270f906 100644 --- 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 @@ -1,7 +1,10 @@ package com.baeldung.algorithms.ga.ant_colony; +import java.util.ArrayList; import java.util.Arrays; +import java.util.List; import java.util.Random; +import java.util.stream.IntStream; public class AntColonyOptimization { @@ -19,7 +22,7 @@ public class AntColonyOptimization { public int numberOfAnts; private double graph[][]; private double trails[][]; - private Ant ants[]; + private List ants = new ArrayList<>(); private Random random = new Random(); private double probabilities[]; @@ -35,101 +38,92 @@ public class AntColonyOptimization { 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); - } + IntStream.range(0, numberOfAnts).forEach(i -> ants.add(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++) { + IntStream.range(0, n).forEach(i -> { + IntStream.range(0, n).forEach(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++) { + public void startAntOptimization() { + IntStream.rangeClosed(1, 3).forEach(i -> { System.out.println("Attempt #" + i); - finalResult = solve(); - } - return finalResult; + solve(); + }); } - + /** * Use this method to run the main logic + * * @return */ - private int[] solve() { + public int[] solve() { setupAnts(); clearTrails(); - int iteration = 0; - while (iteration < maxIterations) { + IntStream.range(0, maxIterations).forEach(i -> { 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++; + IntStream.range(0, numberOfAnts).forEach(i -> { + ants.stream().forEach(ant -> { + ant.clear(); + ant.visitCity(-1, random.nextInt(numberOfCities)); + }); + }); + currentIndex = 0; } - + /** * At each iteration, move ants */ private void moveAnts() { - while (currentIndex < numberOfCities - 1) { - for (Ant a : ants) - a.visitCity(currentIndex, selectNextCity(a)); + IntStream.range(currentIndex, numberOfCities - 1).forEach(i -> { + ants.stream().forEach(ant -> { + ant.visitCity(currentIndex, selectNextCity(ant)); + }); currentIndex++; - } + }); } - + /** * Select next city for each ant + * * @param ant * @return */ private int selectNextCity(Ant ant) { + int t = random.nextInt(numberOfCities - currentIndex); 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; - } - } + IntStream.range(0, numberOfCities).filter(i -> i == t && !ant.visited(i)).findFirst(); } calculateProbabilities(ant); double r = random.nextDouble(); @@ -146,9 +140,10 @@ public class AntColonyOptimization { /** * Calculate the next city picks probabilites + * * @param ant */ - private void calculateProbabilities(Ant ant) { + public void calculateProbabilities(Ant ant) { int i = ant.trail[currentIndex]; double pheromone = 0.0; for (int l = 0; l < numberOfCities; l++) { @@ -189,8 +184,8 @@ public class AntColonyOptimization { */ private void updateBest() { if (bestTourOrder == null) { - bestTourOrder = ants[0].trail; - bestTourLength = ants[0].trailLength(graph); + bestTourOrder = ants.get(0).trail; + bestTourLength = ants.get(0).trailLength(graph); } for (Ant a : ants) { if (a.trailLength(graph) < bestTourLength) { @@ -204,9 +199,9 @@ public class AntColonyOptimization { * 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; + IntStream.range(0, numberOfCities).forEach(i -> { + IntStream.range(0, numberOfCities).forEach(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 index cd8efaa106..40d2464ab6 100644 --- a/core-java/src/test/java/com/baeldung/algorithms/AntColonyOptimizationTest.java +++ b/core-java/src/test/java/com/baeldung/algorithms/AntColonyOptimizationTest.java @@ -16,7 +16,7 @@ public class AntColonyOptimizationTest { @Test public void testStartAntOptimization() { AntColonyOptimization antTSP = new AntColonyOptimization(5); - Assert.assertNotNull(antTSP.startAntOptimization()); + Assert.assertNotNull(antTSP.solve()); } } From 6cc10132e18b2e2595409676f0de3053019ee943 Mon Sep 17 00:00:00 2001 From: lor6 Date: Sun, 5 Mar 2017 22:01:51 +0200 Subject: [PATCH 106/112] move rest-angular app to existing rest project (#1293) * move rest-angular app to existing rest project * upgrade boot version --- spring-data-rest/pom.xml | 2 +- .../java/com/baeldung/config/MvcConfig.java | 25 ++++ .../baeldung/repositories/UserRepository.java | 3 +- spring-data-rest/src/main/webapp/users.html | 51 ++++++++ spring-data-rest/src/main/webapp/view/app.js | 122 ++++++++++++++++++ .../SpringDataRelationshipsTest.java | 7 +- 6 files changed, 205 insertions(+), 5 deletions(-) create mode 100644 spring-data-rest/src/main/java/com/baeldung/config/MvcConfig.java create mode 100644 spring-data-rest/src/main/webapp/users.html create mode 100644 spring-data-rest/src/main/webapp/view/app.js diff --git a/spring-data-rest/pom.xml b/spring-data-rest/pom.xml index 4e8001ae7b..1845d60e94 100644 --- a/spring-data-rest/pom.xml +++ b/spring-data-rest/pom.xml @@ -14,7 +14,7 @@ org.springframework.boot spring-boot-starter-parent - 1.4.4.RELEASE + 1.5.1.RELEASE diff --git a/spring-data-rest/src/main/java/com/baeldung/config/MvcConfig.java b/spring-data-rest/src/main/java/com/baeldung/config/MvcConfig.java new file mode 100644 index 0000000000..bed1a6f846 --- /dev/null +++ b/spring-data-rest/src/main/java/com/baeldung/config/MvcConfig.java @@ -0,0 +1,25 @@ +package com.baeldung.config; + +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 +public class MvcConfig extends WebMvcConfigurerAdapter{ + + public MvcConfig(){ + super(); + } + + @Override + public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { + configurer.enable(); + } + +} diff --git a/spring-data-rest/src/main/java/com/baeldung/repositories/UserRepository.java b/spring-data-rest/src/main/java/com/baeldung/repositories/UserRepository.java index 0b55ac89b6..2bd9c025dd 100644 --- a/spring-data-rest/src/main/java/com/baeldung/repositories/UserRepository.java +++ b/spring-data-rest/src/main/java/com/baeldung/repositories/UserRepository.java @@ -2,9 +2,10 @@ package com.baeldung.repositories; import org.springframework.data.repository.CrudRepository; import org.springframework.data.rest.core.annotation.RepositoryRestResource; - +import org.springframework.web.bind.annotation.CrossOrigin; import com.baeldung.models.WebsiteUser; +@CrossOrigin @RepositoryRestResource(collectionResourceRel = "users", path = "users") public interface UserRepository extends CrudRepository { diff --git a/spring-data-rest/src/main/webapp/users.html b/spring-data-rest/src/main/webapp/users.html new file mode 100644 index 0000000000..c153f4a36e --- /dev/null +++ b/spring-data-rest/src/main/webapp/users.html @@ -0,0 +1,51 @@ + + + + +User CRUD + + + + + +
+ + + + + + + + + + + + + +
ID:
Name:
Email:
+

+ Get User + Update User + Add User + Delete User + +

+

{{message}}

+

{{errorMessage}}

+ +
+
+ Get all Users
+

+
+ {{usr.name}} {{usr.email}} +
+
+ + \ No newline at end of file diff --git a/spring-data-rest/src/main/webapp/view/app.js b/spring-data-rest/src/main/webapp/view/app.js new file mode 100644 index 0000000000..0bdc6e1979 --- /dev/null +++ b/spring-data-rest/src/main/webapp/view/app.js @@ -0,0 +1,122 @@ +var app = angular.module('app',[]); + +app.controller('UserCRUDCtrl', ['$scope','UserCRUDService', function ($scope,UserCRUDService) { + + $scope.updateUser = function () { + UserCRUDService.updateUser($scope.user.id,$scope.user.name,$scope.user.email) + .then(function success(response){ + $scope.message = 'User data updated!'; + $scope.errorMessage = ''; + }, + function error(response){ + $scope.errorMessage = 'Error updating user!'; + $scope.message = ''; + }); + } + + $scope.getUser = function () { + var id = $scope.user.id; + UserCRUDService.getUser($scope.user.id) + .then(function success(response){ + $scope.user = response.data; + $scope.user.id = id; + $scope.message=''; + $scope.errorMessage = ''; + }, + function error (response ){ + $scope.message = ''; + if (response.status === 404){ + $scope.errorMessage = 'User not found!'; + } + else { + $scope.errorMessage = "Error getting user!"; + } + }); + } + + $scope.addUser = function () { + if ($scope.user != null && $scope.user.name) { + UserCRUDService.addUser($scope.user.name, $scope.user.email) + .then (function success(response){ + $scope.message = 'User added!'; + $scope.errorMessage = ''; + }, + function error(response){ + $scope.errorMessage = 'Error adding user!'; + $scope.message = ''; + }); + } + else { + $scope.errorMessage = 'Please enter a name!'; + $scope.message = ''; + } + } + + $scope.deleteUser = function () { + UserCRUDService.deleteUser($scope.user.id) + .then (function success(response){ + $scope.message = 'User deleted!'; + $scope.user = null; + $scope.errorMessage=''; + }, + function error(response){ + $scope.errorMessage = 'Error deleting user!'; + $scope.message=''; + }) + } + + $scope.getAllUsers = function () { + UserCRUDService.getAllUsers() + .then(function success(response){ + $scope.users = response.data._embedded.users; + $scope.message=''; + $scope.errorMessage = ''; + }, + function error (response ){ + $scope.message=''; + $scope.errorMessage = 'Error getting users!'; + }); + } + +}]); + +app.service('UserCRUDService',['$http', function ($http) { + + this.getUser = function getUser(userId){ + return $http({ + method: 'GET', + url: 'users/'+userId + }); + } + + this.addUser = function addUser(name, email){ + return $http({ + method: 'POST', + url: 'users', + data: {name:name, email:email} + }); + } + + this.deleteUser = function deleteUser(id){ + return $http({ + method: 'DELETE', + url: 'users/'+id + }) + } + + this.updateUser = function updateUser(id,name,email){ + return $http({ + method: 'PATCH', + url: 'users/'+id, + data: {name:name, email:email} + }) + } + + this.getAllUsers = function getAllUsers(){ + return $http({ + method: 'GET', + url: 'users' + }); + } + +}]); \ No newline at end of file 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 21a067a645..43b5dd7da6 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 @@ -18,6 +18,7 @@ import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; import org.springframework.http.ResponseEntity; import org.springframework.test.context.junit4.SpringRunner; +import org.json.JSONException; import static org.junit.Assert.assertEquals; @@ -37,7 +38,7 @@ public class SpringDataRelationshipsTest { private static final String AUTHOR_NAME = "George Orwell"; @Test - public void whenSaveOneToOneRelationship_thenCorrect() { + public void whenSaveOneToOneRelationship_thenCorrect() throws JSONException { Library library = new Library(LIBRARY_NAME); template.postForEntity(LIBRARY_ENDPOINT, library, Library.class); @@ -55,7 +56,7 @@ public class SpringDataRelationshipsTest { } @Test - public void whenSaveOneToManyRelationship_thenCorrect() { + public void whenSaveOneToManyRelationship_thenCorrect() throws JSONException{ Library library = new Library(LIBRARY_NAME); template.postForEntity(LIBRARY_ENDPOINT, library, Library.class); @@ -77,7 +78,7 @@ public class SpringDataRelationshipsTest { } @Test - public void whenSaveManyToManyRelationship_thenCorrect() { + public void whenSaveManyToManyRelationship_thenCorrect() throws JSONException{ Author author1 = new Author(AUTHOR_NAME); template.postForEntity(AUTHOR_ENDPOINT, author1, Author.class); From 5392bdd8f33797cb43876e3ea521752ebe40a346 Mon Sep 17 00:00:00 2001 From: lor6 Date: Sun, 5 Mar 2017 22:05:58 +0200 Subject: [PATCH 107/112] remove extra code (#1294) --- spring-rest-angular/pom.xml | 9 -- .../web/controller/EmployeeController.java | 14 -- .../web/dao/EmployeeCRUDRepository.java | 13 -- .../org/baeldung/web/entity/Employee.java | 57 ------- .../java/org/baeldung/web/main/MvcConfig.java | 36 ----- .../baeldung/web/main/PersistenceConfig.java | 2 +- .../src/main/resources/db/sql/employees.sql | 16 -- .../main/webapp/WEB-INF/pages/employee.html | 55 ------- .../src/main/webapp/view/app.js | 146 +----------------- ...EmployeeCRUDRepositoryIntegrationTest.java | 47 ------ 10 files changed, 3 insertions(+), 392 deletions(-) delete mode 100644 spring-rest-angular/src/main/java/org/baeldung/web/controller/EmployeeController.java delete mode 100644 spring-rest-angular/src/main/java/org/baeldung/web/dao/EmployeeCRUDRepository.java delete mode 100644 spring-rest-angular/src/main/java/org/baeldung/web/entity/Employee.java delete mode 100644 spring-rest-angular/src/main/java/org/baeldung/web/main/MvcConfig.java delete mode 100644 spring-rest-angular/src/main/resources/db/sql/employees.sql delete mode 100644 spring-rest-angular/src/main/webapp/WEB-INF/pages/employee.html delete 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 62ab03522d..b6bc95df81 100644 --- a/spring-rest-angular/pom.xml +++ b/spring-rest-angular/pom.xml @@ -74,15 +74,6 @@ 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 deleted file mode 100644 index a8bfc254c3..0000000000 --- a/spring-rest-angular/src/main/java/org/baeldung/web/controller/EmployeeController.java +++ /dev/null @@ -1,14 +0,0 @@ -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 deleted file mode 100644 index 1e5f81ed45..0000000000 --- a/spring-rest-angular/src/main/java/org/baeldung/web/dao/EmployeeCRUDRepository.java +++ /dev/null @@ -1,13 +0,0 @@ -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/entity/Employee.java b/spring-rest-angular/src/main/java/org/baeldung/web/entity/Employee.java deleted file mode 100644 index 8d6831726c..0000000000 --- a/spring-rest-angular/src/main/java/org/baeldung/web/entity/Employee.java +++ /dev/null @@ -1,57 +0,0 @@ -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 deleted file mode 100644 index b24aad1177..0000000000 --- a/spring-rest-angular/src/main/java/org/baeldung/web/main/MvcConfig.java +++ /dev/null @@ -1,36 +0,0 @@ -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 8454ce155a..e0f6002733 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 @@ -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").addScript("db/sql/employees.sql").build(); + EmbeddedDatabase db = builder.setType(EmbeddedDatabaseType.HSQL).addScript("db/sql/data.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 deleted file mode 100644 index 366c0c309a..0000000000 --- a/spring-rest-angular/src/main/resources/db/sql/employees.sql +++ /dev/null @@ -1,16 +0,0 @@ -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 deleted file mode 100644 index 510e981f25..0000000000 --- a/spring-rest-angular/src/main/webapp/WEB-INF/pages/employee.html +++ /dev/null @@ -1,55 +0,0 @@ - - - - -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 9f78d5adf7..b56497807c 100644 --- a/spring-rest-angular/src/main/webapp/view/app.js +++ b/spring-rest-angular/src/main/webapp/view/app.js @@ -5,8 +5,8 @@ app.controller('StudentCtrl', ['$scope','StudentService', function ($scope,Stude pageNumber: 1, pageSize: 5, sort: null - }; - + }; + StudentService.getStudents(paginationOptions.pageNumber, paginationOptions.pageSize).success(function(data){ $scope.gridOptions.data = data.content; @@ -54,145 +54,3 @@ app.service('StudentService',['$http', function ($http) { }; }]); - -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 deleted file mode 100644 index 57fe793596..0000000000 --- a/spring-rest-angular/src/test/java/org/baeldung/web/service/EmployeeCRUDRepositoryIntegrationTest.java +++ /dev/null @@ -1,47 +0,0 @@ -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 57259c4a06a45dfdcab31258dfe79774e8a2a918 Mon Sep 17 00:00:00 2001 From: Abhinab Kanrar Date: Mon, 6 Mar 2017 02:33:15 +0530 Subject: [PATCH 108/112] Twitter4J (#1300) * 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 * Twitter4J * Update Application.java * Update ApplicationTest.java * Update twitter4j.properties --- Twitter4J/pom.xml | 58 +++++++++ .../main/java/com/baeldung/Application.java | 117 ++++++++++++++++++ .../src/main/resources/twitter4j.properties | 4 + .../java/com/baeldung/ApplicationTest.java | 40 ++++++ 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 9 files changed, 219 insertions(+) create mode 100644 Twitter4J/pom.xml create mode 100644 Twitter4J/src/main/java/com/baeldung/Application.java create mode 100644 Twitter4J/src/main/resources/twitter4j.properties create mode 100644 Twitter4J/src/test/java/com/baeldung/ApplicationTest.java 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 diff --git a/Twitter4J/pom.xml b/Twitter4J/pom.xml new file mode 100644 index 0000000000..ebe5a7d409 --- /dev/null +++ b/Twitter4J/pom.xml @@ -0,0 +1,58 @@ + + 4.0.0 + com.mabsisa + Twitter4J + jar + 1.0-SNAPSHOT + Twitter4J + http://maven.apache.org + + + UTF-8 + UTF-8 + 1.8 + 1.8 + 1.8 + + + + + org.twitter4j + twitter4j-core + 4.0.6 + + + org.twitter4j + twitter4j-stream + 4.0.6 + + + junit + junit + 4.12 + + + + + ${project.artifactId} + + + src/main/resources + + + + + org.apache.maven.plugins + maven-surefire-plugin + 2.19.1 + + + **/ApplicationTest.java + + + + + + + diff --git a/Twitter4J/src/main/java/com/baeldung/Application.java b/Twitter4J/src/main/java/com/baeldung/Application.java new file mode 100644 index 0000000000..f7da27db29 --- /dev/null +++ b/Twitter4J/src/main/java/com/baeldung/Application.java @@ -0,0 +1,117 @@ +/** + * + */ +package com.baeldung; + +import java.util.List; +import java.util.stream.Collectors; + +import twitter4j.DirectMessage; +import twitter4j.Query; +import twitter4j.QueryResult; +import twitter4j.StallWarning; +import twitter4j.Status; +import twitter4j.StatusDeletionNotice; +import twitter4j.StatusListener; +import twitter4j.Twitter; +import twitter4j.TwitterException; +import twitter4j.TwitterFactory; +import twitter4j.TwitterStream; +import twitter4j.TwitterStreamFactory; +import twitter4j.conf.ConfigurationBuilder; + +public class Application { + + public static Twitter getTwitterinstance() { + /** + * if not using properties file, we can set access token by following way + */ +// ConfigurationBuilder cb = new ConfigurationBuilder(); +// cb.setDebugEnabled(true) +// .setOAuthConsumerKey("DPHTBsWWO42d8rzshxlK0OwSY") +// .setOAuthConsumerSecret("ACLXkeRY98NiaVCG1ai8fdYt0GoEGJbFeTuxjulSCO7sLKqls1") +// .setOAuthAccessToken("838080188214759428-9MSK1ddPTN5ZZHbddjFI7s75mYgmCFQ") +// .setOAuthAccessTokenSecret("1eXAADpHShAzQh5hGWLBUQHLysOuAKIOapmQQ8U0OVk2c"); +// +// TwitterFactory tf = new TwitterFactory(cb.build()); +// Twitter twitter = tf.getSingleton(); + + Twitter twitter = TwitterFactory.getSingleton(); + return twitter; + + } + + public static String createTweet(String tweet) throws TwitterException { + Twitter twitter = getTwitterinstance(); + Status status = twitter.updateStatus("creating baeldung API"); + return status.getText(); + } + + public static List getTimeLine() throws TwitterException { + Twitter twitter = getTwitterinstance(); + List statuses = twitter.getHomeTimeline(); + return statuses.stream().map( + item -> item.getText()).collect( + Collectors.toList()); + } + + public static String sendDirectMessage(String recipientName, String msg) throws TwitterException { + Twitter twitter = getTwitterinstance(); + DirectMessage message = twitter.sendDirectMessage(recipientName, msg); + return message.getText(); + } + + public static List searchtweets() throws TwitterException { + Twitter twitter = getTwitterinstance(); + Query query = new Query("source:twitter4j baeldung"); + QueryResult result = twitter.search(query); + List statuses = result.getTweets(); + return statuses.stream().map( + item -> item.getText()).collect( + Collectors.toList()); + } + + public static void streamFeed() { + + StatusListener listener = new StatusListener(){ + + @Override + public void onException(Exception e) { + e.printStackTrace(); + } + + @Override + public void onDeletionNotice(StatusDeletionNotice arg) { + System.out.println("Got a status deletion notice id:" + arg.getStatusId()); + } + + @Override + public void onScrubGeo(long userId, long upToStatusId) { + System.out.println("Got scrub_geo event userId:" + userId + " upToStatusId:" + upToStatusId); + } + + @Override + public void onStallWarning(StallWarning warning) { + System.out.println("Got stall warning:" + warning); + } + + @Override + public void onStatus(Status status) { + System.out.println(status.getUser().getName() + " : " + status.getText()); + } + + @Override + public void onTrackLimitationNotice(int numberOfLimitedStatuses) { + System.out.println("Got track limitation notice:" + numberOfLimitedStatuses); + } + }; + + TwitterStream twitterStream = new TwitterStreamFactory().getInstance(); + + twitterStream.addListener(listener); + + twitterStream.sample(); + + } + +} diff --git a/Twitter4J/src/main/resources/twitter4j.properties b/Twitter4J/src/main/resources/twitter4j.properties new file mode 100644 index 0000000000..ee11dc62a1 --- /dev/null +++ b/Twitter4J/src/main/resources/twitter4j.properties @@ -0,0 +1,4 @@ +oauth.consumerKey=//TODO +oauth.consumerSecret=//TODO +oauth.accessToken=//TODO +oauth.accessTokenSecret=//TODO diff --git a/Twitter4J/src/test/java/com/baeldung/ApplicationTest.java b/Twitter4J/src/test/java/com/baeldung/ApplicationTest.java new file mode 100644 index 0000000000..a1c778679c --- /dev/null +++ b/Twitter4J/src/test/java/com/baeldung/ApplicationTest.java @@ -0,0 +1,40 @@ +package com.baeldung; + +import static org.junit.Assert.assertEquals; + +import java.util.ArrayList; +import java.util.List; + +import org.junit.Test; + +import twitter4j.TwitterException; + +public class ApplicationTest { + + /** + * In order run this jUnit test you need to configure your API details in the twitter4j.properties + */ + + String tweet = "baeldung is awsome"; + + @Test + public void givenText_updateStatus() throws TwitterException { + String text = Application.createTweet(tweet); + assertEquals(tweet, text); + } + + @Test + public void givenCredential_fetchStatus() throws TwitterException { + List statuses = Application.getTimeLine(); + List expectedStatuses = new ArrayList(); + expectedStatuses.add(tweet); + assertEquals(expectedStatuses, statuses); + } + + @Test + public void givenRecipientNameAndMessage_sendDirectMessage() throws TwitterException { + String msg = Application.sendDirectMessage("YOUR_RECCIPIENT_ID", tweet); + assertEquals(msg, tweet); + } + +} 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 From 90969c9d58a104082d5105174831b0abe845e709 Mon Sep 17 00:00:00 2001 From: KevinGilmore Date: Sun, 5 Mar 2017 19:20:14 -0600 Subject: [PATCH 109/112] BAEL-9: README.md file updated (#1310) * BAEL-278: Updated README.md * BAEL-554: Add and update README.md files * BAEL-345: fixed assertion * BAEL-109: Updated README.md * BAEL-345: Added README.md * Reinstating reactor-core module in root-level pom * BAEL-393: Adding guide-intro module to root pom * BAEL-9: Updated README.md --- spring-boot/README.MD | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spring-boot/README.MD b/spring-boot/README.MD index 78ef3c843c..9fe18aaacc 100644 --- a/spring-boot/README.MD +++ b/spring-boot/README.MD @@ -10,4 +10,4 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring - [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) -- [How to Register a Servlet in a Java Web Application](http://www.baeldung.com/how-to-register-a-servlet-in-a-java-web-application/) +- [How to Register a Servlet in a Java Web Application](http://www.baeldung.com/register-servlet) From 693f7b96892ab5dd44bb1040f20b59d5a1a541ae Mon Sep 17 00:00:00 2001 From: Tryfon Date: Mon, 6 Mar 2017 09:37:08 +0200 Subject: [PATCH 110/112] Guide to "when" block in Kotlin pull request (#1296) * Char array to string and string to char array test cases added * Minor code renames * Added groupingBy collector unit tests * Added test case for int summary calculation on grouped results * Added the grouping by classes to the main source path * Reverting char array to string test class * Reverting char array to string test class * Reverting char array to string test class * Reverting char array to string test class * Unit test class for Kotlin when block + required types * Minor changes to kotlin when block tests * Minor change * Minor change --- .../com/baeldung/kotlin/WhenBlockTypes.kt | 28 ++++ .../com/baeldung/kotlin/WhenBlockUnitTest.kt | 136 ++++++++++++++++++ 2 files changed, 164 insertions(+) create mode 100644 kotlin/src/main/kotlin/com/baeldung/kotlin/WhenBlockTypes.kt create mode 100644 kotlin/src/test/kotlin/com/baeldung/kotlin/WhenBlockUnitTest.kt diff --git a/kotlin/src/main/kotlin/com/baeldung/kotlin/WhenBlockTypes.kt b/kotlin/src/main/kotlin/com/baeldung/kotlin/WhenBlockTypes.kt new file mode 100644 index 0000000000..6180da10d9 --- /dev/null +++ b/kotlin/src/main/kotlin/com/baeldung/kotlin/WhenBlockTypes.kt @@ -0,0 +1,28 @@ +package com.baeldung.kotlin + +enum class UnixFileType { + D, HYPHEN_MINUS, L +} + +sealed class UnixFile { + + abstract fun getFileType(): UnixFileType + + class RegularFile(val content: String) : UnixFile() { + override fun getFileType(): UnixFileType { + return UnixFileType.HYPHEN_MINUS + } + } + + class Directory(val children: List) : UnixFile() { + override fun getFileType(): UnixFileType { + return UnixFileType.D + } + } + + class SymbolicLink(val originalFile: UnixFile) : UnixFile() { + override fun getFileType(): UnixFileType { + return UnixFileType.L + } + } +} \ No newline at end of file diff --git a/kotlin/src/test/kotlin/com/baeldung/kotlin/WhenBlockUnitTest.kt b/kotlin/src/test/kotlin/com/baeldung/kotlin/WhenBlockUnitTest.kt new file mode 100644 index 0000000000..aa1891fa46 --- /dev/null +++ b/kotlin/src/test/kotlin/com/baeldung/kotlin/WhenBlockUnitTest.kt @@ -0,0 +1,136 @@ +package com.baeldung.kotlin + +import org.junit.Assert.assertEquals +import org.junit.Assert.assertTrue +import org.junit.Test + +class WhenBlockUnitTest { + + @Test + fun testWhenExpression() { + val directoryType = UnixFileType.D + + val objectType = when (directoryType) { + UnixFileType.D -> "d" + UnixFileType.HYPHEN_MINUS -> "-" + UnixFileType.L -> "l" + } + + assertEquals("d", objectType) + } + + @Test + fun testWhenExpressionWithDefaultCase() { + val fileType = UnixFileType.L + + val result = when (fileType) { + UnixFileType.L -> "linking to another file" + else -> "not a link" + } + + assertEquals("linking to another file", result) + } + + @Test(expected = IllegalArgumentException::class) + fun testWhenExpressionWithThrowException() { + val fileType = UnixFileType.L + + val result: Boolean = when (fileType) { + UnixFileType.HYPHEN_MINUS -> true + else -> throw IllegalArgumentException("Wrong type of file") + } + } + + @Test + fun testWhenStatement() { + val fileType = UnixFileType.HYPHEN_MINUS + + when (fileType) { + UnixFileType.HYPHEN_MINUS -> println("Regular file type") + UnixFileType.D -> println("Directory file type") + } + } + + @Test + fun testCaseCombination() { + val fileType = UnixFileType.D + + val frequentFileType: Boolean = when (fileType) { + UnixFileType.HYPHEN_MINUS, UnixFileType.D -> true + else -> false + } + + assertTrue(frequentFileType) + } + + @Test + fun testWhenWithoutArgument() { + val fileType = UnixFileType.L + + val objectType = when { + fileType === UnixFileType.L -> "l" + fileType === UnixFileType.HYPHEN_MINUS -> "-" + fileType === UnixFileType.D -> "d" + else -> "unknown file type" + } + + assertEquals("l", objectType) + } + + @Test + fun testDynamicCaseExpression() { + val unixFile = UnixFile.SymbolicLink(UnixFile.RegularFile("Content")) + + when { + unixFile.getFileType() == UnixFileType.D -> println("It's a directory!") + unixFile.getFileType() == UnixFileType.HYPHEN_MINUS -> println("It's a regular file!") + unixFile.getFileType() == UnixFileType.L -> println("It's a soft link!") + } + } + + @Test + fun testCollectionCaseExpressions() { + val regularFile = UnixFile.RegularFile("Test Content") + val symbolicLink = UnixFile.SymbolicLink(regularFile) + val directory = UnixFile.Directory(listOf(regularFile, symbolicLink)) + + val isRegularFileInDirectory = when (regularFile) { + in directory.children -> true + else -> false + } + + val isSymbolicLinkInDirectory = when { + symbolicLink in directory.children -> true + else -> false + } + + assertTrue(isRegularFileInDirectory) + assertTrue(isSymbolicLinkInDirectory) + } + + @Test + fun testRangeCaseExpressions() { + val fileType = UnixFileType.HYPHEN_MINUS + + val isCorrectType = when (fileType) { + in UnixFileType.D..UnixFileType.L -> true + else -> false + } + + assertTrue(isCorrectType) + } + + @Test + fun testWhenWithIsOperatorWithSmartCase() { + val unixFile: UnixFile = UnixFile.RegularFile("Test Content") + + val result = when (unixFile) { + is UnixFile.RegularFile -> unixFile.content + is UnixFile.Directory -> unixFile.children.map { it.getFileType() }.joinToString(", ") + is UnixFile.SymbolicLink -> unixFile.originalFile.getFileType() + } + + assertEquals("Test Content", result) + } + +} \ No newline at end of file From 1ff8b4c69c26de7e5d496f4c85e4ec5b8749a5e5 Mon Sep 17 00:00:00 2001 From: Tomasz Lelek Date: Mon, 6 Mar 2017 11:34:27 +0100 Subject: [PATCH 111/112] Bael 655 (#1256) BAEL-655 hbase --- hbase/pom.xml | 51 +++++ .../baeldung/hbase/HBaseClientOperations.java | 193 ++++++++++++++++++ .../baeldung/hbase/HbaseClientExample.java | 38 ++++ hbase/src/main/resources/hbase-site.xml | 12 ++ pom.xml | 1 + 5 files changed, 295 insertions(+) create mode 100644 hbase/pom.xml create mode 100644 hbase/src/main/java/org/baeldung/hbase/HBaseClientOperations.java create mode 100644 hbase/src/main/java/org/baeldung/hbase/HbaseClientExample.java create mode 100644 hbase/src/main/resources/hbase-site.xml diff --git a/hbase/pom.xml b/hbase/pom.xml new file mode 100644 index 0000000000..2382e47af2 --- /dev/null +++ b/hbase/pom.xml @@ -0,0 +1,51 @@ + + + + parent-modules + com.baeldung + 1.0.0-SNAPSHOT + + 4.0.0 + + hbase + + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.8 + 1.8 + + + + + + + + org.apache.hbase + hbase-client + ${hbase.version} + + + org.apache.hbase + hbase + ${hbase.version} + + + junit + junit + ${junit.version} + test + + + + + 1.3.0 + 4.12 + + + + \ No newline at end of file diff --git a/hbase/src/main/java/org/baeldung/hbase/HBaseClientOperations.java b/hbase/src/main/java/org/baeldung/hbase/HBaseClientOperations.java new file mode 100644 index 0000000000..c78eacc834 --- /dev/null +++ b/hbase/src/main/java/org/baeldung/hbase/HBaseClientOperations.java @@ -0,0 +1,193 @@ +package org.baeldung.hbase; + +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.hbase.HColumnDescriptor; +import org.apache.hadoop.hbase.HTableDescriptor; +import org.apache.hadoop.hbase.TableName; +import org.apache.hadoop.hbase.client.*; +import org.apache.hadoop.hbase.filter.*; +import org.apache.hadoop.hbase.filter.CompareFilter.CompareOp; +import org.apache.hadoop.hbase.filter.FilterList.Operator; +import org.apache.hadoop.hbase.util.Bytes; + +import java.io.IOException; +import java.util.Arrays; +import java.util.List; + +public class HBaseClientOperations { + private final static byte[] cellData = Bytes.toBytes("cell_data"); + + /** + * Drop tables if this value is set true. + */ + static boolean INITIALIZE_AT_FIRST = true; + + /** + * + * + * Row1Family1:Qualifier1Family1:Qualifier2 + * + * + * Row2Family1:Qualifier1Family2:Qualifier3 + * + * + * Row3Family1:Qualifier1Family2:Qualifier3 + * + *
+ */ + private final TableName table1 = TableName.valueOf("Table1"); + private final String family1 = "Family1"; + private final String family2 = "Family2"; + + private final byte[] row1 = Bytes.toBytes("Row1"); + private final byte[] row2 = Bytes.toBytes("Row2"); + private final byte[] row3 = Bytes.toBytes("Row3"); + private final byte[] qualifier1 = Bytes.toBytes("Qualifier1"); + private final byte[] qualifier2 = Bytes.toBytes("Qualifier2"); + private final byte[] qualifier3 = Bytes.toBytes("Qualifier3"); + + private void createTable(Admin admin) throws IOException { + HTableDescriptor desc = new HTableDescriptor(table1); + desc.addFamily(new HColumnDescriptor(family1)); + desc.addFamily(new HColumnDescriptor(family2)); + admin.createTable(desc); + } + + private void delete(Table table) throws IOException { + final byte[] rowToBeDeleted = Bytes.toBytes("RowToBeDeleted"); + System.out.println("\n*** DELETE ~Insert data and then delete it~ ***"); + + System.out.println("Inserting a data to be deleted later."); + Put put = new Put(rowToBeDeleted); + put.addColumn(family1.getBytes(), qualifier1, cellData); + table.put(put); + + Get get = new Get(rowToBeDeleted); + Result result = table.get(get); + byte[] value = result.getValue(family1.getBytes(), qualifier1); + System.out.println("Fetch the data: " + Bytes.toString(value)); + assert Arrays.equals(cellData, value); + + System.out.println("Deleting"); + Delete delete = new Delete(rowToBeDeleted); + delete.addColumn(family1.getBytes(), qualifier1); + table.delete(delete); + + result = table.get(get); + value = result.getValue(family1.getBytes(), qualifier1); + System.out.println("Fetch the data: " + Bytes.toString(value)); + assert Arrays.equals(null, value); + + System.out.println("Done. "); + } + + private void deleteTable(Admin admin) throws IOException { + if (admin.tableExists(table1)) { + admin.disableTable(table1); + admin.deleteTable(table1); + } + } + + private void filters(Table table) throws IOException { + System.out.println("\n*** FILTERS ~ scanning with filters to fetch a row of which key is larget than \"Row1\"~ ***"); + Filter filter1 = new PrefixFilter(row1); + Filter filter2 = new QualifierFilter(CompareOp.GREATER_OR_EQUAL, new BinaryComparator( + qualifier1)); + + List filters = Arrays.asList(filter1, filter2); + + Scan scan = new Scan(); + scan.setFilter(new FilterList(Operator.MUST_PASS_ALL, filters)); + + try (ResultScanner scanner = table.getScanner(scan)) { + int i = 0; + for (Result result : scanner) { + System.out.println("Filter " + scan.getFilter() + " matched row: " + result); + i++; + } + assert i == 2 : "This filtering sample should return 1 row but was " + i + "."; + } + System.out.println("Done. "); + } + + private void get(Table table) throws IOException { + System.out.println("\n*** GET example ~fetching the data in Family1:Qualifier1~ ***"); + + Get g = new Get(row1); + Result r = table.get(g); + byte[] value = r.getValue(family1.getBytes(), qualifier1); + + System.out.println("Fetched value: " + Bytes.toString(value)); + assert Arrays.equals(cellData, value); + System.out.println("Done. "); + } + + private void put(Admin admin, Table table) throws IOException { + System.out.println("\n*** PUT example ~inserting \"cell-data\" into Family1:Qualifier1 of Table1 ~ ***"); + + // Row1 => Family1:Qualifier1, Family1:Qualifier2 + Put p = new Put(row1); + p.addImmutable(family1.getBytes(), qualifier1, cellData); + p.addImmutable(family1.getBytes(), qualifier2, cellData); + table.put(p); + + // Row2 => Family1:Qualifier1, Family2:Qualifier3 + p = new Put(row2); + p.addImmutable(family1.getBytes(), qualifier1, cellData); + p.addImmutable(family2.getBytes(), qualifier3, cellData); + table.put(p); + + // Row3 => Family1:Qualifier1, Family2:Qualifier3 + p = new Put(row3); + p.addImmutable(family1.getBytes(), qualifier1, cellData); + p.addImmutable(family2.getBytes(), qualifier3, cellData); + table.put(p); + + admin.disableTable(table1); + try { + HColumnDescriptor desc = new HColumnDescriptor(row1); + admin.addColumn(table1, desc); + System.out.println("Success."); + } catch (Exception e) { + System.out.println("Failed."); + System.out.println(e.getMessage()); + } finally { + admin.enableTable(table1); + } + System.out.println("Done. "); + } + + public void run(Configuration config) throws IOException { + try (Connection connection = ConnectionFactory.createConnection(config)) { + + Admin admin = connection.getAdmin(); + if (INITIALIZE_AT_FIRST) { + deleteTable(admin); + } + + if (!admin.tableExists(table1)) { + createTable(admin); + } + + Table table = connection.getTable(table1); + put(admin, table); + get(table); + scan(table); + filters(table); + delete(table); + } + } + + private void scan(Table table) throws IOException { + System.out.println("\n*** SCAN example ~fetching data in Family1:Qualifier1 ~ ***"); + + Scan scan = new Scan(); + scan.addColumn(family1.getBytes(), qualifier1); + + try (ResultScanner scanner = table.getScanner(scan)) { + for (Result result : scanner) + System.out.println("Found row: " + result); + } + System.out.println("Done."); + } +} \ No newline at end of file diff --git a/hbase/src/main/java/org/baeldung/hbase/HbaseClientExample.java b/hbase/src/main/java/org/baeldung/hbase/HbaseClientExample.java new file mode 100644 index 0000000000..07cb7df480 --- /dev/null +++ b/hbase/src/main/java/org/baeldung/hbase/HbaseClientExample.java @@ -0,0 +1,38 @@ +package org.baeldung.hbase; + + +import com.google.protobuf.ServiceException; +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.fs.Path; +import org.apache.hadoop.hbase.HBaseConfiguration; +import org.apache.hadoop.hbase.MasterNotRunningException; +import org.apache.hadoop.hbase.client.HBaseAdmin; + +import java.io.IOException; + +//install hbase locally & hbase master start +public class HbaseClientExample { + + public static void main(String[] args) throws IOException, ServiceException { + new HbaseClientExample().connect(); + } + + private void connect() throws IOException, ServiceException { + Configuration config = HBaseConfiguration.create(); + + String path = this.getClass().getClassLoader().getResource("hbase-site.xml").getPath(); + + config.addResource(new Path(path)); + + try { + HBaseAdmin.checkHBaseAvailable(config); + } catch (MasterNotRunningException e) { + System.out.println("HBase is not running." + e.getMessage()); + return; + } + + HBaseClientOperations HBaseClientOperations = new HBaseClientOperations(); + HBaseClientOperations.run(config); + } + +} \ No newline at end of file diff --git a/hbase/src/main/resources/hbase-site.xml b/hbase/src/main/resources/hbase-site.xml new file mode 100644 index 0000000000..895529161c --- /dev/null +++ b/hbase/src/main/resources/hbase-site.xml @@ -0,0 +1,12 @@ + + + + + hbase.zookeeper.quorum + localhost + + + hbase.zookeeper.property.clientPort + 2181 + + \ No newline at end of file diff --git a/pom.xml b/pom.xml index 72099fc584..9c24200a0b 100644 --- a/pom.xml +++ b/pom.xml @@ -51,6 +51,7 @@ handling-spring-static-resources hazelcast + httpclient hystrix From 9f94f9f78996a42c314168987138905d49f6e95e Mon Sep 17 00:00:00 2001 From: Grzegorz Piwowarek Date: Mon, 6 Mar 2017 14:06:00 +0100 Subject: [PATCH 112/112] Remove unnecessary files and update .gitignore (#1313) --- core-java/.gitignore | 3 + .../.resourceCache/ECBCurrentRateProvider.dat | 42 ------- .../ECBHistoric90RateProvider.dat | 63 ---------- .../IMFHistoricRateProvider.dat | 118 ------------------ core-java/.resourceCache/IMFRateProvider.dat | 118 ------------------ 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 10 files changed, 3 insertions(+), 341 deletions(-) delete mode 100644 core-java/.resourceCache/ECBCurrentRateProvider.dat delete mode 100644 core-java/.resourceCache/ECBHistoric90RateProvider.dat delete mode 100644 core-java/.resourceCache/IMFHistoricRateProvider.dat delete mode 100644 core-java/.resourceCache/IMFRateProvider.dat delete mode 100644 core-java/0.004102810554955205 delete mode 100644 core-java/0.04832801936270381 delete mode 100644 core-java/0.5633433244738808 delete mode 100644 core-java/0.6256429734439612 delete mode 100644 core-java/0.9799201796740292 diff --git a/core-java/.gitignore b/core-java/.gitignore index 251a8755bd..2a03a0f72e 100644 --- a/core-java/.gitignore +++ b/core-java/.gitignore @@ -1,11 +1,14 @@ *.class +0.* + #folders# /target /neoDb* /data /src/main/webapp/WEB-INF/classes */META-INF/* +.resourceCache # Packaged files # *.jar diff --git a/core-java/.resourceCache/ECBCurrentRateProvider.dat b/core-java/.resourceCache/ECBCurrentRateProvider.dat deleted file mode 100644 index 69eed9a0d8..0000000000 --- a/core-java/.resourceCache/ECBCurrentRateProvider.dat +++ /dev/null @@ -1,42 +0,0 @@ - - - Reference rates - - European Central Bank - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/core-java/.resourceCache/ECBHistoric90RateProvider.dat b/core-java/.resourceCache/ECBHistoric90RateProvider.dat deleted file mode 100644 index 157cac1a6a..0000000000 --- a/core-java/.resourceCache/ECBHistoric90RateProvider.dat +++ /dev/null @@ -1,63 +0,0 @@ -Reference ratesEuropean Central Bank - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/core-java/.resourceCache/IMFHistoricRateProvider.dat b/core-java/.resourceCache/IMFHistoricRateProvider.dat deleted file mode 100644 index 12809d35af..0000000000 --- a/core-java/.resourceCache/IMFHistoricRateProvider.dat +++ /dev/null @@ -1,118 +0,0 @@ -SDRs per Currency unit and Currency units per SDR (1) -last five days -SDRs per Currency unit (2) - -Currency March 02, 2017 March 01, 2017 February 28, 2017 February 27, 2017 February 24, 2017 -Chinese Yuan 0.1078010000 0.1075250000 0.1075530000 0.1074200000 -Euro 0.7811130000 0.7827060000 0.7826360000 0.7829640000 -Japanese Yen 0.0065627100 0.0065625100 0.0065915500 0.0065398200 -U.K. Pound Sterling 0.9132630000 0.9188320000 0.9178430000 0.9263250000 -U.S. Dollar 0.7415860000 0.7386110000 0.7392420000 0.7380190000 -Algerian Dinar 0.0067127200 0.0067093100 0.0067141700 0.0067056800 -Australian Dollar 0.5677580000 0.5678440000 0.5688470000 0.5695290000 -Bahrain Dinar 1.9723000000 1.9643900000 1.9660700000 1.9628200000 -Botswana Pula 0.0711181000 0.0711282000 0.0712629000 0.0712926000 -Brazilian Real 0.2393220000 0.2383620000 0.2385650000 0.2409310000 -Brunei Dollar 0.5269940000 0.5260760000 0.5261880000 0.5253930000 -Canadian Dollar 0.5575260000 0.5642640000 0.5632010000 -Chilean Peso 0.0011428700 0.0011448000 0.0011449400 0.0011525100 -Colombian Peso 0.0002540400 0.0002550210 0.0002561010 0.0002570000 -Czech Koruna 0.0289072000 0.0289685000 0.0289649000 0.0289783000 -Danish Krone 0.1050820000 0.1052990000 0.1052900000 0.1053170000 -Hungarian Forint 0.0025403700 0.0025419400 0.0025407000 0.0025337100 -Icelandic Krona 0.0069632500 0.0069197200 0.0068709200 0.0068082900 -Indian Rupee 0.0110936000 0.0110674000 0.0110790000 -Indonesian Rupiah 0.0000555038 0.0000553391 0.0000554196 0.0000553404 -Iranian Rial 0.0000228835 0.0000227945 0.0000228133 -Israeli New Sheqel 0.2041810000 0.2018610000 0.2009360000 0.1995720000 -Kazakhstani Tenge 0.0023615900 0.0023658000 0.0023693200 -Korean Won 0.0006524260 0.0006536180 0.0006480670 -Kuwaiti Dinar 2.4274500000 2.4193000000 2.4213600000 2.4165700000 -Libyan Dinar 0.5174910000 0.5174910000 0.5174910000 0.5174910000 -Malaysian Ringgit 0.1667420000 0.1661670000 0.1663840000 0.1659960000 -Mauritian Rupee 0.0208473000 0.0207651000 0.0207673000 -Mexican Peso 0.0369385000 0.0372748000 0.0372107000 -Nepalese Rupee 0.0069436900 0.0069210200 0.0069133300 0.0069012400 -New Zealand Dollar 0.5305310000 0.5304700000 0.5318850000 0.5331450000 -Norwegian Krone 0.0881434000 0.0882493000 0.0885500000 0.0886062000 -Rial Omani 1.9287000000 1.9209600000 1.9226100000 1.9194300000 -Pakistani Rupee 0.0070732900 0.0070443600 0.0070502500 0.0070384300 -Nuevo Sol 0.2266370000 0.2274590000 0.2271530000 -Philippine Peso 0.0147565000 0.0146938000 0.0147268000 0.0146922000 -Polish Zloty 0.1819580000 0.1811650000 0.1814310000 0.1814160000 -Qatar Riyal 0.2037320000 0.2029150000 0.2030880000 0.2027520000 -Russian Ruble 0.0127033000 0.0127429000 0.0127594000 -Saudi Arabian Riyal 0.1977560000 0.1969630000 0.1971310000 0.1968050000 -Singapore Dollar 0.5269940000 0.5260760000 0.5261880000 0.5253930000 -South African Rand 0.0567982000 0.0567078000 0.0569908000 0.0569347000 -Sri Lanka Rupee 0.0049030500 0.0048833800 0.0048875500 -Swedish Krona 0.0818510000 0.0817029000 0.0818978000 0.0822544000 -Swiss Franc 0.7340980000 0.7348630000 0.7341760000 0.7345670000 -Thai Baht 0.0211936000 0.0211661000 0.0212042000 0.0211098000 -Trinidad And Tobago Dollar 0.1102220000 0.1092030000 -Tunisian Dinar 0.3225800000 0.3211100000 0.3215340000 0.3206630000 -U.A.E. Dirham 0.2019290000 0.2011190000 0.2012910000 0.2009580000 -Peso Uruguayo 0.0258745000 -Bolivar Fuerte 0.0743445000 0.0740462000 0.0741095000 0.0739869000 - -Currency units per SDR(3) - -Currency March 02, 2017 March 01, 2017 February 28, 2017 February 27, 2017 February 24, 2017 -Chinese Yuan 9.276350 9.300160 9.297740 9.309250 -Euro 1.280220 1.277620 1.277730 1.277200 -Japanese Yen 152.376000 152.381000 151.709000 152.909000 -U.K. Pound Sterling 1.094970 1.088340 1.089510 1.079530 -U.S. Dollar 1.348460 1.353890 1.352740 1.354980 -Algerian Dinar 148.971000 149.047000 148.939000 149.127000 -Australian Dollar 1.761310 1.761050 1.757940 1.755840 -Bahrain Dinar 0.507022 0.509064 0.508629 0.509471 -Botswana Pula 14.061100 14.059100 14.032500 14.026700 -Brazilian Real 4.178470 4.195300 4.191730 4.150570 -Brunei Dollar 1.897550 1.900870 1.900460 1.903340 -Canadian Dollar 1.793640 1.772220 1.775570 -Chilean Peso 874.990000 873.515000 873.408000 867.671000 -Colombian Peso 3,936.390000 3,921.250000 3,904.710000 3,891.050000 -Czech Koruna 34.593500 34.520300 34.524500 34.508600 -Danish Krone 9.516380 9.496770 9.497580 9.495140 -Hungarian Forint 393.643000 393.400000 393.592000 394.678000 -Icelandic Krona 143.611000 144.515000 145.541000 146.880000 -Indian Rupee 90.142100 90.355500 90.260900 -Indonesian Rupiah 18,016.800000 18,070.400000 18,044.200000 18,070.000000 -Iranian Rial 43,699.600000 43,870.200000 43,834.100000 -Israeli New Sheqel 4.897620 4.953900 4.976710 5.010720 -Kazakhstani Tenge 423.444000 422.690000 422.062000 -Korean Won 1,532.740000 1,529.950000 1,543.050000 -Kuwaiti Dinar 0.411955 0.413343 0.412991 0.413810 -Libyan Dinar 1.932400 1.932400 1.932400 1.932400 -Malaysian Ringgit 5.997290 6.018040 6.010190 6.024240 -Mauritian Rupee 47.967800 48.157700 48.152600 -Mexican Peso 27.072000 26.827800 26.874000 -Nepalese Rupee 144.016000 144.487000 144.648000 144.901000 -New Zealand Dollar 1.884900 1.885120 1.880110 1.875660 -Norwegian Krone 11.345100 11.331500 11.293100 11.285900 -Rial Omani 0.518484 0.520573 0.520126 0.520988 -Pakistani Rupee 141.377000 141.958000 141.839000 142.077000 -Nuevo Sol 4.412340 4.396400 4.402320 -Philippine Peso 67.766700 68.055900 67.903400 68.063300 -Polish Zloty 5.495770 5.519830 5.511740 5.512190 -Qatar Riyal 4.908410 4.928170 4.923970 4.932130 -Russian Ruble 78.719700 78.475100 78.373600 -Saudi Arabian Riyal 5.056740 5.077100 5.072770 5.081170 -Singapore Dollar 1.897550 1.900870 1.900460 1.903340 -South African Rand 17.606200 17.634300 17.546700 17.564000 -Sri Lanka Rupee 203.955000 204.776000 204.601000 -Swedish Krona 12.217300 12.239500 12.210300 12.157400 -Swiss Franc 1.362220 1.360800 1.362070 1.361350 -Thai Baht 47.184100 47.245400 47.160500 47.371400 -Trinidad And Tobago Dollar 9.072600 9.157260 -Tunisian Dinar 3.100010 3.114200 3.110090 3.118540 -U.A.E. Dirham 4.952240 4.972180 4.967930 4.976160 -Peso Uruguayo 38.648100 -Bolivar Fuerte 13.450900 13.505100 13.493500 13.515900 - - -(1) Exchange rates are published daily except on IMF holidays or whenever the IMF is closed for business. - -(2) The value of the U.S. dollar in terms of the SDR is the reciprocal of the sum of the dollar values, based on market exchange rates, of specified quantities of the SDR basket currencies. See SDR Valuation.The value in terms of the SDR of each of the other currencies shown above is derived from that currency's representative exchange rate against the U.S. dollar as reported by the issuing central bank and the SDR value of the U.S. dollar, except for the Iranian rial and the Libyan dinar, the values of which are officially expressed directly in terms of domestic currency units per SDR. All figures are rounded to six significant digits. See Representative Exchange Rates for Selected Currencies". - -(3) The value in terms of each national currency of the SDR is the reciprocal of the value in terms of the SDR of each national currency, rounded to six significant digits. \ No newline at end of file diff --git a/core-java/.resourceCache/IMFRateProvider.dat b/core-java/.resourceCache/IMFRateProvider.dat deleted file mode 100644 index 12809d35af..0000000000 --- a/core-java/.resourceCache/IMFRateProvider.dat +++ /dev/null @@ -1,118 +0,0 @@ -SDRs per Currency unit and Currency units per SDR (1) -last five days -SDRs per Currency unit (2) - -Currency March 02, 2017 March 01, 2017 February 28, 2017 February 27, 2017 February 24, 2017 -Chinese Yuan 0.1078010000 0.1075250000 0.1075530000 0.1074200000 -Euro 0.7811130000 0.7827060000 0.7826360000 0.7829640000 -Japanese Yen 0.0065627100 0.0065625100 0.0065915500 0.0065398200 -U.K. Pound Sterling 0.9132630000 0.9188320000 0.9178430000 0.9263250000 -U.S. Dollar 0.7415860000 0.7386110000 0.7392420000 0.7380190000 -Algerian Dinar 0.0067127200 0.0067093100 0.0067141700 0.0067056800 -Australian Dollar 0.5677580000 0.5678440000 0.5688470000 0.5695290000 -Bahrain Dinar 1.9723000000 1.9643900000 1.9660700000 1.9628200000 -Botswana Pula 0.0711181000 0.0711282000 0.0712629000 0.0712926000 -Brazilian Real 0.2393220000 0.2383620000 0.2385650000 0.2409310000 -Brunei Dollar 0.5269940000 0.5260760000 0.5261880000 0.5253930000 -Canadian Dollar 0.5575260000 0.5642640000 0.5632010000 -Chilean Peso 0.0011428700 0.0011448000 0.0011449400 0.0011525100 -Colombian Peso 0.0002540400 0.0002550210 0.0002561010 0.0002570000 -Czech Koruna 0.0289072000 0.0289685000 0.0289649000 0.0289783000 -Danish Krone 0.1050820000 0.1052990000 0.1052900000 0.1053170000 -Hungarian Forint 0.0025403700 0.0025419400 0.0025407000 0.0025337100 -Icelandic Krona 0.0069632500 0.0069197200 0.0068709200 0.0068082900 -Indian Rupee 0.0110936000 0.0110674000 0.0110790000 -Indonesian Rupiah 0.0000555038 0.0000553391 0.0000554196 0.0000553404 -Iranian Rial 0.0000228835 0.0000227945 0.0000228133 -Israeli New Sheqel 0.2041810000 0.2018610000 0.2009360000 0.1995720000 -Kazakhstani Tenge 0.0023615900 0.0023658000 0.0023693200 -Korean Won 0.0006524260 0.0006536180 0.0006480670 -Kuwaiti Dinar 2.4274500000 2.4193000000 2.4213600000 2.4165700000 -Libyan Dinar 0.5174910000 0.5174910000 0.5174910000 0.5174910000 -Malaysian Ringgit 0.1667420000 0.1661670000 0.1663840000 0.1659960000 -Mauritian Rupee 0.0208473000 0.0207651000 0.0207673000 -Mexican Peso 0.0369385000 0.0372748000 0.0372107000 -Nepalese Rupee 0.0069436900 0.0069210200 0.0069133300 0.0069012400 -New Zealand Dollar 0.5305310000 0.5304700000 0.5318850000 0.5331450000 -Norwegian Krone 0.0881434000 0.0882493000 0.0885500000 0.0886062000 -Rial Omani 1.9287000000 1.9209600000 1.9226100000 1.9194300000 -Pakistani Rupee 0.0070732900 0.0070443600 0.0070502500 0.0070384300 -Nuevo Sol 0.2266370000 0.2274590000 0.2271530000 -Philippine Peso 0.0147565000 0.0146938000 0.0147268000 0.0146922000 -Polish Zloty 0.1819580000 0.1811650000 0.1814310000 0.1814160000 -Qatar Riyal 0.2037320000 0.2029150000 0.2030880000 0.2027520000 -Russian Ruble 0.0127033000 0.0127429000 0.0127594000 -Saudi Arabian Riyal 0.1977560000 0.1969630000 0.1971310000 0.1968050000 -Singapore Dollar 0.5269940000 0.5260760000 0.5261880000 0.5253930000 -South African Rand 0.0567982000 0.0567078000 0.0569908000 0.0569347000 -Sri Lanka Rupee 0.0049030500 0.0048833800 0.0048875500 -Swedish Krona 0.0818510000 0.0817029000 0.0818978000 0.0822544000 -Swiss Franc 0.7340980000 0.7348630000 0.7341760000 0.7345670000 -Thai Baht 0.0211936000 0.0211661000 0.0212042000 0.0211098000 -Trinidad And Tobago Dollar 0.1102220000 0.1092030000 -Tunisian Dinar 0.3225800000 0.3211100000 0.3215340000 0.3206630000 -U.A.E. Dirham 0.2019290000 0.2011190000 0.2012910000 0.2009580000 -Peso Uruguayo 0.0258745000 -Bolivar Fuerte 0.0743445000 0.0740462000 0.0741095000 0.0739869000 - -Currency units per SDR(3) - -Currency March 02, 2017 March 01, 2017 February 28, 2017 February 27, 2017 February 24, 2017 -Chinese Yuan 9.276350 9.300160 9.297740 9.309250 -Euro 1.280220 1.277620 1.277730 1.277200 -Japanese Yen 152.376000 152.381000 151.709000 152.909000 -U.K. Pound Sterling 1.094970 1.088340 1.089510 1.079530 -U.S. Dollar 1.348460 1.353890 1.352740 1.354980 -Algerian Dinar 148.971000 149.047000 148.939000 149.127000 -Australian Dollar 1.761310 1.761050 1.757940 1.755840 -Bahrain Dinar 0.507022 0.509064 0.508629 0.509471 -Botswana Pula 14.061100 14.059100 14.032500 14.026700 -Brazilian Real 4.178470 4.195300 4.191730 4.150570 -Brunei Dollar 1.897550 1.900870 1.900460 1.903340 -Canadian Dollar 1.793640 1.772220 1.775570 -Chilean Peso 874.990000 873.515000 873.408000 867.671000 -Colombian Peso 3,936.390000 3,921.250000 3,904.710000 3,891.050000 -Czech Koruna 34.593500 34.520300 34.524500 34.508600 -Danish Krone 9.516380 9.496770 9.497580 9.495140 -Hungarian Forint 393.643000 393.400000 393.592000 394.678000 -Icelandic Krona 143.611000 144.515000 145.541000 146.880000 -Indian Rupee 90.142100 90.355500 90.260900 -Indonesian Rupiah 18,016.800000 18,070.400000 18,044.200000 18,070.000000 -Iranian Rial 43,699.600000 43,870.200000 43,834.100000 -Israeli New Sheqel 4.897620 4.953900 4.976710 5.010720 -Kazakhstani Tenge 423.444000 422.690000 422.062000 -Korean Won 1,532.740000 1,529.950000 1,543.050000 -Kuwaiti Dinar 0.411955 0.413343 0.412991 0.413810 -Libyan Dinar 1.932400 1.932400 1.932400 1.932400 -Malaysian Ringgit 5.997290 6.018040 6.010190 6.024240 -Mauritian Rupee 47.967800 48.157700 48.152600 -Mexican Peso 27.072000 26.827800 26.874000 -Nepalese Rupee 144.016000 144.487000 144.648000 144.901000 -New Zealand Dollar 1.884900 1.885120 1.880110 1.875660 -Norwegian Krone 11.345100 11.331500 11.293100 11.285900 -Rial Omani 0.518484 0.520573 0.520126 0.520988 -Pakistani Rupee 141.377000 141.958000 141.839000 142.077000 -Nuevo Sol 4.412340 4.396400 4.402320 -Philippine Peso 67.766700 68.055900 67.903400 68.063300 -Polish Zloty 5.495770 5.519830 5.511740 5.512190 -Qatar Riyal 4.908410 4.928170 4.923970 4.932130 -Russian Ruble 78.719700 78.475100 78.373600 -Saudi Arabian Riyal 5.056740 5.077100 5.072770 5.081170 -Singapore Dollar 1.897550 1.900870 1.900460 1.903340 -South African Rand 17.606200 17.634300 17.546700 17.564000 -Sri Lanka Rupee 203.955000 204.776000 204.601000 -Swedish Krona 12.217300 12.239500 12.210300 12.157400 -Swiss Franc 1.362220 1.360800 1.362070 1.361350 -Thai Baht 47.184100 47.245400 47.160500 47.371400 -Trinidad And Tobago Dollar 9.072600 9.157260 -Tunisian Dinar 3.100010 3.114200 3.110090 3.118540 -U.A.E. Dirham 4.952240 4.972180 4.967930 4.976160 -Peso Uruguayo 38.648100 -Bolivar Fuerte 13.450900 13.505100 13.493500 13.515900 - - -(1) Exchange rates are published daily except on IMF holidays or whenever the IMF is closed for business. - -(2) The value of the U.S. dollar in terms of the SDR is the reciprocal of the sum of the dollar values, based on market exchange rates, of specified quantities of the SDR basket currencies. See SDR Valuation.The value in terms of the SDR of each of the other currencies shown above is derived from that currency's representative exchange rate against the U.S. dollar as reported by the issuing central bank and the SDR value of the U.S. dollar, except for the Iranian rial and the Libyan dinar, the values of which are officially expressed directly in terms of domestic currency units per SDR. All figures are rounded to six significant digits. See Representative Exchange Rates for Selected Currencies". - -(3) The value in terms of each national currency of the SDR is the reciprocal of the value in terms of the SDR of each national currency, rounded to six significant digits. \ No newline at end of file diff --git a/core-java/0.004102810554955205 b/core-java/0.004102810554955205 deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/core-java/0.04832801936270381 b/core-java/0.04832801936270381 deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/core-java/0.5633433244738808 b/core-java/0.5633433244738808 deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/core-java/0.6256429734439612 b/core-java/0.6256429734439612 deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/core-java/0.9799201796740292 b/core-java/0.9799201796740292 deleted file mode 100644 index e69de29bb2..0000000000