diff --git a/core-java-9/src/test/java/com/baeldung/java9/language/stream/CollectorImprovementTest.java b/core-java-9/src/test/java/com/baeldung/java9/language/stream/CollectorImprovementTest.java index 71ee6c6ae4..2496d073ec 100644 --- a/core-java-9/src/test/java/com/baeldung/java9/language/stream/CollectorImprovementTest.java +++ b/core-java-9/src/test/java/com/baeldung/java9/language/stream/CollectorImprovementTest.java @@ -31,8 +31,8 @@ public class CollectorImprovementTest { } } - @Test - public void testFiltering() { + @Test + public void givenList_whenSatifyPredicate_thenMapValueWithOccurences() { List numbers = List.of(1, 2, 3, 5, 5); Map result = numbers.stream() @@ -49,7 +49,7 @@ public class CollectorImprovementTest { } @Test - public void testFlatMapping() { + public void givenListOfBlogs_whenAuthorName_thenMapAuthorWithComments() { Blog blog1 = new CollectorImprovementTest.Blog("1"); blog1.addComment("Nice"); blog1.addComment("Very Nice"); diff --git a/core-java/src/main/java/com/baeldung/concurrent/blockingqueue/BlockingQueueUsage.java b/core-java/src/main/java/com/baeldung/concurrent/blockingqueue/BlockingQueueUsage.java new file mode 100644 index 0000000000..801dc4e35c --- /dev/null +++ b/core-java/src/main/java/com/baeldung/concurrent/blockingqueue/BlockingQueueUsage.java @@ -0,0 +1,22 @@ +package com.baeldung.concurrent.blockingqueue; + +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.LinkedBlockingQueue; + +public class BlockingQueueUsage { + public static void main(String[] args) { + int BOUND = 10; + int N_PRODUCERS = 4; + int N_CONSUMERS = Runtime.getRuntime().availableProcessors(); + + BlockingQueue queue = new LinkedBlockingQueue<>(BOUND); + + for (int i = 0; i < N_PRODUCERS; i++) { + new Thread(new NumbersProducer(queue)).start(); + } + + for (int j = 0; j < N_CONSUMERS; j++) { + new Thread(new NumbersConsumer(queue)).start(); + } + } +} \ No newline at end of file diff --git a/core-java/src/main/java/com/baeldung/concurrent/blockingqueue/NumbersConsumer.java b/core-java/src/main/java/com/baeldung/concurrent/blockingqueue/NumbersConsumer.java new file mode 100644 index 0000000000..774c263a3b --- /dev/null +++ b/core-java/src/main/java/com/baeldung/concurrent/blockingqueue/NumbersConsumer.java @@ -0,0 +1,23 @@ +package com.baeldung.concurrent.blockingqueue; + +import java.util.concurrent.BlockingQueue; + +public class NumbersConsumer implements Runnable { + private final BlockingQueue queue; + + public NumbersConsumer(BlockingQueue queue) { + this.queue = queue; + } + + public void run() { + try { + while (true) { + Integer number = queue.take(); + String result = number.toString(); + System.out.println(Thread.currentThread().getName() + " result: " + result); + } + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + } +} \ No newline at end of file diff --git a/core-java/src/main/java/com/baeldung/concurrent/blockingqueue/NumbersProducer.java b/core-java/src/main/java/com/baeldung/concurrent/blockingqueue/NumbersProducer.java new file mode 100644 index 0000000000..eb927c8b37 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/concurrent/blockingqueue/NumbersProducer.java @@ -0,0 +1,27 @@ +package com.baeldung.concurrent.blockingqueue; + +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.ThreadLocalRandom; + +public class NumbersProducer implements Runnable { + + private final BlockingQueue numbersQueue; + + public NumbersProducer(BlockingQueue numbersQueue) { + this.numbersQueue = numbersQueue; + } + + public void run() { + try { + generateNumbers(); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + } + + private void generateNumbers() throws InterruptedException { + for (int i = 0; i < 100; i++) { + numbersQueue.put(ThreadLocalRandom.current().nextInt(100)); + } + } +} \ No newline at end of file diff --git a/core-java/src/test/java/com/baeldung/java/concurrentmap/ConcurrentMapAggregateStatusTest.java b/core-java/src/test/java/com/baeldung/java/concurrentmap/ConcurrentMapAggregateStatusTest.java new file mode 100644 index 0000000000..cb43cad24b --- /dev/null +++ b/core-java/src/test/java/com/baeldung/java/concurrentmap/ConcurrentMapAggregateStatusTest.java @@ -0,0 +1,80 @@ +package com.baeldung.java.concurrentmap; + +import org.junit.Before; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; + +public class ConcurrentMapAggregateStatusTest { + + private ExecutorService executorService; + private Map concurrentMap; + private List mapSizes; + private int MAX_SIZE = 100000; + + @Before + public void init() { + executorService = Executors.newFixedThreadPool(2); + concurrentMap = new ConcurrentHashMap<>(); + mapSizes = new ArrayList<>(MAX_SIZE); + } + + @Test + public void givenConcurrentMap_whenSizeWithoutConcurrentUpdates_thenCorrect() throws InterruptedException { + Runnable collectMapSizes = () -> { + for (int i = 0; i < MAX_SIZE; i++) { + concurrentMap.put(String.valueOf(i), i); + mapSizes.add(concurrentMap.size()); + } + }; + Runnable retrieveMapData = () -> { + for (int i = 0; i < MAX_SIZE; i++) { + concurrentMap.get(String.valueOf(i)); + } + }; + executorService.execute(retrieveMapData); + executorService.execute(collectMapSizes); + executorService.shutdown(); + executorService.awaitTermination(1, TimeUnit.MINUTES); + + for (int i = 1; i <= MAX_SIZE; i++) { + assertEquals("map size should be consistently reliable", i, mapSizes + .get(i - 1) + .intValue()); + } + assertEquals(MAX_SIZE, concurrentMap.size()); + } + + @Test + public void givenConcurrentMap_whenUpdatingAndGetSize_thenError() throws InterruptedException { + Runnable collectMapSizes = () -> { + for (int i = 0; i < MAX_SIZE; i++) { + mapSizes.add(concurrentMap.size()); + } + }; + Runnable updateMapData = () -> { + for (int i = 0; i < MAX_SIZE; i++) { + concurrentMap.put(String.valueOf(i), i); + } + }; + executorService.execute(updateMapData); + executorService.execute(collectMapSizes); + executorService.shutdown(); + executorService.awaitTermination(1, TimeUnit.MINUTES); + + assertNotEquals("map size collected with concurrent updates not reliable", MAX_SIZE, mapSizes + .get(MAX_SIZE - 1) + .intValue()); + assertEquals(MAX_SIZE, concurrentMap.size()); + } + +} diff --git a/core-java/src/test/java/com/baeldung/java/concurrentmap/ConcurrentMapNullKeyValueTest.java b/core-java/src/test/java/com/baeldung/java/concurrentmap/ConcurrentMapNullKeyValueTest.java new file mode 100644 index 0000000000..62a3d10add --- /dev/null +++ b/core-java/src/test/java/com/baeldung/java/concurrentmap/ConcurrentMapNullKeyValueTest.java @@ -0,0 +1,160 @@ +package com.baeldung.java.concurrentmap; + +import org.junit.Before; +import org.junit.Test; + +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; + +import static org.junit.Assert.assertNull; + +public class ConcurrentMapNullKeyValueTest { + + ConcurrentMap concurrentMap; + + @Before + public void setup() { + concurrentMap = new ConcurrentHashMap<>(); + } + + @Test(expected = NullPointerException.class) + public void givenConcurrentHashMap_whenGetWithNullKey_thenThrowsNPE() { + concurrentMap.get(null); + } + + @Test(expected = NullPointerException.class) + public void givenConcurrentHashMap_whenGetOrDefaultWithNullKey_thenThrowsNPE() { + concurrentMap.getOrDefault(null, new Object()); + } + + @Test(expected = NullPointerException.class) + public void givenConcurrentHashMap_whenPutWithNullKey_thenThrowsNPE() { + concurrentMap.put(null, new Object()); + } + + @Test(expected = NullPointerException.class) + public void givenConcurrentHashMap_whenPutNullValue_thenThrowsNPE() { + concurrentMap.put("test", null); + } + + @Test(expected = NullPointerException.class) + public void givenConcurrentHashMapAndKeyAbsent_whenPutWithNullKey_thenThrowsNPE() { + concurrentMap.putIfAbsent(null, new Object()); + } + + @Test(expected = NullPointerException.class) + public void givenConcurrentHashMapAndMapWithNullKey_whenPutNullKeyMap_thenThrowsNPE() { + Map nullKeyMap = new HashMap<>(); + nullKeyMap.put(null, new Object()); + concurrentMap.putAll(nullKeyMap); + } + + @Test(expected = NullPointerException.class) + public void givenConcurrentHashMapAndMapWithNullValue_whenPutNullValueMap_thenThrowsNPE() { + Map nullValueMap = new HashMap<>(); + nullValueMap.put("test", null); + concurrentMap.putAll(nullValueMap); + } + + @Test(expected = NullPointerException.class) + public void givenConcurrentHashMap_whenReplaceNullKeyWithValues_thenThrowsNPE() { + concurrentMap.replace(null, new Object(), new Object()); + } + + @Test(expected = NullPointerException.class) + public void givenConcurrentHashMap_whenReplaceWithNullNewValue_thenThrowsNPE() { + Object o = new Object(); + concurrentMap.replace("test", o, null); + } + + @Test(expected = NullPointerException.class) + public void givenConcurrentHashMap_whenReplaceOldNullValue_thenThrowsNPE() { + Object o = new Object(); + concurrentMap.replace("test", null, o); + } + + @Test(expected = NullPointerException.class) + public void givenConcurrentHashMap_whenReplaceWithNullValue_thenThrowsNPE() { + concurrentMap.replace("test", null); + } + + @Test(expected = NullPointerException.class) + public void givenConcurrentHashMap_whenReplaceNullKey_thenThrowsNPE() { + concurrentMap.replace(null, "test"); + } + + @Test(expected = NullPointerException.class) + public void givenConcurrentHashMap_whenReplaceAllMappingNull_thenThrowsNPE() { + concurrentMap.put("test", new Object()); + concurrentMap.replaceAll((s, o) -> null); + } + + @Test(expected = NullPointerException.class) + public void givenConcurrentHashMap_whenRemoveNullKey_thenThrowsNPE() { + concurrentMap.remove(null); + } + + @Test(expected = NullPointerException.class) + public void givenConcurrentHashMap_whenRemoveNullKeyWithValue_thenThrowsNPE() { + concurrentMap.remove(null, new Object()); + } + + @Test(expected = NullPointerException.class) + public void givenConcurrentHashMap_whenMergeNullKeyWithValue_thenThrowsNPE() { + concurrentMap.merge(null, new Object(), (o, o2) -> o2); + } + + @Test(expected = NullPointerException.class) + public void givenConcurrentHashMap_whenMergeKeyWithNullValue_thenThrowsNPE() { + concurrentMap.put("test", new Object()); + concurrentMap.merge("test", null, (o, o2) -> o2); + } + + @Test(expected = NullPointerException.class) + public void givenConcurrentHashMapAndAssumeKeyAbsent_whenComputeWithNullKey_thenThrowsNPE() { + concurrentMap.computeIfAbsent(null, s -> s); + } + + @Test(expected = NullPointerException.class) + public void givenConcurrentHashMapAndAssumeKeyPresent_whenComputeWithNullKey_thenThrowsNPE() { + concurrentMap.computeIfPresent(null, (s, o) -> o); + } + + @Test(expected = NullPointerException.class) + public void givenConcurrentHashMap_whenComputeWithNullKey_thenThrowsNPE() { + concurrentMap.compute(null, (s, o) -> o); + } + + @Test + public void givenConcurrentHashMap_whenMergeKeyRemappingNull_thenRemovesMapping() { + Object oldValue = new Object(); + concurrentMap.put("test", oldValue); + concurrentMap.merge("test", new Object(), (o, o2) -> null); + assertNull(concurrentMap.get("test")); + } + + @Test + public void givenConcurrentHashMapAndKeyAbsent_whenComputeWithKeyRemappingNull_thenRemainsAbsent() { + concurrentMap.computeIfPresent("test", (s, o) -> null); + assertNull(concurrentMap.get("test")); + } + + @Test + public void givenKeyPresent_whenComputeIfPresentRemappingNull_thenMappingRemoved() { + Object oldValue = new Object(); + concurrentMap.put("test", oldValue); + concurrentMap.computeIfPresent("test", (s, o) -> null); + assertNull(concurrentMap.get("test")); + } + + @Test + public void givenKeyPresent_whenComputeRemappingNull_thenMappingRemoved() { + Object oldValue = new Object(); + concurrentMap.put("test", oldValue); + concurrentMap.compute("test", (s, o) -> null); + assertNull(concurrentMap.get("test")); + } + +} diff --git a/core-java/src/test/java/com/baeldung/java/concurrentmap/ConcurrentMapPerformanceTest.java b/core-java/src/test/java/com/baeldung/java/concurrentmap/ConcurrentMapPerformanceTest.java new file mode 100644 index 0000000000..3275a47b90 --- /dev/null +++ b/core-java/src/test/java/com/baeldung/java/concurrentmap/ConcurrentMapPerformanceTest.java @@ -0,0 +1,98 @@ +package com.baeldung.java.concurrentmap; + +import org.junit.Test; + +import java.util.Collections; +import java.util.HashMap; +import java.util.Hashtable; +import java.util.Map; +import java.util.concurrent.*; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertTrue; + +public class ConcurrentMapPerformanceTest { + + @Test + public void givenMaps_whenGetPut500KTimes_thenConcurrentMapFaster() throws Exception { + Map hashtable = new Hashtable<>(); + Map synchronizedHashMap = Collections.synchronizedMap(new HashMap<>()); + Map concurrentHashMap = new ConcurrentHashMap<>(); + + long hashtableAvgRuntime = timeElapseForGetPut(hashtable); + long syncHashMapAvgRuntime = timeElapseForGetPut(synchronizedHashMap); + long concurrentHashMapAvgRuntime = timeElapseForGetPut(concurrentHashMap); + + assertTrue(hashtableAvgRuntime > concurrentHashMapAvgRuntime); + assertTrue(syncHashMapAvgRuntime > concurrentHashMapAvgRuntime); + + System.out.println(String.format("Hashtable: %s, syncHashMap: %s, ConcurrentHashMap: %s", hashtableAvgRuntime, syncHashMapAvgRuntime, concurrentHashMapAvgRuntime)); + + } + + private long timeElapseForGetPut(Map map) throws InterruptedException { + ExecutorService executorService = Executors.newFixedThreadPool(4); + long startTime = System.nanoTime(); + for (int i = 0; i < 4; i++) { + executorService.execute(() -> { + for (int j = 0; j < 500_000; j++) { + int value = ThreadLocalRandom + .current() + .nextInt(10000); + String key = String.valueOf(value); + map.put(key, value); + map.get(key); + } + }); + } + executorService.shutdown(); + executorService.awaitTermination(1, TimeUnit.MINUTES); + return (System.nanoTime() - startTime) / 500_000; + } + + @Test + public void givenConcurrentMap_whenKeyWithSameHashCode_thenPerformanceDegrades() throws InterruptedException { + class SameHash { + @Override + public int hashCode() { + return 1; + } + } + int executeTimes = 5000; + + Map mapOfSameHash = new ConcurrentHashMap<>(); + ExecutorService executorService = Executors.newFixedThreadPool(2); + long sameHashStartTime = System.currentTimeMillis(); + for (int i = 0; i < 2; i++) { + executorService.execute(() -> { + for (int j = 0; j < executeTimes; j++) { + mapOfSameHash.put(new SameHash(), 1); + } + }); + } + executorService.shutdown(); + executorService.awaitTermination(5, TimeUnit.SECONDS); + + long mapOfSameHashDuration = System.currentTimeMillis() - sameHashStartTime; + Map mapOfDefaultHash = new ConcurrentHashMap<>(); + executorService = Executors.newFixedThreadPool(2); + long defaultHashStartTime = System.currentTimeMillis(); + for (int i = 0; i < 2; i++) { + executorService.execute(() -> { + for (int j = 0; j < executeTimes; j++) { + mapOfDefaultHash.put(new Object(), 1); + } + }); + } + executorService.shutdown(); + executorService.awaitTermination(5, TimeUnit.SECONDS); + + long mapOfDefaultHashDuration = System.currentTimeMillis() - defaultHashStartTime; + assertEquals(executeTimes * 2, mapOfDefaultHash.size()); + assertNotEquals(executeTimes * 2, mapOfSameHash.size()); + System.out.println(String.format("same-hash: %s, default-hash: %s", mapOfSameHashDuration, mapOfDefaultHashDuration)); + assertTrue("same hashCode() should greatly degrade performance", mapOfSameHashDuration > mapOfDefaultHashDuration * 10); + } + +} diff --git a/core-java/src/test/java/com/baeldung/java/concurrentmap/ConcurretMapMemoryConsistencyTest.java b/core-java/src/test/java/com/baeldung/java/concurrentmap/ConcurretMapMemoryConsistencyTest.java new file mode 100644 index 0000000000..9a6359f187 --- /dev/null +++ b/core-java/src/test/java/com/baeldung/java/concurrentmap/ConcurretMapMemoryConsistencyTest.java @@ -0,0 +1,78 @@ +package com.baeldung.java.concurrentmap; + +import org.junit.Test; + +import java.util.*; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; + +import static org.junit.Assert.*; + +public class ConcurretMapMemoryConsistencyTest { + + @Test + public void givenConcurrentMap_whenSumParallel_thenCorrect() throws Exception { + Map map = new ConcurrentHashMap<>(); + List sumList = parallelSum100(map, 1000); + assertEquals(1, sumList + .stream() + .distinct() + .count()); + long wrongResultCount = sumList + .stream() + .filter(num -> num != 100) + .count(); + assertEquals(0, wrongResultCount); + } + + @Test + public void givenHashtable_whenSumParallel_thenCorrect() throws Exception { + Map map = new Hashtable<>(); + List sumList = parallelSum100(map, 1000); + assertEquals(1, sumList + .stream() + .distinct() + .count()); + long wrongResultCount = sumList + .stream() + .filter(num -> num != 100) + .count(); + assertEquals(0, wrongResultCount); + } + + @Test + public void givenHashMap_whenSumParallel_thenError() throws Exception { + Map map = new HashMap<>(); + List sumList = parallelSum100(map, 100); + assertNotEquals(1, sumList + .stream() + .distinct() + .count()); + long wrongResultCount = sumList + .stream() + .filter(num -> num != 100) + .count(); + assertTrue(wrongResultCount > 0); + } + + private List parallelSum100(Map map, int executionTimes) throws InterruptedException { + List sumList = new ArrayList<>(1000); + for (int i = 0; i < executionTimes; i++) { + map.put("test", 0); + ExecutorService executorService = Executors.newFixedThreadPool(4); + for (int j = 0; j < 10; j++) { + executorService.execute(() -> { + for (int k = 0; k < 10; k++) + map.computeIfPresent("test", (key, value) -> value + 1); + }); + } + executorService.shutdown(); + executorService.awaitTermination(5, TimeUnit.SECONDS); + sumList.add(map.get("test")); + } + return sumList; + } + +} diff --git a/kotlin/pom.xml b/kotlin/pom.xml index eba248e1b9..68944f08ac 100644 --- a/kotlin/pom.xml +++ b/kotlin/pom.xml @@ -38,6 +38,12 @@ ${kotlin-maven-plugin.version} + + + src/main/java + src/main/kotlin + + compile compile @@ -45,6 +51,12 @@ + + + src/test/java + src/test/kotlin + + test-compile test-compile diff --git a/kotlin/src/main/java/com/baeldung/java/StringUtils.java b/kotlin/src/main/java/com/baeldung/java/StringUtils.java new file mode 100644 index 0000000000..f405924cdf --- /dev/null +++ b/kotlin/src/main/java/com/baeldung/java/StringUtils.java @@ -0,0 +1,7 @@ +package com.baeldung.java; + +public class StringUtils { + public static String toUpperCase(String name) { + return name.toUpperCase(); + } +} diff --git a/kotlin/src/main/kotlin/com/baeldung/kotlin/ItemService.kt b/kotlin/src/main/kotlin/com/baeldung/kotlin/ItemService.kt index 3c0d2eacb1..88de1aa9be 100644 --- a/kotlin/src/main/kotlin/com/baeldung/kotlin/ItemService.kt +++ b/kotlin/src/main/kotlin/com/baeldung/kotlin/ItemService.kt @@ -24,29 +24,41 @@ class ItemManager(val categoryId: String, val dbConnection: String) { fun makeAnalyisOfCategory(catId: String): Unit { val result = if (catId == "100") "Yes" else "No" println(result) - + `object`() } + + fun sum(a: Int, b: Int): Int { + return a + b + } + + fun `object`(): String { + return "this is object" + } + } fun main(args: Array) { val numbers = arrayOf("first", "second", "third", "fourth") - var concat = "" for (n in numbers) { - concat += n + println(n) } - var sum = 0 - for (i in 2..9) { - sum += i + for (i in 2..9 step 2) { + println(i) } + val res = 1.rangeTo(10).map { it * 2 } + println(res) + val firstName = "Tom" val secondName = "Mary" val concatOfNames = "$firstName + $secondName" println("Names: $concatOfNames") + val sum = "four: ${2 + 2}" val itemManager = ItemManager("cat_id", "db://connection") + ItemManager(categoryId = "catId", dbConnection = "db://Connection") val result = "function result: ${itemManager.isFromSpecificCategory("1")}" println(result) @@ -63,4 +75,9 @@ fun main(args: Array) { "Alice" -> println("Hi lady") } + val items = listOf(1, 2, 3, 4) + + + val rwList = mutableListOf(1, 2, 3) + rwList.add(5) } \ No newline at end of file diff --git a/kotlin/src/main/kotlin/com/baeldung/kotlin/MathematicsOperations.kt b/kotlin/src/main/kotlin/com/baeldung/kotlin/MathematicsOperations.kt new file mode 100644 index 0000000000..924f9d2323 --- /dev/null +++ b/kotlin/src/main/kotlin/com/baeldung/kotlin/MathematicsOperations.kt @@ -0,0 +1,7 @@ +package com.baeldung.kotlin + +class MathematicsOperations { + fun addTwoNumbers(a: Int, b: Int): Int { + return a + b + } +} \ No newline at end of file diff --git a/kotlin/src/test/java/com/baeldung/kotlin/JavaCallToKotlinTest.java b/kotlin/src/test/java/com/baeldung/kotlin/JavaCallToKotlinTest.java new file mode 100644 index 0000000000..cfad7cefd4 --- /dev/null +++ b/kotlin/src/test/java/com/baeldung/kotlin/JavaCallToKotlinTest.java @@ -0,0 +1,17 @@ +package com.baeldung.kotlin; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class JavaCallToKotlinTest { + @Test + public void givenKotlinClass_whenCallFromJava_shouldProduceResults() { + //when + int res = new MathematicsOperations().addTwoNumbers(2, 4); + + //then + assertEquals(6, res); + + } +} diff --git a/kotlin/src/test/kotlin/com/baeldung/kotlin/KotlinScalaInteroperabilityTest.kt b/kotlin/src/test/kotlin/com/baeldung/kotlin/KotlinScalaInteroperabilityTest.kt new file mode 100644 index 0000000000..f671c3af8b --- /dev/null +++ b/kotlin/src/test/kotlin/com/baeldung/kotlin/KotlinScalaInteroperabilityTest.kt @@ -0,0 +1,20 @@ +package com.baeldung.kotlin + +import com.baeldung.java.StringUtils +import org.junit.Test +import kotlin.test.assertEquals + + +class KotlinScalaInteroperabilityTest { + @Test + fun givenLowercaseString_whenExecuteMethodFromJavaStringUtils_shouldReturnStringUppercase() { + //given + val name = "tom" + + //when + val res = StringUtils.toUpperCase(name) + + //then + assertEquals(res, "TOM") + } +} diff --git a/kotlin/src/test/kotlin/com/baeldung/kotlin/LambdaTest.kt b/kotlin/src/test/kotlin/com/baeldung/kotlin/LambdaTest.kt new file mode 100644 index 0000000000..34217336a0 --- /dev/null +++ b/kotlin/src/test/kotlin/com/baeldung/kotlin/LambdaTest.kt @@ -0,0 +1,19 @@ +package com.baeldung.kotlin + +import org.junit.Test +import kotlin.test.assertEquals + + +class LambdaTest { + @Test + fun givenListOfNumber_whenDoingOperationsUsingLambda_shouldReturnProperResult() { + //given + val listOfNumbers = listOf(1, 2, 3) + + //when + val sum = listOfNumbers.reduce { a, b -> a + b } + + //then + assertEquals(6, sum) + } +} \ No newline at end of file diff --git a/spring-cloud/spring-cloud-rest/application-config/resource.properties b/spring-cloud/spring-cloud-rest/application-config/resource.properties index 8add49ab7e..759bb87895 100644 --- a/spring-cloud/spring-cloud-rest/application-config/resource.properties +++ b/spring-cloud/spring-cloud-rest/application-config/resource.properties @@ -1,5 +1,5 @@ spring.application.name=resource -server.port=0 +#server.port=0 #### cloud eureka.client.serviceUrl.defaultZone=${EUREKA_URI:http://system:systemPass@localhost:8761/eureka} diff --git a/spring-cloud/spring-cloud-rest/spring-cloud-rest-client-1/src/main/resources/bootstrap.properties b/spring-cloud/spring-cloud-rest/spring-cloud-rest-client-1/src/main/resources/bootstrap.properties index f34f1b65ac..2cb3b71ca7 100644 --- a/spring-cloud/spring-cloud-rest/spring-cloud-rest-client-1/src/main/resources/bootstrap.properties +++ b/spring-cloud/spring-cloud-rest/spring-cloud-rest-client-1/src/main/resources/bootstrap.properties @@ -1,3 +1,9 @@ spring.cloud.config.name=resource spring.cloud.config.discovery.service-id=config -spring.cloud.config.discovery.enabled=true \ No newline at end of file +spring.cloud.config.discovery.enabled=true +spring.cloud.config.username=configUser +spring.cloud.config.password=configPassword + +eureka.client.serviceUrl.defaultZone=http://system:systemPass@localhost:8761/eureka + +server.port=8084 \ No newline at end of file diff --git a/spring-cloud/spring-cloud-rest/spring-cloud-rest-client-2/src/main/resources/application.properties b/spring-cloud/spring-cloud-rest/spring-cloud-rest-client-2/src/main/resources/application.properties deleted file mode 100644 index f443e86fd8..0000000000 --- a/spring-cloud/spring-cloud-rest/spring-cloud-rest-client-2/src/main/resources/application.properties +++ /dev/null @@ -1,19 +0,0 @@ -#### cloud -spring.application.name=spring-cloud-eureka-client -server.port=0 -eureka.client.serviceUrl.defaultZone=http://system:systemPass@localhost:8761/eureka -eureka.instance.preferIpAddress=true - -#### persistence -spring.datasource.driver-class-name=org.h2.Driver -spring.datasource.url=jdbc:h2:mem:cloud_rest;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE -spring.datasource.username=sa -spring.datasource.password= - -#### security -security.basic.enabled=true -security.basic.path=/** -security.user.name=user -security.user.password=userPass -security.user.role=USER -security.sessions=always \ No newline at end of file diff --git a/spring-cloud/spring-cloud-rest/spring-cloud-rest-client-2/src/main/resources/bootstrap.properties b/spring-cloud/spring-cloud-rest/spring-cloud-rest-client-2/src/main/resources/bootstrap.properties new file mode 100644 index 0000000000..d6e642afcb --- /dev/null +++ b/spring-cloud/spring-cloud-rest/spring-cloud-rest-client-2/src/main/resources/bootstrap.properties @@ -0,0 +1,9 @@ +spring.cloud.config.name=resource +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://system:systemPass@localhost:8761/eureka + +server.port=8085 \ No newline at end of file diff --git a/spring-cloud/spring-cloud-rest/spring-cloud-rest-client-2/src/test/java/org/baeldung/RestApiLiveTest.java b/spring-cloud/spring-cloud-rest/spring-cloud-rest-client-2/src/test/java/org/baeldung/RestApiLiveTest.java index 022648ffa1..1899385e2a 100644 --- a/spring-cloud/spring-cloud-rest/spring-cloud-rest-client-2/src/test/java/org/baeldung/RestApiLiveTest.java +++ b/spring-cloud/spring-cloud-rest/spring-cloud-rest-client-2/src/test/java/org/baeldung/RestApiLiveTest.java @@ -22,7 +22,7 @@ import org.springframework.test.context.junit4.SpringRunner; @SpringBootTest(classes = { SpringCloudRestClientApplication.class }, webEnvironment = WebEnvironment.DEFINED_PORT) public class RestApiLiveTest { - private static final String API_URI = "http://localhost:8084/reviews"; + private static final String API_URI = "http://localhost:8085/reviews"; @Before public void setUp() { diff --git a/spring-cloud/spring-cloud-rest/spring-cloud-rest-client-2/src/test/resources/application.properties b/spring-cloud/spring-cloud-rest/spring-cloud-rest-client-2/src/test/resources/application.properties index ece9ca1d94..e69cb2f555 100644 --- a/spring-cloud/spring-cloud-rest/spring-cloud-rest-client-2/src/test/resources/application.properties +++ b/spring-cloud/spring-cloud-rest/spring-cloud-rest-client-2/src/test/resources/application.properties @@ -1,6 +1,6 @@ #### cloud spring.application.name=spring-cloud-eureka-client -server.port=8084 +server.port=8085 eureka.client.serviceUrl.defaultZone=${EUREKA_URI:http://system:systemPass@localhost:8761/eureka} eureka.instance.preferIpAddress=true diff --git a/spring-cloud/spring-cloud-rest/spring-cloud-rest-config/pom.xml b/spring-cloud/spring-cloud-rest/spring-cloud-rest-config/pom.xml index 20b5eaf908..a6f045899c 100644 --- a/spring-cloud/spring-cloud-rest/spring-cloud-rest-config/pom.xml +++ b/spring-cloud/spring-cloud-rest/spring-cloud-rest-config/pom.xml @@ -34,6 +34,10 @@ org.springframework.cloud spring-cloud-starter-eureka + + org.springframework.boot + spring-boot-starter-security + org.springframework.boot diff --git a/spring-cloud/spring-cloud-rest/spring-cloud-rest-config/src/main/java/org/baeldung/SpringCloudRestConfigApplication.java b/spring-cloud/spring-cloud-rest/spring-cloud-rest-config/src/main/java/org/baeldung/SpringCloudRestConfigApplication.java index 20e1589d5e..90c6fe3ec9 100644 --- a/spring-cloud/spring-cloud-rest/spring-cloud-rest-config/src/main/java/org/baeldung/SpringCloudRestConfigApplication.java +++ b/spring-cloud/spring-cloud-rest/spring-cloud-rest-config/src/main/java/org/baeldung/SpringCloudRestConfigApplication.java @@ -3,10 +3,11 @@ package org.baeldung; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.config.server.EnableConfigServer; +import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @SpringBootApplication @EnableConfigServer -// @EnableEurekaClient +@EnableEurekaClient public class SpringCloudRestConfigApplication { public static void main(String[] args) { diff --git a/spring-cloud/spring-cloud-rest/spring-cloud-rest-config/src/main/resources/application.properties b/spring-cloud/spring-cloud-rest/spring-cloud-rest-config/src/main/resources/application.properties index 42fed8d0ba..4071dc81ea 100644 --- a/spring-cloud/spring-cloud-rest/spring-cloud-rest-config/src/main/resources/application.properties +++ b/spring-cloud/spring-cloud-rest/spring-cloud-rest-config/src/main/resources/application.properties @@ -1,8 +1,11 @@ server.port=8081 spring.application.name=config -spring.cloud.config.server.git.uri=https://github.com/eugenp/tutorials/tree/master/spring-cloud/spring-cloud-rest/application-config +spring.cloud.config.server.git.uri=${HOME}/application-config -#eureka.client.region = default -#eureka.client.registryFetchIntervalSeconds = 5 -#eureka.client.serviceUrl.defaultZone=${EUREKA_URI:http://system:systemPass@localhost:8761/eureka} +eureka.client.region = default +eureka.client.registryFetchIntervalSeconds = 5 +eureka.client.serviceUrl.defaultZone=${EUREKA_URI:http://system:systemPass@localhost:8761/eureka} + +security.user.name=configUser +security.user.password=configPassword \ No newline at end of file diff --git a/spring-cloud/spring-cloud-rest/spring-cloud-rest-server/src/main/resources/bootstrap.properties b/spring-cloud/spring-cloud-rest/spring-cloud-rest-server/src/main/resources/bootstrap.properties index 296078017e..528cb4cfd3 100644 --- a/spring-cloud/spring-cloud-rest/spring-cloud-rest-server/src/main/resources/bootstrap.properties +++ b/spring-cloud/spring-cloud-rest/spring-cloud-rest-server/src/main/resources/bootstrap.properties @@ -1,2 +1,4 @@ spring.cloud.config.name=spring-cloud-rest-server -spring.cloud.config.uri=http://localhost:8081 \ No newline at end of file +spring.cloud.config.uri=http://localhost:8081 +spring.cloud.config.username=configUser +spring.cloud.config.password=configPassword \ No newline at end of file