Merge branch 'master' of https://github.com/eugenp/tutorials
This commit is contained in:
+12
-3
@@ -23,6 +23,12 @@
|
||||
<groupId>org.codehaus.groovy</groupId>
|
||||
<artifactId>groovy-all</artifactId>
|
||||
<version>${groovy-all.version}</version>
|
||||
<type>pom</type>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.codehaus.groovy</groupId>
|
||||
<artifactId>groovy-dateutil</artifactId>
|
||||
<version>${groovy.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.codehaus.groovy</groupId>
|
||||
@@ -103,9 +109,12 @@
|
||||
|
||||
<properties>
|
||||
<junit.platform.version>1.0.0</junit.platform.version>
|
||||
<groovy.version>2.4.13</groovy.version>
|
||||
<groovy-all.version>2.4.13</groovy-all.version>
|
||||
<groovy-sql.version>2.4.13</groovy-sql.version>
|
||||
<!-- <groovy.version>2.4.13</groovy.version> -->
|
||||
<!-- <groovy-all.version>2.4.13</groovy-all.version> -->
|
||||
<!-- <groovy-sql.version>2.4.13</groovy-sql.version> -->
|
||||
<groovy.version>2.5.6</groovy.version>
|
||||
<groovy-all.version>2.5.6</groovy-all.version>
|
||||
<groovy-sql.version>2.5.6</groovy-sql.version>
|
||||
<hsqldb.version>2.4.0</hsqldb.version>
|
||||
<spock-core.version>1.1-groovy-2.4</spock-core.version>
|
||||
<gmavenplus-plugin.version>1.6</gmavenplus-plugin.version>
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
package com.baeldung.groovy.sql
|
||||
|
||||
import static org.junit.Assert.*
|
||||
import java.util.Calendar.*
|
||||
import java.time.LocalDate
|
||||
import java.text.SimpleDateFormat
|
||||
import org.junit.Test
|
||||
|
||||
|
||||
class DateTest {
|
||||
|
||||
def dateStr = "2019-02-28"
|
||||
def pattern = "yyyy-MM-dd"
|
||||
|
||||
@Test
|
||||
void whenGetStringRepresentation_thenCorrectlyConvertIntoDate() {
|
||||
def dateFormat = new SimpleDateFormat(pattern)
|
||||
def date = dateFormat.parse(dateStr)
|
||||
|
||||
println(" String to Date with DateFormatter : " + date)
|
||||
|
||||
def cal = new GregorianCalendar();
|
||||
cal.setTime(date);
|
||||
|
||||
assertEquals(cal.get(Calendar.YEAR),2019)
|
||||
assertEquals(cal.get(Calendar.DAY_OF_MONTH),28)
|
||||
assertEquals(cal.get(Calendar.MONTH),java.util.Calendar.FEBRUARY)
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenGetStringRepresentation_thenCorrectlyConvertWithDateUtilsExtension() {
|
||||
|
||||
def date = Date.parse(pattern, dateStr)
|
||||
|
||||
println(" String to Date with Date.parse : " + date)
|
||||
|
||||
def cal = new GregorianCalendar();
|
||||
cal.setTime(date);
|
||||
|
||||
assertEquals(cal.get(Calendar.YEAR),2019)
|
||||
assertEquals(cal.get(Calendar.DAY_OF_MONTH),28)
|
||||
assertEquals(cal.get(Calendar.MONTH),java.util.Calendar.FEBRUARY)
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenGetStringRepresentation_thenCorrectlyConvertIntoDateWithLocalDate() {
|
||||
def date = LocalDate.parse(dateStr, pattern)
|
||||
|
||||
println(" String to Date with LocalDate : " + date)
|
||||
|
||||
assertEquals(date.getYear(),2019)
|
||||
assertEquals(date.getMonth(),java.time.Month.FEBRUARY)
|
||||
assertEquals(date.getDayOfMonth(),28)
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -13,4 +13,5 @@
|
||||
- [Working with Network Interfaces in Java](http://www.baeldung.com/java-network-interfaces)
|
||||
- [A Guide to Java Sockets](http://www.baeldung.com/a-guide-to-java-sockets)
|
||||
- [URL Encoding and Decoding in Java](http://www.baeldung.com/java-url-encoding-decoding)
|
||||
- [How to Perform a Simple HTTP Request in Java](http://www.baeldung.com/java-http-request)
|
||||
- [How to Perform a Simple HTTP Request in Java](http://www.baeldung.com/java-http-request)
|
||||
- [Difference between URL and URI](http://www.baeldung.com/java-url-vs-uri)
|
||||
|
||||
@@ -19,7 +19,6 @@
|
||||
- [Introduction to Java Serialization](http://www.baeldung.com/java-serialization)
|
||||
- [Guide to UUID in Java](http://www.baeldung.com/java-uuid)
|
||||
- [Guide to Escaping Characters in Java RegExps](http://www.baeldung.com/java-regexp-escape-char)
|
||||
- [Difference between URL and URI](http://www.baeldung.com/java-url-vs-uri)
|
||||
- [Creating a Java Compiler Plugin](http://www.baeldung.com/java-build-compiler-plugin)
|
||||
- [Quick Guide to Java Stack](http://www.baeldung.com/java-stack)
|
||||
- [Compiling Java *.class Files with javac](http://www.baeldung.com/javac)
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
package com.baeldung.urlconnection;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStream;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
|
||||
public class PostJSONWithHttpURLConnection {
|
||||
|
||||
public static void main (String []args) throws IOException{
|
||||
//Change the URL with any other publicly accessible POST resource, which accepts JSON request body
|
||||
URL url = new URL ("https://reqres.in/api/users");
|
||||
|
||||
HttpURLConnection con = (HttpURLConnection)url.openConnection();
|
||||
con.setRequestMethod("POST");
|
||||
|
||||
con.setRequestProperty("Content-Type", "application/json; utf-8");
|
||||
con.setRequestProperty("Accept", "application/json");
|
||||
|
||||
con.setDoOutput(true);
|
||||
|
||||
//JSON String need to be constructed for the specific resource.
|
||||
//We may construct complex JSON using any third-party JSON libraries such as jackson or org.json
|
||||
String jsonInputString = "{\"name\": \"Upendra\", \"job\": \"Programmer\"}";
|
||||
|
||||
try(OutputStream os = con.getOutputStream()){
|
||||
byte[] input = jsonInputString.getBytes("utf-8");
|
||||
os.write(input, 0, input.length);
|
||||
}
|
||||
|
||||
int code = con.getResponseCode();
|
||||
System.out.println(code);
|
||||
|
||||
try(BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(), "utf-8"))){
|
||||
StringBuilder response = new StringBuilder();
|
||||
String responseLine = null;
|
||||
while ((responseLine = br.readLine()) != null) {
|
||||
response.append(responseLine.trim());
|
||||
}
|
||||
System.out.println(response.toString());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package stringcomparison
|
||||
|
||||
import org.junit.Test
|
||||
import kotlin.test.assertFalse
|
||||
import kotlin.test.assertTrue
|
||||
|
||||
class StringComparisonUnitTest {
|
||||
|
||||
@Test
|
||||
fun `compare using equals operator`() {
|
||||
val first = "kotlin"
|
||||
val second = "kotlin"
|
||||
val firstCapitalized = "KOTLIN"
|
||||
assertTrue { first == second }
|
||||
assertFalse { first == firstCapitalized }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `compare using referential equals operator`() {
|
||||
val first = "kotlin"
|
||||
val second = "kotlin"
|
||||
val copyOfFirst = buildString { "kotlin" }
|
||||
assertTrue { first === second }
|
||||
assertFalse { first === copyOfFirst }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `compare using equals method`() {
|
||||
val first = "kotlin"
|
||||
val second = "kotlin"
|
||||
val firstCapitalized = "KOTLIN"
|
||||
assertTrue { first.equals(second) }
|
||||
assertFalse { first.equals(firstCapitalized) }
|
||||
assertTrue { first.equals(firstCapitalized, true) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `compare using compare method`() {
|
||||
val first = "kotlin"
|
||||
val second = "kotlin"
|
||||
val firstCapitalized = "KOTLIN"
|
||||
assertTrue { first.compareTo(second) == 0 }
|
||||
assertTrue { first.compareTo(firstCapitalized) == 32 }
|
||||
assertTrue { firstCapitalized.compareTo(first) == -32 }
|
||||
assertTrue { first.compareTo(firstCapitalized, true) == 0 }
|
||||
}
|
||||
}
|
||||
@@ -33,24 +33,11 @@
|
||||
</dependencies>
|
||||
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>maven2-repository.dev.java.net</id>
|
||||
<name>Java.net repository</name>
|
||||
<url>http://download.java.net/maven/2</url>
|
||||
</repository>
|
||||
<repository>
|
||||
<id>osgeo</id>
|
||||
<name>Open Source Geospatial Foundation Repository</name>
|
||||
<url>http://download.osgeo.org/webdav/geotools/</url>
|
||||
</repository>
|
||||
<repository>
|
||||
<snapshots>
|
||||
<enabled>true</enabled>
|
||||
</snapshots>
|
||||
<id>opengeo</id>
|
||||
<name>OpenGeo Maven Repository</name>
|
||||
<url>http://repo.opengeo.org</url>
|
||||
</repository>
|
||||
</repositories>
|
||||
|
||||
<properties>
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
package com.baeldung.reduce.application;
|
||||
|
||||
import com.baeldung.reduce.entities.User;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import org.openjdk.jmh.annotations.Benchmark;
|
||||
import org.openjdk.jmh.annotations.BenchmarkMode;
|
||||
import org.openjdk.jmh.annotations.Fork;
|
||||
import org.openjdk.jmh.annotations.Mode;
|
||||
import org.openjdk.jmh.annotations.Warmup;
|
||||
|
||||
public class Application {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6);
|
||||
int result1 = numbers.stream().reduce(0, (subtotal, element) -> subtotal + element);
|
||||
System.out.println(result1);
|
||||
|
||||
int result2 = numbers.stream().reduce(0, Integer::sum);
|
||||
System.out.println(result2);
|
||||
|
||||
List<String> letters = Arrays.asList("a", "b", "c", "d", "e");
|
||||
String result3 = letters.stream().reduce("", (partialString, element) -> partialString + element);
|
||||
System.out.println(result3);
|
||||
|
||||
String result4 = letters.stream().reduce("", String::concat);
|
||||
System.out.println(result4);
|
||||
|
||||
String result5 = letters.stream().reduce("", (partialString, element) -> partialString.toUpperCase() + element.toUpperCase());
|
||||
System.out.println(result5);
|
||||
|
||||
List<User> users = Arrays.asList(new User("John", 30), new User("Julie", 35));
|
||||
int result6 = users.stream().reduce(0, (partialAgeResult, user) -> partialAgeResult + user.getAge(), Integer::sum);
|
||||
System.out.println(result6);
|
||||
|
||||
String result7 = letters.parallelStream().reduce("", String::concat);
|
||||
System.out.println(result7);
|
||||
|
||||
int result8 = users.parallelStream().reduce(0, (partialAgeResult, user) -> partialAgeResult + user.getAge(), Integer::sum);
|
||||
System.out.println(result8);
|
||||
|
||||
org.openjdk.jmh.Main.main(args);
|
||||
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
@Fork(value = 1, warmups = 2)
|
||||
@Warmup(iterations = 2)
|
||||
@BenchmarkMode(Mode.AverageTime)
|
||||
public void executeReduceOnParallelizedStream() {
|
||||
List<User> userList = new ArrayList<>();
|
||||
for (int i = 0; i <= 1000000; i++) {
|
||||
userList.add(new User("John" + i, i));
|
||||
}
|
||||
userList.parallelStream().reduce(0, (partialAgeResult, user) -> partialAgeResult + user.getAge(), Integer::sum);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
@Fork(value = 1, warmups = 2)
|
||||
@Warmup(iterations = 2)
|
||||
@BenchmarkMode(Mode.AverageTime)
|
||||
public void executeReduceOnSequentialStream() {
|
||||
List<User> userList = new ArrayList<>();
|
||||
for (int i = 0; i <= 1000000; i++) {
|
||||
userList.add(new User("John" + i, i));
|
||||
}
|
||||
userList.stream().reduce(0, (partialAgeResult, user) -> partialAgeResult + user.getAge(), Integer::sum);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.baeldung.reduce.entities;
|
||||
|
||||
public class User {
|
||||
|
||||
private final String name;
|
||||
private final int age;
|
||||
|
||||
public User(String name, int age) {
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public int getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "User{" + "name=" + name + ", age=" + age + '}';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package com.baeldung.reduce.utilities;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.function.BiFunction;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
public abstract class NumberUtils {
|
||||
|
||||
private static final Logger LOGGER = Logger.getLogger(NumberUtils.class.getName());
|
||||
|
||||
public static int divideListElements(List<Integer> values, Integer divider) {
|
||||
return values.stream()
|
||||
.reduce(0, (a, b) -> {
|
||||
try {
|
||||
return a / divider + b / divider;
|
||||
} catch (ArithmeticException e) {
|
||||
LOGGER.log(Level.INFO, "Arithmetic Exception: Division by Zero");
|
||||
}
|
||||
return 0;
|
||||
});
|
||||
}
|
||||
|
||||
public static int divideListElementsWithExtractedTryCatchBlock(List<Integer> values, int divider) {
|
||||
return values.stream().reduce(0, (a, b) -> divide(a, divider) + divide(b, divider));
|
||||
}
|
||||
|
||||
public static int divideListElementsWithApplyFunctionMethod(List<Integer> values, int divider) {
|
||||
BiFunction<Integer, Integer, Integer> division = (a, b) -> a / b;
|
||||
return values.stream().reduce(0, (a, b) -> applyFunction(division, a, divider) + applyFunction(division, b, divider));
|
||||
}
|
||||
|
||||
private static int divide(int value, int factor) {
|
||||
int result = 0;
|
||||
try {
|
||||
result = value / factor;
|
||||
} catch (ArithmeticException e) {
|
||||
LOGGER.log(Level.INFO, "Arithmetic Exception: Division by Zero");
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private static int applyFunction(BiFunction<Integer, Integer, Integer> function, int a, int b) {
|
||||
try {
|
||||
return function.apply(a, b);
|
||||
}
|
||||
catch(Exception e) {
|
||||
LOGGER.log(Level.INFO, "Exception occurred!");
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
package com.baeldung.reduce.tests;
|
||||
|
||||
import com.baeldung.reduce.entities.User;
|
||||
import com.baeldung.reduce.utilities.NumberUtils;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import org.junit.Test;
|
||||
|
||||
public class StreamReduceUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenIntegerList_whenReduceWithSumAccumulatorLambda_thenCorrect() {
|
||||
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6);
|
||||
int result = numbers.stream().reduce(0, (a, b) -> a + b);
|
||||
assertThat(result).isEqualTo(21);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenIntegerList_whenReduceWithSumAccumulatorMethodReference_thenCorrect() {
|
||||
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6);
|
||||
int result = numbers.stream().reduce(0, Integer::sum);
|
||||
assertThat(result).isEqualTo(21);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenStringList_whenReduceWithConcatenatorAccumulatorLambda_thenCorrect() {
|
||||
List<String> letters = Arrays.asList("a", "b", "c", "d", "e");
|
||||
String result = letters.stream().reduce("", (a, b) -> a + b);
|
||||
assertThat(result).isEqualTo("abcde");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenStringList_whenReduceWithConcatenatorAccumulatorMethodReference_thenCorrect() {
|
||||
List<String> letters = Arrays.asList("a", "b", "c", "d", "e");
|
||||
String result = letters.stream().reduce("", String::concat);
|
||||
assertThat(result).isEqualTo("abcde");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenStringList_whenReduceWithUppercaseConcatenatorAccumulator_thenCorrect() {
|
||||
List<String> letters = Arrays.asList("a", "b", "c", "d", "e");
|
||||
String result = letters.stream().reduce("", (a, b) -> a.toUpperCase() + b.toUpperCase());
|
||||
assertThat(result).isEqualTo("ABCDE");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUserList_whenReduceWithAgeAccumulatorAndSumCombiner_thenCorrect() {
|
||||
List<User> users = Arrays.asList(new User("John", 30), new User("Julie", 35));
|
||||
int result = users.stream().reduce(0, (partialAgeResult, user) -> partialAgeResult + user.getAge(), Integer::sum);
|
||||
assertThat(result).isEqualTo(65);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenStringList_whenReduceWithParallelStream_thenCorrect() {
|
||||
List<String> letters = Arrays.asList("a", "b", "c", "d", "e");
|
||||
String result = letters.parallelStream().reduce("", String::concat);
|
||||
assertThat(result).isEqualTo("abcde");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenNumberUtilsClass_whenCalledDivideListElements_thenCorrect() {
|
||||
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6);
|
||||
assertThat(NumberUtils.divideListElements(numbers, 1)).isEqualTo(21);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenNumberUtilsClass_whenCalledDivideListElementsWithExtractedTryCatchBlock_thenCorrect() {
|
||||
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6);
|
||||
assertThat(NumberUtils.divideListElementsWithExtractedTryCatchBlock(numbers, 1)).isEqualTo(21);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenStream_whneCalleddivideListElementsWithApplyFunctionMethod_thenCorrect() {
|
||||
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6);
|
||||
assertThat(NumberUtils.divideListElementsWithApplyFunctionMethod(numbers, 1)).isEqualTo(21);
|
||||
}
|
||||
}
|
||||
@@ -2,9 +2,8 @@
|
||||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung.jhipster</groupId>
|
||||
<artifactId>jhipster-microservice-uaa</artifactId>
|
||||
<name>JHipster Microservice with UAA</name>
|
||||
<artifactId>jhipster-uaa</artifactId>
|
||||
<name>jhipster-uaa</name>
|
||||
<packaging>pom</packaging>
|
||||
|
||||
<parent>
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>lombok-custom</artifactId>
|
||||
<name>lombok-custom</name>
|
||||
<version>0.1-SNAPSHOT</version>
|
||||
|
||||
<parent>
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>custom-rule</artifactId>
|
||||
<name>custom-rule</name>
|
||||
|
||||
<properties>
|
||||
<api.version>3.0.0-M2</api.version>
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>maven-enforcer</artifactId>
|
||||
<name>maven-enforcer</name>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>osgi-intro-sample-activator</artifactId>
|
||||
<name>osgi-intro-sample-activator</name>
|
||||
<!-- Please, note this is not the usual 'jar'. -->
|
||||
<packaging>bundle</packaging>
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<!-- mvn:com.baeldung/osgi-intro-sample-client/1.0-SNAPSHOT -->
|
||||
<artifactId>osgi-intro-sample-client</artifactId>
|
||||
<name>osgi-intro-sample-client</name>
|
||||
<packaging>bundle</packaging>
|
||||
|
||||
<parent>
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>osgi-intro-sample-service</artifactId>
|
||||
<name>osgi-intro-sample-service</name>
|
||||
<!-- Please, note this is not the usual 'jar'. -->
|
||||
<packaging>bundle</packaging>
|
||||
|
||||
|
||||
@@ -55,7 +55,7 @@
|
||||
|
||||
<properties>
|
||||
<rest-assured.version>3.1.0</rest-assured.version>
|
||||
<spring-boot.version>1.5.16.RELEASE</spring-boot.version>
|
||||
<spring-boot.version>1.5.19.RELEASE</spring-boot.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
||||
@@ -77,9 +77,7 @@
|
||||
<properties>
|
||||
<rest-assured.version>3.1.0</rest-assured.version>
|
||||
<!-- plugins -->
|
||||
<thin.version>1.0.11.RELEASE</thin.version>
|
||||
<spring-boot.version>2.1.1.RELEASE</spring-boot.version>
|
||||
<thin.version>1.0.21.RELEASE</thin.version>
|
||||
<spring-boot.version>2.1.3.RELEASE</spring-boot.version>
|
||||
</properties>
|
||||
</project>
|
||||
|
||||
|
||||
|
||||
@@ -18,3 +18,4 @@
|
||||
- [Chain of Responsibility Design Pattern in Java](http://www.baeldung.com/chain-of-responsibility-pattern)
|
||||
- [The Command Pattern in Java](http://www.baeldung.com/java-command-pattern)
|
||||
- [Java Constructors vs Static Factory Methods](https://www.baeldung.com/java-constructors-vs-static-factory-methods)
|
||||
- [The Adapter Pattern in Java](https://www.baeldung.com/java-adapter-pattern)
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>solid</artifactId>
|
||||
<name>solid</name>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
<parent>
|
||||
|
||||
+6
@@ -1,5 +1,7 @@
|
||||
package com.baeldung.dao.repositories;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import com.baeldung.domain.Item;
|
||||
@@ -12,4 +14,8 @@ public interface CustomItemRepository {
|
||||
Item findItemById(Long id);
|
||||
|
||||
void findThenDelete(Long id);
|
||||
|
||||
List<Item> findItemsByColorAndGrade();
|
||||
|
||||
List<Item> findItemByColorOrGrade();
|
||||
}
|
||||
|
||||
+57
-1
@@ -1,12 +1,18 @@
|
||||
package com.baeldung.dao.repositories.impl;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.criteria.CriteriaBuilder;
|
||||
import javax.persistence.criteria.CriteriaQuery;
|
||||
import javax.persistence.criteria.Predicate;
|
||||
import javax.persistence.criteria.Root;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import com.baeldung.domain.Item;
|
||||
import com.baeldung.dao.repositories.CustomItemRepository;
|
||||
import com.baeldung.domain.Item;
|
||||
|
||||
@Repository
|
||||
public class CustomItemRepositoryImpl implements CustomItemRepository {
|
||||
@@ -29,4 +35,54 @@ public class CustomItemRepositoryImpl implements CustomItemRepository {
|
||||
final Item item = entityManager.find(Item.class, id);
|
||||
entityManager.remove(item);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Item> findItemsByColorAndGrade() {
|
||||
|
||||
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
|
||||
CriteriaQuery<Item> criteriaQuery = criteriaBuilder.createQuery(Item.class);
|
||||
Root<Item> itemRoot = criteriaQuery.from(Item.class);
|
||||
|
||||
Predicate predicateForBlueColor = criteriaBuilder.equal(itemRoot.get("color"), "blue");
|
||||
Predicate predicateForRedColor = criteriaBuilder.equal(itemRoot.get("color"), "red");
|
||||
Predicate predicateForColor = criteriaBuilder.or(predicateForBlueColor, predicateForRedColor);
|
||||
|
||||
Predicate predicateForGradeA = criteriaBuilder.equal(itemRoot.get("grade"), "A");
|
||||
Predicate predicateForGradeB = criteriaBuilder.equal(itemRoot.get("grade"), "B");
|
||||
Predicate predicateForGrade = criteriaBuilder.or(predicateForGradeA, predicateForGradeB);
|
||||
|
||||
// final search filter
|
||||
Predicate finalPredicate = criteriaBuilder.and(predicateForColor, predicateForGrade);
|
||||
|
||||
criteriaQuery.where(finalPredicate);
|
||||
|
||||
List<Item> items = entityManager.createQuery(criteriaQuery)
|
||||
.getResultList();
|
||||
return items;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Item> findItemByColorOrGrade() {
|
||||
|
||||
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
|
||||
CriteriaQuery<Item> criteriaQuery = criteriaBuilder.createQuery(Item.class);
|
||||
Root<Item> itemRoot = criteriaQuery.from(Item.class);
|
||||
|
||||
Predicate predicateForBlueColor = criteriaBuilder.equal(itemRoot.get("color"), "red");
|
||||
Predicate predicateForGradeA = criteriaBuilder.equal(itemRoot.get("grade"), "D");
|
||||
Predicate predicateForBlueColorAndGradeA = criteriaBuilder.and(predicateForBlueColor, predicateForGradeA);
|
||||
|
||||
Predicate predicateForRedColor = criteriaBuilder.equal(itemRoot.get("color"), "blue");
|
||||
Predicate predicateForGradeB = criteriaBuilder.equal(itemRoot.get("grade"), "B");
|
||||
Predicate predicateForRedColorAndGradeB = criteriaBuilder.and(predicateForRedColor, predicateForGradeB);
|
||||
|
||||
// final search filter
|
||||
Predicate finalPredicate = criteriaBuilder.or(predicateForBlueColorAndGradeA, predicateForRedColorAndGradeB);
|
||||
|
||||
criteriaQuery.where(finalPredicate);
|
||||
|
||||
List<Item> items = entityManager.createQuery(criteriaQuery)
|
||||
.getResultList();
|
||||
return items;
|
||||
}
|
||||
}
|
||||
|
||||
+94
@@ -0,0 +1,94 @@
|
||||
package com.baeldung.dao.repositories.impl;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
|
||||
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.autoconfigure.orm.jpa.DataJpaTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
import com.baeldung.config.PersistenceConfiguration;
|
||||
import com.baeldung.config.PersistenceProductConfiguration;
|
||||
import com.baeldung.config.PersistenceUserConfiguration;
|
||||
import com.baeldung.dao.repositories.CustomItemRepository;
|
||||
import com.baeldung.domain.Item;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@DataJpaTest(excludeAutoConfiguration = { PersistenceConfiguration.class, PersistenceUserConfiguration.class, PersistenceProductConfiguration.class })
|
||||
public class CustomItemRepositoryIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
CustomItemRepository customItemRepositoryImpl;
|
||||
|
||||
@Autowired
|
||||
EntityManager entityManager;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
|
||||
Item firstItem = new Item();
|
||||
firstItem.setColor("blue");
|
||||
firstItem.setGrade("C");
|
||||
firstItem.setId(10l);
|
||||
|
||||
entityManager.persist(firstItem);
|
||||
|
||||
Item secondItem = new Item();
|
||||
secondItem.setColor("red");
|
||||
secondItem.setGrade("C");
|
||||
secondItem.setId(11l);
|
||||
|
||||
entityManager.persist(secondItem);
|
||||
|
||||
Item thirdItem = new Item();
|
||||
thirdItem.setColor("blue");
|
||||
thirdItem.setGrade("A");
|
||||
thirdItem.setId(12l);
|
||||
|
||||
entityManager.persist(thirdItem);
|
||||
|
||||
Item fourthItem = new Item();
|
||||
fourthItem.setColor("red");
|
||||
fourthItem.setGrade("D");
|
||||
fourthItem.setId(13l);
|
||||
|
||||
entityManager.persist(fourthItem);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenItems_whenFindItemsByColorAndGrade_thenReturnItems() {
|
||||
|
||||
List<Item> items = customItemRepositoryImpl.findItemsByColorAndGrade();
|
||||
|
||||
assertFalse("No items found", CollectionUtils.isEmpty(items));
|
||||
assertEquals("There should be only one item", 1, items.size());
|
||||
|
||||
Item item = items.get(0);
|
||||
|
||||
assertEquals("this item do not have blue color", "blue", item.getColor());
|
||||
assertEquals("this item does not belong to A grade", "A", item.getGrade());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenItems_whenFindItemByColorOrGrade_thenReturnItems() {
|
||||
|
||||
List<Item> items = customItemRepositoryImpl.findItemByColorOrGrade();
|
||||
|
||||
assertFalse("No items found", CollectionUtils.isEmpty(items));
|
||||
assertEquals("There should be only one item", 1, items.size());
|
||||
|
||||
Item item = items.get(0);
|
||||
|
||||
assertEquals("this item do not have red color", "red", item.getColor());
|
||||
assertEquals("this item does not belong to D grade", "D", item.getGrade());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -15,7 +15,6 @@
|
||||
- [Self-Contained Testing Using an In-Memory Database](http://www.baeldung.com/spring-jpa-test-in-memory-database)
|
||||
- [A Guide to Spring AbstractRoutingDatasource](http://www.baeldung.com/spring-abstract-routing-data-source)
|
||||
- [A Guide to Hibernate with Spring 4](http://www.baeldung.com/the-persistence-layer-with-spring-and-jpa)
|
||||
- [Testing REST with multiple MIME types](http://www.baeldung.com/testing-rest-api-with-multiple-media-types)
|
||||
- [Obtaining Auto-generated Keys in Spring JDBC](http://www.baeldung.com/spring-jdbc-autogenerated-keys)
|
||||
- [Transactions with Spring 4 and JPA](http://www.baeldung.com/transaction-configuration-with-jpa-and-spring)
|
||||
- [Use Criteria Queries in a Spring Data Application](https://www.baeldung.com/spring-data-criteria-queries)
|
||||
|
||||
@@ -412,7 +412,7 @@
|
||||
<module>feign</module>
|
||||
<module>flyway-cdi-extension</module>
|
||||
|
||||
<!-- <module>geotools</module> --> <!-- Hangs the build. Fixing in BAEL-10943 -->
|
||||
<module>geotools</module>
|
||||
<module>google-cloud</module>
|
||||
<module>google-web-toolkit</module>
|
||||
<!-- <module>gradle</module> --> <!-- Not a maven project -->
|
||||
@@ -542,7 +542,7 @@
|
||||
<module>persistence-modules/spring-data-couchbase-2</module>
|
||||
<module>persistence-modules/spring-data-dynamodb</module>
|
||||
<module>persistence-modules/spring-data-eclipselink</module>
|
||||
<!-- <module>persistence-modules/spring-data-elasticsearch</module> --> <!-- Fixing in BAEL-10995 -->
|
||||
<module>persistence-modules/spring-data-elasticsearch</module>
|
||||
<module>persistence-modules/spring-data-gemfire</module>
|
||||
<module>persistence-modules/spring-data-jpa</module>
|
||||
<module>persistence-modules/spring-data-keyvalue</module>
|
||||
@@ -1130,7 +1130,7 @@
|
||||
<module>feign</module>
|
||||
<module>flyway-cdi-extension</module>
|
||||
|
||||
<!-- <module>geotools</module> --> <!-- Hangs the build. Fixing in BAEL-10943 -->
|
||||
<module>geotools</module>
|
||||
<module>google-cloud</module>
|
||||
<module>google-web-toolkit</module>
|
||||
<!-- <module>gradle</module> --> <!-- Not a maven project -->
|
||||
@@ -1260,7 +1260,7 @@
|
||||
<module>persistence-modules/spring-data-couchbase-2</module>
|
||||
<module>persistence-modules/spring-data-dynamodb</module>
|
||||
<module>persistence-modules/spring-data-eclipselink</module>
|
||||
<!-- <module>persistence-modules/spring-data-elasticsearch</module> --> <!-- Fixing in BAEL-10995 -->
|
||||
<module>persistence-modules/spring-data-elasticsearch</module>
|
||||
<module>persistence-modules/spring-data-gemfire</module>
|
||||
<module>persistence-modules/spring-data-jpa</module>
|
||||
<module>persistence-modules/spring-data-keyvalue</module>
|
||||
|
||||
+2
-2
@@ -5,7 +5,7 @@
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>restx</artifactId>
|
||||
<version>0.1-SNAPSHOT</version>
|
||||
<name>restx-demo</name>
|
||||
<name>restx</name>
|
||||
<packaging>war</packaging>
|
||||
|
||||
<parent>
|
||||
@@ -151,6 +151,6 @@
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<restx.version>0.35-rc4</restx.version>
|
||||
<logback-classic.version>1.2.3</logback-classic.version>
|
||||
<logback-classic.version>1.2.3</logback-classic.version>
|
||||
</properties>
|
||||
</project>
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>rxjava-2</artifactId>
|
||||
<name>rxjava-2</name>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
<parent>
|
||||
|
||||
@@ -2,7 +2,8 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" 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">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung.springbootangular</groupId>
|
||||
<artifactId>springbootangular</artifactId>
|
||||
<artifactId>spring-boot-angular</artifactId>
|
||||
<name>spring-boot-angular</name>
|
||||
<version>1.0</version>
|
||||
<packaging>jar</packaging>
|
||||
<parent>
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user