MsMasterpull
erge branch 'master' of https://github.com/eugenp/tutorials
This commit is contained in:
@@ -1,12 +1,7 @@
|
|||||||
issuer:
|
issuer:
|
||||||
uri: http://localhost:8080/uaa
|
uri: http://localhost:8080/uaa
|
||||||
|
|
||||||
spring_profiles: postgresql,default
|
spring_profiles: default,hsqldb
|
||||||
|
|
||||||
database.driverClassName: org.postgresql.Driver
|
|
||||||
database.url: jdbc:postgresql:uaadb2
|
|
||||||
database.username: postgres
|
|
||||||
database.password: postgres
|
|
||||||
|
|
||||||
encryption:
|
encryption:
|
||||||
active_key_label: CHANGE-THIS-KEY
|
active_key_label: CHANGE-THIS-KEY
|
||||||
|
|||||||
@@ -1,23 +1,11 @@
|
|||||||
# SECURITY OAUTH2 CLIENT (OAuth2ClientProperties)
|
|
||||||
#spring.security.oauth2.client.provider.*= # OAuth provider details.
|
|
||||||
#spring.security.oauth2.client.registration.*= # OAuth client registrations.
|
|
||||||
|
|
||||||
server.port=8081
|
server.port=8081
|
||||||
#server.servlet.context-path=/uaa-client-webapp
|
|
||||||
|
|
||||||
uaa.url=http://localhost:8080/uaa
|
|
||||||
resource.server.url=http://localhost:8082
|
resource.server.url=http://localhost:8082
|
||||||
|
|
||||||
spring.security.oauth2.client.registration.uaa.client-name=UAA OAuth2 Client
|
spring.security.oauth2.client.registration.uaa.client-name=Web App Client
|
||||||
spring.security.oauth2.client.registration.uaa.client-id=client1
|
spring.security.oauth2.client.registration.uaa.client-id=webappclient
|
||||||
spring.security.oauth2.client.registration.uaa.client-secret=client1
|
spring.security.oauth2.client.registration.uaa.client-secret=webappclientsecret
|
||||||
spring.security.oauth2.client.registration.uaa.authorization-grant-type=authorization_code
|
|
||||||
spring.security.oauth2.client.registration.uaa.scope=resource.read,resource.write,openid,profile
|
spring.security.oauth2.client.registration.uaa.scope=resource.read,resource.write,openid,profile
|
||||||
spring.security.oauth2.client.registration.uaa.redirect-uri=http://localhost:8081/login/oauth2/code/uaa
|
|
||||||
#spring.security.oauth2.client.registration.uaa.redirect-uri=http://localhost:8081/**
|
|
||||||
|
|
||||||
spring.security.oauth2.client.provider.uaa.token-uri=${uaa.url}/oauth/token
|
spring.security.oauth2.client.provider.uaa.issuer-uri=http://localhost:8080/uaa/oauth/token
|
||||||
spring.security.oauth2.client.provider.uaa.authorization-uri=${uaa.url}/oauth/authorize
|
|
||||||
spring.security.oauth2.client.provider.uaa.jwk-set-uri=${uaa.url}/token_keys
|
|
||||||
spring.security.oauth2.client.provider.uaa.user-info-uri=${uaa.url}/userinfo
|
|
||||||
spring.security.oauth2.client.provider.uaa.user-name-attribute=user_name
|
|
||||||
|
|||||||
+1
-14
@@ -1,16 +1,3 @@
|
|||||||
server.port=8082
|
server.port=8082
|
||||||
|
|
||||||
uaa.url=http://localhost:8080/uaa
|
spring.security.oauth2.resourceserver.jwt.issuer-uri=http://localhost:8080/uaa/oauth/token
|
||||||
|
|
||||||
#approch1
|
|
||||||
spring.security.oauth2.resourceserver.jwt.issuer-uri=${uaa.url}/oauth/token
|
|
||||||
|
|
||||||
#approch2
|
|
||||||
#spring.security.oauth2.resourceserver.jwt.jwk-set-uri=${uaa.url}/token_key
|
|
||||||
|
|
||||||
# SECURITY OAUTH2 CLIENT (OAuth2ClientProperties)
|
|
||||||
#security.oauth2.client.client-id=client1
|
|
||||||
#security.oauth2.client.client-secret=client1
|
|
||||||
|
|
||||||
#security.oauth2.resource.jwt.key-uri=${uaa.url}/token_key
|
|
||||||
#security.oauth2.resource.token-info-uri=${uaa.url}/oauth/check_token
|
|
||||||
|
|||||||
@@ -9,3 +9,5 @@
|
|||||||
- [A Quick Guide to Iterating a Map in Groovy](https://www.baeldung.com/groovy-map-iterating)
|
- [A Quick Guide to Iterating a Map in Groovy](https://www.baeldung.com/groovy-map-iterating)
|
||||||
- [An Introduction to Traits in Groovy](https://www.baeldung.com/groovy-traits)
|
- [An Introduction to Traits in Groovy](https://www.baeldung.com/groovy-traits)
|
||||||
- [Lists in Groovy](https://www.baeldung.com/groovy-lists)
|
- [Lists in Groovy](https://www.baeldung.com/groovy-lists)
|
||||||
|
- [Converting a String to a Date in Groovy](https://www.baeldung.com/groovy-string-to-date)
|
||||||
|
- [Guide to I/O in Groovy](https://www.baeldung.com/groovy-io)
|
||||||
|
|||||||
@@ -0,0 +1,37 @@
|
|||||||
|
package com.baeldung
|
||||||
|
|
||||||
|
class Person {
|
||||||
|
private String firstname
|
||||||
|
private String lastname
|
||||||
|
private Integer age
|
||||||
|
|
||||||
|
Person(String firstname, String lastname, Integer age) {
|
||||||
|
this.firstname = firstname
|
||||||
|
this.lastname = lastname
|
||||||
|
this.age = age
|
||||||
|
}
|
||||||
|
|
||||||
|
String getFirstname() {
|
||||||
|
return firstname
|
||||||
|
}
|
||||||
|
|
||||||
|
void setFirstname(String firstname) {
|
||||||
|
this.firstname = firstname
|
||||||
|
}
|
||||||
|
|
||||||
|
String getLastname() {
|
||||||
|
return lastname
|
||||||
|
}
|
||||||
|
|
||||||
|
void setLastname(String lastname) {
|
||||||
|
this.lastname = lastname
|
||||||
|
}
|
||||||
|
|
||||||
|
Integer getAge() {
|
||||||
|
return age
|
||||||
|
}
|
||||||
|
|
||||||
|
void setAge(Integer age) {
|
||||||
|
this.age = age
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,87 @@
|
|||||||
|
package com.baeldung.closures
|
||||||
|
|
||||||
|
class Closures {
|
||||||
|
|
||||||
|
def printWelcome = {
|
||||||
|
println "Welcome to Closures!"
|
||||||
|
}
|
||||||
|
|
||||||
|
def print = { name ->
|
||||||
|
println name
|
||||||
|
}
|
||||||
|
|
||||||
|
def formatToLowerCase(name) {
|
||||||
|
return name.toLowerCase()
|
||||||
|
}
|
||||||
|
def formatToLowerCaseClosure = { name ->
|
||||||
|
return name.toLowerCase()
|
||||||
|
}
|
||||||
|
|
||||||
|
def count=0
|
||||||
|
|
||||||
|
def increaseCount = {
|
||||||
|
count++
|
||||||
|
}
|
||||||
|
|
||||||
|
def greet = {
|
||||||
|
return "Hello! ${it}"
|
||||||
|
}
|
||||||
|
|
||||||
|
def multiply = { x, y ->
|
||||||
|
return x*y
|
||||||
|
}
|
||||||
|
|
||||||
|
def calculate = {int x, int y, String operation ->
|
||||||
|
|
||||||
|
//log closure
|
||||||
|
def log = {
|
||||||
|
println "Performing $it"
|
||||||
|
}
|
||||||
|
|
||||||
|
def result = 0
|
||||||
|
switch(operation) {
|
||||||
|
case "ADD":
|
||||||
|
log("Addition")
|
||||||
|
result = x+y
|
||||||
|
break
|
||||||
|
case "SUB":
|
||||||
|
log("Subtraction")
|
||||||
|
result = x-y
|
||||||
|
break
|
||||||
|
case "MUL":
|
||||||
|
log("Multiplication")
|
||||||
|
result = x*y
|
||||||
|
break
|
||||||
|
case "DIV":
|
||||||
|
log("Division")
|
||||||
|
result = x/y
|
||||||
|
break
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
def addAll = { int... args ->
|
||||||
|
return args.sum()
|
||||||
|
}
|
||||||
|
|
||||||
|
def volume(Closure areaCalculator, int... dimensions) {
|
||||||
|
if(dimensions.size() == 3) {
|
||||||
|
|
||||||
|
//consider dimension[0] = length, dimension[1] = breadth, dimension[2] = height
|
||||||
|
//for cube and cuboid
|
||||||
|
return areaCalculator(dimensions[0], dimensions[1]) * dimensions[2]
|
||||||
|
} else if(dimensions.size() == 2) {
|
||||||
|
|
||||||
|
//consider dimension[0] = radius, dimension[1] = height
|
||||||
|
//for cylinder and cone
|
||||||
|
return areaCalculator(dimensions[0]) * dimensions[1]
|
||||||
|
} else if(dimensions.size() == 1) {
|
||||||
|
|
||||||
|
//consider dimension[0] = radius
|
||||||
|
//for sphere
|
||||||
|
return areaCalculator(dimensions[0]) * dimensions[0]
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
package com.baeldung.closures
|
||||||
|
|
||||||
|
class Employee {
|
||||||
|
|
||||||
|
String fullName
|
||||||
|
}
|
||||||
@@ -0,0 +1,80 @@
|
|||||||
|
package com.baeldung.closures
|
||||||
|
|
||||||
|
import spock.lang.Specification
|
||||||
|
|
||||||
|
class ClosuresUnitTest extends GroovyTestCase {
|
||||||
|
|
||||||
|
Closures closures = new Closures()
|
||||||
|
|
||||||
|
void testDeclaration() {
|
||||||
|
|
||||||
|
closures.print("Hello! Closure")
|
||||||
|
closures.formatToLowerCaseClosure("Hello! Closure")
|
||||||
|
|
||||||
|
closures.print.call("Hello! Closure")
|
||||||
|
closures.formatToLowerCaseClosure.call("Hello! Closure")
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void testClosureVsMethods() {
|
||||||
|
assert closures.formatToLowerCase("TONY STARK") == closures.formatToLowerCaseClosure("Tony STark")
|
||||||
|
}
|
||||||
|
|
||||||
|
void testParameters() {
|
||||||
|
//implicit parameter
|
||||||
|
assert closures.greet("Alex") == "Hello! Alex"
|
||||||
|
|
||||||
|
//multiple parameters
|
||||||
|
assert closures.multiply(2, 4) == 8
|
||||||
|
|
||||||
|
assert closures.calculate(12, 4, "ADD") == 16
|
||||||
|
assert closures.calculate(12, 4, "SUB") == 8
|
||||||
|
assert closures.calculate(43, 8, "DIV") == 5.375
|
||||||
|
|
||||||
|
//varags
|
||||||
|
assert closures.addAll(12, 10, 14) == 36
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void testClosureAsAnArgument() {
|
||||||
|
assert closures.volume({ l, b -> return l*b }, 12, 6, 10) == 720
|
||||||
|
|
||||||
|
assert closures.volume({ radius -> return Math.PI*radius*radius/3 }, 5, 10) == Math.PI * 250/3
|
||||||
|
}
|
||||||
|
|
||||||
|
void testGStringsLazyEvaluation() {
|
||||||
|
def name = "Samwell"
|
||||||
|
def welcomeMsg = "Welcome! $name"
|
||||||
|
|
||||||
|
assert welcomeMsg == "Welcome! Samwell"
|
||||||
|
|
||||||
|
// changing the name does not affect original interpolated value
|
||||||
|
name = "Tarly"
|
||||||
|
assert welcomeMsg != "Welcome! Tarly"
|
||||||
|
|
||||||
|
def fullName = "Tarly Samson"
|
||||||
|
def greetStr = "Hello! ${-> fullName}"
|
||||||
|
|
||||||
|
assert greetStr == "Hello! Tarly Samson"
|
||||||
|
|
||||||
|
// this time changing the variable affects the interpolated String's value
|
||||||
|
fullName = "Jon Smith"
|
||||||
|
assert greetStr == "Hello! Jon Smith"
|
||||||
|
}
|
||||||
|
|
||||||
|
void testClosureInLists() {
|
||||||
|
def list = [10, 11, 12, 13, 14, true, false, "BUNTHER"]
|
||||||
|
list.each {
|
||||||
|
println it
|
||||||
|
}
|
||||||
|
|
||||||
|
assert [13, 14] == list.findAll{ it instanceof Integer && it >= 13}
|
||||||
|
}
|
||||||
|
|
||||||
|
void testClosureInMaps() {
|
||||||
|
def map = [1:10, 2:30, 4:5]
|
||||||
|
|
||||||
|
assert [10, 60, 20] == map.collect{it.key * it.value}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -129,13 +129,13 @@ class ListTest{
|
|||||||
assertTrue(filterList.findAll{it > 3} == [4, 5, 6, 76])
|
assertTrue(filterList.findAll{it > 3} == [4, 5, 6, 76])
|
||||||
|
|
||||||
assertTrue(filterList.findAll{ it instanceof Number} == [2, 1, 3, 4, 5, 6, 76])
|
assertTrue(filterList.findAll{ it instanceof Number} == [2, 1, 3, 4, 5, 6, 76])
|
||||||
|
|
||||||
assertTrue(filterList.grep( Number )== [2, 1, 3, 4, 5, 6, 76])
|
assertTrue(filterList.grep( Number )== [2, 1, 3, 4, 5, 6, 76])
|
||||||
|
|
||||||
assertTrue(filterList.grep{ it> 6 }== [76])
|
assertTrue(filterList.grep{ it> 6 }== [76])
|
||||||
|
|
||||||
def conditionList = [2, 1, 3, 4, 5, 6, 76]
|
def conditionList = [2, 1, 3, 4, 5, 6, 76]
|
||||||
|
|
||||||
assertFalse(conditionList.every{ it < 6})
|
assertFalse(conditionList.every{ it < 6})
|
||||||
|
|
||||||
assertTrue(conditionList.any{ it%2 == 0})
|
assertTrue(conditionList.any{ it%2 == 0})
|
||||||
@@ -165,7 +165,7 @@ class ListTest{
|
|||||||
|
|
||||||
def strList = ["na", "ppp", "as"]
|
def strList = ["na", "ppp", "as"]
|
||||||
assertTrue(strList.max() == "ppp")
|
assertTrue(strList.max() == "ppp")
|
||||||
|
|
||||||
Comparator minc = {a,b -> a == b? 0: a < b? -1 : 1}
|
Comparator minc = {a,b -> a == b? 0: a < b? -1 : 1}
|
||||||
def numberList = [3, 2, 0, 7]
|
def numberList = [3, 2, 0, 7]
|
||||||
assertTrue(numberList.min(minc) == 0)
|
assertTrue(numberList.min(minc) == 0)
|
||||||
|
|||||||
@@ -0,0 +1,58 @@
|
|||||||
|
package com.baeldung.lists
|
||||||
|
|
||||||
|
import com.baeldung.Person
|
||||||
|
import org.junit.Test
|
||||||
|
|
||||||
|
import static org.junit.Assert.*
|
||||||
|
|
||||||
|
class ListUnitTest {
|
||||||
|
|
||||||
|
private final personList = [
|
||||||
|
new Person("Regina", "Fitzpatrick", 25),
|
||||||
|
new Person("Abagail", "Ballard", 26),
|
||||||
|
new Person("Lucian", "Walter", 30),
|
||||||
|
]
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenListContainsElement_thenCheckReturnsTrue() {
|
||||||
|
def list = ['a', 'b', 'c']
|
||||||
|
|
||||||
|
assertTrue(list.indexOf('a') > -1)
|
||||||
|
assertTrue(list.contains('a'))
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenListContainsElement_thenCheckWithMembershipOperatorReturnsTrue() {
|
||||||
|
def list = ['a', 'b', 'c']
|
||||||
|
|
||||||
|
assertTrue('a' in list)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenListOfPerson_whenUsingStreamMatching_thenShouldEvaluateList() {
|
||||||
|
assertTrue(personList.stream().anyMatch {it.age > 20})
|
||||||
|
assertFalse(personList.stream().allMatch {it.age < 30})
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenListOfPerson_whenUsingCollectionMatching_thenShouldEvaluateList() {
|
||||||
|
assertTrue(personList.any {it.age > 20})
|
||||||
|
assertFalse(personList.every {it.age < 30})
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenListOfPerson_whenUsingStreamFind_thenShouldReturnMatchingElements() {
|
||||||
|
assertTrue(personList.stream().filter {it.age > 20}.findAny().isPresent())
|
||||||
|
assertFalse(personList.stream().filter {it.age > 30}.findAny().isPresent())
|
||||||
|
assertTrue(personList.stream().filter {it.age > 20}.findAll().size() == 3)
|
||||||
|
assertTrue(personList.stream().filter {it.age > 30}.findAll().isEmpty())
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenListOfPerson_whenUsingCollectionFind_thenShouldReturnMatchingElements() {
|
||||||
|
assertNotNull(personList.find {it.age > 20})
|
||||||
|
assertNull(personList.find {it.age > 30})
|
||||||
|
assertTrue(personList.findAll {it.age > 20}.size() == 3)
|
||||||
|
assertTrue(personList.findAll {it.age > 30}.isEmpty())
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,10 +1,18 @@
|
|||||||
package com.baeldung.map
|
package com.baeldung.map
|
||||||
|
|
||||||
import static org.junit.Assert.*
|
import com.baeldung.Person
|
||||||
import org.junit.Test
|
import org.junit.Test
|
||||||
|
|
||||||
|
import static org.junit.Assert.*
|
||||||
|
|
||||||
class MapUnitTest {
|
class MapUnitTest {
|
||||||
|
|
||||||
|
private final personMap = [
|
||||||
|
Regina : new Person("Regina", "Fitzpatrick", 25),
|
||||||
|
Abagail: new Person("Abagail", "Ballard", 26),
|
||||||
|
Lucian : new Person("Lucian", "Walter", 30)
|
||||||
|
]
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void whenUsingEach_thenMapIsIterated() {
|
void whenUsingEach_thenMapIsIterated() {
|
||||||
def map = [
|
def map = [
|
||||||
@@ -63,7 +71,7 @@ class MapUnitTest {
|
|||||||
'FF6347' : 'Tomato',
|
'FF6347' : 'Tomato',
|
||||||
'FF4500' : 'Orange Red'
|
'FF4500' : 'Orange Red'
|
||||||
]
|
]
|
||||||
|
|
||||||
map.eachWithIndex { key, val, index ->
|
map.eachWithIndex { key, val, index ->
|
||||||
def indent = ((index == 0 || index % 2 == 0) ? " " : "")
|
def indent = ((index == 0 || index % 2 == 0) ? " " : "")
|
||||||
println "$indent Hex Code: $key = Color Name: $val"
|
println "$indent Hex Code: $key = Color Name: $val"
|
||||||
@@ -82,4 +90,65 @@ class MapUnitTest {
|
|||||||
println "Hex Code: $entry.key = Color Name: $entry.value"
|
println "Hex Code: $entry.key = Color Name: $entry.value"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenMapContainsKeyElement_thenCheckReturnsTrue() {
|
||||||
|
def map = [a: 'd', b: 'e', c: 'f']
|
||||||
|
|
||||||
|
assertTrue(map.containsKey('a'))
|
||||||
|
assertFalse(map.containsKey('e'))
|
||||||
|
assertTrue(map.containsValue('e'))
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenMapContainsKeyElement_thenCheckByMembershipReturnsTrue() {
|
||||||
|
def map = [a: 'd', b: 'e', c: 'f']
|
||||||
|
|
||||||
|
assertTrue('a' in map)
|
||||||
|
assertFalse('f' in map)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenMapContainsFalseBooleanValues_thenCheckReturnsFalse() {
|
||||||
|
def map = [a: true, b: false, c: null]
|
||||||
|
|
||||||
|
assertTrue(map.containsKey('b'))
|
||||||
|
assertTrue('a' in map)
|
||||||
|
assertFalse('b' in map)
|
||||||
|
assertFalse('c' in map)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenMapOfPerson_whenUsingStreamMatching_thenShouldEvaluateMap() {
|
||||||
|
assertTrue(personMap.keySet().stream().anyMatch {it == "Regina"})
|
||||||
|
assertFalse(personMap.keySet().stream().allMatch {it == "Albert"})
|
||||||
|
assertFalse(personMap.values().stream().allMatch {it.age < 30})
|
||||||
|
assertTrue(personMap.entrySet().stream().anyMatch {it.key == "Abagail" && it.value.lastname == "Ballard"})
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenMapOfPerson_whenUsingCollectionMatching_thenShouldEvaluateMap() {
|
||||||
|
assertTrue(personMap.keySet().any {it == "Regina"})
|
||||||
|
assertFalse(personMap.keySet().every {it == "Albert"})
|
||||||
|
assertFalse(personMap.values().every {it.age < 30})
|
||||||
|
assertTrue(personMap.any {firstname, person -> firstname == "Abagail" && person.lastname == "Ballard"})
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenMapOfPerson_whenUsingCollectionFind_thenShouldReturnElements() {
|
||||||
|
assertNotNull(personMap.find {it.key == "Abagail" && it.value.lastname == "Ballard"})
|
||||||
|
assertTrue(personMap.findAll {it.value.age > 20}.size() == 3)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenMapOfPerson_whenUsingStreamFind_thenShouldReturnElements() {
|
||||||
|
assertTrue(
|
||||||
|
personMap.entrySet().stream()
|
||||||
|
.filter {it.key == "Abagail" && it.value.lastname == "Ballard"}
|
||||||
|
.findAny().isPresent())
|
||||||
|
assertTrue(
|
||||||
|
personMap.entrySet().stream()
|
||||||
|
.filter {it.value.age > 20}
|
||||||
|
.findAll().size() == 3)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,16 @@
|
|||||||
|
package com.baeldung.set
|
||||||
|
|
||||||
|
import org.junit.Test
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertTrue
|
||||||
|
|
||||||
|
class SetUnitTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void whenSetContainsElement_thenCheckReturnsTrue() {
|
||||||
|
def set = ['a', 'b', 'c'] as Set
|
||||||
|
|
||||||
|
assertTrue(set.contains('a'))
|
||||||
|
assertTrue('a' in set)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -5,3 +5,4 @@
|
|||||||
- [Java 11 String API Additions](https://www.baeldung.com/java-11-string-api)
|
- [Java 11 String API Additions](https://www.baeldung.com/java-11-string-api)
|
||||||
- [Java 11 Nest Based Access Control](https://www.baeldung.com/java-nest-based-access-control)
|
- [Java 11 Nest Based Access Control](https://www.baeldung.com/java-nest-based-access-control)
|
||||||
- [Exploring the New HTTP Client in Java 9 and 11](https://www.baeldung.com/java-9-http-client)
|
- [Exploring the New HTTP Client in Java 9 and 11](https://www.baeldung.com/java-9-http-client)
|
||||||
|
- [An Introduction to Epsilon GC: A No-Op Experimental Garbage Collector](https://www.baeldung.com/jvm-epsilon-gc-garbage-collector)
|
||||||
|
|||||||
@@ -0,0 +1,12 @@
|
|||||||
|
package com.baeldung.jlink;
|
||||||
|
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
|
public class HelloWorld {
|
||||||
|
|
||||||
|
private static final Logger LOG = Logger.getLogger(HelloWorld.class.getName());
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
LOG.info("Hello World!");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
module jlinkModule {
|
||||||
|
requires java.logging;
|
||||||
|
}
|
||||||
@@ -0,0 +1,48 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-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</groupId>
|
||||||
|
<artifactId>core-java-12</artifactId>
|
||||||
|
<version>0.1.0-SNAPSHOT</version>
|
||||||
|
<name>core-java-12</name>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
<url>http://maven.apache.org</url>
|
||||||
|
|
||||||
|
<parent>
|
||||||
|
<groupId>com.baeldung</groupId>
|
||||||
|
<artifactId>parent-modules</artifactId>
|
||||||
|
<version>1.0.0-SNAPSHOT</version>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.assertj</groupId>
|
||||||
|
<artifactId>assertj-core</artifactId>
|
||||||
|
<version>${assertj.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<version>${maven-compiler-plugin.version}</version>
|
||||||
|
<configuration>
|
||||||
|
<source>${maven.compiler.source.version}</source>
|
||||||
|
<target>${maven.compiler.target.version}</target>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<maven.compiler.source.version>12</maven.compiler.source.version>
|
||||||
|
<maven.compiler.target.version>12</maven.compiler.target.version>
|
||||||
|
<assertj.version>3.6.1</assertj.version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
</project>
|
||||||
@@ -0,0 +1,71 @@
|
|||||||
|
package com.baeldung.collectors;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import static java.util.stream.Collectors.maxBy;
|
||||||
|
import static java.util.stream.Collectors.minBy;
|
||||||
|
import static java.util.stream.Collectors.teeing;
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Unit tests for collectors additions in Java 12.
|
||||||
|
*/
|
||||||
|
public class CollectorsUnitTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenTeeing_ItShouldCombineTheResultsAsExpected() {
|
||||||
|
List<Integer> numbers = Arrays.asList(42, 4, 2, 24);
|
||||||
|
Range range = numbers.stream()
|
||||||
|
.collect(teeing(minBy(Integer::compareTo), maxBy(Integer::compareTo), (min, max) -> new Range(min.orElse(null), max.orElse(null))));
|
||||||
|
|
||||||
|
assertThat(range).isEqualTo(new Range(2, 42));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Represents a closed range of numbers between {@link #min} and
|
||||||
|
* {@link #max}, both inclusive.
|
||||||
|
*/
|
||||||
|
private static class Range {
|
||||||
|
|
||||||
|
private final Integer min;
|
||||||
|
|
||||||
|
private final Integer max;
|
||||||
|
|
||||||
|
Range(Integer min, Integer max) {
|
||||||
|
this.min = min;
|
||||||
|
this.max = max;
|
||||||
|
}
|
||||||
|
|
||||||
|
Integer getMin() {
|
||||||
|
return min;
|
||||||
|
}
|
||||||
|
|
||||||
|
Integer getMax() {
|
||||||
|
return max;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object o) {
|
||||||
|
if (this == o)
|
||||||
|
return true;
|
||||||
|
if (o == null || getClass() != o.getClass())
|
||||||
|
return false;
|
||||||
|
Range range = (Range) o;
|
||||||
|
return Objects.equals(getMin(), range.getMin()) && Objects.equals(getMax(), range.getMax());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return Objects.hash(getMin(), getMax());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Range{" + "min=" + min + ", max=" + max + '}';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -124,11 +124,44 @@ public class Java8SortUnitTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public final void givenStreamCustomOrdering_whenSortingEntitiesByName_thenCorrectlySorted() {
|
public final void givenStreamCustomOrdering_whenSortingEntitiesByName_thenCorrectlySorted() {
|
||||||
|
|
||||||
final List<Human> humans = Lists.newArrayList(new Human("Sarah", 10), new Human("Jack", 12));
|
final List<Human> humans = Lists.newArrayList(new Human("Sarah", 10), new Human("Jack", 12));
|
||||||
final Comparator<Human> nameComparator = (h1, h2) -> h1.getName().compareTo(h2.getName());
|
final Comparator<Human> nameComparator = (h1, h2) -> h1.getName().compareTo(h2.getName());
|
||||||
|
|
||||||
final List<Human> sortedHumans = humans.stream().sorted(nameComparator).collect(Collectors.toList());
|
final List<Human> sortedHumans = humans.stream().sorted(nameComparator).collect(Collectors.toList());
|
||||||
Assert.assertThat(sortedHumans.get(0), equalTo(new Human("Jack", 12)));
|
Assert.assertThat(sortedHumans.get(0), equalTo(new Human("Jack", 12)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public final void givenStreamComparatorOrdering_whenSortingEntitiesByName_thenCorrectlySorted() {
|
||||||
|
final List<Human> humans = Lists.newArrayList(new Human("Sarah", 10), new Human("Jack", 12));
|
||||||
|
|
||||||
|
final List<Human> sortedHumans = humans.stream().sorted(Comparator.comparing(Human::getName)).collect(Collectors.toList());
|
||||||
|
Assert.assertThat(sortedHumans.get(0), equalTo(new Human("Jack", 12)));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public final void givenStreamNaturalOrdering_whenSortingEntitiesByNameReversed_thenCorrectlySorted() {
|
||||||
|
final List<String> letters = Lists.newArrayList("B", "A", "C");
|
||||||
|
|
||||||
|
final List<String> reverseSortedLetters = letters.stream().sorted(Comparator.reverseOrder()).collect(Collectors.toList());
|
||||||
|
Assert.assertThat(reverseSortedLetters.get(0), equalTo("C"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public final void givenStreamCustomOrdering_whenSortingEntitiesByNameReversed_thenCorrectlySorted() {
|
||||||
|
final List<Human> humans = Lists.newArrayList(new Human("Sarah", 10), new Human("Jack", 12));
|
||||||
|
final Comparator<Human> reverseNameComparator = (h1, h2) -> h2.getName().compareTo(h1.getName());
|
||||||
|
|
||||||
|
final List<Human> reverseSortedHumans = humans.stream().sorted(reverseNameComparator).collect(Collectors.toList());
|
||||||
|
Assert.assertThat(reverseSortedHumans.get(0), equalTo(new Human("Sarah", 10)));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public final void givenStreamComparatorOrdering_whenSortingEntitiesByNameReversed_thenCorrectlySorted() {
|
||||||
|
final List<Human> humans = Lists.newArrayList(new Human("Sarah", 10), new Human("Jack", 12));
|
||||||
|
|
||||||
|
final List<Human> reverseSortedHumans = humans.stream().sorted(Comparator.comparing(Human::getName, Comparator.reverseOrder())).collect(Collectors.toList());
|
||||||
|
Assert.assertThat(reverseSortedHumans.get(0), equalTo(new Human("Sarah", 10)));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,3 +27,6 @@
|
|||||||
- [Immutable Set in Java](https://www.baeldung.com/java-immutable-set)
|
- [Immutable Set in Java](https://www.baeldung.com/java-immutable-set)
|
||||||
- [Multi-Release Jar Files](https://www.baeldung.com/java-multi-release-jar)
|
- [Multi-Release Jar Files](https://www.baeldung.com/java-multi-release-jar)
|
||||||
- [Ahead of Time Compilation (AoT)](https://www.baeldung.com/ahead-of-time-compilation)
|
- [Ahead of Time Compilation (AoT)](https://www.baeldung.com/ahead-of-time-compilation)
|
||||||
|
- [Java 9 Process API Improvements](https://www.baeldung.com/java-9-process-api)
|
||||||
|
- [Guide to java.lang.Process API](https://www.baeldung.com/java-process-api)
|
||||||
|
|
||||||
|
|||||||
@@ -33,3 +33,4 @@
|
|||||||
- [Differences Between HashMap and Hashtable](https://www.baeldung.com/hashmap-hashtable-differences)
|
- [Differences Between HashMap and Hashtable](https://www.baeldung.com/hashmap-hashtable-differences)
|
||||||
- [Java ArrayList vs Vector](https://www.baeldung.com/java-arraylist-vs-vector)
|
- [Java ArrayList vs Vector](https://www.baeldung.com/java-arraylist-vs-vector)
|
||||||
- [Defining a Char Stack in Java](https://www.baeldung.com/java-char-stack)
|
- [Defining a Char Stack in Java](https://www.baeldung.com/java-char-stack)
|
||||||
|
- [Time Comparison of Arrays.sort(Object[]) and Arrays.sort(int[])](https://www.baeldung.com/arrays-sortobject-vs-sortint)
|
||||||
|
|||||||
@@ -16,3 +16,4 @@
|
|||||||
- [Life Cycle of a Thread in Java](http://www.baeldung.com/java-thread-lifecycle)
|
- [Life Cycle of a Thread in Java](http://www.baeldung.com/java-thread-lifecycle)
|
||||||
- [Runnable vs. Callable in Java](http://www.baeldung.com/java-runnable-callable)
|
- [Runnable vs. Callable in Java](http://www.baeldung.com/java-runnable-callable)
|
||||||
- [What is Thread-Safety and How to Achieve it](https://www.baeldung.com/java-thread-safety)
|
- [What is Thread-Safety and How to Achieve it](https://www.baeldung.com/java-thread-safety)
|
||||||
|
- [How to Start a Thread in Java](https://www.baeldung.com/java-start-thread)
|
||||||
|
|||||||
@@ -0,0 +1,6 @@
|
|||||||
|
=========
|
||||||
|
|
||||||
|
## Core Java JVM Cookbooks and Examples
|
||||||
|
|
||||||
|
### Relevant Articles:
|
||||||
|
- [Method Inlining in the JVM](http://www.baeldung.com/method-inlining-in-the-jvm/)
|
||||||
@@ -0,0 +1,40 @@
|
|||||||
|
<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</groupId>
|
||||||
|
<artifactId>core-java-jvm</artifactId>
|
||||||
|
<version>0.1.0-SNAPSHOT</version>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
<name>core-java-jvm</name>
|
||||||
|
|
||||||
|
<parent>
|
||||||
|
<groupId>com.baeldung</groupId>
|
||||||
|
<artifactId>parent-modules</artifactId>
|
||||||
|
<version>1.0.0-SNAPSHOT</version>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>junit</groupId>
|
||||||
|
<artifactId>junit</artifactId>
|
||||||
|
<version>${junit.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.commons</groupId>
|
||||||
|
<artifactId>commons-lang3</artifactId>
|
||||||
|
<version>${commons-lang3.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.assertj</groupId>
|
||||||
|
<artifactId>assertj-core</artifactId>
|
||||||
|
<version>${assertj.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<commons-lang3.version>3.5</commons-lang3.version>
|
||||||
|
<assertj.version>3.6.1</assertj.version>
|
||||||
|
</properties>
|
||||||
|
</project>
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
package com.baeldung.socket.read;
|
||||||
|
|
||||||
|
import java.net.*;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.io.*;
|
||||||
|
|
||||||
|
public class Client {
|
||||||
|
|
||||||
|
public void runClient(String ip, int port) {
|
||||||
|
try {
|
||||||
|
Socket socket = new Socket(ip, port);
|
||||||
|
System.out.println("Connected to server ...");
|
||||||
|
DataInputStream in = new DataInputStream(System.in);
|
||||||
|
DataOutputStream out = new DataOutputStream(socket.getOutputStream());
|
||||||
|
|
||||||
|
char type = 's'; // s for string
|
||||||
|
int length = 29;
|
||||||
|
String data = "This is a string of length 29";
|
||||||
|
byte[] dataInBytes = data.getBytes(StandardCharsets.UTF_8);
|
||||||
|
//Sending data in TLV format
|
||||||
|
out.writeChar(type);
|
||||||
|
out.writeInt(length);
|
||||||
|
out.write(dataInBytes);
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,49 @@
|
|||||||
|
package com.baeldung.socket.read;
|
||||||
|
|
||||||
|
import java.net.*;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.io.*;
|
||||||
|
|
||||||
|
public class Server {
|
||||||
|
|
||||||
|
public void runServer(int port) {
|
||||||
|
//Start the server and wait for connection
|
||||||
|
try {
|
||||||
|
ServerSocket server = new ServerSocket(port);
|
||||||
|
System.out.println("Server Started. Waiting for connection ...");
|
||||||
|
Socket socket = server.accept();
|
||||||
|
System.out.println("Got connection from client.");
|
||||||
|
//Get input stream from socket variable and convert the same to DataInputStream
|
||||||
|
DataInputStream in = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
|
||||||
|
//Read type and length of data
|
||||||
|
char dataType = in.readChar();
|
||||||
|
int length = in.readInt();
|
||||||
|
System.out.println("Type : "+dataType);
|
||||||
|
System.out.println("Lenght :"+length);
|
||||||
|
if(dataType == 's') {
|
||||||
|
//Read String data in bytes
|
||||||
|
byte[] messageByte = new byte[length];
|
||||||
|
boolean end = false;
|
||||||
|
StringBuilder dataString = new StringBuilder(length);
|
||||||
|
int totalBytesRead = 0;
|
||||||
|
//We need to run while loop, to read all data in that stream
|
||||||
|
while(!end) {
|
||||||
|
int currentBytesRead = in.read(messageByte);
|
||||||
|
totalBytesRead = currentBytesRead + totalBytesRead;
|
||||||
|
if(totalBytesRead <= length) {
|
||||||
|
dataString.append(new String(messageByte,0,currentBytesRead,StandardCharsets.UTF_8));
|
||||||
|
} else {
|
||||||
|
dataString.append(new String(messageByte,0,length - totalBytesRead + currentBytesRead,StandardCharsets.UTF_8));
|
||||||
|
}
|
||||||
|
if(dataString.length()>=length) {
|
||||||
|
end = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
System.out.println("Read "+length+" bytes of message from client. Message = "+dataString);
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
+37
@@ -0,0 +1,37 @@
|
|||||||
|
package com.baeldung.socket.read;
|
||||||
|
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
public class SocketReadAllDataLiveTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenServerAndClient_whenClientSendsAndServerReceivesData_thenCorrect() {
|
||||||
|
//Run server in new thread
|
||||||
|
Runnable runnable1 = () -> { runServer(); };
|
||||||
|
Thread thread1 = new Thread(runnable1);
|
||||||
|
thread1.start();
|
||||||
|
//Wait for 10 seconds
|
||||||
|
try {
|
||||||
|
TimeUnit.SECONDS.sleep(10);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
//Run client in a new thread
|
||||||
|
Runnable runnable2 = () -> { runClient(); };
|
||||||
|
Thread thread2 = new Thread(runnable2);
|
||||||
|
thread2.start();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void runServer() {
|
||||||
|
//Run Server
|
||||||
|
Server server = new Server();
|
||||||
|
server.runServer(5555);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void runClient() {
|
||||||
|
//Run Client
|
||||||
|
Client client = new Client();
|
||||||
|
client.runClient("127.0.0.1", 5555);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -6,3 +6,5 @@ This module uses Java 9, so make sure to have the JDK 9 installed to run it.
|
|||||||
### Relevant Articles:
|
### Relevant Articles:
|
||||||
- [Java 9 Process API Improvements](http://www.baeldung.com/java-9-process-api)
|
- [Java 9 Process API Improvements](http://www.baeldung.com/java-9-process-api)
|
||||||
- [Guide to java.lang.Process API](https://www.baeldung.com/java-process-api)
|
- [Guide to java.lang.Process API](https://www.baeldung.com/java-process-api)
|
||||||
|
- [Guide to java.lang.ProcessBuilder API](https://www.baeldung.com/java-lang-processbuilder-api)
|
||||||
|
|
||||||
|
|||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user