diff --git a/algorithms-miscellaneous-3/src/main/java/com/baeldung/algorithms/bucketsort/IntegerBucketSorter.java b/algorithms-miscellaneous-3/src/main/java/com/baeldung/algorithms/bucketsort/IntegerBucketSorter.java new file mode 100644 index 0000000000..4d885a6b3a --- /dev/null +++ b/algorithms-miscellaneous-3/src/main/java/com/baeldung/algorithms/bucketsort/IntegerBucketSorter.java @@ -0,0 +1,70 @@ +package com.baeldung.bucketsort; + +import java.util.ArrayList; +import java.util.Comparator; +import java.util.List; + +public class IntegerBucketSorter implements Sorter { + + private final Comparator comparator; + + public IntegerBucketSorter(Comparator comparator) { + this.comparator = comparator; + } + + public IntegerBucketSorter() { + comparator = Comparator.naturalOrder(); + } + + public List sort(List arrayToSort) { + + List> buckets = splitIntoUnsortedBuckets(arrayToSort); + + for(List bucket : buckets){ + bucket.sort(comparator); + } + + return concatenateSortedBuckets(buckets); + } + + private List concatenateSortedBuckets(List> buckets){ + List sortedArray = new ArrayList<>(); + int index = 0; + for(List bucket : buckets){ + for(int number : bucket){ + sortedArray.add(index++, number); + } + } + return sortedArray; + } + + private List> splitIntoUnsortedBuckets(List initialList){ + + final int[] codes = createHashes(initialList); + + List> buckets = new ArrayList<>(codes[1]); + for(int i = 0; i < codes[1]; i++) buckets.add(new ArrayList<>()); + + //distribute the data + for (int i : initialList) { + buckets.get(hash(i, codes)).add(i); + } + return buckets; + + } + + private int[] createHashes(List input){ + int m = input.get(0); + for (int i = 1; i < input.size(); i++) { + if (m < input.get(i)) { + m = input.get(i); + } + } + return new int[]{m, (int) Math.sqrt(input.size())}; + } + + private static int hash(int i, int[] code) { + return (int) ((double) i / code[0] * (code[1] - 1)); + } + +} diff --git a/algorithms-miscellaneous-3/src/main/java/com/baeldung/algorithms/bucketsort/Sorter.java b/algorithms-miscellaneous-3/src/main/java/com/baeldung/algorithms/bucketsort/Sorter.java new file mode 100644 index 0000000000..b86f60324f --- /dev/null +++ b/algorithms-miscellaneous-3/src/main/java/com/baeldung/algorithms/bucketsort/Sorter.java @@ -0,0 +1,8 @@ +package com.baeldung.bucketsort; + +import java.util.List; + +public interface Sorter { + + List sort(List arrayToSort); +} diff --git a/algorithms-miscellaneous-3/src/test/java/com/baeldung/algorithms/bucketsort/IntegerBucketSorterUnitTest.java b/algorithms-miscellaneous-3/src/test/java/com/baeldung/algorithms/bucketsort/IntegerBucketSorterUnitTest.java new file mode 100644 index 0000000000..2773d8a68f --- /dev/null +++ b/algorithms-miscellaneous-3/src/test/java/com/baeldung/algorithms/bucketsort/IntegerBucketSorterUnitTest.java @@ -0,0 +1,33 @@ +package com.baeldung.bucketsort; + +import com.baeldung.bucketsort.IntegerBucketSorter; +import org.junit.Before; +import org.junit.Test; + +import java.util.Arrays; +import java.util.List; + +import static org.junit.Assert.assertEquals; + +public class IntegerBucketSorterUnitTest { + + private IntegerBucketSorter sorter; + + @Before + public void setUp() throws Exception { + sorter = new IntegerBucketSorter(); + } + + @Test + public void givenUnsortedList_whenSortedUsingBucketSorter_checkSortingCorrect() { + + List unsorted = Arrays.asList(80,50,60,30,20,10,70,0,40,500,600,602,200,15); + List expected = Arrays.asList(0,10,15,20,30,40,50,60,70,80,200,500,600,602); + + List actual = sorter.sort(unsorted); + + assertEquals(expected, actual); + + + } +} \ No newline at end of file diff --git a/algorithms-sorting/src/main/java/com/baeldung/algorithms/radixsort/RadixSort.java b/algorithms-sorting/src/main/java/com/baeldung/algorithms/radixsort/RadixSort.java new file mode 100644 index 0000000000..26c5c9a486 --- /dev/null +++ b/algorithms-sorting/src/main/java/com/baeldung/algorithms/radixsort/RadixSort.java @@ -0,0 +1,53 @@ +package com.baeldung.algorithms.radixsort; + +import java.util.Arrays; + +public class RadixSort { + + public static void sort(int numbers[]) { + int maximumNumber = findMaximumNumberIn(numbers); + + int numberOfDigits = calculateNumberOfDigitsIn(maximumNumber); + + int placeValue = 1; + + while (numberOfDigits-- > 0) { + applyCountingSortOn(numbers, placeValue); + placeValue *= 10; + } + } + + private static void applyCountingSortOn(int[] numbers, int placeValue) { + int range = 10; // radix or the base + + int length = numbers.length; + int[] frequency = new int[range]; + int[] sortedValues = new int[length]; + + for (int i = 0; i < length; i++) { + int digit = (numbers[i] / placeValue) % range; + frequency[digit]++; + } + + for (int i = 1; i < 10; i++) { + frequency[i] += frequency[i - 1]; + } + + for (int i = length - 1; i >= 0; i--) { + int digit = (numbers[i] / placeValue) % range; + sortedValues[frequency[digit] - 1] = numbers[i]; + frequency[digit]--; + } + + System.arraycopy(sortedValues, 0, numbers, 0, length); + } + + private static int calculateNumberOfDigitsIn(int number) { + return (int) Math.log10(number) + 1; // valid only if number > 0 + } + + private static int findMaximumNumberIn(int[] arr) { + return Arrays.stream(arr).max().getAsInt(); + } + +} diff --git a/algorithms-sorting/src/test/java/com/baeldung/algorithms/radixsort/RadixSortUnitTest.java b/algorithms-sorting/src/test/java/com/baeldung/algorithms/radixsort/RadixSortUnitTest.java new file mode 100644 index 0000000000..f1b50f5c99 --- /dev/null +++ b/algorithms-sorting/src/test/java/com/baeldung/algorithms/radixsort/RadixSortUnitTest.java @@ -0,0 +1,16 @@ +package com.baeldung.algorithms.radixsort; + +import static org.junit.Assert.assertArrayEquals; + +import org.junit.Test; + +public class RadixSortUnitTest { + + @Test + public void givenUnsortedArrayWhenRadixSortThenArraySorted() { + int[] numbers = { 387, 468, 134, 123, 68, 221, 769, 37, 7 }; + RadixSort.sort(numbers); + int[] numbersSorted = { 7, 37, 68, 123, 134, 221, 387, 468, 769 }; + assertArrayEquals(numbersSorted, numbers); + } +} diff --git a/core-groovy-2/pom.xml b/core-groovy-2/pom.xml index ba3fd0802b..91e31d4cba 100644 --- a/core-groovy-2/pom.xml +++ b/core-groovy-2/pom.xml @@ -48,6 +48,11 @@ ${spock-core.version} test + + com.github.groovy-wslite + groovy-wslite + ${groovy-wslite.version} + @@ -175,6 +180,7 @@ 1.0.0 2.4.0 1.1-groovy-2.4 + 1.1.3 3.8.1 1.2.3 2.5.7 diff --git a/core-groovy-2/src/test/groovy/com/baeldung/webservice/WebserviceUnitTest.groovy b/core-groovy-2/src/test/groovy/com/baeldung/webservice/WebserviceUnitTest.groovy new file mode 100644 index 0000000000..302959d0d9 --- /dev/null +++ b/core-groovy-2/src/test/groovy/com/baeldung/webservice/WebserviceUnitTest.groovy @@ -0,0 +1,152 @@ +package com.baeldung.webservice + +import groovy.json.JsonSlurper +import wslite.rest.ContentType +import wslite.rest.RESTClient +import wslite.rest.RESTClientException +import wslite.soap.SOAPClient +import wslite.soap.SOAPMessageBuilder +import wslite.http.auth.HTTPBasicAuthorization +import org.junit.Test + +class WebserviceUnitTest extends GroovyTestCase { + + JsonSlurper jsonSlurper = new JsonSlurper() + + static RESTClient client = new RESTClient("https://postman-echo.com") + + static { + client.defaultAcceptHeader = ContentType.JSON + client.httpClient.sslTrustAllCerts = true + } + + void test_whenSendingGet_thenRespose200() { + def postmanGet = new URL('https://postman-echo.com/get') + def getConnection = postmanGet.openConnection() + getConnection.requestMethod = 'GET' + assert getConnection.responseCode == 200 + if (getConnection.responseCode == 200) { + assert jsonSlurper.parseText(getConnection.content.text)?.headers?.host == "postman-echo.com" + } + } + + void test_whenSendingPost_thenRespose200() { + def postmanPost = new URL('https://postman-echo.com/post') + def query = "q=This is post request form parameter." + def postConnection = postmanPost.openConnection() + postConnection.requestMethod = 'POST' + assert postConnection.responseCode == 200 + } + + void test_whenSendingPostWithParams_thenRespose200() { + def postmanPost = new URL('https://postman-echo.com/post') + def form = "param1=This is request parameter." + def postConnection = postmanPost.openConnection() + postConnection.requestMethod = 'POST' + postConnection.doOutput = true + def text + postConnection.with { + outputStream.withWriter { outputStreamWriter -> + outputStreamWriter << form + } + text = content.text + } + assert postConnection.responseCode == 200 + assert jsonSlurper.parseText(text)?.json.param1 == "This is request parameter." + } + + void test_whenReadingRSS_thenReceiveFeed() { + def rssFeed = new XmlParser().parse("https://news.google.com/rss?hl=en-US&gl=US&ceid=US:en") + def stories = [] + (0..4).each { + def item = rssFeed.channel.item.get(it) + stories << item.title.text() + } + assert stories.size() == 5 + } + + void test_whenReadingAtom_thenReceiveFeed() { + def atomFeed = new XmlParser().parse("https://news.google.com/atom?hl=en-US&gl=US&ceid=US:en") + def stories = [] + (0..4).each { + def entry = atomFeed.entry.get(it) + stories << entry.title.text() + } + assert stories.size() == 5 + } + + void test_whenConsumingSoap_thenReceiveResponse() { + def url = "http://www.dataaccess.com/webservicesserver/numberconversion.wso" + def soapClient = new SOAPClient(url) + def message = new SOAPMessageBuilder().build({ + body { + NumberToWords(xmlns: "http://www.dataaccess.com/webservicesserver/") { + ubiNum(1234) + } + } + }) + def response = soapClient.send(message.toString()); + def words = response.NumberToWordsResponse + assert words == "one thousand two hundred and thirty four " + } + + void test_whenConsumingRestGet_thenReceiveResponse() { + def path = "/get" + def response + try { + response = client.get(path: path) + assert response.statusCode == 200 + assert response.json?.headers?.host == "postman-echo.com" + } catch (RESTClientException e) { + assert e?.response?.statusCode != 200 + } + } + + void test_whenConsumingRestPost_thenReceiveResponse() { + def path = "/post" + def params = ["foo":1,"bar":2] + def response + try { + response = client.post(path: path) { + type ContentType.JSON + json params + } + assert response.json?.data == params + } catch (RESTClientException e) { + e.printStackTrace() + assert e?.response?.statusCode != 200 + } + } + + void test_whenBasicAuthentication_thenReceive200() { + def path = "/basic-auth" + def response + try { + client.authorization = new HTTPBasicAuthorization("postman", "password") + response = client.get(path: path) + assert response.statusCode == 200 + assert response.json?.authenticated == true + } catch (RESTClientException e) { + e.printStackTrace() + assert e?.response?.statusCode != 200 + } + } + + void test_whenOAuth_thenReceive200() { + RESTClient oAuthClient = new RESTClient("https://postman-echo.com") + oAuthClient.defaultAcceptHeader = ContentType.JSON + oAuthClient.httpClient.sslTrustAllCerts = true + + def path = "/oauth1" + def params = [oauth_consumer_key: "RKCGzna7bv9YD57c", oauth_signature_method: "HMAC-SHA1", oauth_timestamp:1567089944, oauth_nonce: "URT7v4", oauth_version: 1.0, oauth_signature: 'RGgR/ktDmclkM0ISWaFzebtlO0A='] + def response + try { + response = oAuthClient.get(path: path, query: params) + assert response.statusCode == 200 + assert response.statusMessage == "OK" + assert response.json.status == "pass" + } catch (RESTClientException e) { + assert e?.response?.statusCode != 200 + } + } +} \ No newline at end of file diff --git a/core-java-modules/core-java-8/README.md b/core-java-modules/core-java-8/README.md index 6e79eddd16..aee7121fb3 100644 --- a/core-java-modules/core-java-8/README.md +++ b/core-java-modules/core-java-8/README.md @@ -20,7 +20,6 @@ - [Finding Min/Max in an Array with Java](http://www.baeldung.com/java-array-min-max) - [Internationalization and Localization in Java 8](http://www.baeldung.com/java-8-localization) - [Java Optional – orElse() vs orElseGet()](http://www.baeldung.com/java-optional-or-else-vs-or-else-get) -- [Method Parameter Reflection in Java](http://www.baeldung.com/java-parameter-reflection) - [Java 8 Unsigned Arithmetic Support](http://www.baeldung.com/java-unsigned-arithmetic) - [Generalized Target-Type Inference in Java](http://www.baeldung.com/java-generalized-target-type-inference) - [Overriding System Time for Testing in Java](http://www.baeldung.com/java-override-system-time) diff --git a/core-java-modules/core-java-8/pom.xml b/core-java-modules/core-java-8/pom.xml index 28182e515b..6e547b7fad 100644 --- a/core-java-modules/core-java-8/pom.xml +++ b/core-java-modules/core-java-8/pom.xml @@ -134,16 +134,6 @@ - - org.apache.maven.plugins - maven-compiler-plugin - ${maven-compiler-plugin.version} - - 1.8 - 1.8 - -parameters - - org.springframework.boot spring-boot-maven-plugin @@ -192,7 +182,6 @@ 1.19 2.0.4.RELEASE - 3.8.0 2.22.1 diff --git a/core-java-modules/core-java-collections-array-list/src/main/java/com/baeldung/java/list/RemoveFromList.java b/core-java-modules/core-java-collections-array-list/src/main/java/com/baeldung/java/list/RemoveFromList.java new file mode 100644 index 0000000000..df9b3d987d --- /dev/null +++ b/core-java-modules/core-java-collections-array-list/src/main/java/com/baeldung/java/list/RemoveFromList.java @@ -0,0 +1,40 @@ +package com.baeldung.java.list; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +public class RemoveFromList { + + public static void main(String[] args) { + List sports = new ArrayList<>(); + sports.add("Football"); + sports.add("Basketball"); + sports.add("Baseball"); + sports.add("Boxing"); + sports.add("Cycling"); + + System.out.println("List before removing: " + sports); + + // Remove with index + sports.remove(1); + + // Remove with an element + sports.remove("Baseball"); + + // Iterator remove method + Iterator iterator = sports.iterator(); + while (iterator.hasNext()) { + if (iterator.next().equals("Boxing")) { + iterator.remove(); + break; + } + } + + // ArrayList removeIf method (Java 8) + sports.removeIf(p -> p.equals("Cycling")); + + System.out.println("List after removing: " + sports); + } + +} diff --git a/core-java-modules/core-java-collections-list-2/README.md b/core-java-modules/core-java-collections-list-2/README.md index be10a0210c..6192442edd 100644 --- a/core-java-modules/core-java-collections-list-2/README.md +++ b/core-java-modules/core-java-collections-list-2/README.md @@ -10,8 +10,4 @@ - [Java List Initialization in One Line](https://www.baeldung.com/java-init-list-one-line) - [Ways to Iterate Over a List in Java](https://www.baeldung.com/java-iterate-list) - [Flattening Nested Collections in Java](http://www.baeldung.com/java-flatten-nested-collections) -- [Intersection of Two Lists in Java](https://www.baeldung.com/java-lists-intersection) -- [Determine If All Elements Are the Same in a Java List](https://www.baeldung.com/java-list-all-equal) -- [List of Primitive Integer Values in Java](https://www.baeldung.com/java-list-primitive-int) -- [Performance Comparison of Primitive Lists in Java](https://www.baeldung.com/java-list-primitive-performance) -- [Filtering a Java Collection by a List](https://www.baeldung.com/java-filter-collection-by-list) \ No newline at end of file +- [Intersection of Two Lists in Java](https://www.baeldung.com/java-lists-intersection) \ No newline at end of file diff --git a/core-java-modules/core-java-collections-list-2/pom.xml b/core-java-modules/core-java-collections-list-2/pom.xml index d200a3c90c..727de0818a 100644 --- a/core-java-modules/core-java-collections-list-2/pom.xml +++ b/core-java-modules/core-java-collections-list-2/pom.xml @@ -36,41 +36,11 @@ ${lombok.version} provided - - - net.sf.trove4j - trove4j - ${trove4j.version} - - - it.unimi.dsi - fastutil - ${fastutil.version} - - - colt - colt - ${colt.version} - - - - org.openjdk.jmh - jmh-core - ${jmh-core.version} - - - org.openjdk.jmh - jmh-generator-annprocess - ${jmh-core.version} - 4.1 3.8.1 3.11.1 - 3.0.2 - 8.1.0 - 1.2.0 diff --git a/core-java-modules/core-java-collections-list-3/README.md b/core-java-modules/core-java-collections-list-3/README.md new file mode 100644 index 0000000000..267996044c --- /dev/null +++ b/core-java-modules/core-java-collections-list-3/README.md @@ -0,0 +1,11 @@ +========= + +## Core Java Collections List Cookbooks and Examples + +### Relevant Articles: +- [Collections.emptyList() vs. New List Instance](https://www.baeldung.com/java-collections-emptylist-new-list) +- [Copy a List to Another List in Java](http://www.baeldung.com/java-copy-list-to-another) +- [Determine If All Elements Are the Same in a Java List](https://www.baeldung.com/java-list-all-equal) +- [List of Primitive Integer Values in Java](https://www.baeldung.com/java-list-primitive-int) +- [Performance Comparison of Primitive Lists in Java](https://www.baeldung.com/java-list-primitive-performance) +- [Filtering a Java Collection by a List](https://www.baeldung.com/java-filter-collection-by-list) diff --git a/core-java-modules/core-java-collections-list-3/pom.xml b/core-java-modules/core-java-collections-list-3/pom.xml new file mode 100644 index 0000000000..064b65d19e --- /dev/null +++ b/core-java-modules/core-java-collections-list-3/pom.xml @@ -0,0 +1,76 @@ + + 4.0.0 + core-java-collections-list-3 + 0.1.0-SNAPSHOT + core-java-collections-list-3 + jar + + + com.baeldung + parent-java + 0.0.1-SNAPSHOT + ../../parent-java + + + + + org.apache.commons + commons-collections4 + ${commons-collections4.version} + + + org.apache.commons + commons-lang3 + ${commons-lang3.version} + + + org.assertj + assertj-core + ${assertj.version} + test + + + org.projectlombok + lombok + ${lombok.version} + provided + + + + net.sf.trove4j + trove4j + ${trove4j.version} + + + it.unimi.dsi + fastutil + ${fastutil.version} + + + colt + colt + ${colt.version} + + + + org.openjdk.jmh + jmh-core + ${jmh-core.version} + + + org.openjdk.jmh + jmh-generator-annprocess + ${jmh-core.version} + + + + + 4.1 + 3.8.1 + 3.11.1 + 3.0.2 + 8.1.0 + 1.2.0 + + diff --git a/core-java-modules/core-java-collections-list-2/src/main/java/com/baeldung/allequalelements/VerifyAllEqualListElements.java b/core-java-modules/core-java-collections-list-3/src/main/java/com/baeldung/allequalelements/VerifyAllEqualListElements.java similarity index 100% rename from core-java-modules/core-java-collections-list-2/src/main/java/com/baeldung/allequalelements/VerifyAllEqualListElements.java rename to core-java-modules/core-java-collections-list-3/src/main/java/com/baeldung/allequalelements/VerifyAllEqualListElements.java diff --git a/core-java-modules/core-java-collections-list-2/src/main/java/com/baeldung/collection/filtering/Employee.java b/core-java-modules/core-java-collections-list-3/src/main/java/com/baeldung/collection/filtering/Employee.java similarity index 100% rename from core-java-modules/core-java-collections-list-2/src/main/java/com/baeldung/collection/filtering/Employee.java rename to core-java-modules/core-java-collections-list-3/src/main/java/com/baeldung/collection/filtering/Employee.java diff --git a/core-java-modules/core-java-collections-list/src/main/java/com/baeldung/java/list/CopyListService.java b/core-java-modules/core-java-collections-list-3/src/main/java/com/baeldung/java/list/CopyListService.java similarity index 100% rename from core-java-modules/core-java-collections-list/src/main/java/com/baeldung/java/list/CopyListService.java rename to core-java-modules/core-java-collections-list-3/src/main/java/com/baeldung/java/list/CopyListService.java diff --git a/core-java-modules/core-java-collections-list/src/main/java/com/baeldung/java/list/Flower.java b/core-java-modules/core-java-collections-list-3/src/main/java/com/baeldung/java/list/Flower.java similarity index 100% rename from core-java-modules/core-java-collections-list/src/main/java/com/baeldung/java/list/Flower.java rename to core-java-modules/core-java-collections-list-3/src/main/java/com/baeldung/java/list/Flower.java diff --git a/core-java-modules/core-java-collections-list-2/src/main/java/com/baeldung/list/primitive/PrimitiveCollections.java b/core-java-modules/core-java-collections-list-3/src/main/java/com/baeldung/list/primitive/PrimitiveCollections.java similarity index 100% rename from core-java-modules/core-java-collections-list-2/src/main/java/com/baeldung/list/primitive/PrimitiveCollections.java rename to core-java-modules/core-java-collections-list-3/src/main/java/com/baeldung/list/primitive/PrimitiveCollections.java diff --git a/core-java-modules/core-java-collections-list-2/src/main/java/com/baeldung/list/primitive/PrimitivesListPerformance.java b/core-java-modules/core-java-collections-list-3/src/main/java/com/baeldung/list/primitive/PrimitivesListPerformance.java similarity index 100% rename from core-java-modules/core-java-collections-list-2/src/main/java/com/baeldung/list/primitive/PrimitivesListPerformance.java rename to core-java-modules/core-java-collections-list-3/src/main/java/com/baeldung/list/primitive/PrimitivesListPerformance.java diff --git a/testing-modules/mocks/jmockit/src/main/resources/logback.xml b/core-java-modules/core-java-collections-list-3/src/main/resources/logback.xml similarity index 100% rename from testing-modules/mocks/jmockit/src/main/resources/logback.xml rename to core-java-modules/core-java-collections-list-3/src/main/resources/logback.xml diff --git a/core-java-modules/core-java-collections-list-2/src/test/java/com/baeldung/allequalelements/VerifyAllEqualListElementsUnitTest.java b/core-java-modules/core-java-collections-list-3/src/test/java/com/baeldung/allequalelements/VerifyAllEqualListElementsUnitTest.java similarity index 100% rename from core-java-modules/core-java-collections-list-2/src/test/java/com/baeldung/allequalelements/VerifyAllEqualListElementsUnitTest.java rename to core-java-modules/core-java-collections-list-3/src/test/java/com/baeldung/allequalelements/VerifyAllEqualListElementsUnitTest.java diff --git a/core-java-modules/core-java-collections-list/src/test/java/com/baeldung/collection/CollectionsEmpty.java b/core-java-modules/core-java-collections-list-3/src/test/java/com/baeldung/collection/CollectionsEmpty.java similarity index 100% rename from core-java-modules/core-java-collections-list/src/test/java/com/baeldung/collection/CollectionsEmpty.java rename to core-java-modules/core-java-collections-list-3/src/test/java/com/baeldung/collection/CollectionsEmpty.java diff --git a/core-java-modules/core-java-collections-list-2/src/test/java/com/baeldung/collection/filtering/CollectionFilteringUnitTest.java b/core-java-modules/core-java-collections-list-3/src/test/java/com/baeldung/collection/filtering/CollectionFilteringUnitTest.java similarity index 100% rename from core-java-modules/core-java-collections-list-2/src/test/java/com/baeldung/collection/filtering/CollectionFilteringUnitTest.java rename to core-java-modules/core-java-collections-list-3/src/test/java/com/baeldung/collection/filtering/CollectionFilteringUnitTest.java diff --git a/core-java-modules/core-java-collections-list/src/test/java/com/baeldung/java/list/CopyListServiceUnitTest.java b/core-java-modules/core-java-collections-list-3/src/test/java/com/baeldung/java/list/CopyListServiceUnitTest.java similarity index 100% rename from core-java-modules/core-java-collections-list/src/test/java/com/baeldung/java/list/CopyListServiceUnitTest.java rename to core-java-modules/core-java-collections-list-3/src/test/java/com/baeldung/java/list/CopyListServiceUnitTest.java diff --git a/core-java-modules/core-java-collections-list/README.md b/core-java-modules/core-java-collections-list/README.md index 4bc1c5fb57..e83fcce5ff 100644 --- a/core-java-modules/core-java-collections-list/README.md +++ b/core-java-modules/core-java-collections-list/README.md @@ -10,7 +10,5 @@ - [Iterating Backward Through a List](http://www.baeldung.com/java-list-iterate-backwards) - [Remove the First Element from a List](http://www.baeldung.com/java-remove-first-element-from-list) - [How to Find an Element in a List with Java](http://www.baeldung.com/find-list-element-java) -- [Copy a List to Another List in Java](http://www.baeldung.com/java-copy-list-to-another) - [Finding Max/Min of a List or Collection](http://www.baeldung.com/java-collection-min-max) -- [Collections.emptyList() vs. New List Instance](https://www.baeldung.com/java-collections-emptylist-new-list) - [Remove All Occurrences of a Specific Value from a List](https://www.baeldung.com/java-remove-value-from-list) \ No newline at end of file diff --git a/core-java-modules/core-java-io-2/src/test/java/com/baeldung/filereader/FileReaderExampleUnitTest.java b/core-java-modules/core-java-io-2/src/test/java/com/baeldung/filereader/FileReaderExampleUnitTest.java index 968c041115..4f893fb327 100644 --- a/core-java-modules/core-java-io-2/src/test/java/com/baeldung/filereader/FileReaderExampleUnitTest.java +++ b/core-java-modules/core-java-io-2/src/test/java/com/baeldung/filereader/FileReaderExampleUnitTest.java @@ -17,15 +17,9 @@ public class FileReaderExampleUnitTest { public void givenFileReader_whenReadAllCharacters_thenReturnsContent() throws IOException { String expectedText = "Hello, World!"; File file = new File(FILE_PATH); - FileReader fileReader = null; - try { - fileReader = new FileReader(file); + try (FileReader fileReader = new FileReader(file)) { String content = FileReaderExample.readAllCharactersOneByOne(fileReader); Assert.assertEquals(expectedText, content); - } finally { - if (fileReader != null) { - fileReader.close(); - } } } @@ -33,15 +27,9 @@ public class FileReaderExampleUnitTest { public void givenFileReader_whenReadMultipleCharacters_thenReturnsContent() throws IOException { String expectedText = "Hello"; File file = new File(FILE_PATH); - FileReader fileReader = null; - try { - fileReader = new FileReader(file); + try (FileReader fileReader = new FileReader(file)) { String content = FileReaderExample.readMultipleCharacters(fileReader, 5); Assert.assertEquals(expectedText, content); - } finally { - if (fileReader != null) { - fileReader.close(); - } } } diff --git a/core-java-modules/core-java-io/pom.xml b/core-java-modules/core-java-io/pom.xml index 1a133d2cbe..84bf3baeed 100644 --- a/core-java-modules/core-java-io/pom.xml +++ b/core-java-modules/core-java-io/pom.xml @@ -207,6 +207,21 @@ ${maven.compiler.target} + + + + org.apache.maven.plugins + maven-jar-plugin + ${maven-jar-plugin.version} + + + + true + com.baeldung.resource.MyResourceLoader + + + + @@ -274,6 +289,8 @@ 1.18 0.1.5 + 3.1.0 + \ No newline at end of file diff --git a/core-java-modules/core-java-io/src/main/java/com/baeldung/resource/MyResourceLoader.java b/core-java-modules/core-java-io/src/main/java/com/baeldung/resource/MyResourceLoader.java new file mode 100644 index 0000000000..7512b177df --- /dev/null +++ b/core-java-modules/core-java-io/src/main/java/com/baeldung/resource/MyResourceLoader.java @@ -0,0 +1,42 @@ +package com.baeldung.resource; + +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.util.stream.Collectors; + +public class MyResourceLoader { + + private void loadFileWithReader() throws IOException { + + try (FileReader fileReader = new FileReader("src/main/resources/input.txt"); + BufferedReader reader = new BufferedReader(fileReader)) { + String contents = reader.lines() + .collect(Collectors.joining(System.lineSeparator())); + System.out.println(contents); + } + + } + + private void loadFileAsResource() throws IOException { + + try (InputStream inputStream = getClass().getResourceAsStream("/input.txt"); + BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) { + String contents = reader.lines() + .collect(Collectors.joining(System.lineSeparator())); + System.out.println(contents); + } + } + + public static void main(String[] args) throws IOException { + + MyResourceLoader resourceLoader = new MyResourceLoader(); + + resourceLoader.loadFileAsResource(); + resourceLoader.loadFileWithReader(); + + } + +} diff --git a/core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/copyconstructor/Employee.java b/core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/copyconstructor/Employee.java new file mode 100644 index 0000000000..7ad445e8ee --- /dev/null +++ b/core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/copyconstructor/Employee.java @@ -0,0 +1,30 @@ +package com.baeldung.copyconstructor; + +import java.util.Date; + +public class Employee { + + protected int id; + protected String name; + protected Date startDate; + + public Employee(int id, String name, Date startDate) { + this.id = id; + this.name = name; + this.startDate = startDate; + } + + public Employee(Employee employee) { + this.id = employee.id; + this.name = employee.name; + this.startDate = new Date(employee.startDate.getTime()); + } + + Date getStartDate() { + return startDate; + } + + public Employee copy() { + return new Employee(this); + } +} diff --git a/core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/copyconstructor/Manager.java b/core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/copyconstructor/Manager.java new file mode 100644 index 0000000000..97b8580b8e --- /dev/null +++ b/core-java-modules/core-java-lang-oop-2/src/main/java/com/baeldung/copyconstructor/Manager.java @@ -0,0 +1,31 @@ +package com.baeldung.copyconstructor; + +import java.util.Date; +import java.util.List; +import java.util.stream.Collectors; + +public class Manager extends Employee { + + private List directReports; + + public Manager(int id, String name, Date startDate, List directReports) { + super(id, name, startDate); + this.directReports = directReports; + } + + public Manager(Manager manager) { + super(manager.id, manager.name, manager.startDate); + this.directReports = manager.directReports.stream() + .collect(Collectors.toList()); + } + + @Override + public Employee copy() { + return new Manager(this); + } + + List getDirectReport() { + return this.directReports; + } + +} diff --git a/core-java-modules/core-java-lang-oop-2/src/test/java/com/baeldung/copyconstructor/EmployeeUnitTest.java b/core-java-modules/core-java-lang-oop-2/src/test/java/com/baeldung/copyconstructor/EmployeeUnitTest.java new file mode 100644 index 0000000000..19a4001b62 --- /dev/null +++ b/core-java-modules/core-java-lang-oop-2/src/test/java/com/baeldung/copyconstructor/EmployeeUnitTest.java @@ -0,0 +1,36 @@ +package com.baeldung.copyconstructor; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; + +import java.util.Date; + +import org.junit.Test; + +public class EmployeeUnitTest { + @Test + public void givenCopyConstructor_whenDeepCopy_thenDistinct() { + Date d1 = new Date(123); + Employee e1 = new Employee(1, "Baeldung", d1); + Employee e2 = new Employee(e1); + assertEquals(d1, e1.getStartDate()); + assertEquals(d1, e2.getStartDate()); + + d1.setTime(456); + assertEquals(d1, e1.getStartDate()); + assertNotEquals(d1, e2.getStartDate()); + } + + @Test + public void givenCopyMethod_whenCopy_thenDistinct() { + Date d1 = new Date(123); + Employee e1 = new Employee(1, "Baeldung", d1); + Employee e2 = e1.copy(); + assertEquals(d1, e1.getStartDate()); + assertEquals(d1, e2.getStartDate()); + + d1.setTime(456); + assertEquals(d1, e1.getStartDate()); + assertNotEquals(d1, e2.getStartDate()); + } +} diff --git a/core-java-modules/core-java-lang-oop-2/src/test/java/com/baeldung/copyconstructor/ManagerUnitTest.java b/core-java-modules/core-java-lang-oop-2/src/test/java/com/baeldung/copyconstructor/ManagerUnitTest.java new file mode 100644 index 0000000000..ef9f261360 --- /dev/null +++ b/core-java-modules/core-java-lang-oop-2/src/test/java/com/baeldung/copyconstructor/ManagerUnitTest.java @@ -0,0 +1,67 @@ +package com.baeldung.copyconstructor; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +import org.junit.Test; + +public class ManagerUnitTest { + @Test + public void givenCopyConstructor_whenDeepCopy_thenDistinct() { + Date startDate = new Date(123); + Employee e1 = new Employee(1, "Baeldung", startDate); + Employee e2 = new Employee(e1); + List directReports = new ArrayList(); + directReports.add(e1); + directReports.add(e2); + + Manager m1 = new Manager(1, "Baeldung Manager", startDate, directReports); + Manager m2 = new Manager(m1); + List directReports1 = m1.getDirectReport(); + List directReports2 = m2.getDirectReport(); + assertEquals(directReports1.size(), directReports2.size()); + assertArrayEquals(directReports1.toArray(), directReports2.toArray()); + + // clear m1's direct reports list. m2's list should not be affected + directReports.clear(); + directReports1 = m1.getDirectReport(); + directReports2 = m2.getDirectReport(); + assertEquals(0, directReports1.size()); + assertEquals(2, directReports2.size()); + + } + + @Test + public void givenCopyMethod_whenCopy_thenDistinct() { + Date startDate = new Date(123); + Employee e1 = new Employee(1, "Baeldung", startDate); + Employee e2 = new Employee(e1); + List directReports = new ArrayList(); + directReports.add(e1); + directReports.add(e2); + + // a Manager object whose declaration type is Employee. + Employee source = new Manager(1, "Baeldung Manager", startDate, directReports); + Employee clone = source.copy(); + + // after copy, clone should be still a Manager object. + assertTrue(clone instanceof Manager); + List directReports1 = ((Manager) source).getDirectReport(); + List directReports2 = ((Manager) clone).getDirectReport(); + assertEquals(directReports1.size(), directReports2.size()); + assertArrayEquals(directReports1.toArray(), directReports2.toArray()); + + // clear source's direct reports list. clone's list should not be affected + directReports.clear(); + directReports1 = ((Manager) source).getDirectReport(); + directReports2 = ((Manager) clone).getDirectReport(); + assertEquals(0, directReports1.size()); + assertEquals(2, directReports2.size()); + + } +} diff --git a/core-java-modules/core-java-lang-operators/src/main/java/com/baeldung/booleanoperators/Car.java b/core-java-modules/core-java-lang-operators/src/main/java/com/baeldung/booleanoperators/Car.java new file mode 100644 index 0000000000..37fb139917 --- /dev/null +++ b/core-java-modules/core-java-lang-operators/src/main/java/com/baeldung/booleanoperators/Car.java @@ -0,0 +1,36 @@ +package com.baeldung.booleanoperators; + +public class Car { + + private boolean diesel; + private boolean manual; + + public Car(boolean diesel, boolean manual) { + this.diesel = diesel; + this.manual = manual; + } + + public boolean isDiesel() { + return diesel; + } + + public boolean isManual() { + return manual; + } + + static Car dieselAndManualCar() { + return new Car(true, true); + } + + static Car dieselAndAutomaticCar() { + return new Car(true, false); + } + + static Car oilAndManualCar() { + return new Car(false, true); + } + + static Car oilAndAutomaticCar() { + return new Car(false, false); + } +} diff --git a/core-java-modules/core-java-lang-operators/src/test/java/com/baeldung/booleanoperators/XorUnitTest.java b/core-java-modules/core-java-lang-operators/src/test/java/com/baeldung/booleanoperators/XorUnitTest.java new file mode 100644 index 0000000000..efe38c6f45 --- /dev/null +++ b/core-java-modules/core-java-lang-operators/src/test/java/com/baeldung/booleanoperators/XorUnitTest.java @@ -0,0 +1,69 @@ +package com.baeldung.booleanoperators; + +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +class XorUnitTest { + + @Test + void givenDieselManualCar_whenXorOldSchool_thenFalse() { + Car car = Car.dieselAndManualCar(); + boolean dieselXorManual = (car.isDiesel() && !car.isManual()) || (!car.isDiesel() && car.isManual()); + assertThat(dieselXorManual).isFalse(); + } + + @Test + void givenDieselAutomaticCar_whenXorOldSchool_thenTrue() { + Car car = Car.dieselAndAutomaticCar(); + boolean dieselXorManual = (car.isDiesel() && !car.isManual()) || (!car.isDiesel() && car.isManual()); + assertThat(dieselXorManual).isTrue(); + } + + @Test + void givenNonDieselManualCar_whenXorOldSchool_thenTrue() { + Car car = Car.oilAndManualCar(); + boolean dieselXorManual = (car.isDiesel() && !car.isManual()) || (!car.isDiesel() && car.isManual()); + assertThat(dieselXorManual).isTrue(); + } + + @Test + void givenNonDieselAutomaticCar_whenXorOldSchool_thenFalse() { + Car car = Car.oilAndAutomaticCar(); + boolean dieselXorManual = (car.isDiesel() && !car.isManual()) || (!car.isDiesel() && car.isManual()); + assertThat(dieselXorManual).isFalse(); + } + + @Test + void givenDieselManualCar_whenXor_thenFalse() { + Car car = Car.dieselAndManualCar(); + boolean dieselXorManual = car.isDiesel() ^ car.isManual(); + assertThat(dieselXorManual).isFalse(); + } + + @Test + void givenDieselAutomaticCar_whenXor_thenTrue() { + Car car = Car.dieselAndAutomaticCar(); + boolean dieselXorManual = car.isDiesel() ^ car.isManual(); + assertThat(dieselXorManual).isTrue(); + } + + @Test + void givenNonDieselManualCar_whenXor_thenTrue() { + Car car = Car.oilAndManualCar(); + boolean dieselXorManual = car.isDiesel() ^ car.isManual(); + assertThat(dieselXorManual).isTrue(); + } + + @Test + void givenNonDieselAutomaticCar_whenXor_thenFalse() { + Car car = Car.oilAndAutomaticCar(); + boolean dieselXorManual = car.isDiesel() ^ car.isManual(); + assertThat(dieselXorManual).isFalse(); + } + + @Test + void givenNumbersOneAndThree_whenXor_thenTwo() { + assertThat(1 ^ 3).isEqualTo(2); + } +} diff --git a/core-java-modules/core-java-lang/README.md b/core-java-modules/core-java-lang/README.md index 74936eac21..f3b6d3d8e5 100644 --- a/core-java-modules/core-java-lang/README.md +++ b/core-java-modules/core-java-lang/README.md @@ -3,13 +3,10 @@ ## Core Java Lang Cookbooks and Examples ### Relevant Articles: -- [Guide to Java Reflection](http://www.baeldung.com/java-reflection) + - [Generate equals() and hashCode() with Eclipse](http://www.baeldung.com/java-eclipse-equals-and-hashcode) - [Chained Exceptions in Java](http://www.baeldung.com/java-chained-exceptions) -- [Call Methods at Runtime Using Java Reflection](http://www.baeldung.com/java-method-reflection) - [Iterating Over Enum Values in Java](http://www.baeldung.com/java-enum-iteration) -- [Changing Annotation Parameters At Runtime](http://www.baeldung.com/java-reflection-change-annotation-params) -- [Dynamic Proxies in Java](http://www.baeldung.com/java-dynamic-proxies) - [Java Double Brace Initialization](http://www.baeldung.com/java-double-brace-initialization) - [Guide to the Diamond Operator in Java](http://www.baeldung.com/java-diamond-operator) - [Comparator and Comparable in Java](http://www.baeldung.com/java-comparator-comparable) diff --git a/core-java-modules/core-java-reflection/README.MD b/core-java-modules/core-java-reflection/README.MD index d9cc95ad08..840d488968 100644 --- a/core-java-modules/core-java-reflection/README.MD +++ b/core-java-modules/core-java-reflection/README.MD @@ -1,4 +1,9 @@ ## Relevant Articles - [Void Type in Java](https://www.baeldung.com/java-void-type) -- [Retrieve Fields from a Java Class Using Reflection](https://www.baeldung.com/java-reflection-class-fields) \ No newline at end of file +- [Retrieve Fields from a Java Class Using Reflection](https://www.baeldung.com/java-reflection-class-fields) +- [Method Parameter Reflection in Java](http://www.baeldung.com/java-parameter-reflection) +- [Guide to Java Reflection](http://www.baeldung.com/java-reflection) +- [Call Methods at Runtime Using Java Reflection](http://www.baeldung.com/java-method-reflection) +- [Changing Annotation Parameters At Runtime](http://www.baeldung.com/java-reflection-change-annotation-params) +- [Dynamic Proxies in Java](http://www.baeldung.com/java-dynamic-proxies) \ No newline at end of file diff --git a/core-java-modules/core-java-reflection/pom.xml b/core-java-modules/core-java-reflection/pom.xml index e6c7f81d6c..b3c3390df8 100644 --- a/core-java-modules/core-java-reflection/pom.xml +++ b/core-java-modules/core-java-reflection/pom.xml @@ -23,7 +23,30 @@ + + core-java-reflection + + + src/main/resources + true + + + + + org.apache.maven.plugins + maven-compiler-plugin + ${maven-compiler-plugin.version} + + 1.8 + 1.8 + -parameters + + + + + + 3.8.0 3.10.0 \ No newline at end of file diff --git a/core-java-modules/core-java-lang/src/main/java/com/baeldung/dynamicproxy/DynamicInvocationHandler.java b/core-java-modules/core-java-reflection/src/main/java/com/baeldung/dynamicproxy/DynamicInvocationHandler.java similarity index 100% rename from core-java-modules/core-java-lang/src/main/java/com/baeldung/dynamicproxy/DynamicInvocationHandler.java rename to core-java-modules/core-java-reflection/src/main/java/com/baeldung/dynamicproxy/DynamicInvocationHandler.java diff --git a/core-java-modules/core-java-lang/src/main/java/com/baeldung/dynamicproxy/TimingDynamicInvocationHandler.java b/core-java-modules/core-java-reflection/src/main/java/com/baeldung/dynamicproxy/TimingDynamicInvocationHandler.java similarity index 100% rename from core-java-modules/core-java-lang/src/main/java/com/baeldung/dynamicproxy/TimingDynamicInvocationHandler.java rename to core-java-modules/core-java-reflection/src/main/java/com/baeldung/dynamicproxy/TimingDynamicInvocationHandler.java diff --git a/core-java-modules/core-java-8/src/main/java/com/baeldung/reflect/Person.java b/core-java-modules/core-java-reflection/src/main/java/com/baeldung/reflect/Person.java similarity index 100% rename from core-java-modules/core-java-8/src/main/java/com/baeldung/reflect/Person.java rename to core-java-modules/core-java-reflection/src/main/java/com/baeldung/reflect/Person.java diff --git a/core-java-modules/core-java-lang/src/main/java/com/baeldung/java/reflection/Animal.java b/core-java-modules/core-java-reflection/src/main/java/com/baeldung/reflection/java/reflection/Animal.java similarity index 100% rename from core-java-modules/core-java-lang/src/main/java/com/baeldung/java/reflection/Animal.java rename to core-java-modules/core-java-reflection/src/main/java/com/baeldung/reflection/java/reflection/Animal.java diff --git a/core-java-modules/core-java-lang/src/main/java/com/baeldung/java/reflection/Bird.java b/core-java-modules/core-java-reflection/src/main/java/com/baeldung/reflection/java/reflection/Bird.java similarity index 100% rename from core-java-modules/core-java-lang/src/main/java/com/baeldung/java/reflection/Bird.java rename to core-java-modules/core-java-reflection/src/main/java/com/baeldung/reflection/java/reflection/Bird.java diff --git a/core-java-modules/core-java-lang/src/main/java/com/baeldung/java/reflection/DynamicGreeter.java b/core-java-modules/core-java-reflection/src/main/java/com/baeldung/reflection/java/reflection/DynamicGreeter.java similarity index 100% rename from core-java-modules/core-java-lang/src/main/java/com/baeldung/java/reflection/DynamicGreeter.java rename to core-java-modules/core-java-reflection/src/main/java/com/baeldung/reflection/java/reflection/DynamicGreeter.java diff --git a/core-java-modules/core-java-lang/src/main/java/com/baeldung/java/reflection/Eating.java b/core-java-modules/core-java-reflection/src/main/java/com/baeldung/reflection/java/reflection/Eating.java similarity index 100% rename from core-java-modules/core-java-lang/src/main/java/com/baeldung/java/reflection/Eating.java rename to core-java-modules/core-java-reflection/src/main/java/com/baeldung/reflection/java/reflection/Eating.java diff --git a/core-java-modules/core-java-lang/src/main/java/com/baeldung/java/reflection/Goat.java b/core-java-modules/core-java-reflection/src/main/java/com/baeldung/reflection/java/reflection/Goat.java similarity index 100% rename from core-java-modules/core-java-lang/src/main/java/com/baeldung/java/reflection/Goat.java rename to core-java-modules/core-java-reflection/src/main/java/com/baeldung/reflection/java/reflection/Goat.java diff --git a/core-java-modules/core-java-lang/src/main/java/com/baeldung/java/reflection/Greeter.java b/core-java-modules/core-java-reflection/src/main/java/com/baeldung/reflection/java/reflection/Greeter.java similarity index 100% rename from core-java-modules/core-java-lang/src/main/java/com/baeldung/java/reflection/Greeter.java rename to core-java-modules/core-java-reflection/src/main/java/com/baeldung/reflection/java/reflection/Greeter.java diff --git a/core-java-modules/core-java-lang/src/main/java/com/baeldung/java/reflection/GreetingAnnotation.java b/core-java-modules/core-java-reflection/src/main/java/com/baeldung/reflection/java/reflection/GreetingAnnotation.java similarity index 100% rename from core-java-modules/core-java-lang/src/main/java/com/baeldung/java/reflection/GreetingAnnotation.java rename to core-java-modules/core-java-reflection/src/main/java/com/baeldung/reflection/java/reflection/GreetingAnnotation.java diff --git a/core-java-modules/core-java-lang/src/main/java/com/baeldung/java/reflection/Greetings.java b/core-java-modules/core-java-reflection/src/main/java/com/baeldung/reflection/java/reflection/Greetings.java similarity index 100% rename from core-java-modules/core-java-lang/src/main/java/com/baeldung/java/reflection/Greetings.java rename to core-java-modules/core-java-reflection/src/main/java/com/baeldung/reflection/java/reflection/Greetings.java diff --git a/core-java-modules/core-java-lang/src/main/java/com/baeldung/java/reflection/Locomotion.java b/core-java-modules/core-java-reflection/src/main/java/com/baeldung/reflection/java/reflection/Locomotion.java similarity index 100% rename from core-java-modules/core-java-lang/src/main/java/com/baeldung/java/reflection/Locomotion.java rename to core-java-modules/core-java-reflection/src/main/java/com/baeldung/reflection/java/reflection/Locomotion.java diff --git a/core-java-modules/core-java-lang/src/main/java/com/baeldung/java/reflection/Operations.java b/core-java-modules/core-java-reflection/src/main/java/com/baeldung/reflection/java/reflection/Operations.java similarity index 100% rename from core-java-modules/core-java-lang/src/main/java/com/baeldung/java/reflection/Operations.java rename to core-java-modules/core-java-reflection/src/main/java/com/baeldung/reflection/java/reflection/Operations.java diff --git a/core-java-modules/core-java-lang/src/main/java/com/baeldung/java/reflection/Person.java b/core-java-modules/core-java-reflection/src/main/java/com/baeldung/reflection/java/reflection/Person.java similarity index 100% rename from core-java-modules/core-java-lang/src/main/java/com/baeldung/java/reflection/Person.java rename to core-java-modules/core-java-reflection/src/main/java/com/baeldung/reflection/java/reflection/Person.java diff --git a/core-java-modules/core-java-lang/src/test/java/com/baeldung/dynamicproxy/DynamicProxyIntegrationTest.java b/core-java-modules/core-java-reflection/src/test/java/com/baeldung/dynamicproxy/DynamicProxyIntegrationTest.java similarity index 100% rename from core-java-modules/core-java-lang/src/test/java/com/baeldung/dynamicproxy/DynamicProxyIntegrationTest.java rename to core-java-modules/core-java-reflection/src/test/java/com/baeldung/dynamicproxy/DynamicProxyIntegrationTest.java diff --git a/core-java-modules/core-java-8/src/test/java/com/baeldung/reflect/MethodParamNameUnitTest.java b/core-java-modules/core-java-reflection/src/test/java/com/baeldung/reflect/MethodParamNameUnitTest.java similarity index 100% rename from core-java-modules/core-java-8/src/test/java/com/baeldung/reflect/MethodParamNameUnitTest.java rename to core-java-modules/core-java-reflection/src/test/java/com/baeldung/reflect/MethodParamNameUnitTest.java diff --git a/core-java-modules/core-java-lang/src/test/java/com/baeldung/java/reflection/OperationsUnitTest.java b/core-java-modules/core-java-reflection/src/test/java/com/baeldung/reflection/java/reflection/OperationsUnitTest.java similarity index 100% rename from core-java-modules/core-java-lang/src/test/java/com/baeldung/java/reflection/OperationsUnitTest.java rename to core-java-modules/core-java-reflection/src/test/java/com/baeldung/reflection/java/reflection/OperationsUnitTest.java diff --git a/core-java-modules/core-java-lang/src/test/java/com/baeldung/java/reflection/ReflectionUnitTest.java b/core-java-modules/core-java-reflection/src/test/java/com/baeldung/reflection/java/reflection/ReflectionUnitTest.java similarity index 100% rename from core-java-modules/core-java-lang/src/test/java/com/baeldung/java/reflection/ReflectionUnitTest.java rename to core-java-modules/core-java-reflection/src/test/java/com/baeldung/reflection/java/reflection/ReflectionUnitTest.java diff --git a/core-java-modules/core-java/src/main/java/com/baeldung/uuid/UUIDGenerator.java b/core-java-modules/core-java/src/main/java/com/baeldung/uuid/UUIDGenerator.java index dcf186de93..2659b29491 100644 --- a/core-java-modules/core-java/src/main/java/com/baeldung/uuid/UUIDGenerator.java +++ b/core-java-modules/core-java/src/main/java/com/baeldung/uuid/UUIDGenerator.java @@ -3,6 +3,7 @@ package com.baeldung.uuid; import java.io.UnsupportedEncodingException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; +import java.util.Arrays; import java.util.UUID; public class UUIDGenerator { @@ -65,9 +66,9 @@ public class UUIDGenerator { try { md = MessageDigest.getInstance("SHA-1"); } catch (NoSuchAlgorithmException nsae) { - throw new InternalError("MD5 not supported", nsae); + throw new InternalError("SHA-1 not supported", nsae); } - byte[] bytes = md.digest(name); + byte[] bytes = Arrays.copyOfRange(md.digest(name), 0, 16); bytes[6] &= 0x0f; /* clear version */ bytes[6] |= 0x50; /* set to version 5 */ bytes[8] &= 0x3f; /* clear variant */ diff --git a/javaxval/README.md b/javaxval/README.md index 3a975022ad..fadd174166 100644 --- a/javaxval/README.md +++ b/javaxval/README.md @@ -2,9 +2,6 @@ ## Java Bean Validation Examples -###The Course -The "REST With Spring" Classes: http://bit.ly/restwithspring - ### Relevant Articles: - [Java Bean Validation Basics](http://www.baeldung.com/javax-validation) - [Validating Container Elements with Bean Validation 2.0](http://www.baeldung.com/bean-validation-container-elements) diff --git a/libraries-primitive/pom.xml b/libraries-primitive/pom.xml index 12fc35c1b5..cddb3ab1fe 100644 --- a/libraries-primitive/pom.xml +++ b/libraries-primitive/pom.xml @@ -9,9 +9,14 @@ 1.0-SNAPSHOT libraries-primitive + + 1.8 + 1.8 + + - - + + it.unimi.dsi fastutil 8.2.2 @@ -36,5 +41,18 @@ 1.19 test + + + org.eclipse.collections + eclipse-collections-api + 10.0.0 + + + + org.eclipse.collections + eclipse-collections + 10.0.0 + + - + \ No newline at end of file diff --git a/libraries-primitive/src/test/java/com/baeldung/PrimitiveCollectionsUnitTest.java b/libraries-primitive/src/test/java/com/baeldung/PrimitiveCollectionsUnitTest.java new file mode 100644 index 0000000000..2adecd37fc --- /dev/null +++ b/libraries-primitive/src/test/java/com/baeldung/PrimitiveCollectionsUnitTest.java @@ -0,0 +1,70 @@ +package com.baeldung; + +import org.eclipse.collections.api.list.primitive.ImmutableIntList; +import org.eclipse.collections.api.list.primitive.MutableLongList; +import org.eclipse.collections.api.map.primitive.MutableIntIntMap; +import org.eclipse.collections.api.set.primitive.MutableIntSet; +import org.eclipse.collections.impl.factory.primitive.*; +import org.eclipse.collections.impl.list.Interval; +import org.eclipse.collections.impl.list.primitive.IntInterval; +import org.eclipse.collections.impl.map.mutable.primitive.IntIntHashMap; +import org.junit.Test; + +import java.util.stream.DoubleStream; + +import static org.junit.Assert.assertEquals; + + +public class PrimitiveCollectionsUnitTest { + + @Test + public void whenListOfLongHasOneTwoThree_thenSumIsSix() { + MutableLongList longList = LongLists.mutable.of(1L, 2L, 3L); + assertEquals(6, longList.sum()); + } + + @Test + public void whenListOfIntHasOneTwoThree_thenMaxIsThree() { + ImmutableIntList intList = IntLists.immutable.of(1, 2, 3); + assertEquals(3, intList.max()); + } + + @Test + public void whenConvertFromIterableToPrimitive_thenValuesAreEquals() { + Iterable iterable = Interval.oneTo(3); + MutableIntSet intSet = IntSets.mutable.withAll(iterable); + IntInterval intInterval = IntInterval.oneTo(3); + assertEquals(intInterval.toSet(), intSet); + } + + @Test + public void testOperationsOnIntIntMap() { + MutableIntIntMap map = new IntIntHashMap(); + assertEquals(5, map.addToValue(0, 5)); + assertEquals(5, map.get(0)); + assertEquals(3, map.getIfAbsentPut(1, 3)); + } + + @Test + public void whenCreateDoubleStream_thenAverageIsThree() { + DoubleStream doubleStream = DoubleLists + .mutable.with(1.0, 2.0, 3.0, 4.0, 5.0) + .primitiveStream(); + assertEquals(3, doubleStream.average().getAsDouble(), 0.001); + } + + @Test + public void whenCreateMapFromStream_thenValuesMustMatch() { + Iterable integers = Interval.oneTo(3); + MutableIntIntMap map = + IntIntMaps.mutable.from( + integers, + key -> key, + value -> value * value); + MutableIntIntMap expected = IntIntMaps.mutable.empty() + .withKeyValue(1, 1) + .withKeyValue(2, 4) + .withKeyValue(3, 9); + assertEquals(expected, map); + } +} \ No newline at end of file diff --git a/mapstruct/src/main/java/com/baeldung/dto/CustomerDto.java b/mapstruct/src/main/java/com/baeldung/dto/CustomerDto.java new file mode 100644 index 0000000000..617f2c6e0c --- /dev/null +++ b/mapstruct/src/main/java/com/baeldung/dto/CustomerDto.java @@ -0,0 +1,25 @@ +package com.baeldung.dto; + +public class CustomerDto { + + private String forename; + private String surname; + + public String getForename() { + return forename; + } + + public CustomerDto setForename(String forename) { + this.forename = forename; + return this; + } + + public String getSurname() { + return surname; + } + + public CustomerDto setSurname(String surname) { + this.surname = surname; + return this; + } +} diff --git a/mapstruct/src/main/java/com/baeldung/entity/Address.java b/mapstruct/src/main/java/com/baeldung/entity/Address.java new file mode 100644 index 0000000000..4a6edbd75d --- /dev/null +++ b/mapstruct/src/main/java/com/baeldung/entity/Address.java @@ -0,0 +1,35 @@ +package com.baeldung.entity; + +public class Address { + + private String street; + private String postalcode; + private String county; + + public String getStreet() { + return street; + } + + public Address setStreet(String street) { + this.street = street; + return this; + } + + public String getPostalcode() { + return postalcode; + } + + public Address setPostalcode(String postalcode) { + this.postalcode = postalcode; + return this; + } + + public String getCounty() { + return county; + } + + public Address setCounty(String county) { + this.county = county; + return this; + } +} diff --git a/mapstruct/src/main/java/com/baeldung/entity/Customer.java b/mapstruct/src/main/java/com/baeldung/entity/Customer.java new file mode 100644 index 0000000000..cdde6b542a --- /dev/null +++ b/mapstruct/src/main/java/com/baeldung/entity/Customer.java @@ -0,0 +1,25 @@ +package com.baeldung.entity; + +public class Customer { + + private String firstName; + private String lastName; + + public String getFirstName() { + return firstName; + } + + public Customer setFirstName(String firstName) { + this.firstName = firstName; + return this; + } + + public String getLastName() { + return lastName; + } + + public Customer setLastName(String lastName) { + this.lastName = lastName; + return this; + } +} diff --git a/mapstruct/src/main/java/com/baeldung/entity/DeliveryAddress.java b/mapstruct/src/main/java/com/baeldung/entity/DeliveryAddress.java new file mode 100644 index 0000000000..084c5f86ef --- /dev/null +++ b/mapstruct/src/main/java/com/baeldung/entity/DeliveryAddress.java @@ -0,0 +1,55 @@ +package com.baeldung.entity; + +public class DeliveryAddress { + + private String forename; + private String surname; + private String street; + private String postalcode; + private String county; + + public String getForename() { + return forename; + } + + public DeliveryAddress setForename(String forename) { + this.forename = forename; + return this; + } + + public String getSurname() { + return surname; + } + + public DeliveryAddress setSurname(String surname) { + this.surname = surname; + return this; + } + + public String getStreet() { + return street; + } + + public DeliveryAddress setStreet(String street) { + this.street = street; + return this; + } + + public String getPostalcode() { + return postalcode; + } + + public DeliveryAddress setPostalcode(String postalcode) { + this.postalcode = postalcode; + return this; + } + + public String getCounty() { + return county; + } + + public DeliveryAddress setCounty(String county) { + this.county = county; + return this; + } +} diff --git a/mapstruct/src/main/java/com/baeldung/mapper/CustomerDtoMapper.java b/mapstruct/src/main/java/com/baeldung/mapper/CustomerDtoMapper.java new file mode 100644 index 0000000000..2c84f80167 --- /dev/null +++ b/mapstruct/src/main/java/com/baeldung/mapper/CustomerDtoMapper.java @@ -0,0 +1,15 @@ +package com.baeldung.mapper; + +import org.mapstruct.Mapper; +import org.mapstruct.Mapping; + +import com.baeldung.dto.CustomerDto; +import com.baeldung.entity.Customer; + +@Mapper +public interface CustomerDtoMapper { + + @Mapping(source = "firstName", target = "forename") + @Mapping(source = "lastName", target = "surname") + CustomerDto from(Customer customer); +} diff --git a/mapstruct/src/main/java/com/baeldung/mapper/DeliveryAddressMapper.java b/mapstruct/src/main/java/com/baeldung/mapper/DeliveryAddressMapper.java new file mode 100644 index 0000000000..6a337fbb9e --- /dev/null +++ b/mapstruct/src/main/java/com/baeldung/mapper/DeliveryAddressMapper.java @@ -0,0 +1,24 @@ +package com.baeldung.mapper; + +import org.mapstruct.Mapper; +import org.mapstruct.Mapping; +import org.mapstruct.MappingTarget; + +import com.baeldung.entity.Address; +import com.baeldung.entity.Customer; +import com.baeldung.entity.DeliveryAddress; + +@Mapper +public interface DeliveryAddressMapper { + + @Mapping(source = "customer.firstName", target = "forename") + @Mapping(source = "customer.lastName", target = "surname") + @Mapping(source = "address.street", target = "street") + @Mapping(source = "address.postalcode", target = "postalcode") + @Mapping(source = "address.county", target = "county") + DeliveryAddress from(Customer customer, Address address); + + @Mapping(source = "address.postalcode", target = "postalcode") + @Mapping(source = "address.county", target = "county") + DeliveryAddress updateAddress(@MappingTarget DeliveryAddress deliveryAddress, Address address); +} diff --git a/mapstruct/src/test/java/com/baeldung/mapper/CustomerDtoMapperUnitTest.java b/mapstruct/src/test/java/com/baeldung/mapper/CustomerDtoMapperUnitTest.java new file mode 100644 index 0000000000..cded90138b --- /dev/null +++ b/mapstruct/src/test/java/com/baeldung/mapper/CustomerDtoMapperUnitTest.java @@ -0,0 +1,29 @@ +package com.baeldung.mapper; + +import static org.junit.Assert.assertEquals; + +import org.junit.jupiter.api.Test; +import org.mapstruct.factory.Mappers; + +import com.baeldung.dto.CustomerDto; +import com.baeldung.entity.Customer; + +public class CustomerDtoMapperUnitTest { + + private CustomerDtoMapper customerDtoMapper = Mappers.getMapper(CustomerDtoMapper.class); + + @Test + void testGivenCustomer_mapsToCustomerDto() { + + // given + Customer customer = new Customer().setFirstName("Max") + .setLastName("Powers"); + + // when + CustomerDto customerDto = customerDtoMapper.from(customer); + + // then + assertEquals(customerDto.getForename(), customer.getFirstName()); + assertEquals(customerDto.getSurname(), customer.getLastName()); + } +} diff --git a/mapstruct/src/test/java/com/baeldung/mapper/DeliveryAddressMapperUnitTest.java b/mapstruct/src/test/java/com/baeldung/mapper/DeliveryAddressMapperUnitTest.java new file mode 100644 index 0000000000..e2c12fcba3 --- /dev/null +++ b/mapstruct/src/test/java/com/baeldung/mapper/DeliveryAddressMapperUnitTest.java @@ -0,0 +1,64 @@ +package com.baeldung.mapper; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertSame; + +import org.junit.Test; +import org.mapstruct.factory.Mappers; + +import com.baeldung.entity.Address; +import com.baeldung.entity.Customer; +import com.baeldung.entity.DeliveryAddress; + +public class DeliveryAddressMapperUnitTest { + + private DeliveryAddressMapper deliveryAddressMapper = Mappers.getMapper(DeliveryAddressMapper.class); + + @Test + public void testGivenCustomerAndAddress_mapsToDeliveryAddress() { + + // given + Customer customer = new Customer().setFirstName("Max") + .setLastName("Powers"); + + Address homeAddress = new Address().setStreet("123 Some Street") + .setCounty("Nevada") + .setPostalcode("89123"); + + // when + DeliveryAddress deliveryAddress = deliveryAddressMapper.from(customer, homeAddress); + + // then + assertEquals(deliveryAddress.getForename(), customer.getFirstName()); + assertEquals(deliveryAddress.getSurname(), customer.getLastName()); + assertEquals(deliveryAddress.getStreet(), homeAddress.getStreet()); + assertEquals(deliveryAddress.getCounty(), homeAddress.getCounty()); + assertEquals(deliveryAddress.getPostalcode(), homeAddress.getPostalcode()); + } + + @Test + public void testGivenDeliveryAddressAndSomeOtherAddress_updatesDeliveryAddress() { + + // given + Customer customer = new Customer().setFirstName("Max") + .setLastName("Powers"); + + DeliveryAddress deliveryAddress = new DeliveryAddress().setStreet("123 Some Street") + .setCounty("Nevada") + .setPostalcode("89123"); + + Address otherAddress = new Address().setStreet("456 Some other street") + .setCounty("Arizona") + .setPostalcode("12345"); + + // when + DeliveryAddress updatedDeliveryAddress = deliveryAddressMapper.updateAddress(deliveryAddress, otherAddress); + + // then + assertSame(deliveryAddress, updatedDeliveryAddress); + + assertEquals(deliveryAddress.getStreet(), otherAddress.getStreet()); + assertEquals(deliveryAddress.getCounty(), otherAddress.getCounty()); + assertEquals(deliveryAddress.getPostalcode(), otherAddress.getPostalcode()); + } +} diff --git a/ml/README.md b/ml/README.md new file mode 100644 index 0000000000..f4712a94da --- /dev/null +++ b/ml/README.md @@ -0,0 +1,5 @@ +### Logistic Regression in Java +This is a soft introduction to ML using [deeplearning4j](https://deeplearning4j.org) library + +### Relevant Articles: +- [Logistic Regression in Java](http://www.baeldung.com/) diff --git a/ml/pom.xml b/ml/pom.xml new file mode 100644 index 0000000000..80afcc24f4 --- /dev/null +++ b/ml/pom.xml @@ -0,0 +1,52 @@ + + 4.0.0 + com.baeldung.deeplearning4j + ml + 1.0-SNAPSHOT + Machine Learning + jar + + + com.baeldung + parent-modules + 1.0.0-SNAPSHOT + + + + + org.nd4j + nd4j-native-platform + ${dl4j.version} + + + org.deeplearning4j + deeplearning4j-core + ${dl4j.version} + + + org.deeplearning4j + deeplearning4j-nn + ${dl4j.version} + + + + org.datavec + datavec-api + ${dl4j.version} + + + org.apache.httpcomponents + httpclient + 4.3.5 + + + + + + + 1.0.0-beta4 + + + \ No newline at end of file diff --git a/ml/src/main/java/com/baeldung/logreg/MnistClassifier.java b/ml/src/main/java/com/baeldung/logreg/MnistClassifier.java new file mode 100644 index 0000000000..1246de973f --- /dev/null +++ b/ml/src/main/java/com/baeldung/logreg/MnistClassifier.java @@ -0,0 +1,168 @@ +package com.baeldung.logreg; + +import java.io.File; +import java.util.HashMap; +import java.util.Map; +import java.util.Random; + +import org.datavec.api.io.labels.ParentPathLabelGenerator; +import org.datavec.api.split.FileSplit; +import org.datavec.image.loader.NativeImageLoader; +import org.datavec.image.recordreader.ImageRecordReader; +import org.deeplearning4j.datasets.datavec.RecordReaderDataSetIterator; +import org.deeplearning4j.nn.conf.MultiLayerConfiguration; +import org.deeplearning4j.nn.conf.NeuralNetConfiguration; +import org.deeplearning4j.nn.conf.inputs.InputType; +import org.deeplearning4j.nn.conf.layers.ConvolutionLayer; +import org.deeplearning4j.nn.conf.layers.DenseLayer; +import org.deeplearning4j.nn.conf.layers.OutputLayer; +import org.deeplearning4j.nn.conf.layers.SubsamplingLayer; +import org.deeplearning4j.nn.multilayer.MultiLayerNetwork; +import org.deeplearning4j.nn.weights.WeightInit; +import org.deeplearning4j.optimize.listeners.ScoreIterationListener; +import org.deeplearning4j.util.ModelSerializer; +import org.nd4j.evaluation.classification.Evaluation; +import org.nd4j.linalg.activations.Activation; +import org.nd4j.linalg.dataset.api.iterator.DataSetIterator; +import org.nd4j.linalg.dataset.api.preprocessor.DataNormalization; +import org.nd4j.linalg.dataset.api.preprocessor.ImagePreProcessingScaler; +import org.nd4j.linalg.learning.config.Nesterovs; +import org.nd4j.linalg.lossfunctions.LossFunctions; +import org.nd4j.linalg.schedule.MapSchedule; +import org.nd4j.linalg.schedule.ScheduleType; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Handwritten digit image classification based on LeNet-5 architecture by Yann LeCun. + * + * This code accompanies the article "Logistic regression in Java" and is heavily based on + * MnistClassifier. + * Some minor changes have been made in order to make article's flow smoother. + * + */ + +public class MnistClassifier { + private static final Logger logger = LoggerFactory.getLogger(MnistClassifier.class); + private static final String basePath = System.getProperty("java.io.tmpdir") + "mnist" + File.separator; + private static final File modelPath = new File(basePath + "mnist-model.zip"); + private static final String dataUrl = "http://github.com/myleott/mnist_png/raw/master/mnist_png.tar.gz"; + + public static void main(String[] args) throws Exception { + // input image sizes in pixels + int height = 28; + int width = 28; + // input image colour depth (1 for gray scale images) + int channels = 1; + // the number of output classes + int outputClasses = 10; + // number of samples that will be propagated through the network in each iteration + int batchSize = 54; + // total number of training epochs + int epochs = 1; + + // initialize a pseudorandom number generator + int seed = 1234; + Random randNumGen = new Random(seed); + + final String path = basePath + "mnist_png" + File.separator; + if (!new File(path).exists()) { + logger.info("Downloading data {}", dataUrl); + String localFilePath = basePath + "mnist_png.tar.gz"; + File file = new File(localFilePath); + if (!file.exists()) { + file.getParentFile() + .mkdirs(); + Utils.downloadAndSave(dataUrl, file); + Utils.extractTarArchive(file, basePath); + } + } else { + logger.info("Using the local data from folder {}", path); + } + + logger.info("Vectorizing the data from folder {}", path); + // vectorization of train data + File trainData = new File(path + "training"); + FileSplit trainSplit = new FileSplit(trainData, NativeImageLoader.ALLOWED_FORMATS, randNumGen); + // use parent directory name as the image label + ParentPathLabelGenerator labelMaker = new ParentPathLabelGenerator(); + ImageRecordReader trainRR = new ImageRecordReader(height, width, channels, labelMaker); + trainRR.initialize(trainSplit); + DataSetIterator train = new RecordReaderDataSetIterator(trainRR, batchSize, 1, outputClasses); + + // pixel values from 0-255 to 0-1 (min-max scaling) + DataNormalization imageScaler = new ImagePreProcessingScaler(); + imageScaler.fit(train); + train.setPreProcessor(imageScaler); + + // vectorization of test data + File testData = new File(path + "testing"); + FileSplit testSplit = new FileSplit(testData, NativeImageLoader.ALLOWED_FORMATS, randNumGen); + ImageRecordReader testRR = new ImageRecordReader(height, width, channels, labelMaker); + testRR.initialize(testSplit); + DataSetIterator test = new RecordReaderDataSetIterator(testRR, batchSize, 1, outputClasses); + // same normalization for better results + test.setPreProcessor(imageScaler); + + logger.info("Network configuration and training..."); + // reduce the learning rate as the number of training epochs increases + // iteration #, learning rate + Map learningRateSchedule = new HashMap<>(); + learningRateSchedule.put(0, 0.06); + learningRateSchedule.put(200, 0.05); + learningRateSchedule.put(600, 0.028); + learningRateSchedule.put(800, 0.0060); + learningRateSchedule.put(1000, 0.001); + + final ConvolutionLayer layer1 = new ConvolutionLayer.Builder(5, 5).nIn(channels) + .stride(1, 1) + .nOut(20) + .activation(Activation.IDENTITY) + .build(); + final SubsamplingLayer layer2 = new SubsamplingLayer.Builder(SubsamplingLayer.PoolingType.MAX).kernelSize(2, 2) + .stride(2, 2) + .build(); + // nIn need not specified in later layers + final ConvolutionLayer layer3 = new ConvolutionLayer.Builder(5, 5).stride(1, 1) + .nOut(50) + .activation(Activation.IDENTITY) + .build(); + final DenseLayer layer4 = new DenseLayer.Builder().activation(Activation.RELU) + .nOut(500) + .build(); + final OutputLayer layer5 = new OutputLayer.Builder(LossFunctions.LossFunction.NEGATIVELOGLIKELIHOOD).nOut(outputClasses) + .activation(Activation.SOFTMAX) + .build(); + final MultiLayerConfiguration config = new NeuralNetConfiguration.Builder().seed(seed) + .l2(0.0005) // ridge regression value + .updater(new Nesterovs(new MapSchedule(ScheduleType.ITERATION, learningRateSchedule))) + .weightInit(WeightInit.XAVIER) + .list() + .layer(layer1) + .layer(layer2) + .layer(layer3) + .layer(layer2) + .layer(layer4) + .layer(layer5) + .setInputType(InputType.convolutionalFlat(height, width, channels)) + .build(); + + final MultiLayerNetwork model = new MultiLayerNetwork(config); + model.init(); + model.setListeners(new ScoreIterationListener(100)); + logger.info("Total num of params: {}", model.numParams()); + + // evaluation while training (the score should go down) + for (int i = 0; i < epochs; i++) { + model.fit(train); + logger.info("Completed epoch {}", i); + train.reset(); + test.reset(); + } + Evaluation eval = model.evaluate(test); + logger.info(eval.stats()); + + ModelSerializer.writeModel(model, modelPath, true); + logger.info("The MINIST model has been saved in {}", modelPath.getPath()); + } +} \ No newline at end of file diff --git a/ml/src/main/java/com/baeldung/logreg/MnistPrediction.java b/ml/src/main/java/com/baeldung/logreg/MnistPrediction.java new file mode 100644 index 0000000000..56097d9a45 --- /dev/null +++ b/ml/src/main/java/com/baeldung/logreg/MnistPrediction.java @@ -0,0 +1,57 @@ +package com.baeldung.logreg; + +import java.io.File; +import java.io.IOException; + +import javax.swing.JFileChooser; + +import org.datavec.image.loader.NativeImageLoader; +import org.deeplearning4j.nn.multilayer.MultiLayerNetwork; +import org.deeplearning4j.util.ModelSerializer; +import org.nd4j.linalg.api.ndarray.INDArray; +import org.nd4j.linalg.dataset.api.preprocessor.ImagePreProcessingScaler; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class MnistPrediction { + private static final Logger logger = LoggerFactory.getLogger(MnistPrediction.class); + private static final File modelPath = new File(System.getProperty("java.io.tmpdir") + "mnist" + File.separator + "mnist-model.zip"); + private static final int height = 28; + private static final int width = 28; + private static final int channels = 1; + + /** + * Opens a popup that allows to select a file from the filesystem. + * @return + */ + public static String fileChose() { + JFileChooser fc = new JFileChooser(); + int ret = fc.showOpenDialog(null); + if (ret == JFileChooser.APPROVE_OPTION) { + File file = fc.getSelectedFile(); + return file.getAbsolutePath(); + } else { + return null; + } + } + + public static void main(String[] args) throws IOException { + if (!modelPath.exists()) { + logger.info("The model not found. Have you trained it?"); + return; + } + MultiLayerNetwork model = ModelSerializer.restoreMultiLayerNetwork(modelPath); + String path = fileChose(); + File file = new File(path); + + INDArray image = new NativeImageLoader(height, width, channels).asMatrix(file); + new ImagePreProcessingScaler(0, 1).transform(image); + + // Pass through to neural Net + INDArray output = model.output(image); + + logger.info("File: {}", path); + logger.info("Probabilities: {}", output); + } + +} diff --git a/ml/src/main/java/com/baeldung/logreg/Utils.java b/ml/src/main/java/com/baeldung/logreg/Utils.java new file mode 100644 index 0000000000..fa4be127cd --- /dev/null +++ b/ml/src/main/java/com/baeldung/logreg/Utils.java @@ -0,0 +1,103 @@ +package com.baeldung.logreg; + +import java.io.BufferedInputStream; +import java.io.BufferedOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; + +import org.apache.commons.compress.archivers.ArchiveEntry; +import org.apache.commons.compress.archivers.tar.TarArchiveEntry; +import org.apache.commons.compress.archivers.tar.TarArchiveInputStream; +import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream; +import org.apache.http.HttpEntity; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClientBuilder; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Utility class for digit classifier. + * + */ +public class Utils { + + private static final Logger logger = LoggerFactory.getLogger(Utils.class); + + private Utils() { + } + + /** + * Download the content of the given url and save it into a file. + * @param url + * @param file + */ + public static void downloadAndSave(String url, File file) throws IOException { + CloseableHttpClient client = HttpClientBuilder.create() + .build(); + logger.info("Connecting to {}", url); + try (CloseableHttpResponse response = client.execute(new HttpGet(url))) { + HttpEntity entity = response.getEntity(); + if (entity != null) { + logger.info("Downloaded {} bytes", entity.getContentLength()); + try (FileOutputStream outstream = new FileOutputStream(file)) { + logger.info("Saving to the local file"); + entity.writeTo(outstream); + outstream.flush(); + logger.info("Local file saved"); + } + } + } + } + + /** + * Extract a "tar.gz" file into a given folder. + * @param file + * @param folder + */ + public static void extractTarArchive(File file, String folder) throws IOException { + logger.info("Extracting archive {} into folder {}", file.getName(), folder); + // @formatter:off + try (FileInputStream fis = new FileInputStream(file); + BufferedInputStream bis = new BufferedInputStream(fis); + GzipCompressorInputStream gzip = new GzipCompressorInputStream(bis); + TarArchiveInputStream tar = new TarArchiveInputStream(gzip)) { + // @formatter:on + TarArchiveEntry entry; + while ((entry = (TarArchiveEntry) tar.getNextEntry()) != null) { + extractEntry(entry, tar, folder); + } + } + logger.info("Archive extracted"); + } + + /** + * Extract an entry of the input stream into a given folder + * @param entry + * @param tar + * @param folder + * @throws IOException + */ + public static void extractEntry(ArchiveEntry entry, InputStream tar, String folder) throws IOException { + final int bufferSize = 4096; + final String path = folder + entry.getName(); + if (entry.isDirectory()) { + new File(path).mkdirs(); + } else { + int count; + byte[] data = new byte[bufferSize]; + // @formatter:off + try (FileOutputStream os = new FileOutputStream(path); + BufferedOutputStream dest = new BufferedOutputStream(os, bufferSize)) { + // @formatter:off + while ((count = tar.read(data, 0, bufferSize)) != -1) { + dest.write(data, 0, count); + } + } + } + } +} diff --git a/testing-modules/mocks/mock-comparisons/src/main/resources/logback.xml b/ml/src/main/resources/logback.xml similarity index 100% rename from testing-modules/mocks/mock-comparisons/src/main/resources/logback.xml rename to ml/src/main/resources/logback.xml diff --git a/persistence-modules/java-cassandra/pom.xml b/persistence-modules/java-cassandra/pom.xml index e7c93bc4e5..3f8367d130 100644 --- a/persistence-modules/java-cassandra/pom.xml +++ b/persistence-modules/java-cassandra/pom.xml @@ -35,6 +35,11 @@ java-driver-core ${datastax-cassandra.version} + + com.datastax.oss + java-driver-query-builder + ${datastax-cassandra.version} + io.netty diff --git a/persistence-modules/java-cassandra/src/main/java/com/baeldung/datastax/cassandra/Application.java b/persistence-modules/java-cassandra/src/main/java/com/baeldung/datastax/cassandra/Application.java index 23140e0455..f067ee8b73 100644 --- a/persistence-modules/java-cassandra/src/main/java/com/baeldung/datastax/cassandra/Application.java +++ b/persistence-modules/java-cassandra/src/main/java/com/baeldung/datastax/cassandra/Application.java @@ -26,7 +26,7 @@ public class Application { KeyspaceRepository keyspaceRepository = new KeyspaceRepository(session); - keyspaceRepository.createKeyspace("testKeyspace", "SimpleStrategy", 1); + keyspaceRepository.createKeyspace("testKeyspace", 1); keyspaceRepository.useKeyspace("testKeyspace"); VideoRepository videoRepository = new VideoRepository(session); diff --git a/persistence-modules/java-cassandra/src/main/java/com/baeldung/datastax/cassandra/repository/KeyspaceRepository.java b/persistence-modules/java-cassandra/src/main/java/com/baeldung/datastax/cassandra/repository/KeyspaceRepository.java index fd4581d055..899481738a 100644 --- a/persistence-modules/java-cassandra/src/main/java/com/baeldung/datastax/cassandra/repository/KeyspaceRepository.java +++ b/persistence-modules/java-cassandra/src/main/java/com/baeldung/datastax/cassandra/repository/KeyspaceRepository.java @@ -1,7 +1,11 @@ package com.baeldung.datastax.cassandra.repository; +import com.datastax.oss.driver.api.core.CqlIdentifier; import com.datastax.oss.driver.api.core.CqlSession; +import com.datastax.oss.driver.api.querybuilder.SchemaBuilder; +import com.datastax.oss.driver.api.querybuilder.schema.CreateKeyspace; + public class KeyspaceRepository { private final CqlSession session; @@ -9,19 +13,15 @@ public class KeyspaceRepository { this.session = session; } - public void createKeyspace(String keyspaceName, String replicationStrategy, int numberOfReplicas) { - StringBuilder sb = new StringBuilder("CREATE KEYSPACE IF NOT EXISTS ").append(keyspaceName) - .append(" WITH replication = {") - .append("'class':'").append(replicationStrategy) - .append("','replication_factor':").append(numberOfReplicas) - .append("};"); + public void createKeyspace(String keyspaceName, int numberOfReplicas) { + CreateKeyspace createKeyspace = SchemaBuilder.createKeyspace(keyspaceName) + .ifNotExists() + .withSimpleStrategy(numberOfReplicas); - final String query = sb.toString(); - - session.execute(query); + session.execute(createKeyspace.build()); } public void useKeyspace(String keyspace) { - session.execute("USE " + keyspace); + session.execute("USE " + CqlIdentifier.fromCql(keyspace)); } } diff --git a/persistence-modules/java-cassandra/src/main/java/com/baeldung/datastax/cassandra/repository/VideoRepository.java b/persistence-modules/java-cassandra/src/main/java/com/baeldung/datastax/cassandra/repository/VideoRepository.java index 9a09d77072..48eb000eba 100644 --- a/persistence-modules/java-cassandra/src/main/java/com/baeldung/datastax/cassandra/repository/VideoRepository.java +++ b/persistence-modules/java-cassandra/src/main/java/com/baeldung/datastax/cassandra/repository/VideoRepository.java @@ -7,6 +7,12 @@ import com.datastax.oss.driver.api.core.cql.BoundStatement; import com.datastax.oss.driver.api.core.cql.PreparedStatement; import com.datastax.oss.driver.api.core.cql.ResultSet; import com.datastax.oss.driver.api.core.cql.SimpleStatement; +import com.datastax.oss.driver.api.core.type.DataTypes; +import com.datastax.oss.driver.api.querybuilder.QueryBuilder; +import com.datastax.oss.driver.api.querybuilder.SchemaBuilder; +import com.datastax.oss.driver.api.querybuilder.insert.RegularInsert; +import com.datastax.oss.driver.api.querybuilder.schema.CreateTable; +import com.datastax.oss.driver.api.querybuilder.select.Select; import java.util.ArrayList; import java.util.List; @@ -27,15 +33,12 @@ public class VideoRepository { } public void createTable(String keyspace) { - StringBuilder sb = new StringBuilder("CREATE TABLE IF NOT EXISTS ").append(TABLE_NAME).append(" (") - .append("video_id UUID,") - .append("title TEXT,") - .append("creation_date TIMESTAMP,") - .append("PRIMARY KEY(video_id));"); + CreateTable createTable = SchemaBuilder.createTable(TABLE_NAME).ifNotExists() + .withPartitionKey("video_id", DataTypes.UUID) + .withColumn("title", DataTypes.TEXT) + .withColumn("creation_date", DataTypes.TIMESTAMP); - String query = sb.toString(); - - executeStatement(SimpleStatement.newInstance(query), keyspace); + executeStatement(createTable.build(), keyspace); } public UUID insertVideo(Video video) { @@ -47,17 +50,23 @@ public class VideoRepository { video.setId(videoId); - String absoluteTableName = keyspace != null ? keyspace + "." + TABLE_NAME: TABLE_NAME; + RegularInsert insertInto = QueryBuilder.insertInto(TABLE_NAME) + .value("video_id", QueryBuilder.bindMarker()) + .value("title", QueryBuilder.bindMarker()) + .value("creation_date", QueryBuilder.bindMarker()); - StringBuilder sb = new StringBuilder("INSERT INTO ").append(absoluteTableName) - .append("(video_id, title, creation_date) values (:video_id, :title, :creation_date)"); + SimpleStatement insertStatement = insertInto.build(); - PreparedStatement preparedStatement = session.prepare(sb.toString()); + if (keyspace != null) { + insertStatement = insertStatement.setKeyspace(keyspace); + } + + PreparedStatement preparedStatement = session.prepare(insertStatement); BoundStatement statement = preparedStatement.bind() - .setUuid("video_id", video.getId()) - .setString("title", video.getTitle()) - .setInstant("creation_date", video.getCreationDate()); + .setUuid(0, video.getId()) + .setString(1, video.getTitle()) + .setInstant(2, video.getCreationDate()); session.execute(statement); @@ -69,11 +78,9 @@ public class VideoRepository { } public List + + javax.el + javax.el-api + ${javax.el.version} + + + org.glassfish + javax.el + ${javax.el.version} + - 4.3.4.RELEASE + 4.3.4.RELEASE 2.1.5.RELEASE 5.3.3.Final 2.9.6 + 3.0.0 diff --git a/persistence-modules/spring-data-couchbase-2/src/test/java/org/baeldung/SpringContextLiveTest.java b/persistence-modules/spring-data-couchbase-2/src/test/java/org/baeldung/SpringContextLiveTest.java index af228735b8..5e20a98a1d 100644 --- a/persistence-modules/spring-data-couchbase-2/src/test/java/org/baeldung/SpringContextLiveTest.java +++ b/persistence-modules/spring-data-couchbase-2/src/test/java/org/baeldung/SpringContextLiveTest.java @@ -9,6 +9,45 @@ import org.springframework.test.context.TestExecutionListeners; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.support.DependencyInjectionTestExecutionListener; +/** + * This LiveTest requires: + * + * 1- Couchbase instance running (e.g. with `docker run -d --name db -p 8091-8096:8091-8096 -p 11210-11211:11210-11211 couchbase`) + * + * + * 2- Couchbase configured with (we can use the console in localhost:8091): + * + * 2.1- Buckets: named 'baeldung' and 'baeldung2' + * + * 2.2- Security: users 'baeldung' and 'baeldung2'. Note: in newer versions an empty password is not allowed, then we have to change the passwords in the project) + * + * 2.3- Spacial View: Add new spacial view (in Index tab) in document 'campus_spatial', view 'byLocation' with the following function: + * {@code + * function (doc) { + * if (doc.location && + * doc._class == "org.baeldung.spring.data.couchbase.model.Campus") { + * emit([doc.location.x, doc.location.y], null); + * } + * }} + * + * 2.4- MapReduce Views: Add new views in document 'campus': + * 2.4.1- view 'all' with function: + * {@code + * function (doc, meta) { + * if(doc._class == "org.baeldung.spring.data.couchbase.model.Campus") { + * emit(meta.id, null); + * } + * }} + * + * 2.4.2- view 'byName' with function: + * {@code + * function (doc, meta) { + * if(doc._class == "org.baeldung.spring.data.couchbase.model.Campus" && + * doc.name) { + * emit(doc.name, null); + * } + * }} + */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = { MultiBucketCouchbaseConfig.class, MultiBucketIntegrationTestConfig.class }) @TestExecutionListeners(listeners = { DependencyInjectionTestExecutionListener.class }) diff --git a/persistence-modules/spring-data-mongodb/src/test/java/com/baeldung/contexttests/mongoconfig/SpringContextLiveTest.java b/persistence-modules/spring-data-mongodb/src/test/java/com/baeldung/contexttests/mongoconfig/SpringContextLiveTest.java new file mode 100644 index 0000000000..a2a3d1761f --- /dev/null +++ b/persistence-modules/spring-data-mongodb/src/test/java/com/baeldung/contexttests/mongoconfig/SpringContextLiveTest.java @@ -0,0 +1,26 @@ +package com.baeldung.contexttests.mongoconfig; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import com.baeldung.config.MongoConfig; + +/** + * This Live test requires: + * * mongodb instance running on the environment + * + * (e.g. `docker run -d -p 27017:27017 --name bael-mongo mongo`) + * + * + */ +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = MongoConfig.class) +public class SpringContextLiveTest { + + @Test + public void whenSpringContextIsBootstrapped_thenNoExceptions() { + } + +} diff --git a/persistence-modules/spring-data-mongodb/src/test/java/com/baeldung/contexttests/mongoreactiveconfig/SpringContextLiveTest.java b/persistence-modules/spring-data-mongodb/src/test/java/com/baeldung/contexttests/mongoreactiveconfig/SpringContextLiveTest.java new file mode 100644 index 0000000000..d2a946fb90 --- /dev/null +++ b/persistence-modules/spring-data-mongodb/src/test/java/com/baeldung/contexttests/mongoreactiveconfig/SpringContextLiveTest.java @@ -0,0 +1,26 @@ +package com.baeldung.contexttests.mongoreactiveconfig; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import com.baeldung.config.MongoReactiveConfig; + +/** + * This Live test requires: + * * mongodb instance running on the environment + * + * (e.g. `docker run -d -p 27017:27017 --name bael-mongo mongo`) + * + * + */ +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = MongoReactiveConfig.class) +public class SpringContextLiveTest { + + @Test + public void whenSpringContextIsBootstrapped_thenNoExceptions() { + } + +} diff --git a/persistence-modules/spring-data-mongodb/src/test/java/com/baeldung/contexttests/simplemongoconfig/SpringContextLiveTest.java b/persistence-modules/spring-data-mongodb/src/test/java/com/baeldung/contexttests/simplemongoconfig/SpringContextLiveTest.java new file mode 100644 index 0000000000..6e8905c139 --- /dev/null +++ b/persistence-modules/spring-data-mongodb/src/test/java/com/baeldung/contexttests/simplemongoconfig/SpringContextLiveTest.java @@ -0,0 +1,26 @@ +package com.baeldung.contexttests.simplemongoconfig; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import com.baeldung.config.SimpleMongoConfig; + +/** + * This Live test requires: + * * mongodb instance running on the environment + * + * (e.g. `docker run -d -p 27017:27017 --name bael-mongo mongo`) + * + * + */ +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = SimpleMongoConfig.class) +public class SpringContextLiveTest { + + @Test + public void whenSpringContextIsBootstrapped_thenNoExceptions() { + } + +} diff --git a/persistence-modules/spring-jpa/README.md b/persistence-modules/spring-jpa/README.md index 56ce68c126..d04e9f6f41 100644 --- a/persistence-modules/spring-jpa/README.md +++ b/persistence-modules/spring-jpa/README.md @@ -9,7 +9,6 @@ - [Sorting with JPA](http://www.baeldung.com/jpa-sort) - [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) - [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) diff --git a/play-framework/introduction/.gitignore b/play-framework/introduction/.gitignore index eb372fc719..e497f3fc67 100644 --- a/play-framework/introduction/.gitignore +++ b/play-framework/introduction/.gitignore @@ -1,6 +1,7 @@ logs target /.idea +/.g8 /.idea_modules /.classpath /.project diff --git a/play-framework/introduction/LICENSE b/play-framework/introduction/LICENSE deleted file mode 100644 index 4baedcb95f..0000000000 --- a/play-framework/introduction/LICENSE +++ /dev/null @@ -1,8 +0,0 @@ -This software is licensed under the Apache 2 license, quoted below. - -Licensed under the Apache License, Version 2.0 (the "License"); you may not use this project except in compliance with -the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0. - -Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an -"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific -language governing permissions and limitations under the License. \ No newline at end of file diff --git a/play-framework/introduction/README b/play-framework/introduction/README deleted file mode 100644 index f21d092edf..0000000000 --- a/play-framework/introduction/README +++ /dev/null @@ -1,49 +0,0 @@ -This is your new Play application -================================= - -This file will be packaged with your application when using `activator dist`. - -There are several demonstration files available in this template. - -Controllers -=========== - -- HomeController.java: - - Shows how to handle simple HTTP requests. - -- AsyncController.java: - - Shows how to do asynchronous programming when handling a request. - -- CountController.java: - - Shows how to inject a component into a controller and use the component when - handling requests. - -Components -========== - -- Module.java: - - Shows how to use Guice to bind all the components needed by your application. - -- Counter.java: - - An example of a component that contains state, in this case a simple counter. - -- ApplicationTimer.java: - - An example of a component that starts when the application starts and stops - when the application stops. - -Filters -======= - -- Filters.java: - - Creates the list of HTTP filters used by your application. - -- ExampleFilter.java - - A simple filter that adds a header to every response. \ No newline at end of file diff --git a/play-framework/introduction/app/Filters.java b/play-framework/introduction/app/Filters.java deleted file mode 100644 index 255de8ca93..0000000000 --- a/play-framework/introduction/app/Filters.java +++ /dev/null @@ -1,46 +0,0 @@ -import javax.inject.*; -import play.*; -import play.mvc.EssentialFilter; -import play.http.HttpFilters; -import play.mvc.*; - -import filters.ExampleFilter; - -/** - * This class configures filters that run on every request. This - * class is queried by Play to get a list of filters. - * - * Play will automatically use filters from any class called - * Filters that is placed the root package. You can load filters - * from a different class by adding a `play.http.filters` setting to - * the application.conf configuration file. - */ -@Singleton -public class Filters implements HttpFilters { - - private final Environment env; - private final EssentialFilter exampleFilter; - - /** - * @param env Basic environment settings for the current application. - * @param exampleFilter A demonstration filter that adds a header to - */ - @Inject - public Filters(Environment env, ExampleFilter exampleFilter) { - this.env = env; - this.exampleFilter = exampleFilter; - } - - @Override - public EssentialFilter[] filters() { - // Use the example filter if we're running development mode. If - // we're running in production or test mode then don't use any - // filters at all. - if (env.mode().equals(Mode.DEV)) { - return new EssentialFilter[] { exampleFilter }; - } else { - return new EssentialFilter[] {}; - } - } - -} diff --git a/play-framework/introduction/app/Module.java b/play-framework/introduction/app/Module.java deleted file mode 100644 index 6e7d1766ef..0000000000 --- a/play-framework/introduction/app/Module.java +++ /dev/null @@ -1,31 +0,0 @@ -import com.google.inject.AbstractModule; -import java.time.Clock; - -import services.ApplicationTimer; -import services.AtomicCounter; -import services.Counter; - -/** - * This class is a Guice module that tells Guice how to bind several - * different types. This Guice module is created when the Play - * application starts. - * - * Play will automatically use any class called `Module` that is in - * the root package. You can create modules in other locations by - * adding `play.modules.enabled` settings to the `application.conf` - * configuration file. - */ -public class Module extends AbstractModule { - - @Override - public void configure() { - // Use the system clock as the default implementation of Clock - bind(Clock.class).toInstance(Clock.systemDefaultZone()); - // Ask Guice to create an instance of ApplicationTimer when the - // application starts. - bind(ApplicationTimer.class).asEagerSingleton(); - // Set AtomicCounter as the implementation for Counter. - bind(Counter.class).to(AtomicCounter.class); - } - -} diff --git a/play-framework/introduction/app/controllers/AsyncController.java b/play-framework/introduction/app/controllers/AsyncController.java deleted file mode 100644 index 92c84bb755..0000000000 --- a/play-framework/introduction/app/controllers/AsyncController.java +++ /dev/null @@ -1,64 +0,0 @@ -package controllers; - -import akka.actor.ActorSystem; - -import javax.inject.*; - -import play.*; -import play.mvc.*; - -import java.util.concurrent.Executor; -import java.util.concurrent.CompletableFuture; -import java.util.concurrent.CompletionStage; -import java.util.concurrent.TimeUnit; - -import scala.concurrent.duration.Duration; -import scala.concurrent.ExecutionContextExecutor; - -/** - * This controller contains an action that demonstrates how to write - * simple asynchronous code in a controller. It uses a timer to - * asynchronously delay sending a response for 1 second. - * - * @param actorSystem We need the {@link ActorSystem}'s - * {@link Scheduler} to run code after a delay. - * @param exec We need a Java {@link Executor} to apply the result - * of the {@link CompletableFuture} and a Scala - * {@link ExecutionContext} so we can use the Akka {@link Scheduler}. - * An {@link ExecutionContextExecutor} implements both interfaces. - */ -@Singleton -public class AsyncController extends Controller { - - private final ActorSystem actorSystem; - private final ExecutionContextExecutor exec; - - @Inject - public AsyncController(ActorSystem actorSystem, ExecutionContextExecutor exec) { - this.actorSystem = actorSystem; - this.exec = exec; - } - - /** - * An action that returns a plain text message after a delay - * of 1 second. - *

- * The configuration in the routes file means that this method - * will be called when the application receives a GET request with - * a path of /message. - */ - public CompletionStage message() { - return getFutureMessage(1, TimeUnit.SECONDS).thenApplyAsync(Results::ok, exec); - } - - private CompletionStage getFutureMessage(long time, TimeUnit timeUnit) { - CompletableFuture future = new CompletableFuture<>(); - actorSystem.scheduler().scheduleOnce( - Duration.create(time, timeUnit), - () -> future.complete("Hi!"), - exec - ); - return future; - } - -} diff --git a/play-framework/introduction/app/controllers/CountController.java b/play-framework/introduction/app/controllers/CountController.java deleted file mode 100644 index 02fcb15f8e..0000000000 --- a/play-framework/introduction/app/controllers/CountController.java +++ /dev/null @@ -1,35 +0,0 @@ -package controllers; - -import javax.inject.*; -import play.*; -import play.mvc.*; - -import services.Counter; - -/** - * This controller demonstrates how to use dependency injection to - * bind a component into a controller class. The class contains an - * action that shows an incrementing count to users. The {@link Counter} - * object is injected by the Guice dependency injection system. - */ -@Singleton -public class CountController extends Controller { - - private final Counter counter; - - @Inject - public CountController(Counter counter) { - this.counter = counter; - } - - /** - * An action that responds with the {@link Counter}'s current - * count. The result is plain text. This action is mapped to - * GET requests with a path of /count - * requests by an entry in the routes config file. - */ - public Result count() { - return ok(Integer.toString(counter.nextCount())); - } - -} diff --git a/play-framework/introduction/app/controllers/HomeController.java b/play-framework/introduction/app/controllers/HomeController.java index 6a79856eb4..9b1146886e 100644 --- a/play-framework/introduction/app/controllers/HomeController.java +++ b/play-framework/introduction/app/controllers/HomeController.java @@ -1,14 +1,25 @@ package controllers; +import play.libs.concurrent.HttpExecutionContext; import play.mvc.*; +import play.twirl.api.Html; -import views.html.*; +import javax.inject.Inject; +import java.util.concurrent.CompletionStage; + +import static java.util.concurrent.CompletableFuture.supplyAsync; /** * This controller contains an action to handle HTTP requests * to the application's home page. */ public class HomeController extends Controller { + private HttpExecutionContext ec; + + @Inject + public HomeController(HttpExecutionContext ec) { + this.ec = ec; + } /** * An action that renders an HTML page with a welcome message. @@ -17,7 +28,41 @@ public class HomeController extends Controller { * GET request with a path of /. */ public Result index() { - return ok(index.render("Your new application is ready.")); + return ok(views.html.index.render()); } + public Result applyHtml() { + return ok(Html.apply("

This text will appear as a heading 1

")); + } + + public Result badRequestPage() { + return badRequest("Your request data has issues."); + } + + public Result notFoundPage() { + return notFound("Could not find the page you requested."); + } + + public Result customContentType() { + return ok("This is some text content").as("text/html"); + } + + public CompletionStage asyncOperation() { + return supplyAsync(() -> { + return longRunningTask(); + }, ec.current()) + .thenApplyAsync(s -> { + return ok("Got result -> " + s); + }, ec.current()); + } + + private String longRunningTask() { + return "Long running task has completed"; + } + + public Result setHeaders() { + return ok("This is some text content") + .as("text/html") + .withHeader("Header-Key", "Some value"); + } } diff --git a/play-framework/introduction/app/controllers/StudentController.java b/play-framework/introduction/app/controllers/StudentController.java deleted file mode 100644 index 8b759b9598..0000000000 --- a/play-framework/introduction/app/controllers/StudentController.java +++ /dev/null @@ -1,63 +0,0 @@ -package controllers; - -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.ObjectMapper; -import models.Student; -import models.StudentStore; -import play.libs.Json; -import play.mvc.Controller; -import play.mvc.Result; -import util.Util; - -import java.util.Set; - -public class StudentController extends Controller { - public Result create() { - JsonNode json = request().body().asJson(); - if (json == null) { - return badRequest(Util.createResponse("Expecting Json data", false)); - } - Student student = StudentStore.getInstance().addStudent(Json.fromJson(json, Student.class)); - JsonNode jsonObject = Json.toJson(student); - return created(Util.createResponse(jsonObject, true)); - } - - public Result update() { - JsonNode json = request().body().asJson(); - if (json == null) { - return badRequest(Util.createResponse("Expecting Json data", false)); - } - Student student = StudentStore.getInstance().updateStudent(Json.fromJson(json, Student.class)); - if (student == null) { - return notFound(Util.createResponse("Student not found", false)); - } - - JsonNode jsonObject = Json.toJson(student); - return ok(Util.createResponse(jsonObject, true)); - } - - public Result retrieve(int id) { - if (StudentStore.getInstance().getStudent(id) == null) { - return notFound(Util.createResponse("Student with id:" + id + " not found", false)); - } - JsonNode jsonObjects = Json.toJson(StudentStore.getInstance().getStudent(id)); - return ok(Util.createResponse(jsonObjects, true)); - } - - public Result listStudents() { - Set result = StudentStore.getInstance().getAllStudents(); - ObjectMapper mapper = new ObjectMapper(); - - JsonNode jsonData = mapper.convertValue(result, JsonNode.class); - return ok(Util.createResponse(jsonData, true)); - - } - - public Result delete(int id) { - if (!StudentStore.getInstance().deleteStudent(id)) { - return notFound(Util.createResponse("Student with id:" + id + " not found", false)); - } - return ok(Util.createResponse("Student with id:" + id + " deleted", true)); - } - -} diff --git a/play-framework/introduction/app/filters/ExampleFilter.java b/play-framework/introduction/app/filters/ExampleFilter.java deleted file mode 100644 index 67a6a36cc3..0000000000 --- a/play-framework/introduction/app/filters/ExampleFilter.java +++ /dev/null @@ -1,45 +0,0 @@ -package filters; - -import akka.stream.Materializer; -import java.util.concurrent.CompletionStage; -import java.util.concurrent.Executor; -import java.util.function.Function; -import javax.inject.*; -import play.mvc.*; -import play.mvc.Http.RequestHeader; - - -/** - * This is a simple filter that adds a header to all requests. It's - * added to the application's list of filters by the - * {@link Filters} class. - */ -@Singleton -public class ExampleFilter extends Filter { - - private final Executor exec; - - /** - * @param mat This object is needed to handle streaming of requests - * and responses. - * @param exec This class is needed to execute code asynchronously. - * It is used below by the thenAsyncApply method. - */ - @Inject - public ExampleFilter(Materializer mat, Executor exec) { - super(mat); - this.exec = exec; - } - - @Override - public CompletionStage apply( - Function> next, - RequestHeader requestHeader) { - - return next.apply(requestHeader).thenApplyAsync( - result -> result.withHeader("X-ExampleFilter", "foo"), - exec - ); - } - -} diff --git a/play-framework/introduction/app/models/StudentStore.java b/play-framework/introduction/app/models/StudentStore.java deleted file mode 100644 index add6a5dbd6..0000000000 --- a/play-framework/introduction/app/models/StudentStore.java +++ /dev/null @@ -1,46 +0,0 @@ -package models; - -import java.util.HashMap; -import java.util.HashSet; -import java.util.Map; -import java.util.Set; - -public class StudentStore { - private static StudentStore instance; - private Map students = new HashMap<>(); - - public static StudentStore getInstance() { - if (instance == null) { - instance = new StudentStore(); - } - return instance; - } - - public Student addStudent(Student student) { - int id = students.size(); - student.setId(id); - students.put(id, student); - return student; - } - - public Student getStudent(int id) { - return students.get(id); - } - - public Set getAllStudents() { - return new HashSet<>(students.values()); - } - - public Student updateStudent(Student student) { - int id = student.getId(); - if (students.containsKey(id)) { - students.put(id, student); - return student; - } - return null; - } - - public boolean deleteStudent(int id) { - return students.remove(id) != null; - } -} \ No newline at end of file diff --git a/play-framework/introduction/app/services/ApplicationTimer.java b/play-framework/introduction/app/services/ApplicationTimer.java deleted file mode 100644 index a951562b1d..0000000000 --- a/play-framework/introduction/app/services/ApplicationTimer.java +++ /dev/null @@ -1,50 +0,0 @@ -package services; - -import java.time.Clock; -import java.time.Instant; -import java.util.concurrent.CompletableFuture; -import javax.inject.*; -import play.Logger; -import play.inject.ApplicationLifecycle; - -/** - * This class demonstrates how to run code when the - * application starts and stops. It starts a timer when the - * application starts. When the application stops it prints out how - * long the application was running for. - * - * This class is registered for Guice dependency injection in the - * {@link Module} class. We want the class to start when the application - * starts, so it is registered as an "eager singleton". See the code - * in the {@link Module} class to see how this happens. - * - * This class needs to run code when the server stops. It uses the - * application's {@link ApplicationLifecycle} to register a stop hook. - */ -@Singleton -public class ApplicationTimer { - - private final Clock clock; - private final ApplicationLifecycle appLifecycle; - private final Instant start; - - @Inject - public ApplicationTimer(Clock clock, ApplicationLifecycle appLifecycle) { - this.clock = clock; - this.appLifecycle = appLifecycle; - // This code is called when the application starts. - start = clock.instant(); - Logger.info("ApplicationTimer demo: Starting application at " + start); - - // When the application starts, register a stop hook with the - // ApplicationLifecycle object. The code inside the stop hook will - // be run when the application stops. - appLifecycle.addStopHook(() -> { - Instant stop = clock.instant(); - Long runningTime = stop.getEpochSecond() - start.getEpochSecond(); - Logger.info("ApplicationTimer demo: Stopping application at " + clock.instant() + " after " + runningTime + "s."); - return CompletableFuture.completedFuture(null); - }); - } - -} diff --git a/play-framework/introduction/app/services/AtomicCounter.java b/play-framework/introduction/app/services/AtomicCounter.java deleted file mode 100644 index 41f741cbf7..0000000000 --- a/play-framework/introduction/app/services/AtomicCounter.java +++ /dev/null @@ -1,26 +0,0 @@ -package services; - -import java.util.concurrent.atomic.AtomicInteger; -import javax.inject.*; - -/** - * This class is a concrete implementation of the {@link Counter} trait. - * It is configured for Guice dependency injection in the {@link Module} - * class. - * - * This class has a {@link Singleton} annotation because we need to make - * sure we only use one counter per application. Without this - * annotation we would get a new instance every time a {@link Counter} is - * injected. - */ -@Singleton -public class AtomicCounter implements Counter { - - private final AtomicInteger atomicCounter = new AtomicInteger(); - - @Override - public int nextCount() { - return atomicCounter.getAndIncrement(); - } - -} diff --git a/play-framework/introduction/app/services/Counter.java b/play-framework/introduction/app/services/Counter.java deleted file mode 100644 index dadad8b09d..0000000000 --- a/play-framework/introduction/app/services/Counter.java +++ /dev/null @@ -1,13 +0,0 @@ -package services; - -/** - * This interface demonstrates how to create a component that is injected - * into a controller. The interface represents a counter that returns a - * incremented number each time it is called. - * - * The {@link Modules} class binds this interface to the - * {@link AtomicCounter} implementation. - */ -public interface Counter { - int nextCount(); -} diff --git a/play-framework/introduction/app/views/index.scala.html b/play-framework/introduction/app/views/index.scala.html index 4539f5a10b..68d37fb1d4 100644 --- a/play-framework/introduction/app/views/index.scala.html +++ b/play-framework/introduction/app/views/index.scala.html @@ -1,20 +1,5 @@ -@* - * This template takes a single argument, a String containing a - * message to display. - *@ -@(message: String) +@() -@* - * Call the `main` template with two arguments. The first - * argument is a `String` with the title of the page, the second - * argument is an `Html` object containing the body of the page. - *@ @main("Welcome to Play") { - - @* - * Get an `Html` object by calling the built-in Play welcome - * template and passing a `String` message. - *@ - @play20.welcome(message, style = "Java") - +

Welcome to Play!

} diff --git a/play-framework/introduction/app/views/main.scala.html b/play-framework/introduction/app/views/main.scala.html index 9414f4be6e..c5f755f236 100644 --- a/play-framework/introduction/app/views/main.scala.html +++ b/play-framework/introduction/app/views/main.scala.html @@ -13,11 +13,12 @@ @title - @* And here's where we render the `Html` object containing * the page content. *@ @content + + diff --git a/play-framework/introduction/bin/activator b/play-framework/introduction/bin/activator deleted file mode 100644 index a8b11d482f..0000000000 --- a/play-framework/introduction/bin/activator +++ /dev/null @@ -1,397 +0,0 @@ -#!/usr/bin/env bash - -### ------------------------------- ### -### Helper methods for BASH scripts ### -### ------------------------------- ### - -realpath () { -( - TARGET_FILE="$1" - FIX_CYGPATH="$2" - - cd "$(dirname "$TARGET_FILE")" - TARGET_FILE=$(basename "$TARGET_FILE") - - COUNT=0 - while [ -L "$TARGET_FILE" -a $COUNT -lt 100 ] - do - TARGET_FILE=$(readlink "$TARGET_FILE") - cd "$(dirname "$TARGET_FILE")" - TARGET_FILE=$(basename "$TARGET_FILE") - COUNT=$(($COUNT + 1)) - done - - # make sure we grab the actual windows path, instead of cygwin's path. - if [[ "x$FIX_CYGPATH" != "x" ]]; then - echo "$(cygwinpath "$(pwd -P)/$TARGET_FILE")" - else - echo "$(pwd -P)/$TARGET_FILE" - fi -) -} - - -# Uses uname to detect if we're in the odd cygwin environment. -is_cygwin() { - local os=$(uname -s) - case "$os" in - CYGWIN*) return 0 ;; - *) return 1 ;; - esac -} - -# TODO - Use nicer bash-isms here. -CYGWIN_FLAG=$(if is_cygwin; then echo true; else echo false; fi) - - -# This can fix cygwin style /cygdrive paths so we get the -# windows style paths. -cygwinpath() { - local file="$1" - if [[ "$CYGWIN_FLAG" == "true" ]]; then - echo $(cygpath -w $file) - else - echo $file - fi -} - -# Make something URI friendly -make_url() { - url="$1" - local nospaces=${url// /%20} - if is_cygwin; then - echo "/${nospaces//\\//}" - else - echo "$nospaces" - fi -} - -declare -a residual_args -declare -a java_args -declare -a scalac_args -declare -a sbt_commands -declare java_cmd=java -declare java_version -declare -r real_script_path="$(realpath "$0")" -declare -r sbt_home="$(realpath "$(dirname "$(dirname "$real_script_path")")")" -declare -r sbt_bin_dir="$(dirname "$real_script_path")" -declare -r app_version="1.3.10" - -declare -r script_name=activator -declare -r java_opts=( "${ACTIVATOR_OPTS[@]}" "${SBT_OPTS[@]}" "${JAVA_OPTS[@]}" "${java_opts[@]}" ) -userhome="$HOME" -if is_cygwin; then - # cygwin sets home to something f-d up, set to real windows homedir - userhome="$USERPROFILE" -fi -declare -r activator_user_home_dir="${userhome}/.activator" -declare -r java_opts_config_home="${activator_user_home_dir}/activatorconfig.txt" -declare -r java_opts_config_version="${activator_user_home_dir}/${app_version}/activatorconfig.txt" - -echoerr () { - echo 1>&2 "$@" -} -vlog () { - [[ $verbose || $debug ]] && echoerr "$@" -} -dlog () { - [[ $debug ]] && echoerr "$@" -} - -jar_file () { - echo "$(cygwinpath "${sbt_home}/libexec/activator-launch-${app_version}.jar")" -} - -acquire_sbt_jar () { - sbt_jar="$(jar_file)" - - if [[ ! -f "$sbt_jar" ]]; then - echoerr "Could not find launcher jar: $sbt_jar" - exit 2 - fi -} - -execRunner () { - # print the arguments one to a line, quoting any containing spaces - [[ $verbose || $debug ]] && echo "# Executing command line:" && { - for arg; do - if printf "%s\n" "$arg" | grep -q ' '; then - printf "\"%s\"\n" "$arg" - else - printf "%s\n" "$arg" - fi - done - echo "" - } - - # THis used to be exec, but we loose the ability to re-hook stty then - # for cygwin... Maybe we should flag the feature here... - "$@" -} - -addJava () { - dlog "[addJava] arg = '$1'" - java_args=( "${java_args[@]}" "$1" ) -} -addSbt () { - dlog "[addSbt] arg = '$1'" - sbt_commands=( "${sbt_commands[@]}" "$1" ) -} -addResidual () { - dlog "[residual] arg = '$1'" - residual_args=( "${residual_args[@]}" "$1" ) -} -addDebugger () { - addJava "-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=$1" -} - -get_mem_opts () { - # if we detect any of these settings in ${JAVA_OPTS} we need to NOT output our settings. - # The reason is the Xms/Xmx, if they don't line up, cause errors. - if [[ "${JAVA_OPTS}" == *-Xmx* ]] || [[ "${JAVA_OPTS}" == *-Xms* ]] || [[ "${JAVA_OPTS}" == *-XX:MaxPermSize* ]] || [[ "${JAVA_OPTS}" == *-XX:MaxMetaspaceSize* ]] || [[ "${JAVA_OPTS}" == *-XX:ReservedCodeCacheSize* ]]; then - echo "" - else - # a ham-fisted attempt to move some memory settings in concert - # so they need not be messed around with individually. - local mem=${1:-1024} - local codecache=$(( $mem / 8 )) - (( $codecache > 128 )) || codecache=128 - (( $codecache < 512 )) || codecache=512 - local class_metadata_size=$(( $codecache * 2 )) - local class_metadata_opt=$([[ "$java_version" < "1.8" ]] && echo "MaxPermSize" || echo "MaxMetaspaceSize") - - echo "-Xms${mem}m -Xmx${mem}m -XX:ReservedCodeCacheSize=${codecache}m -XX:${class_metadata_opt}=${class_metadata_size}m" - fi -} - -require_arg () { - local type="$1" - local opt="$2" - local arg="$3" - if [[ -z "$arg" ]] || [[ "${arg:0:1}" == "-" ]]; then - echo "$opt requires <$type> argument" - exit 1 - fi -} - -is_function_defined() { - declare -f "$1" > /dev/null -} - -# If we're *not* running in a terminal, and we don't have any arguments, then we need to add the 'ui' parameter -detect_terminal_for_ui() { - [[ ! -t 0 ]] && [[ "${#residual_args}" == "0" ]] && { - addResidual "ui" - } - # SPECIAL TEST FOR MAC - [[ "$(uname)" == "Darwin" ]] && [[ "$HOME" == "$PWD" ]] && [[ "${#residual_args}" == "0" ]] && { - echo "Detected MAC OSX launched script...." - echo "Swapping to UI" - addResidual "ui" - } -} - -process_args () { - while [[ $# -gt 0 ]]; do - case "$1" in - -h|-help) usage; exit 1 ;; - -v|-verbose) verbose=1 && shift ;; - -d|-debug) debug=1 && shift ;; - - -ivy) require_arg path "$1" "$2" && addJava "-Dsbt.ivy.home=$2" && shift 2 ;; - -mem) require_arg integer "$1" "$2" && sbt_mem="$2" && shift 2 ;; - -jvm-debug) require_arg port "$1" "$2" && addDebugger $2 && shift 2 ;; - -batch) exec &1 | awk -F '"' '/version/ {print $2}') - vlog "[process_args] java_version = '$java_version'" -} - -# Detect that we have java installed. -checkJava() { - local required_version="$1" - # Now check to see if it's a good enough version - if [[ "$java_version" == "" ]]; then - echo - echo No java installations was detected. - echo Please go to http://www.java.com/getjava/ and download - echo - exit 1 - elif [[ ! "$java_version" > "$required_version" ]]; then - echo - echo The java installation you have is not up to date - echo $script_name requires at least version $required_version+, you have - echo version $java_version - echo - echo Please go to http://www.java.com/getjava/ and download - echo a valid Java Runtime and install before running $script_name. - echo - exit 1 - fi -} - - -run() { - # no jar? download it. - [[ -f "$sbt_jar" ]] || acquire_sbt_jar "$sbt_version" || { - # still no jar? uh-oh. - echo "Download failed. Obtain the sbt-launch.jar manually and place it at $sbt_jar" - exit 1 - } - - # process the combined args, then reset "$@" to the residuals - process_args "$@" - detect_terminal_for_ui - set -- "${residual_args[@]}" - argumentCount=$# - - # TODO - java check should be configurable... - checkJava "1.6" - - #If we're in cygwin, we should use the windows config, and terminal hacks - if [[ "$CYGWIN_FLAG" == "true" ]]; then - stty -icanon min 1 -echo > /dev/null 2>&1 - addJava "-Djline.terminal=jline.UnixTerminal" - addJava "-Dsbt.cygwin=true" - fi - - # run sbt - execRunner "$java_cmd" \ - "-Dactivator.home=$(make_url "$sbt_home")" \ - ${SBT_OPTS:-$default_sbt_opts} \ - $(get_mem_opts $sbt_mem) \ - ${JAVA_OPTS} \ - ${java_args[@]} \ - -jar "$sbt_jar" \ - "${sbt_commands[@]}" \ - "${residual_args[@]}" - - exit_code=$? - - # Clean up the terminal from cygwin hacks. - if [[ "$CYGWIN_FLAG" == "true" ]]; then - stty icanon echo > /dev/null 2>&1 - fi - exit $exit_code -} - - -declare -r noshare_opts="-Dsbt.global.base=project/.sbtboot -Dsbt.boot.directory=project/.boot -Dsbt.ivy.home=project/.ivy" -declare -r sbt_opts_file=".sbtopts" -declare -r etc_sbt_opts_file="${sbt_home}/conf/sbtopts" -declare -r win_sbt_opts_file="${sbt_home}/conf/sbtconfig.txt" - -usage() { - cat < path to global settings/plugins directory (default: ~/.sbt) - -sbt-boot path to shared boot directory (default: ~/.sbt/boot in 0.11 series) - -ivy path to local Ivy repository (default: ~/.ivy2) - -mem set memory options (default: $sbt_mem, which is $(get_mem_opts $sbt_mem)) - -no-share use all local caches; no sharing - -no-global uses global caches, but does not use global ~/.sbt directory. - -jvm-debug Turn on JVM debugging, open at the given port. - -batch Disable interactive mode - - # sbt version (default: from project/build.properties if present, else latest release) - -sbt-version use the specified version of sbt - -sbt-jar use the specified jar as the sbt launcher - -sbt-rc use an RC version of sbt - -sbt-snapshot use a snapshot version of sbt - - # java version (default: java from PATH, currently $(java -version 2>&1 | grep version)) - -java-home alternate JAVA_HOME - - # jvm options and output control - JAVA_OPTS environment variable, if unset uses "$java_opts" - SBT_OPTS environment variable, if unset uses "$default_sbt_opts" - ACTIVATOR_OPTS Environment variable, if unset uses "" - .sbtopts if this file exists in the current directory, it is - prepended to the runner args - /etc/sbt/sbtopts if this file exists, it is prepended to the runner args - -Dkey=val pass -Dkey=val directly to the java runtime - -J-X pass option -X directly to the java runtime - (-J is stripped) - -S-X add -X to sbt's scalacOptions (-S is stripped) - -In the case of duplicated or conflicting options, the order above -shows precedence: JAVA_OPTS lowest, command line options highest. -EOM -} - - - -process_my_args () { - while [[ $# -gt 0 ]]; do - case "$1" in - -no-colors) addJava "-Dsbt.log.noformat=true" && shift ;; - -no-share) addJava "$noshare_opts" && shift ;; - -no-global) addJava "-Dsbt.global.base=$(pwd)/project/.sbtboot" && shift ;; - -sbt-boot) require_arg path "$1" "$2" && addJava "-Dsbt.boot.directory=$2" && shift 2 ;; - -sbt-dir) require_arg path "$1" "$2" && addJava "-Dsbt.global.base=$2" && shift 2 ;; - -debug-inc) addJava "-Dxsbt.inc.debug=true" && shift ;; - -batch) exec ^&1') do ( - if %%~j==java set JAVAINSTALLED=1 - if %%~j==openjdk set JAVAINSTALLED=1 -) - -rem Detect the same thing about javac -if "%_JAVACCMD%"=="" ( - if not "%JAVA_HOME%"=="" ( - if exist "%JAVA_HOME%\bin\javac.exe" set "_JAVACCMD=%JAVA_HOME%\bin\javac.exe" - ) -) -if "%_JAVACCMD%"=="" set _JAVACCMD=javac -for /F %%j in ('"%_JAVACCMD%" -version 2^>^&1') do ( - if %%~j==javac set JAVACINSTALLED=1 -) - -rem BAT has no logical or, so we do it OLD SCHOOL! Oppan Redmond Style -set JAVAOK=true -if not defined JAVAINSTALLED set JAVAOK=false -if not defined JAVACINSTALLED set JAVAOK=false - -if "%JAVAOK%"=="false" ( - echo. - echo A Java JDK is not installed or can't be found. - if not "%JAVA_HOME%"=="" ( - echo JAVA_HOME = "%JAVA_HOME%" - ) - echo. - echo Please go to - echo http://www.oracle.com/technetwork/java/javase/downloads/index.html - echo and download a valid Java JDK and install before running Activator. - echo. - echo If you think this message is in error, please check - echo your environment variables to see if "java.exe" and "javac.exe" are - echo available via JAVA_HOME or PATH. - echo. - if defined DOUBLECLICKED pause - exit /B 1 -) - -rem Check what Java version is being used to determine what memory options to use -for /f "tokens=3" %%g in ('java -version 2^>^&1 ^| findstr /i "version"') do ( - set JAVA_VERSION=%%g -) - -rem Strips away the " characters -set JAVA_VERSION=%JAVA_VERSION:"=% - -rem TODO Check if there are existing mem settings in JAVA_OPTS/CFG_OPTS and use those instead of the below -for /f "delims=. tokens=1-3" %%v in ("%JAVA_VERSION%") do ( - set MAJOR=%%v - set MINOR=%%w - set BUILD=%%x - - set META_SIZE=-XX:MetaspaceSize=64M -XX:MaxMetaspaceSize=256M - if "!MINOR!" LSS "8" ( - set META_SIZE=-XX:PermSize=64M -XX:MaxPermSize=256M - ) - - set MEM_OPTS=!META_SIZE! - ) - -rem We use the value of the JAVA_OPTS environment variable if defined, rather than the config. -set _JAVA_OPTS=%JAVA_OPTS% -if "%_JAVA_OPTS%"=="" set _JAVA_OPTS=%CFG_OPTS% - -set DEBUG_OPTS= - -rem Loop through the arguments, building remaining args in args variable -set args= -:argsloop -if not "%~1"=="" ( - rem Checks if the argument contains "-D" and if true, adds argument 1 with 2 and puts an equal sign between them. - rem This is done since batch considers "=" to be a delimiter so we need to circumvent this behavior with a small hack. - set arg1=%~1 - if "!arg1:~0,2!"=="-D" ( - set "args=%args% "%~1"="%~2"" - shift - shift - goto argsloop - ) - - if "%~1"=="-jvm-debug" ( - if not "%~2"=="" ( - rem This piece of magic somehow checks that an argument is a number - for /F "delims=0123456789" %%i in ("%~2") do ( - set var="%%i" - ) - if defined var ( - rem Not a number, assume no argument given and default to 9999 - set JPDA_PORT=9999 - ) else ( - rem Port was given, shift arguments - set JPDA_PORT=%~2 - shift - ) - ) else ( - set JPDA_PORT=9999 - ) - shift - - set DEBUG_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=!JPDA_PORT! - goto argsloop - ) - rem else - set "args=%args% "%~1"" - shift - goto argsloop -) - -:run - -if "!args!"=="" ( - if defined DOUBLECLICKED ( - set CMDS="ui" - ) else set CMDS=!args! -) else set CMDS=!args! - -rem We add a / in front, so we get file:///C: instead of file://C: -rem Java considers the later a UNC path. -rem We also attempt a solid effort at making it URI friendly. -rem We don't even bother with UNC paths. -set JAVA_FRIENDLY_HOME_1=/!ACTIVATOR_HOME:\=/! -set JAVA_FRIENDLY_HOME=/!JAVA_FRIENDLY_HOME_1: =%%20! - -rem Checks if the command contains spaces to know if it should be wrapped in quotes or not -set NON_SPACED_CMD=%_JAVACMD: =% -if "%_JAVACMD%"=="%NON_SPACED_CMD%" %_JAVACMD% %DEBUG_OPTS% %MEM_OPTS% %ACTIVATOR_OPTS% %SBT_OPTS% %_JAVA_OPTS% "-Dactivator.home=%JAVA_FRIENDLY_HOME%" -jar "%ACTIVATOR_HOME%\libexec\%ACTIVATOR_LAUNCH_JAR%" %CMDS% -if NOT "%_JAVACMD%"=="%NON_SPACED_CMD%" "%_JAVACMD%" %DEBUG_OPTS% %MEM_OPTS% %ACTIVATOR_OPTS% %SBT_OPTS% %_JAVA_OPTS% "-Dactivator.home=%JAVA_FRIENDLY_HOME%" -jar "%ACTIVATOR_HOME%\libexec\%ACTIVATOR_LAUNCH_JAR%" %CMDS% - -if ERRORLEVEL 1 goto error -goto end - -:error -set ERROR_CODE=1 - -:end - -@endlocal - -exit /B %ERROR_CODE% diff --git a/play-framework/introduction/build.sbt b/play-framework/introduction/build.sbt index 0f5ea736f6..5b05db6719 100644 --- a/play-framework/introduction/build.sbt +++ b/play-framework/introduction/build.sbt @@ -1,13 +1,10 @@ -name := """student-api""" +name := """introduction""" +organization := "com.baeldung" version := "1.0-SNAPSHOT" lazy val root = (project in file(".")).enablePlugins(PlayJava) -scalaVersion := "2.11.7" +scalaVersion := "2.13.0" -libraryDependencies ++= Seq( - javaJdbc, - cache, - javaWs -) +libraryDependencies += guice diff --git a/play-framework/introduction/conf/application.conf b/play-framework/introduction/conf/application.conf index 489d3f9b3e..85c184dcb1 100644 --- a/play-framework/introduction/conf/application.conf +++ b/play-framework/introduction/conf/application.conf @@ -1,353 +1,2 @@ # This is the main configuration file for the application. # https://www.playframework.com/documentation/latest/ConfigFile -# ~~~~~ -# Play uses HOCON as its configuration file format. HOCON has a number -# of advantages over other config formats, but there are two things that -# can be used when modifying settings. -# -# You can include other configuration files in this main application.conf file: -#include "extra-config.conf" -# -# You can declare variables and substitute for them: -#mykey = ${some.value} -# -# And if an environment variable exists when there is no other subsitution, then -# HOCON will fall back to substituting environment variable: -#mykey = ${JAVA_HOME} - -## Akka -# https://www.playframework.com/documentation/latest/ScalaAkka#Configuration -# https://www.playframework.com/documentation/latest/JavaAkka#Configuration -# ~~~~~ -# Play uses Akka internally and exposes Akka Streams and actors in Websockets and -# other streaming HTTP responses. -akka { - # "akka.log-config-on-start" is extraordinarly useful because it log the complete - # configuration at INFO level, including defaults and overrides, so it s worth - # putting at the very top. - # - # Put the following in your conf/logback.xml file: - # - # - # - # And then uncomment this line to debug the configuration. - # - #log-config-on-start = true -} - -## Secret key -# http://www.playframework.com/documentation/latest/ApplicationSecret -# ~~~~~ -# The secret key is used to sign Play's session cookie. -# This must be changed for production, but we don't recommend you change it in this file. -play.crypto.secret = "changeme" - -## Modules -# https://www.playframework.com/documentation/latest/Modules -# ~~~~~ -# Control which modules are loaded when Play starts. Note that modules are -# the replacement for "GlobalSettings", which are deprecated in 2.5.x. -# Please see https://www.playframework.com/documentation/latest/GlobalSettings -# for more information. -# -# You can also extend Play functionality by using one of the publically available -# Play modules: https://playframework.com/documentation/latest/ModuleDirectory -play.modules { - # By default, Play will load any class called Module that is defined - # in the root package (the "app" directory), or you can define them - # explicitly below. - # If there are any built-in modules that you want to disable, you can list them here. - #enabled += my.application.Module - - # If there are any built-in modules that you want to disable, you can list them here. - #disabled += "" -} - -## IDE -# https://www.playframework.com/documentation/latest/IDE -# ~~~~~ -# Depending on your IDE, you can add a hyperlink for errors that will jump you -# directly to the code location in the IDE in dev mode. The following line makes -# use of the IntelliJ IDEA REST interface: -#play.editor="http://localhost:63342/api/file/?file=%s&line=%s" - -## Internationalisation -# https://www.playframework.com/documentation/latest/JavaI18N -# https://www.playframework.com/documentation/latest/ScalaI18N -# ~~~~~ -# Play comes with its own i18n settings, which allow the user's preferred language -# to map through to internal messages, or allow the language to be stored in a cookie. -play.i18n { - # The application languages - langs = [ "en" ] - - # Whether the language cookie should be secure or not - #langCookieSecure = true - - # Whether the HTTP only attribute of the cookie should be set to true - #langCookieHttpOnly = true -} - -## Play HTTP settings -# ~~~~~ -play.http { - ## Router - # https://www.playframework.com/documentation/latest/JavaRouting - # https://www.playframework.com/documentation/latest/ScalaRouting - # ~~~~~ - # Define the Router object to use for this application. - # This router will be looked up first when the application is starting up, - # so make sure this is the entry point. - # Furthermore, it's assumed your route file is named properly. - # So for an application router like `my.application.Router`, - # you may need to define a router file `conf/my.application.routes`. - # Default to Routes in the root package (aka "apps" folder) (and conf/routes) - #router = my.application.Router - - ## Action Creator - # https://www.playframework.com/documentation/latest/JavaActionCreator - # ~~~~~ - #actionCreator = null - - ## ErrorHandler - # https://www.playframework.com/documentation/latest/JavaRouting - # https://www.playframework.com/documentation/latest/ScalaRouting - # ~~~~~ - # If null, will attempt to load a class called ErrorHandler in the root package, - #errorHandler = null - - ## Filters - # https://www.playframework.com/documentation/latest/ScalaHttpFilters - # https://www.playframework.com/documentation/latest/JavaHttpFilters - # ~~~~~ - # Filters run code on every request. They can be used to perform - # common logic for all your actions, e.g. adding common headers. - # Defaults to "Filters" in the root package (aka "apps" folder) - # Alternatively you can explicitly register a class here. - #filters = my.application.Filters - - ## Session & Flash - # https://www.playframework.com/documentation/latest/JavaSessionFlash - # https://www.playframework.com/documentation/latest/ScalaSessionFlash - # ~~~~~ - session { - # Sets the cookie to be sent only over HTTPS. - #secure = true - - # Sets the cookie to be accessed only by the server. - #httpOnly = true - - # Sets the max-age field of the cookie to 5 minutes. - # NOTE: this only sets when the browser will discard the cookie. Play will consider any - # cookie value with a valid signature to be a valid session forever. To implement a server side session timeout, - # you need to put a timestamp in the session and check it at regular intervals to possibly expire it. - #maxAge = 300 - - # Sets the domain on the session cookie. - #domain = "example.com" - } - - flash { - # Sets the cookie to be sent only over HTTPS. - #secure = true - - # Sets the cookie to be accessed only by the server. - #httpOnly = true - } -} - -## Netty Provider -# https://www.playframework.com/documentation/latest/SettingsNetty -# ~~~~~ -play.server.netty { - # Whether the Netty wire should be logged - #log.wire = true - - # If you run Play on Linux, you can use Netty's native socket transport - # for higher performance with less garbage. - #transport = "native" -} - -## WS (HTTP Client) -# https://www.playframework.com/documentation/latest/ScalaWS#Configuring-WS -# ~~~~~ -# The HTTP client primarily used for REST APIs. The default client can be -# configured directly, but you can also create different client instances -# with customized settings. You must enable this by adding to build.sbt: -# -# libraryDependencies += ws // or javaWs if using java -# -play.ws { - # Sets HTTP requests not to follow 302 requests - #followRedirects = false - - # Sets the maximum number of open HTTP connections for the client. - #ahc.maxConnectionsTotal = 50 - - ## WS SSL - # https://www.playframework.com/documentation/latest/WsSSL - # ~~~~~ - ssl { - # Configuring HTTPS with Play WS does not require programming. You can - # set up both trustManager and keyManager for mutual authentication, and - # turn on JSSE debugging in development with a reload. - #debug.handshake = true - #trustManager = { - # stores = [ - # { type = "JKS", path = "exampletrust.jks" } - # ] - #} - } -} - -## Cache -# https://www.playframework.com/documentation/latest/JavaCache -# https://www.playframework.com/documentation/latest/ScalaCache -# ~~~~~ -# Play comes with an integrated cache API that can reduce the operational -# overhead of repeated requests. You must enable this by adding to build.sbt: -# -# libraryDependencies += cache -# -play.cache { - # If you want to bind several caches, you can bind the individually - #bindCaches = ["db-cache", "user-cache", "session-cache"] -} - -## Filters -# https://www.playframework.com/documentation/latest/Filters -# ~~~~~ -# There are a number of built-in filters that can be enabled and configured -# to give Play greater security. You must enable this by adding to build.sbt: -# -# libraryDependencies += filters -# -play.filters { - ## CORS filter configuration - # https://www.playframework.com/documentation/latest/CorsFilter - # ~~~~~ - # CORS is a protocol that allows web applications to make requests from the browser - # across different domains. - # NOTE: You MUST apply the CORS configuration before the CSRF filter, as CSRF has - # dependencies on CORS settings. - cors { - # Filter paths by a whitelist of path prefixes - #pathPrefixes = ["/some/path", ...] - - # The allowed origins. If null, all origins are allowed. - #allowedOrigins = ["http://www.example.com"] - - # The allowed HTTP methods. If null, all methods are allowed - #allowedHttpMethods = ["GET", "POST"] - } - - ## CSRF Filter - # https://www.playframework.com/documentation/latest/ScalaCsrf#Applying-a-global-CSRF-filter - # https://www.playframework.com/documentation/latest/JavaCsrf#Applying-a-global-CSRF-filter - # ~~~~~ - # Play supports multiple methods for verifying that a request is not a CSRF request. - # The primary mechanism is a CSRF token. This token gets placed either in the query string - # or body of every form submitted, and also gets placed in the users session. - # Play then verifies that both tokens are present and match. - csrf { - # Sets the cookie to be sent only over HTTPS - #cookie.secure = true - - # Defaults to CSRFErrorHandler in the root package. - #errorHandler = MyCSRFErrorHandler - } - - ## Security headers filter configuration - # https://www.playframework.com/documentation/latest/SecurityHeaders - # ~~~~~ - # Defines security headers that prevent XSS attacks. - # If enabled, then all options are set to the below configuration by default: - headers { - # The X-Frame-Options header. If null, the header is not set. - #frameOptions = "DENY" - - # The X-XSS-Protection header. If null, the header is not set. - #xssProtection = "1; mode=block" - - # The X-Content-Type-Options header. If null, the header is not set. - #contentTypeOptions = "nosniff" - - # The X-Permitted-Cross-Domain-Policies header. If null, the header is not set. - #permittedCrossDomainPolicies = "master-only" - - # The Content-Security-Policy header. If null, the header is not set. - #contentSecurityPolicy = "default-src 'self'" - } - - ## Allowed hosts filter configuration - # https://www.playframework.com/documentation/latest/AllowedHostsFilter - # ~~~~~ - # Play provides a filter that lets you configure which hosts can access your application. - # This is useful to prevent cache poisoning attacks. - hosts { - # Allow requests to example.com, its subdomains, and localhost:9000. - #allowed = [".example.com", "localhost:9000"] - } -} - -## Evolutions -# https://www.playframework.com/documentation/latest/Evolutions -# ~~~~~ -# Evolutions allows database scripts to be automatically run on startup in dev mode -# for database migrations. You must enable this by adding to build.sbt: -# -# libraryDependencies += evolutions -# -play.evolutions { - # You can disable evolutions for a specific datasource if necessary - #db.default.enabled = false -} - -## Database Connection Pool -# https://www.playframework.com/documentation/latest/SettingsJDBC -# ~~~~~ -# Play doesn't require a JDBC database to run, but you can easily enable one. -# -# libraryDependencies += jdbc -# -play.db { - # The combination of these two settings results in "db.default" as the - # default JDBC pool: - #config = "db" - #default = "default" - - # Play uses HikariCP as the default connection pool. You can override - # settings by changing the prototype: - prototype { - # Sets a fixed JDBC connection pool size of 50 - #hikaricp.minimumIdle = 50 - #hikaricp.maximumPoolSize = 50 - } -} - -## JDBC Datasource -# https://www.playframework.com/documentation/latest/JavaDatabase -# https://www.playframework.com/documentation/latest/ScalaDatabase -# ~~~~~ -# Once JDBC datasource is set up, you can work with several different -# database options: -# -# Slick (Scala preferred option): https://www.playframework.com/documentation/latest/PlaySlick -# JPA (Java preferred option): https://playframework.com/documentation/latest/JavaJPA -# EBean: https://playframework.com/documentation/latest/JavaEbean -# Anorm: https://www.playframework.com/documentation/latest/ScalaAnorm -# -db { - # You can declare as many datasources as you want. - # By convention, the default datasource is named `default` - - # https://www.playframework.com/documentation/latest/Developing-with-the-H2-Database - #default.driver = org.h2.Driver - #default.url = "jdbc:h2:mem:play" - #default.username = sa - #default.password = "" - - # You can turn on SQL logging for any datasource - # https://www.playframework.com/documentation/latest/Highlights25#Logging-SQL-statements - #default.logSql=true -} diff --git a/play-framework/introduction/conf/logback.xml b/play-framework/introduction/conf/logback.xml index 86ec12c0af..e8c982543f 100644 --- a/play-framework/introduction/conf/logback.xml +++ b/play-framework/introduction/conf/logback.xml @@ -27,12 +27,6 @@ - - - - - - diff --git a/play-framework/introduction/conf/routes b/play-framework/introduction/conf/routes index ab55792683..e26dc10383 100644 --- a/play-framework/introduction/conf/routes +++ b/play-framework/introduction/conf/routes @@ -2,11 +2,14 @@ # This file defines all application routes (Higher priority routes first) # ~~~~ -GET / controllers.StudentController.listStudents() -POST /:id controllers.StudentController.retrieve(id:Int) -POST / controllers.StudentController.create() -PUT / controllers.StudentController.update() -DELETE /:id controllers.StudentController.delete(id:Int) +# An example controller showing a sample home page +GET / controllers.HomeController.index +GET /baeldung/html controllers.HomeController.applyHtml +GET /baeldung/bad-req controllers.HomeController.badRequestPage +GET /baeldung/not-found controllers.HomeController.notFoundPage +GET /baeldung/custom-content-type controllers.HomeController.customContentType +GET /baeldung/async controllers.HomeController.asyncOperation +GET /baeldung/headers controllers.HomeController.setHeaders # Map static resources from the /public folder to the /assets URL path -GET /assets/*file controllers.Assets.versioned(path="/public", file: Asset) +GET /assets/*file controllers.Assets.versioned(path="/public", file: Asset) diff --git a/play-framework/introduction/libexec/activator-launch-1.3.10.jar b/play-framework/introduction/libexec/activator-launch-1.3.10.jar deleted file mode 100644 index 69050e7dec..0000000000 Binary files a/play-framework/introduction/libexec/activator-launch-1.3.10.jar and /dev/null differ diff --git a/play-framework/introduction/project/build.properties b/play-framework/introduction/project/build.properties index 6d9fa6badb..c0bab04941 100644 --- a/play-framework/introduction/project/build.properties +++ b/play-framework/introduction/project/build.properties @@ -1,4 +1 @@ -#Activator-generated Properties -#Wed Sep 07 12:29:40 EAT 2016 -template.uuid=c373963b-f5ad-433b-8e74-178e7ae25b1c -sbt.version=0.13.11 +sbt.version=1.2.8 diff --git a/play-framework/introduction/project/plugins.sbt b/play-framework/introduction/project/plugins.sbt index 51c5b2a35a..1c8c62a0d5 100644 --- a/play-framework/introduction/project/plugins.sbt +++ b/play-framework/introduction/project/plugins.sbt @@ -1,21 +1,7 @@ // The Play plugin -addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.5.6") +addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.7.3") -// Web plugins -addSbtPlugin("com.typesafe.sbt" % "sbt-coffeescript" % "1.0.0") -addSbtPlugin("com.typesafe.sbt" % "sbt-less" % "1.1.0") -addSbtPlugin("com.typesafe.sbt" % "sbt-jshint" % "1.0.3") -addSbtPlugin("com.typesafe.sbt" % "sbt-rjs" % "1.0.7") -addSbtPlugin("com.typesafe.sbt" % "sbt-digest" % "1.1.0") -addSbtPlugin("com.typesafe.sbt" % "sbt-mocha" % "1.1.0") -addSbtPlugin("org.irundaia.sbt" % "sbt-sassify" % "1.4.2") - -// Play enhancer - this automatically generates getters/setters for public fields -// and rewrites accessors of these fields to use the getters/setters. Remove this -// plugin if you prefer not to have this feature, or disable on a per project -// basis using disablePlugins(PlayEnhancer) in your build.sbt -addSbtPlugin("com.typesafe.sbt" % "sbt-play-enhancer" % "1.1.0") - -// Play Ebean support, to enable, uncomment this line, and enable in your build.sbt using -// enablePlugins(PlayEbean). -// addSbtPlugin("com.typesafe.sbt" % "sbt-play-ebean" % "1.0.0") +// Defines scaffolding (found under .g8 folder) +// http://www.foundweekends.org/giter8/scaffolding.html +// sbt "g8Scaffold form" +addSbtPlugin("org.foundweekends.giter8" % "sbt-giter8-scaffold" % "0.11.0") diff --git a/play-framework/introduction/public/javascripts/hello.js b/play-framework/introduction/public/javascripts/hello.js deleted file mode 100644 index 02ee13c7ca..0000000000 --- a/play-framework/introduction/public/javascripts/hello.js +++ /dev/null @@ -1,3 +0,0 @@ -if (window.console) { - console.log("Welcome to your Play application's JavaScript!"); -} diff --git a/play-framework/introduction/public/javascripts/main.js b/play-framework/introduction/public/javascripts/main.js new file mode 100644 index 0000000000..e69de29bb2 diff --git a/play-framework/introduction/test/ApplicationLiveTest.java b/play-framework/introduction/test/ApplicationLiveTest.java deleted file mode 100644 index beeef1a602..0000000000 --- a/play-framework/introduction/test/ApplicationLiveTest.java +++ /dev/null @@ -1,172 +0,0 @@ - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - -import java.io.BufferedReader; -import java.io.DataOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.net.HttpURLConnection; -import java.net.MalformedURLException; -import java.net.URL; -import java.util.Arrays; - -import org.json.JSONArray; -import org.json.JSONObject; -import org.junit.Before; -import org.junit.Test; -import model.Student; -import play.test.*; -import static play.test.Helpers.*; - -public class ApplicationLiveTest{ - private static final String BASE_URL = "http://localhost:9000"; - - @Test -public void testInServer() throws Exception { - TestServer server = testServer(3333); - running(server, () -> { - try { - WSClient ws = play.libs.ws.WS.newClient(3333); - CompletionStage completionStage = ws.url("/").get(); - WSResponse response = completionStage.toCompletableFuture().get(); - ws.close(); - assertEquals(OK, response.getStatus()); - } catch (Exception e) { - logger.error(e.getMessage(), e); - } - }); -} - @Test - public void whenCreatesRecord_thenCorrect() { - Student student = new Student("jody", "west", 50); - JSONObject obj = new JSONObject(makeRequest(BASE_URL, "POST", new JSONObject(student))); - assertTrue(obj.getBoolean("isSuccessfull")); - JSONObject body = obj.getJSONObject("body"); - assertEquals(student.getAge(), body.getInt("age")); - assertEquals(student.getFirstName(), body.getString("firstName")); - assertEquals(student.getLastName(), body.getString("lastName")); - } - - @Test - public void whenDeletesCreatedRecord_thenCorrect() { - Student student = new Student("Usain", "Bolt", 25); - JSONObject ob1 = new JSONObject(makeRequest(BASE_URL, "POST", new JSONObject(student))).getJSONObject("body"); - int id = ob1.getInt("id"); - JSONObject obj1 = new JSONObject(makeRequest(BASE_URL + "/" + id, "POST", new JSONObject())); - assertTrue(obj1.getBoolean("isSuccessfull")); - makeRequest(BASE_URL + "/" + id, "DELETE", null); - JSONObject obj2 = new JSONObject(makeRequest(BASE_URL + "/" + id, "POST", new JSONObject())); - assertFalse(obj2.getBoolean("isSuccessfull")); - } - - @Test - public void whenUpdatesCreatedRecord_thenCorrect() { - Student student = new Student("john", "doe", 50); - JSONObject body1 = new JSONObject(makeRequest(BASE_URL, "POST", new JSONObject(student))).getJSONObject("body"); - assertEquals(student.getAge(), body1.getInt("age")); - int newAge = 60; - body1.put("age", newAge); - JSONObject body2 = new JSONObject(makeRequest(BASE_URL, "PUT", body1)).getJSONObject("body"); - assertFalse(student.getAge() == body2.getInt("age")); - assertTrue(newAge == body2.getInt("age")); - } - - @Test - public void whenGetsAllRecords_thenCorrect() { - Student student1 = new Student("jane", "daisy", 50); - Student student2 = new Student("john", "daniel", 60); - Student student3 = new Student("don", "mason", 55); - Student student4 = new Student("scarlet", "ohara", 90); - - makeRequest(BASE_URL, "POST", new JSONObject(student1)); - makeRequest(BASE_URL, "POST", new JSONObject(student2)); - makeRequest(BASE_URL, "POST", new JSONObject(student3)); - makeRequest(BASE_URL, "POST", new JSONObject(student4)); - - JSONObject objects = new JSONObject(makeRequest(BASE_URL, "GET", null)); - assertTrue(objects.getBoolean("isSuccessfull")); - JSONArray array = objects.getJSONArray("body"); - assertTrue(array.length() >= 4); - } - - public static String makeRequest(String myUrl, String httpMethod, JSONObject parameters) { - - URL url = null; - try { - url = new URL(myUrl); - } catch (MalformedURLException e) { - e.printStackTrace(); - } - HttpURLConnection conn = null; - try { - - conn = (HttpURLConnection) url.openConnection(); - } catch (IOException e) { - e.printStackTrace(); - } - conn.setDoInput(true); - - conn.setReadTimeout(10000); - - conn.setRequestProperty("Content-Type", "application/json"); - DataOutputStream dos = null; - int respCode = 0; - String inputString = null; - try { - conn.setRequestMethod(httpMethod); - - if (Arrays.asList("POST", "PUT").contains(httpMethod)) { - String params = parameters.toString(); - - conn.setDoOutput(true); - - dos = new DataOutputStream(conn.getOutputStream()); - dos.writeBytes(params); - dos.flush(); - dos.close(); - } - respCode = conn.getResponseCode(); - if (respCode != 200 && respCode != 201) { - String error = inputStreamToString(conn.getErrorStream()); - return error; - } - inputString = inputStreamToString(conn.getInputStream()); - - } catch (IOException e) { - - e.printStackTrace(); - } - return inputString; - } - - public static String inputStreamToString(InputStream is) { - BufferedReader br = null; - StringBuilder sb = new StringBuilder(); - - String line; - try { - - br = new BufferedReader(new InputStreamReader(is)); - while ((line = br.readLine()) != null) { - sb.append(line); - } - - } catch (IOException e) { - e.printStackTrace(); - } finally { - if (br != null) { - try { - br.close(); - } catch (IOException e) { - e.printStackTrace(); - } - } - } - - return sb.toString(); - - } -} diff --git a/play-framework/introduction/test/controllers/HomeControllerUnitTest.java b/play-framework/introduction/test/controllers/HomeControllerUnitTest.java new file mode 100644 index 0000000000..42a2e871f0 --- /dev/null +++ b/play-framework/introduction/test/controllers/HomeControllerUnitTest.java @@ -0,0 +1,97 @@ +package controllers; + +import org.junit.Test; +import play.Application; +import play.inject.guice.GuiceApplicationBuilder; +import play.mvc.Http; +import play.mvc.Result; +import play.test.WithApplication; + +import java.util.Optional; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static play.mvc.Http.Status.*; +import static play.test.Helpers.GET; +import static play.test.Helpers.route; + +public class HomeControllerUnitTest extends WithApplication { + + @Override + protected Application provideApplication() { + return new GuiceApplicationBuilder().build(); + } + + @Test + public void givenRequest_whenRootPath_ThenStatusOkay() { + Http.RequestBuilder request = new Http.RequestBuilder() + .method(GET) + .uri("/"); + + Result result = route(app, request); + assertEquals(OK, result.status()); + } + + @Test + public void givenRequest_whenHtmlPath_ThenStatusOkay() { + Http.RequestBuilder request = new Http.RequestBuilder() + .method(GET) + .uri("/baeldung/html"); + + Result result = route(app, request); + assertEquals(OK, result.status()); + } + + @Test + public void givenRequest_whenBadRequest_ThenStatusBadRequest() { + Http.RequestBuilder request = new Http.RequestBuilder() + .method(GET) + .uri("/baeldung/bad-req"); + + Result result = route(app, request); + assertEquals(BAD_REQUEST, result.status()); + } + + @Test + public void givenRequest_whenNotFound_ThenStatusNotFound() { + Http.RequestBuilder request = new Http.RequestBuilder() + .method(GET) + .uri("/baeldung/not-found"); + + Result result = route(app, request); + assertEquals(NOT_FOUND, result.status()); + } + + @Test + public void givenRequest_whenCustomContentTypePath_ThenContextTypeTextHtml() { + Http.RequestBuilder request = new Http.RequestBuilder() + .method(GET) + .uri("/baeldung/custom-content-type"); + + Result result = route(app, request); + assertTrue(result.contentType().isPresent()); + assertEquals("text/html", result.contentType().get()); + } + + @Test + public void givenRequest_whenAsyncPath_ThenStatusOkay() { + Http.RequestBuilder request = new Http.RequestBuilder() + .method(GET) + .uri("/baeldung/async"); + + Result result = route(app, request); + assertEquals(OK, result.status()); + } + + @Test + public void givenRequest_whenHeadersPath_ThenCustomHeaderFieldSet() { + Http.RequestBuilder request = new Http.RequestBuilder() + .method(GET) + .uri("/baeldung/headers"); + + Result result = route(app, request); + final Optional headerOptional = result.header("Header-Key"); + assertTrue(headerOptional.isPresent()); + assertEquals("Some value", headerOptional.get()); + } +} diff --git a/play-framework/routing-in-play/LICENSE b/play-framework/routing-in-play/LICENSE deleted file mode 100644 index 4baedcb95f..0000000000 --- a/play-framework/routing-in-play/LICENSE +++ /dev/null @@ -1,8 +0,0 @@ -This software is licensed under the Apache 2 license, quoted below. - -Licensed under the Apache License, Version 2.0 (the "License"); you may not use this project except in compliance with -the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0. - -Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an -"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific -language governing permissions and limitations under the License. \ No newline at end of file diff --git a/play-framework/routing-in-play/README b/play-framework/routing-in-play/README deleted file mode 100644 index f21d092edf..0000000000 --- a/play-framework/routing-in-play/README +++ /dev/null @@ -1,49 +0,0 @@ -This is your new Play application -================================= - -This file will be packaged with your application when using `activator dist`. - -There are several demonstration files available in this template. - -Controllers -=========== - -- HomeController.java: - - Shows how to handle simple HTTP requests. - -- AsyncController.java: - - Shows how to do asynchronous programming when handling a request. - -- CountController.java: - - Shows how to inject a component into a controller and use the component when - handling requests. - -Components -========== - -- Module.java: - - Shows how to use Guice to bind all the components needed by your application. - -- Counter.java: - - An example of a component that contains state, in this case a simple counter. - -- ApplicationTimer.java: - - An example of a component that starts when the application starts and stops - when the application stops. - -Filters -======= - -- Filters.java: - - Creates the list of HTTP filters used by your application. - -- ExampleFilter.java - - A simple filter that adds a header to every response. \ No newline at end of file diff --git a/play-framework/routing-in-play/app/Filters.java b/play-framework/routing-in-play/app/Filters.java deleted file mode 100644 index 255de8ca93..0000000000 --- a/play-framework/routing-in-play/app/Filters.java +++ /dev/null @@ -1,46 +0,0 @@ -import javax.inject.*; -import play.*; -import play.mvc.EssentialFilter; -import play.http.HttpFilters; -import play.mvc.*; - -import filters.ExampleFilter; - -/** - * This class configures filters that run on every request. This - * class is queried by Play to get a list of filters. - * - * Play will automatically use filters from any class called - * Filters that is placed the root package. You can load filters - * from a different class by adding a `play.http.filters` setting to - * the application.conf configuration file. - */ -@Singleton -public class Filters implements HttpFilters { - - private final Environment env; - private final EssentialFilter exampleFilter; - - /** - * @param env Basic environment settings for the current application. - * @param exampleFilter A demonstration filter that adds a header to - */ - @Inject - public Filters(Environment env, ExampleFilter exampleFilter) { - this.env = env; - this.exampleFilter = exampleFilter; - } - - @Override - public EssentialFilter[] filters() { - // Use the example filter if we're running development mode. If - // we're running in production or test mode then don't use any - // filters at all. - if (env.mode().equals(Mode.DEV)) { - return new EssentialFilter[] { exampleFilter }; - } else { - return new EssentialFilter[] {}; - } - } - -} diff --git a/play-framework/routing-in-play/app/Module.java b/play-framework/routing-in-play/app/Module.java deleted file mode 100644 index 6e7d1766ef..0000000000 --- a/play-framework/routing-in-play/app/Module.java +++ /dev/null @@ -1,31 +0,0 @@ -import com.google.inject.AbstractModule; -import java.time.Clock; - -import services.ApplicationTimer; -import services.AtomicCounter; -import services.Counter; - -/** - * This class is a Guice module that tells Guice how to bind several - * different types. This Guice module is created when the Play - * application starts. - * - * Play will automatically use any class called `Module` that is in - * the root package. You can create modules in other locations by - * adding `play.modules.enabled` settings to the `application.conf` - * configuration file. - */ -public class Module extends AbstractModule { - - @Override - public void configure() { - // Use the system clock as the default implementation of Clock - bind(Clock.class).toInstance(Clock.systemDefaultZone()); - // Ask Guice to create an instance of ApplicationTimer when the - // application starts. - bind(ApplicationTimer.class).asEagerSingleton(); - // Set AtomicCounter as the implementation for Counter. - bind(Counter.class).to(AtomicCounter.class); - } - -} diff --git a/play-framework/routing-in-play/app/controllers/HomeController.java b/play-framework/routing-in-play/app/controllers/HomeController.java index 6e340d594f..c7f6daa898 100644 --- a/play-framework/routing-in-play/app/controllers/HomeController.java +++ b/play-framework/routing-in-play/app/controllers/HomeController.java @@ -1,8 +1,13 @@ package controllers; +import play.libs.concurrent.HttpExecutionContext; import play.mvc.*; +import play.twirl.api.Html; -import views.html.*; +import javax.inject.Inject; +import java.util.concurrent.CompletionStage; + +import static java.util.concurrent.CompletableFuture.supplyAsync; /** * This controller contains an action to handle HTTP requests @@ -16,18 +21,33 @@ public class HomeController extends Controller { * this method will be called when the application receives a * GET request with a path of /. */ - public Result index(String author,int id) { - return ok("Routing in Play by:"+author+" ID:"+id); - } - public Result greet(String name,int age) { - return ok("Hello "+name+", you are "+age+" years old"); - } - public Result introduceMe(String data) { - String[] clientData=data.split(","); - return ok("Your name is "+clientData[0]+", you are "+clientData[1]+" years old"); - } - public Result squareMe(Long num) { - return ok(num+" Squared is "+(num*num)); + public Result index() { + return ok(views.html.index.render()); } + public Result writer(String author) { + return ok("Routing in Play by " + author); + } + + public Result viewUser(String userId) { + final String response = String.format("Got user id {} in request.", userId); + return ok(response); + } + + public Result greet(String name, int age) { + return ok("Hello " + name + ", you are " + age + " years old"); + } + + public Result squareMe(Long num) { + return ok(num + " Squared is " + (num * num)); + } + + public Result writer(String author, int id) { + return ok("Routing in Play by: " + author + " ID: " + id); + } + + public Result introduceMe(String data) { + String[] clientData = data.split(","); + return ok("Your name is " + clientData[0] + ", you are " + clientData[1] + " years old"); + } } diff --git a/play-framework/routing-in-play/app/filters/ExampleFilter.java b/play-framework/routing-in-play/app/filters/ExampleFilter.java deleted file mode 100644 index 67a6a36cc3..0000000000 --- a/play-framework/routing-in-play/app/filters/ExampleFilter.java +++ /dev/null @@ -1,45 +0,0 @@ -package filters; - -import akka.stream.Materializer; -import java.util.concurrent.CompletionStage; -import java.util.concurrent.Executor; -import java.util.function.Function; -import javax.inject.*; -import play.mvc.*; -import play.mvc.Http.RequestHeader; - - -/** - * This is a simple filter that adds a header to all requests. It's - * added to the application's list of filters by the - * {@link Filters} class. - */ -@Singleton -public class ExampleFilter extends Filter { - - private final Executor exec; - - /** - * @param mat This object is needed to handle streaming of requests - * and responses. - * @param exec This class is needed to execute code asynchronously. - * It is used below by the thenAsyncApply method. - */ - @Inject - public ExampleFilter(Materializer mat, Executor exec) { - super(mat); - this.exec = exec; - } - - @Override - public CompletionStage apply( - Function> next, - RequestHeader requestHeader) { - - return next.apply(requestHeader).thenApplyAsync( - result -> result.withHeader("X-ExampleFilter", "foo"), - exec - ); - } - -} diff --git a/play-framework/routing-in-play/app/services/ApplicationTimer.java b/play-framework/routing-in-play/app/services/ApplicationTimer.java deleted file mode 100644 index a951562b1d..0000000000 --- a/play-framework/routing-in-play/app/services/ApplicationTimer.java +++ /dev/null @@ -1,50 +0,0 @@ -package services; - -import java.time.Clock; -import java.time.Instant; -import java.util.concurrent.CompletableFuture; -import javax.inject.*; -import play.Logger; -import play.inject.ApplicationLifecycle; - -/** - * This class demonstrates how to run code when the - * application starts and stops. It starts a timer when the - * application starts. When the application stops it prints out how - * long the application was running for. - * - * This class is registered for Guice dependency injection in the - * {@link Module} class. We want the class to start when the application - * starts, so it is registered as an "eager singleton". See the code - * in the {@link Module} class to see how this happens. - * - * This class needs to run code when the server stops. It uses the - * application's {@link ApplicationLifecycle} to register a stop hook. - */ -@Singleton -public class ApplicationTimer { - - private final Clock clock; - private final ApplicationLifecycle appLifecycle; - private final Instant start; - - @Inject - public ApplicationTimer(Clock clock, ApplicationLifecycle appLifecycle) { - this.clock = clock; - this.appLifecycle = appLifecycle; - // This code is called when the application starts. - start = clock.instant(); - Logger.info("ApplicationTimer demo: Starting application at " + start); - - // When the application starts, register a stop hook with the - // ApplicationLifecycle object. The code inside the stop hook will - // be run when the application stops. - appLifecycle.addStopHook(() -> { - Instant stop = clock.instant(); - Long runningTime = stop.getEpochSecond() - start.getEpochSecond(); - Logger.info("ApplicationTimer demo: Stopping application at " + clock.instant() + " after " + runningTime + "s."); - return CompletableFuture.completedFuture(null); - }); - } - -} diff --git a/play-framework/routing-in-play/app/services/AtomicCounter.java b/play-framework/routing-in-play/app/services/AtomicCounter.java deleted file mode 100644 index 41f741cbf7..0000000000 --- a/play-framework/routing-in-play/app/services/AtomicCounter.java +++ /dev/null @@ -1,26 +0,0 @@ -package services; - -import java.util.concurrent.atomic.AtomicInteger; -import javax.inject.*; - -/** - * This class is a concrete implementation of the {@link Counter} trait. - * It is configured for Guice dependency injection in the {@link Module} - * class. - * - * This class has a {@link Singleton} annotation because we need to make - * sure we only use one counter per application. Without this - * annotation we would get a new instance every time a {@link Counter} is - * injected. - */ -@Singleton -public class AtomicCounter implements Counter { - - private final AtomicInteger atomicCounter = new AtomicInteger(); - - @Override - public int nextCount() { - return atomicCounter.getAndIncrement(); - } - -} diff --git a/play-framework/routing-in-play/app/services/Counter.java b/play-framework/routing-in-play/app/services/Counter.java deleted file mode 100644 index dadad8b09d..0000000000 --- a/play-framework/routing-in-play/app/services/Counter.java +++ /dev/null @@ -1,13 +0,0 @@ -package services; - -/** - * This interface demonstrates how to create a component that is injected - * into a controller. The interface represents a counter that returns a - * incremented number each time it is called. - * - * The {@link Modules} class binds this interface to the - * {@link AtomicCounter} implementation. - */ -public interface Counter { - int nextCount(); -} diff --git a/play-framework/routing-in-play/app/views/index.scala.html b/play-framework/routing-in-play/app/views/index.scala.html index 4539f5a10b..68d37fb1d4 100644 --- a/play-framework/routing-in-play/app/views/index.scala.html +++ b/play-framework/routing-in-play/app/views/index.scala.html @@ -1,20 +1,5 @@ -@* - * This template takes a single argument, a String containing a - * message to display. - *@ -@(message: String) +@() -@* - * Call the `main` template with two arguments. The first - * argument is a `String` with the title of the page, the second - * argument is an `Html` object containing the body of the page. - *@ @main("Welcome to Play") { - - @* - * Get an `Html` object by calling the built-in Play welcome - * template and passing a `String` message. - *@ - @play20.welcome(message, style = "Java") - +

Welcome to Play!

} diff --git a/play-framework/routing-in-play/app/views/main.scala.html b/play-framework/routing-in-play/app/views/main.scala.html index 9414f4be6e..c5f755f236 100644 --- a/play-framework/routing-in-play/app/views/main.scala.html +++ b/play-framework/routing-in-play/app/views/main.scala.html @@ -13,11 +13,12 @@ @title - @* And here's where we render the `Html` object containing * the page content. *@ @content + + diff --git a/play-framework/routing-in-play/bin/activator b/play-framework/routing-in-play/bin/activator deleted file mode 100644 index a8b11d482f..0000000000 --- a/play-framework/routing-in-play/bin/activator +++ /dev/null @@ -1,397 +0,0 @@ -#!/usr/bin/env bash - -### ------------------------------- ### -### Helper methods for BASH scripts ### -### ------------------------------- ### - -realpath () { -( - TARGET_FILE="$1" - FIX_CYGPATH="$2" - - cd "$(dirname "$TARGET_FILE")" - TARGET_FILE=$(basename "$TARGET_FILE") - - COUNT=0 - while [ -L "$TARGET_FILE" -a $COUNT -lt 100 ] - do - TARGET_FILE=$(readlink "$TARGET_FILE") - cd "$(dirname "$TARGET_FILE")" - TARGET_FILE=$(basename "$TARGET_FILE") - COUNT=$(($COUNT + 1)) - done - - # make sure we grab the actual windows path, instead of cygwin's path. - if [[ "x$FIX_CYGPATH" != "x" ]]; then - echo "$(cygwinpath "$(pwd -P)/$TARGET_FILE")" - else - echo "$(pwd -P)/$TARGET_FILE" - fi -) -} - - -# Uses uname to detect if we're in the odd cygwin environment. -is_cygwin() { - local os=$(uname -s) - case "$os" in - CYGWIN*) return 0 ;; - *) return 1 ;; - esac -} - -# TODO - Use nicer bash-isms here. -CYGWIN_FLAG=$(if is_cygwin; then echo true; else echo false; fi) - - -# This can fix cygwin style /cygdrive paths so we get the -# windows style paths. -cygwinpath() { - local file="$1" - if [[ "$CYGWIN_FLAG" == "true" ]]; then - echo $(cygpath -w $file) - else - echo $file - fi -} - -# Make something URI friendly -make_url() { - url="$1" - local nospaces=${url// /%20} - if is_cygwin; then - echo "/${nospaces//\\//}" - else - echo "$nospaces" - fi -} - -declare -a residual_args -declare -a java_args -declare -a scalac_args -declare -a sbt_commands -declare java_cmd=java -declare java_version -declare -r real_script_path="$(realpath "$0")" -declare -r sbt_home="$(realpath "$(dirname "$(dirname "$real_script_path")")")" -declare -r sbt_bin_dir="$(dirname "$real_script_path")" -declare -r app_version="1.3.10" - -declare -r script_name=activator -declare -r java_opts=( "${ACTIVATOR_OPTS[@]}" "${SBT_OPTS[@]}" "${JAVA_OPTS[@]}" "${java_opts[@]}" ) -userhome="$HOME" -if is_cygwin; then - # cygwin sets home to something f-d up, set to real windows homedir - userhome="$USERPROFILE" -fi -declare -r activator_user_home_dir="${userhome}/.activator" -declare -r java_opts_config_home="${activator_user_home_dir}/activatorconfig.txt" -declare -r java_opts_config_version="${activator_user_home_dir}/${app_version}/activatorconfig.txt" - -echoerr () { - echo 1>&2 "$@" -} -vlog () { - [[ $verbose || $debug ]] && echoerr "$@" -} -dlog () { - [[ $debug ]] && echoerr "$@" -} - -jar_file () { - echo "$(cygwinpath "${sbt_home}/libexec/activator-launch-${app_version}.jar")" -} - -acquire_sbt_jar () { - sbt_jar="$(jar_file)" - - if [[ ! -f "$sbt_jar" ]]; then - echoerr "Could not find launcher jar: $sbt_jar" - exit 2 - fi -} - -execRunner () { - # print the arguments one to a line, quoting any containing spaces - [[ $verbose || $debug ]] && echo "# Executing command line:" && { - for arg; do - if printf "%s\n" "$arg" | grep -q ' '; then - printf "\"%s\"\n" "$arg" - else - printf "%s\n" "$arg" - fi - done - echo "" - } - - # THis used to be exec, but we loose the ability to re-hook stty then - # for cygwin... Maybe we should flag the feature here... - "$@" -} - -addJava () { - dlog "[addJava] arg = '$1'" - java_args=( "${java_args[@]}" "$1" ) -} -addSbt () { - dlog "[addSbt] arg = '$1'" - sbt_commands=( "${sbt_commands[@]}" "$1" ) -} -addResidual () { - dlog "[residual] arg = '$1'" - residual_args=( "${residual_args[@]}" "$1" ) -} -addDebugger () { - addJava "-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=$1" -} - -get_mem_opts () { - # if we detect any of these settings in ${JAVA_OPTS} we need to NOT output our settings. - # The reason is the Xms/Xmx, if they don't line up, cause errors. - if [[ "${JAVA_OPTS}" == *-Xmx* ]] || [[ "${JAVA_OPTS}" == *-Xms* ]] || [[ "${JAVA_OPTS}" == *-XX:MaxPermSize* ]] || [[ "${JAVA_OPTS}" == *-XX:MaxMetaspaceSize* ]] || [[ "${JAVA_OPTS}" == *-XX:ReservedCodeCacheSize* ]]; then - echo "" - else - # a ham-fisted attempt to move some memory settings in concert - # so they need not be messed around with individually. - local mem=${1:-1024} - local codecache=$(( $mem / 8 )) - (( $codecache > 128 )) || codecache=128 - (( $codecache < 512 )) || codecache=512 - local class_metadata_size=$(( $codecache * 2 )) - local class_metadata_opt=$([[ "$java_version" < "1.8" ]] && echo "MaxPermSize" || echo "MaxMetaspaceSize") - - echo "-Xms${mem}m -Xmx${mem}m -XX:ReservedCodeCacheSize=${codecache}m -XX:${class_metadata_opt}=${class_metadata_size}m" - fi -} - -require_arg () { - local type="$1" - local opt="$2" - local arg="$3" - if [[ -z "$arg" ]] || [[ "${arg:0:1}" == "-" ]]; then - echo "$opt requires <$type> argument" - exit 1 - fi -} - -is_function_defined() { - declare -f "$1" > /dev/null -} - -# If we're *not* running in a terminal, and we don't have any arguments, then we need to add the 'ui' parameter -detect_terminal_for_ui() { - [[ ! -t 0 ]] && [[ "${#residual_args}" == "0" ]] && { - addResidual "ui" - } - # SPECIAL TEST FOR MAC - [[ "$(uname)" == "Darwin" ]] && [[ "$HOME" == "$PWD" ]] && [[ "${#residual_args}" == "0" ]] && { - echo "Detected MAC OSX launched script...." - echo "Swapping to UI" - addResidual "ui" - } -} - -process_args () { - while [[ $# -gt 0 ]]; do - case "$1" in - -h|-help) usage; exit 1 ;; - -v|-verbose) verbose=1 && shift ;; - -d|-debug) debug=1 && shift ;; - - -ivy) require_arg path "$1" "$2" && addJava "-Dsbt.ivy.home=$2" && shift 2 ;; - -mem) require_arg integer "$1" "$2" && sbt_mem="$2" && shift 2 ;; - -jvm-debug) require_arg port "$1" "$2" && addDebugger $2 && shift 2 ;; - -batch) exec &1 | awk -F '"' '/version/ {print $2}') - vlog "[process_args] java_version = '$java_version'" -} - -# Detect that we have java installed. -checkJava() { - local required_version="$1" - # Now check to see if it's a good enough version - if [[ "$java_version" == "" ]]; then - echo - echo No java installations was detected. - echo Please go to http://www.java.com/getjava/ and download - echo - exit 1 - elif [[ ! "$java_version" > "$required_version" ]]; then - echo - echo The java installation you have is not up to date - echo $script_name requires at least version $required_version+, you have - echo version $java_version - echo - echo Please go to http://www.java.com/getjava/ and download - echo a valid Java Runtime and install before running $script_name. - echo - exit 1 - fi -} - - -run() { - # no jar? download it. - [[ -f "$sbt_jar" ]] || acquire_sbt_jar "$sbt_version" || { - # still no jar? uh-oh. - echo "Download failed. Obtain the sbt-launch.jar manually and place it at $sbt_jar" - exit 1 - } - - # process the combined args, then reset "$@" to the residuals - process_args "$@" - detect_terminal_for_ui - set -- "${residual_args[@]}" - argumentCount=$# - - # TODO - java check should be configurable... - checkJava "1.6" - - #If we're in cygwin, we should use the windows config, and terminal hacks - if [[ "$CYGWIN_FLAG" == "true" ]]; then - stty -icanon min 1 -echo > /dev/null 2>&1 - addJava "-Djline.terminal=jline.UnixTerminal" - addJava "-Dsbt.cygwin=true" - fi - - # run sbt - execRunner "$java_cmd" \ - "-Dactivator.home=$(make_url "$sbt_home")" \ - ${SBT_OPTS:-$default_sbt_opts} \ - $(get_mem_opts $sbt_mem) \ - ${JAVA_OPTS} \ - ${java_args[@]} \ - -jar "$sbt_jar" \ - "${sbt_commands[@]}" \ - "${residual_args[@]}" - - exit_code=$? - - # Clean up the terminal from cygwin hacks. - if [[ "$CYGWIN_FLAG" == "true" ]]; then - stty icanon echo > /dev/null 2>&1 - fi - exit $exit_code -} - - -declare -r noshare_opts="-Dsbt.global.base=project/.sbtboot -Dsbt.boot.directory=project/.boot -Dsbt.ivy.home=project/.ivy" -declare -r sbt_opts_file=".sbtopts" -declare -r etc_sbt_opts_file="${sbt_home}/conf/sbtopts" -declare -r win_sbt_opts_file="${sbt_home}/conf/sbtconfig.txt" - -usage() { - cat < path to global settings/plugins directory (default: ~/.sbt) - -sbt-boot path to shared boot directory (default: ~/.sbt/boot in 0.11 series) - -ivy path to local Ivy repository (default: ~/.ivy2) - -mem set memory options (default: $sbt_mem, which is $(get_mem_opts $sbt_mem)) - -no-share use all local caches; no sharing - -no-global uses global caches, but does not use global ~/.sbt directory. - -jvm-debug Turn on JVM debugging, open at the given port. - -batch Disable interactive mode - - # sbt version (default: from project/build.properties if present, else latest release) - -sbt-version use the specified version of sbt - -sbt-jar use the specified jar as the sbt launcher - -sbt-rc use an RC version of sbt - -sbt-snapshot use a snapshot version of sbt - - # java version (default: java from PATH, currently $(java -version 2>&1 | grep version)) - -java-home alternate JAVA_HOME - - # jvm options and output control - JAVA_OPTS environment variable, if unset uses "$java_opts" - SBT_OPTS environment variable, if unset uses "$default_sbt_opts" - ACTIVATOR_OPTS Environment variable, if unset uses "" - .sbtopts if this file exists in the current directory, it is - prepended to the runner args - /etc/sbt/sbtopts if this file exists, it is prepended to the runner args - -Dkey=val pass -Dkey=val directly to the java runtime - -J-X pass option -X directly to the java runtime - (-J is stripped) - -S-X add -X to sbt's scalacOptions (-S is stripped) - -In the case of duplicated or conflicting options, the order above -shows precedence: JAVA_OPTS lowest, command line options highest. -EOM -} - - - -process_my_args () { - while [[ $# -gt 0 ]]; do - case "$1" in - -no-colors) addJava "-Dsbt.log.noformat=true" && shift ;; - -no-share) addJava "$noshare_opts" && shift ;; - -no-global) addJava "-Dsbt.global.base=$(pwd)/project/.sbtboot" && shift ;; - -sbt-boot) require_arg path "$1" "$2" && addJava "-Dsbt.boot.directory=$2" && shift 2 ;; - -sbt-dir) require_arg path "$1" "$2" && addJava "-Dsbt.global.base=$2" && shift 2 ;; - -debug-inc) addJava "-Dxsbt.inc.debug=true" && shift ;; - -batch) exec ^&1') do ( - if %%~j==java set JAVAINSTALLED=1 - if %%~j==openjdk set JAVAINSTALLED=1 -) - -rem Detect the same thing about javac -if "%_JAVACCMD%"=="" ( - if not "%JAVA_HOME%"=="" ( - if exist "%JAVA_HOME%\bin\javac.exe" set "_JAVACCMD=%JAVA_HOME%\bin\javac.exe" - ) -) -if "%_JAVACCMD%"=="" set _JAVACCMD=javac -for /F %%j in ('"%_JAVACCMD%" -version 2^>^&1') do ( - if %%~j==javac set JAVACINSTALLED=1 -) - -rem BAT has no logical or, so we do it OLD SCHOOL! Oppan Redmond Style -set JAVAOK=true -if not defined JAVAINSTALLED set JAVAOK=false -if not defined JAVACINSTALLED set JAVAOK=false - -if "%JAVAOK%"=="false" ( - echo. - echo A Java JDK is not installed or can't be found. - if not "%JAVA_HOME%"=="" ( - echo JAVA_HOME = "%JAVA_HOME%" - ) - echo. - echo Please go to - echo http://www.oracle.com/technetwork/java/javase/downloads/index.html - echo and download a valid Java JDK and install before running Activator. - echo. - echo If you think this message is in error, please check - echo your environment variables to see if "java.exe" and "javac.exe" are - echo available via JAVA_HOME or PATH. - echo. - if defined DOUBLECLICKED pause - exit /B 1 -) - -rem Check what Java version is being used to determine what memory options to use -for /f "tokens=3" %%g in ('java -version 2^>^&1 ^| findstr /i "version"') do ( - set JAVA_VERSION=%%g -) - -rem Strips away the " characters -set JAVA_VERSION=%JAVA_VERSION:"=% - -rem TODO Check if there are existing mem settings in JAVA_OPTS/CFG_OPTS and use those instead of the below -for /f "delims=. tokens=1-3" %%v in ("%JAVA_VERSION%") do ( - set MAJOR=%%v - set MINOR=%%w - set BUILD=%%x - - set META_SIZE=-XX:MetaspaceSize=64M -XX:MaxMetaspaceSize=256M - if "!MINOR!" LSS "8" ( - set META_SIZE=-XX:PermSize=64M -XX:MaxPermSize=256M - ) - - set MEM_OPTS=!META_SIZE! - ) - -rem We use the value of the JAVA_OPTS environment variable if defined, rather than the config. -set _JAVA_OPTS=%JAVA_OPTS% -if "%_JAVA_OPTS%"=="" set _JAVA_OPTS=%CFG_OPTS% - -set DEBUG_OPTS= - -rem Loop through the arguments, building remaining args in args variable -set args= -:argsloop -if not "%~1"=="" ( - rem Checks if the argument contains "-D" and if true, adds argument 1 with 2 and puts an equal sign between them. - rem This is done since batch considers "=" to be a delimiter so we need to circumvent this behavior with a small hack. - set arg1=%~1 - if "!arg1:~0,2!"=="-D" ( - set "args=%args% "%~1"="%~2"" - shift - shift - goto argsloop - ) - - if "%~1"=="-jvm-debug" ( - if not "%~2"=="" ( - rem This piece of magic somehow checks that an argument is a number - for /F "delims=0123456789" %%i in ("%~2") do ( - set var="%%i" - ) - if defined var ( - rem Not a number, assume no argument given and default to 9999 - set JPDA_PORT=9999 - ) else ( - rem Port was given, shift arguments - set JPDA_PORT=%~2 - shift - ) - ) else ( - set JPDA_PORT=9999 - ) - shift - - set DEBUG_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=!JPDA_PORT! - goto argsloop - ) - rem else - set "args=%args% "%~1"" - shift - goto argsloop -) - -:run - -if "!args!"=="" ( - if defined DOUBLECLICKED ( - set CMDS="ui" - ) else set CMDS=!args! -) else set CMDS=!args! - -rem We add a / in front, so we get file:///C: instead of file://C: -rem Java considers the later a UNC path. -rem We also attempt a solid effort at making it URI friendly. -rem We don't even bother with UNC paths. -set JAVA_FRIENDLY_HOME_1=/!ACTIVATOR_HOME:\=/! -set JAVA_FRIENDLY_HOME=/!JAVA_FRIENDLY_HOME_1: =%%20! - -rem Checks if the command contains spaces to know if it should be wrapped in quotes or not -set NON_SPACED_CMD=%_JAVACMD: =% -if "%_JAVACMD%"=="%NON_SPACED_CMD%" %_JAVACMD% %DEBUG_OPTS% %MEM_OPTS% %ACTIVATOR_OPTS% %SBT_OPTS% %_JAVA_OPTS% "-Dactivator.home=%JAVA_FRIENDLY_HOME%" -jar "%ACTIVATOR_HOME%\libexec\%ACTIVATOR_LAUNCH_JAR%" %CMDS% -if NOT "%_JAVACMD%"=="%NON_SPACED_CMD%" "%_JAVACMD%" %DEBUG_OPTS% %MEM_OPTS% %ACTIVATOR_OPTS% %SBT_OPTS% %_JAVA_OPTS% "-Dactivator.home=%JAVA_FRIENDLY_HOME%" -jar "%ACTIVATOR_HOME%\libexec\%ACTIVATOR_LAUNCH_JAR%" %CMDS% - -if ERRORLEVEL 1 goto error -goto end - -:error -set ERROR_CODE=1 - -:end - -@endlocal - -exit /B %ERROR_CODE% diff --git a/play-framework/routing-in-play/build.sbt b/play-framework/routing-in-play/build.sbt index 083d071676..4fb0126eaa 100644 --- a/play-framework/routing-in-play/build.sbt +++ b/play-framework/routing-in-play/build.sbt @@ -1,13 +1,10 @@ -name := """webapp""" +name := """play-routing""" +organization := "com.baeldung" version := "1.0-SNAPSHOT" lazy val root = (project in file(".")).enablePlugins(PlayJava) -scalaVersion := "2.11.7" +scalaVersion := "2.13.0" -libraryDependencies ++= Seq( - javaJdbc, - cache, - javaWs -) +libraryDependencies += guice diff --git a/play-framework/routing-in-play/conf/application.conf b/play-framework/routing-in-play/conf/application.conf index 489d3f9b3e..85c184dcb1 100644 --- a/play-framework/routing-in-play/conf/application.conf +++ b/play-framework/routing-in-play/conf/application.conf @@ -1,353 +1,2 @@ # This is the main configuration file for the application. # https://www.playframework.com/documentation/latest/ConfigFile -# ~~~~~ -# Play uses HOCON as its configuration file format. HOCON has a number -# of advantages over other config formats, but there are two things that -# can be used when modifying settings. -# -# You can include other configuration files in this main application.conf file: -#include "extra-config.conf" -# -# You can declare variables and substitute for them: -#mykey = ${some.value} -# -# And if an environment variable exists when there is no other subsitution, then -# HOCON will fall back to substituting environment variable: -#mykey = ${JAVA_HOME} - -## Akka -# https://www.playframework.com/documentation/latest/ScalaAkka#Configuration -# https://www.playframework.com/documentation/latest/JavaAkka#Configuration -# ~~~~~ -# Play uses Akka internally and exposes Akka Streams and actors in Websockets and -# other streaming HTTP responses. -akka { - # "akka.log-config-on-start" is extraordinarly useful because it log the complete - # configuration at INFO level, including defaults and overrides, so it s worth - # putting at the very top. - # - # Put the following in your conf/logback.xml file: - # - # - # - # And then uncomment this line to debug the configuration. - # - #log-config-on-start = true -} - -## Secret key -# http://www.playframework.com/documentation/latest/ApplicationSecret -# ~~~~~ -# The secret key is used to sign Play's session cookie. -# This must be changed for production, but we don't recommend you change it in this file. -play.crypto.secret = "changeme" - -## Modules -# https://www.playframework.com/documentation/latest/Modules -# ~~~~~ -# Control which modules are loaded when Play starts. Note that modules are -# the replacement for "GlobalSettings", which are deprecated in 2.5.x. -# Please see https://www.playframework.com/documentation/latest/GlobalSettings -# for more information. -# -# You can also extend Play functionality by using one of the publically available -# Play modules: https://playframework.com/documentation/latest/ModuleDirectory -play.modules { - # By default, Play will load any class called Module that is defined - # in the root package (the "app" directory), or you can define them - # explicitly below. - # If there are any built-in modules that you want to disable, you can list them here. - #enabled += my.application.Module - - # If there are any built-in modules that you want to disable, you can list them here. - #disabled += "" -} - -## IDE -# https://www.playframework.com/documentation/latest/IDE -# ~~~~~ -# Depending on your IDE, you can add a hyperlink for errors that will jump you -# directly to the code location in the IDE in dev mode. The following line makes -# use of the IntelliJ IDEA REST interface: -#play.editor="http://localhost:63342/api/file/?file=%s&line=%s" - -## Internationalisation -# https://www.playframework.com/documentation/latest/JavaI18N -# https://www.playframework.com/documentation/latest/ScalaI18N -# ~~~~~ -# Play comes with its own i18n settings, which allow the user's preferred language -# to map through to internal messages, or allow the language to be stored in a cookie. -play.i18n { - # The application languages - langs = [ "en" ] - - # Whether the language cookie should be secure or not - #langCookieSecure = true - - # Whether the HTTP only attribute of the cookie should be set to true - #langCookieHttpOnly = true -} - -## Play HTTP settings -# ~~~~~ -play.http { - ## Router - # https://www.playframework.com/documentation/latest/JavaRouting - # https://www.playframework.com/documentation/latest/ScalaRouting - # ~~~~~ - # Define the Router object to use for this application. - # This router will be looked up first when the application is starting up, - # so make sure this is the entry point. - # Furthermore, it's assumed your route file is named properly. - # So for an application router like `my.application.Router`, - # you may need to define a router file `conf/my.application.routes`. - # Default to Routes in the root package (aka "apps" folder) (and conf/routes) - #router = my.application.Router - - ## Action Creator - # https://www.playframework.com/documentation/latest/JavaActionCreator - # ~~~~~ - #actionCreator = null - - ## ErrorHandler - # https://www.playframework.com/documentation/latest/JavaRouting - # https://www.playframework.com/documentation/latest/ScalaRouting - # ~~~~~ - # If null, will attempt to load a class called ErrorHandler in the root package, - #errorHandler = null - - ## Filters - # https://www.playframework.com/documentation/latest/ScalaHttpFilters - # https://www.playframework.com/documentation/latest/JavaHttpFilters - # ~~~~~ - # Filters run code on every request. They can be used to perform - # common logic for all your actions, e.g. adding common headers. - # Defaults to "Filters" in the root package (aka "apps" folder) - # Alternatively you can explicitly register a class here. - #filters = my.application.Filters - - ## Session & Flash - # https://www.playframework.com/documentation/latest/JavaSessionFlash - # https://www.playframework.com/documentation/latest/ScalaSessionFlash - # ~~~~~ - session { - # Sets the cookie to be sent only over HTTPS. - #secure = true - - # Sets the cookie to be accessed only by the server. - #httpOnly = true - - # Sets the max-age field of the cookie to 5 minutes. - # NOTE: this only sets when the browser will discard the cookie. Play will consider any - # cookie value with a valid signature to be a valid session forever. To implement a server side session timeout, - # you need to put a timestamp in the session and check it at regular intervals to possibly expire it. - #maxAge = 300 - - # Sets the domain on the session cookie. - #domain = "example.com" - } - - flash { - # Sets the cookie to be sent only over HTTPS. - #secure = true - - # Sets the cookie to be accessed only by the server. - #httpOnly = true - } -} - -## Netty Provider -# https://www.playframework.com/documentation/latest/SettingsNetty -# ~~~~~ -play.server.netty { - # Whether the Netty wire should be logged - #log.wire = true - - # If you run Play on Linux, you can use Netty's native socket transport - # for higher performance with less garbage. - #transport = "native" -} - -## WS (HTTP Client) -# https://www.playframework.com/documentation/latest/ScalaWS#Configuring-WS -# ~~~~~ -# The HTTP client primarily used for REST APIs. The default client can be -# configured directly, but you can also create different client instances -# with customized settings. You must enable this by adding to build.sbt: -# -# libraryDependencies += ws // or javaWs if using java -# -play.ws { - # Sets HTTP requests not to follow 302 requests - #followRedirects = false - - # Sets the maximum number of open HTTP connections for the client. - #ahc.maxConnectionsTotal = 50 - - ## WS SSL - # https://www.playframework.com/documentation/latest/WsSSL - # ~~~~~ - ssl { - # Configuring HTTPS with Play WS does not require programming. You can - # set up both trustManager and keyManager for mutual authentication, and - # turn on JSSE debugging in development with a reload. - #debug.handshake = true - #trustManager = { - # stores = [ - # { type = "JKS", path = "exampletrust.jks" } - # ] - #} - } -} - -## Cache -# https://www.playframework.com/documentation/latest/JavaCache -# https://www.playframework.com/documentation/latest/ScalaCache -# ~~~~~ -# Play comes with an integrated cache API that can reduce the operational -# overhead of repeated requests. You must enable this by adding to build.sbt: -# -# libraryDependencies += cache -# -play.cache { - # If you want to bind several caches, you can bind the individually - #bindCaches = ["db-cache", "user-cache", "session-cache"] -} - -## Filters -# https://www.playframework.com/documentation/latest/Filters -# ~~~~~ -# There are a number of built-in filters that can be enabled and configured -# to give Play greater security. You must enable this by adding to build.sbt: -# -# libraryDependencies += filters -# -play.filters { - ## CORS filter configuration - # https://www.playframework.com/documentation/latest/CorsFilter - # ~~~~~ - # CORS is a protocol that allows web applications to make requests from the browser - # across different domains. - # NOTE: You MUST apply the CORS configuration before the CSRF filter, as CSRF has - # dependencies on CORS settings. - cors { - # Filter paths by a whitelist of path prefixes - #pathPrefixes = ["/some/path", ...] - - # The allowed origins. If null, all origins are allowed. - #allowedOrigins = ["http://www.example.com"] - - # The allowed HTTP methods. If null, all methods are allowed - #allowedHttpMethods = ["GET", "POST"] - } - - ## CSRF Filter - # https://www.playframework.com/documentation/latest/ScalaCsrf#Applying-a-global-CSRF-filter - # https://www.playframework.com/documentation/latest/JavaCsrf#Applying-a-global-CSRF-filter - # ~~~~~ - # Play supports multiple methods for verifying that a request is not a CSRF request. - # The primary mechanism is a CSRF token. This token gets placed either in the query string - # or body of every form submitted, and also gets placed in the users session. - # Play then verifies that both tokens are present and match. - csrf { - # Sets the cookie to be sent only over HTTPS - #cookie.secure = true - - # Defaults to CSRFErrorHandler in the root package. - #errorHandler = MyCSRFErrorHandler - } - - ## Security headers filter configuration - # https://www.playframework.com/documentation/latest/SecurityHeaders - # ~~~~~ - # Defines security headers that prevent XSS attacks. - # If enabled, then all options are set to the below configuration by default: - headers { - # The X-Frame-Options header. If null, the header is not set. - #frameOptions = "DENY" - - # The X-XSS-Protection header. If null, the header is not set. - #xssProtection = "1; mode=block" - - # The X-Content-Type-Options header. If null, the header is not set. - #contentTypeOptions = "nosniff" - - # The X-Permitted-Cross-Domain-Policies header. If null, the header is not set. - #permittedCrossDomainPolicies = "master-only" - - # The Content-Security-Policy header. If null, the header is not set. - #contentSecurityPolicy = "default-src 'self'" - } - - ## Allowed hosts filter configuration - # https://www.playframework.com/documentation/latest/AllowedHostsFilter - # ~~~~~ - # Play provides a filter that lets you configure which hosts can access your application. - # This is useful to prevent cache poisoning attacks. - hosts { - # Allow requests to example.com, its subdomains, and localhost:9000. - #allowed = [".example.com", "localhost:9000"] - } -} - -## Evolutions -# https://www.playframework.com/documentation/latest/Evolutions -# ~~~~~ -# Evolutions allows database scripts to be automatically run on startup in dev mode -# for database migrations. You must enable this by adding to build.sbt: -# -# libraryDependencies += evolutions -# -play.evolutions { - # You can disable evolutions for a specific datasource if necessary - #db.default.enabled = false -} - -## Database Connection Pool -# https://www.playframework.com/documentation/latest/SettingsJDBC -# ~~~~~ -# Play doesn't require a JDBC database to run, but you can easily enable one. -# -# libraryDependencies += jdbc -# -play.db { - # The combination of these two settings results in "db.default" as the - # default JDBC pool: - #config = "db" - #default = "default" - - # Play uses HikariCP as the default connection pool. You can override - # settings by changing the prototype: - prototype { - # Sets a fixed JDBC connection pool size of 50 - #hikaricp.minimumIdle = 50 - #hikaricp.maximumPoolSize = 50 - } -} - -## JDBC Datasource -# https://www.playframework.com/documentation/latest/JavaDatabase -# https://www.playframework.com/documentation/latest/ScalaDatabase -# ~~~~~ -# Once JDBC datasource is set up, you can work with several different -# database options: -# -# Slick (Scala preferred option): https://www.playframework.com/documentation/latest/PlaySlick -# JPA (Java preferred option): https://playframework.com/documentation/latest/JavaJPA -# EBean: https://playframework.com/documentation/latest/JavaEbean -# Anorm: https://www.playframework.com/documentation/latest/ScalaAnorm -# -db { - # You can declare as many datasources as you want. - # By convention, the default datasource is named `default` - - # https://www.playframework.com/documentation/latest/Developing-with-the-H2-Database - #default.driver = org.h2.Driver - #default.url = "jdbc:h2:mem:play" - #default.username = sa - #default.password = "" - - # You can turn on SQL logging for any datasource - # https://www.playframework.com/documentation/latest/Highlights25#Logging-SQL-statements - #default.logSql=true -} diff --git a/play-framework/routing-in-play/conf/logback.xml b/play-framework/routing-in-play/conf/logback.xml index 86ec12c0af..e8c982543f 100644 --- a/play-framework/routing-in-play/conf/logback.xml +++ b/play-framework/routing-in-play/conf/logback.xml @@ -27,12 +27,6 @@ - - - - - - diff --git a/play-framework/routing-in-play/conf/routes b/play-framework/routing-in-play/conf/routes index f72d8a1a14..43dc4c8733 100644 --- a/play-framework/routing-in-play/conf/routes +++ b/play-framework/routing-in-play/conf/routes @@ -1,7 +1,15 @@ -GET / controllers.HomeController.index(author="Baeldung",id:Int?=1) -GET /:author controllers.HomeController.index(author,id:Int?=1) -GET /greet/:name/:age controllers.HomeController.greet(name,age:Integer) -GET /square/$num<[0-9]+> controllers.HomeController.squareMe(num:Long) +# Routes +# This file defines all application routes (Higher priority routes first) +# ~~~~ + +# An example controller showing a sample home page +GET / controllers.HomeController.index +GET /writer controllers.HomeController.writer(author = "Baeldung", id: Int ?= 1) +GET /writer/:author controllers.HomeController.writer(author: String, id: Int ?= 1) +GET /baeldung/:id controllers.HomeController.viewUser(id: String) +GET /greet/:name/:age controllers.HomeController.greet(name: String, age: Integer) +GET /square/$num<[0-9]+> controllers.HomeController.squareMe(num: Long) +GET /*data controllers.HomeController.introduceMe(data) + # Map static resources from the /public folder to the /assets URL path -GET /assets/*file controllers.Assets.versioned(path="/public", file: Asset) -GET /*data controllers.HomeController.introduceMe(data) +GET /assets/*file controllers.Assets.versioned(path="/public", file: Asset) diff --git a/play-framework/routing-in-play/libexec/activator-launch-1.3.10.jar b/play-framework/routing-in-play/libexec/activator-launch-1.3.10.jar deleted file mode 100644 index 69050e7dec..0000000000 Binary files a/play-framework/routing-in-play/libexec/activator-launch-1.3.10.jar and /dev/null differ diff --git a/play-framework/routing-in-play/project/build.properties b/play-framework/routing-in-play/project/build.properties index 6d22e3f2d1..c0bab04941 100644 --- a/play-framework/routing-in-play/project/build.properties +++ b/play-framework/routing-in-play/project/build.properties @@ -1,4 +1 @@ -#Activator-generated Properties -#Fri Sep 30 14:25:10 EAT 2016 -template.uuid=26c759a5-daf0-4e02-bcfa-ac69725267c0 -sbt.version=0.13.11 +sbt.version=1.2.8 diff --git a/play-framework/routing-in-play/project/plugins.sbt b/play-framework/routing-in-play/project/plugins.sbt index e8e7f602f7..1c8c62a0d5 100644 --- a/play-framework/routing-in-play/project/plugins.sbt +++ b/play-framework/routing-in-play/project/plugins.sbt @@ -1,21 +1,7 @@ // The Play plugin -addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.5.8") +addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.7.3") -// Web plugins -addSbtPlugin("com.typesafe.sbt" % "sbt-coffeescript" % "1.0.0") -addSbtPlugin("com.typesafe.sbt" % "sbt-less" % "1.1.0") -addSbtPlugin("com.typesafe.sbt" % "sbt-jshint" % "1.0.4") -addSbtPlugin("com.typesafe.sbt" % "sbt-rjs" % "1.0.8") -addSbtPlugin("com.typesafe.sbt" % "sbt-digest" % "1.1.1") -addSbtPlugin("com.typesafe.sbt" % "sbt-mocha" % "1.1.0") -addSbtPlugin("org.irundaia.sbt" % "sbt-sassify" % "1.4.6") - -// Play enhancer - this automatically generates getters/setters for public fields -// and rewrites accessors of these fields to use the getters/setters. Remove this -// plugin if you prefer not to have this feature, or disable on a per project -// basis using disablePlugins(PlayEnhancer) in your build.sbt -addSbtPlugin("com.typesafe.sbt" % "sbt-play-enhancer" % "1.1.0") - -// Play Ebean support, to enable, uncomment this line, and enable in your build.sbt using -// enablePlugins(PlayEbean). -// addSbtPlugin("com.typesafe.sbt" % "sbt-play-ebean" % "3.0.2") +// Defines scaffolding (found under .g8 folder) +// http://www.foundweekends.org/giter8/scaffolding.html +// sbt "g8Scaffold form" +addSbtPlugin("org.foundweekends.giter8" % "sbt-giter8-scaffold" % "0.11.0") diff --git a/play-framework/routing-in-play/public/javascripts/hello.js b/play-framework/routing-in-play/public/javascripts/hello.js deleted file mode 100644 index 02ee13c7ca..0000000000 --- a/play-framework/routing-in-play/public/javascripts/hello.js +++ /dev/null @@ -1,3 +0,0 @@ -if (window.console) { - console.log("Welcome to your Play application's JavaScript!"); -} diff --git a/play-framework/routing-in-play/public/javascripts/main.js b/play-framework/routing-in-play/public/javascripts/main.js new file mode 100644 index 0000000000..e69de29bb2 diff --git a/play-framework/routing-in-play/test/ApplicationUnitTest.java b/play-framework/routing-in-play/test/ApplicationUnitTest.java deleted file mode 100644 index 572ce282bc..0000000000 --- a/play-framework/routing-in-play/test/ApplicationUnitTest.java +++ /dev/null @@ -1,45 +0,0 @@ -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import com.fasterxml.jackson.databind.JsonNode; -import org.junit.*; - -import play.mvc.*; -import play.test.*; -import play.data.DynamicForm; -import play.data.validation.ValidationError; -import play.data.validation.Constraints.RequiredValidator; -import play.i18n.Lang; -import play.libs.F; -import play.libs.F.*; -import play.twirl.api.Content; - -import static play.test.Helpers.*; -import static org.junit.Assert.*; - - -/** - * - * Simple (JUnit) tests that can call all parts of a play app. - * If you are interested in mocking a whole application, see the wiki for more details. - * - */ -public class ApplicationUnitTest { - - @Test - public void simpleCheck() { - int a = 1 + 1; - assertEquals(2, a); - } - - @Test - public void renderTemplate() { - Content html = views.html.index.render("Your new application is ready."); - assertEquals("text/html", html.contentType()); - assertTrue(html.body().contains("Your new application is ready.")); - } - - -} diff --git a/play-framework/routing-in-play/test/IntegrationTest.java b/play-framework/routing-in-play/test/IntegrationTest.java deleted file mode 100644 index c53c71e124..0000000000 --- a/play-framework/routing-in-play/test/IntegrationTest.java +++ /dev/null @@ -1,25 +0,0 @@ -import org.junit.*; - -import play.mvc.*; -import play.test.*; - -import static play.test.Helpers.*; -import static org.junit.Assert.*; - -import static org.fluentlenium.core.filter.FilterConstructor.*; - -public class IntegrationTest { - - /** - * add your integration test here - * in this example we just check if the welcome page is being shown - */ - @Test - public void test() { - running(testServer(3333, fakeApplication(inMemoryDatabase())), HTMLUNIT, browser -> { - browser.goTo("http://localhost:3333"); - assertTrue(browser.pageSource().contains("Your new application is ready.")); - }); - } - -} diff --git a/play-framework/routing-in-play/test/controllers/HomeControllerUnitTest.java b/play-framework/routing-in-play/test/controllers/HomeControllerUnitTest.java new file mode 100644 index 0000000000..b911242ac7 --- /dev/null +++ b/play-framework/routing-in-play/test/controllers/HomeControllerUnitTest.java @@ -0,0 +1,36 @@ +package controllers; + +import org.junit.Test; +import play.Application; +import play.inject.guice.GuiceApplicationBuilder; +import play.mvc.Http; +import play.mvc.Result; +import play.test.WithApplication; +import play.twirl.api.Html; + +import java.util.Optional; +import java.util.concurrent.CompletionStage; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static play.mvc.Http.Status.*; +import static play.test.Helpers.GET; +import static play.test.Helpers.route; + +public class HomeControllerUnitTest extends WithApplication { + + @Override + protected Application provideApplication() { + return new GuiceApplicationBuilder().build(); + } + + @Test + public void givenRequest_whenRootPath_ThenStatusOkay() { + Http.RequestBuilder request = new Http.RequestBuilder() + .method(GET) + .uri("/"); + + Result result = route(app, request); + assertEquals(OK, result.status()); + } +} diff --git a/play-framework/routing-in-play/.gitignore b/play-framework/student-api/.gitignore similarity index 94% rename from play-framework/routing-in-play/.gitignore rename to play-framework/student-api/.gitignore index eb372fc719..e497f3fc67 100644 --- a/play-framework/routing-in-play/.gitignore +++ b/play-framework/student-api/.gitignore @@ -1,6 +1,7 @@ logs target /.idea +/.g8 /.idea_modules /.classpath /.project diff --git a/play-framework/student-api/app/controllers/StudentController.java b/play-framework/student-api/app/controllers/StudentController.java new file mode 100644 index 0000000000..5373511560 --- /dev/null +++ b/play-framework/student-api/app/controllers/StudentController.java @@ -0,0 +1,91 @@ +package controllers; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import model.Student; +import play.libs.Json; +import play.libs.concurrent.HttpExecutionContext; +import play.mvc.Controller; +import play.mvc.Http; +import play.mvc.Result; +import store.StudentStore; +import utils.Util; + +import javax.inject.Inject; +import java.util.Optional; +import java.util.Set; +import java.util.concurrent.CompletionStage; + +import static java.util.concurrent.CompletableFuture.supplyAsync; + +public class StudentController extends Controller { + private HttpExecutionContext ec; + private StudentStore studentStore; + + @Inject + public StudentController(HttpExecutionContext ec, StudentStore studentStore) { + this.studentStore = studentStore; + this.ec = ec; + } + + public CompletionStage create(Http.Request request) { + JsonNode json = request.body().asJson(); + return supplyAsync(() -> { + if (json == null) { + return badRequest(Util.createResponse("Expecting Json data", false)); + } + + Optional studentOptional = studentStore.addStudent(Json.fromJson(json, Student.class)); + return studentOptional.map(student -> { + JsonNode jsonObject = Json.toJson(student); + return created(Util.createResponse(jsonObject, true)); + }).orElse(internalServerError(Util.createResponse("Could not create data.", false))); + }, ec.current()); + } + + public CompletionStage listStudents() { + return supplyAsync(() -> { + Set result = studentStore.getAllStudents(); + ObjectMapper mapper = new ObjectMapper(); + JsonNode jsonData = mapper.convertValue(result, JsonNode.class); + return ok(Util.createResponse(jsonData, true)); + }, ec.current()); + } + + public CompletionStage retrieve(int id) { + return supplyAsync(() -> { + final Optional studentOptional = studentStore.getStudent(id); + return studentOptional.map(student -> { + JsonNode jsonObjects = Json.toJson(student); + return ok(Util.createResponse(jsonObjects, true)); + }).orElse(notFound(Util.createResponse("Student with id:" + id + " not found", false))); + }, ec.current()); + } + + public CompletionStage update(Http.Request request) { + JsonNode json = request.body().asJson(); + return supplyAsync(() -> { + if (json == null) { + return badRequest(Util.createResponse("Expecting Json data", false)); + } + Optional studentOptional = studentStore.updateStudent(Json.fromJson(json, Student.class)); + return studentOptional.map(student -> { + if (student == null) { + return notFound(Util.createResponse("Student not found", false)); + } + JsonNode jsonObject = Json.toJson(student); + return ok(Util.createResponse(jsonObject, true)); + }).orElse(internalServerError(Util.createResponse("Could not create data.", false))); + }, ec.current()); + } + + public CompletionStage delete(int id) { + return supplyAsync(() -> { + boolean status = studentStore.deleteStudent(id); + if (!status) { + return notFound(Util.createResponse("Student with id:" + id + " not found", false)); + } + return ok(Util.createResponse("Student with id:" + id + " deleted", true)); + }, ec.current()); + } +} diff --git a/play-framework/introduction/app/models/Student.java b/play-framework/student-api/app/model/Student.java similarity index 84% rename from play-framework/introduction/app/models/Student.java rename to play-framework/student-api/app/model/Student.java index dc539767bd..39cbfe0040 100644 --- a/play-framework/introduction/app/models/Student.java +++ b/play-framework/student-api/app/model/Student.java @@ -1,15 +1,19 @@ -package models; +package model; + public class Student { private String firstName; private String lastName; private int age; private int id; - public Student(){} - public Student(String firstName, String lastName, int age) { - super(); + + public Student() { + } + + public Student(String firstName, String lastName, int age, int id) { this.firstName = firstName; this.lastName = lastName; this.age = age; + this.id = id; } public String getFirstName() { @@ -43,5 +47,4 @@ public class Student { public void setId(int id) { this.id = id; } - } diff --git a/play-framework/student-api/app/store/StudentStore.java b/play-framework/student-api/app/store/StudentStore.java new file mode 100644 index 0000000000..315db5b916 --- /dev/null +++ b/play-framework/student-api/app/store/StudentStore.java @@ -0,0 +1,37 @@ +package store; + +import model.Student; + +import java.util.*; + +public class StudentStore { + private Map students = new HashMap<>(); + + public Optional addStudent(Student student) { + int id = students.size(); + student.setId(id); + students.put(id, student); + return Optional.ofNullable(student); + } + + public Optional getStudent(int id) { + return Optional.ofNullable(students.get(id)); + } + + public Set getAllStudents() { + return new HashSet<>(students.values()); + } + + public Optional updateStudent(Student student) { + int id = student.getId(); + if (students.containsKey(id)) { + students.put(id, student); + return Optional.ofNullable(student); + } + return Optional.empty(); + } + + public boolean deleteStudent(int id) { + return students.remove(id) != null; + } +} diff --git a/play-framework/introduction/app/util/Util.java b/play-framework/student-api/app/utils/Util.java similarity index 60% rename from play-framework/introduction/app/util/Util.java rename to play-framework/student-api/app/utils/Util.java index a853a4cb43..3fb9833ada 100644 --- a/play-framework/introduction/app/util/Util.java +++ b/play-framework/student-api/app/utils/Util.java @@ -1,17 +1,17 @@ -package util; +package utils; -import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.node.ObjectNode; import play.libs.Json; public class Util { public static ObjectNode createResponse(Object response, boolean ok) { ObjectNode result = Json.newObject(); - result.put("isSuccessfull", ok); - if (response instanceof String) + result.put("isSuccessful", ok); + if (response instanceof String) { result.put("body", (String) response); - else result.put("body", (JsonNode) response); - + } else { + result.putPOJO("body", response); + } return result; } -} \ No newline at end of file +} diff --git a/play-framework/student-api/build.sbt b/play-framework/student-api/build.sbt new file mode 100644 index 0000000000..13d3fe96cd --- /dev/null +++ b/play-framework/student-api/build.sbt @@ -0,0 +1,10 @@ +name := """student-api""" +organization := "com.baeldung" + +version := "1.0-SNAPSHOT" + +lazy val root = (project in file(".")).enablePlugins(PlayJava) + +scalaVersion := "2.13.0" + +libraryDependencies += guice diff --git a/play-framework/student-api/conf/application.conf b/play-framework/student-api/conf/application.conf new file mode 100644 index 0000000000..85c184dcb1 --- /dev/null +++ b/play-framework/student-api/conf/application.conf @@ -0,0 +1,2 @@ +# This is the main configuration file for the application. +# https://www.playframework.com/documentation/latest/ConfigFile diff --git a/play-framework/student-api/conf/logback.xml b/play-framework/student-api/conf/logback.xml new file mode 100644 index 0000000000..e8c982543f --- /dev/null +++ b/play-framework/student-api/conf/logback.xml @@ -0,0 +1,35 @@ + + + + + + + ${application.home:-.}/logs/application.log + + %date [%level] from %logger in %thread - %message%n%xException + + + + + + %coloredLevel %logger{15} - %message%n%xException{10} + + + + + + + + + + + + + + + + + + + + diff --git a/play-framework/student-api/conf/routes b/play-framework/student-api/conf/routes new file mode 100644 index 0000000000..f89b866953 --- /dev/null +++ b/play-framework/student-api/conf/routes @@ -0,0 +1,12 @@ +# Routes +# This file defines all application routes (Higher priority routes first) +# ~~~~ + +GET / controllers.StudentController.listStudents() +GET /:id controllers.StudentController.retrieve(id:Int) +POST / controllers.StudentController.create(request: Request) +PUT / controllers.StudentController.update(request: Request) +DELETE /:id controllers.StudentController.delete(id:Int) + +# Map static resources from the /public folder to the /assets URL path +GET /assets/*file controllers.Assets.versioned(path="/public", file: Asset) diff --git a/play-framework/student-api/project/build.properties b/play-framework/student-api/project/build.properties new file mode 100644 index 0000000000..c0bab04941 --- /dev/null +++ b/play-framework/student-api/project/build.properties @@ -0,0 +1 @@ +sbt.version=1.2.8 diff --git a/play-framework/student-api/project/plugins.sbt b/play-framework/student-api/project/plugins.sbt new file mode 100644 index 0000000000..1c8c62a0d5 --- /dev/null +++ b/play-framework/student-api/project/plugins.sbt @@ -0,0 +1,7 @@ +// The Play plugin +addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.7.3") + +// Defines scaffolding (found under .g8 folder) +// http://www.foundweekends.org/giter8/scaffolding.html +// sbt "g8Scaffold form" +addSbtPlugin("org.foundweekends.giter8" % "sbt-giter8-scaffold" % "0.11.0") diff --git a/play-framework/student-api/test/ApplicationLiveTest.java b/play-framework/student-api/test/ApplicationLiveTest.java deleted file mode 100644 index beeef1a602..0000000000 --- a/play-framework/student-api/test/ApplicationLiveTest.java +++ /dev/null @@ -1,172 +0,0 @@ - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - -import java.io.BufferedReader; -import java.io.DataOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.net.HttpURLConnection; -import java.net.MalformedURLException; -import java.net.URL; -import java.util.Arrays; - -import org.json.JSONArray; -import org.json.JSONObject; -import org.junit.Before; -import org.junit.Test; -import model.Student; -import play.test.*; -import static play.test.Helpers.*; - -public class ApplicationLiveTest{ - private static final String BASE_URL = "http://localhost:9000"; - - @Test -public void testInServer() throws Exception { - TestServer server = testServer(3333); - running(server, () -> { - try { - WSClient ws = play.libs.ws.WS.newClient(3333); - CompletionStage completionStage = ws.url("/").get(); - WSResponse response = completionStage.toCompletableFuture().get(); - ws.close(); - assertEquals(OK, response.getStatus()); - } catch (Exception e) { - logger.error(e.getMessage(), e); - } - }); -} - @Test - public void whenCreatesRecord_thenCorrect() { - Student student = new Student("jody", "west", 50); - JSONObject obj = new JSONObject(makeRequest(BASE_URL, "POST", new JSONObject(student))); - assertTrue(obj.getBoolean("isSuccessfull")); - JSONObject body = obj.getJSONObject("body"); - assertEquals(student.getAge(), body.getInt("age")); - assertEquals(student.getFirstName(), body.getString("firstName")); - assertEquals(student.getLastName(), body.getString("lastName")); - } - - @Test - public void whenDeletesCreatedRecord_thenCorrect() { - Student student = new Student("Usain", "Bolt", 25); - JSONObject ob1 = new JSONObject(makeRequest(BASE_URL, "POST", new JSONObject(student))).getJSONObject("body"); - int id = ob1.getInt("id"); - JSONObject obj1 = new JSONObject(makeRequest(BASE_URL + "/" + id, "POST", new JSONObject())); - assertTrue(obj1.getBoolean("isSuccessfull")); - makeRequest(BASE_URL + "/" + id, "DELETE", null); - JSONObject obj2 = new JSONObject(makeRequest(BASE_URL + "/" + id, "POST", new JSONObject())); - assertFalse(obj2.getBoolean("isSuccessfull")); - } - - @Test - public void whenUpdatesCreatedRecord_thenCorrect() { - Student student = new Student("john", "doe", 50); - JSONObject body1 = new JSONObject(makeRequest(BASE_URL, "POST", new JSONObject(student))).getJSONObject("body"); - assertEquals(student.getAge(), body1.getInt("age")); - int newAge = 60; - body1.put("age", newAge); - JSONObject body2 = new JSONObject(makeRequest(BASE_URL, "PUT", body1)).getJSONObject("body"); - assertFalse(student.getAge() == body2.getInt("age")); - assertTrue(newAge == body2.getInt("age")); - } - - @Test - public void whenGetsAllRecords_thenCorrect() { - Student student1 = new Student("jane", "daisy", 50); - Student student2 = new Student("john", "daniel", 60); - Student student3 = new Student("don", "mason", 55); - Student student4 = new Student("scarlet", "ohara", 90); - - makeRequest(BASE_URL, "POST", new JSONObject(student1)); - makeRequest(BASE_URL, "POST", new JSONObject(student2)); - makeRequest(BASE_URL, "POST", new JSONObject(student3)); - makeRequest(BASE_URL, "POST", new JSONObject(student4)); - - JSONObject objects = new JSONObject(makeRequest(BASE_URL, "GET", null)); - assertTrue(objects.getBoolean("isSuccessfull")); - JSONArray array = objects.getJSONArray("body"); - assertTrue(array.length() >= 4); - } - - public static String makeRequest(String myUrl, String httpMethod, JSONObject parameters) { - - URL url = null; - try { - url = new URL(myUrl); - } catch (MalformedURLException e) { - e.printStackTrace(); - } - HttpURLConnection conn = null; - try { - - conn = (HttpURLConnection) url.openConnection(); - } catch (IOException e) { - e.printStackTrace(); - } - conn.setDoInput(true); - - conn.setReadTimeout(10000); - - conn.setRequestProperty("Content-Type", "application/json"); - DataOutputStream dos = null; - int respCode = 0; - String inputString = null; - try { - conn.setRequestMethod(httpMethod); - - if (Arrays.asList("POST", "PUT").contains(httpMethod)) { - String params = parameters.toString(); - - conn.setDoOutput(true); - - dos = new DataOutputStream(conn.getOutputStream()); - dos.writeBytes(params); - dos.flush(); - dos.close(); - } - respCode = conn.getResponseCode(); - if (respCode != 200 && respCode != 201) { - String error = inputStreamToString(conn.getErrorStream()); - return error; - } - inputString = inputStreamToString(conn.getInputStream()); - - } catch (IOException e) { - - e.printStackTrace(); - } - return inputString; - } - - public static String inputStreamToString(InputStream is) { - BufferedReader br = null; - StringBuilder sb = new StringBuilder(); - - String line; - try { - - br = new BufferedReader(new InputStreamReader(is)); - while ((line = br.readLine()) != null) { - sb.append(line); - } - - } catch (IOException e) { - e.printStackTrace(); - } finally { - if (br != null) { - try { - br.close(); - } catch (IOException e) { - e.printStackTrace(); - } - } - } - - return sb.toString(); - - } -} diff --git a/play-framework/student-api/test/controllers/StudentControllerUnitTest.java b/play-framework/student-api/test/controllers/StudentControllerUnitTest.java new file mode 100644 index 0000000000..bf645b72f9 --- /dev/null +++ b/play-framework/student-api/test/controllers/StudentControllerUnitTest.java @@ -0,0 +1,95 @@ +package controllers; + +import com.fasterxml.jackson.databind.node.ObjectNode; +import org.junit.Test; +import play.Application; +import play.inject.guice.GuiceApplicationBuilder; +import play.libs.Json; +import play.mvc.Http; +import play.mvc.Result; +import play.test.WithApplication; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static play.mvc.Http.Status.OK; +import static play.test.Helpers.*; + +public class StudentControllerUnitTest extends WithApplication { + + @Override + protected Application provideApplication() { + return new GuiceApplicationBuilder().build(); + } + + @Test + public void givenStudentPostData_whenCreatingStudent_ThenShouldReturnCreatedStudent() { + final ObjectNode jsonNode = Json.newObject(); + jsonNode.put("firstName", "John"); + jsonNode.put("lastName", "Baeldung"); + jsonNode.put("age", 25); + + Http.RequestBuilder request = new Http.RequestBuilder() + .method(POST) + .bodyJson(jsonNode) + .uri("/"); + + Result result = route(app, request); + assertEquals(CREATED, result.status()); + assertTrue(result.contentType().isPresent()); + assertEquals("application/json", result.contentType().get()); + } + + @Test + public void givenUrlToListStudents_whenListingStudents_ThenShouldReturnStudentList() { + Http.RequestBuilder request = new Http.RequestBuilder() + .method(GET) + .uri("/"); + + Result result = route(app, request); + assertEquals(OK, result.status()); + assertTrue(result.contentType().isPresent()); + assertEquals("application/json", result.contentType().get()); + } + + @Test + public void givenUrlToRetrieveSingleStudent_whenRetrievingStudent_ThenShouldReturn404NotFound() { + Http.RequestBuilder request = new Http.RequestBuilder() + .method(GET) + .uri("/1"); + + Result result = route(app, request); + assertEquals(NOT_FOUND, result.status()); + assertTrue(result.contentType().isPresent()); + assertEquals("application/json", result.contentType().get()); + } + + @Test + public void givenUrlToUpdateStudent_whenaUpdatingStudent_ThenShouldFail() { + final ObjectNode jsonNode = Json.newObject(); + jsonNode.put("firstName", "John"); + jsonNode.put("lastName", "Baeldung"); + jsonNode.put("age", 25); + + Http.RequestBuilder request = new Http.RequestBuilder() + .method(PUT) + .bodyJson(jsonNode) + .uri("/"); + + Result result = route(app, request); + assertEquals(INTERNAL_SERVER_ERROR, result.status()); + assertTrue(result.contentType().isPresent()); + assertEquals("application/json", result.contentType().get()); + } + + @Test + public void givenIdToDeleteStudent_whenDeletingStudent_ThenShouldFail() { + Http.RequestBuilder request = new Http.RequestBuilder() + .method(DELETE) + .uri("/1"); + + Result result = route(app, request); + assertEquals(NOT_FOUND, result.status()); + assertTrue(result.contentType().isPresent()); + assertEquals("application/json", result.contentType().get()); + } +} diff --git a/pom.xml b/pom.xml index 81338b9f81..7f92acef6c 100644 --- a/pom.xml +++ b/pom.xml @@ -395,6 +395,7 @@ core-java-modules/core-java-collections core-java-modules/core-java-collections-list core-java-modules/core-java-collections-list-2 + core-java-modules/core-java-collections-list-3 core-java-modules/core-java-collections-array-list core-java-modules/core-java-collections-set core-java-modules/core-java-concurrency-basic @@ -535,6 +536,7 @@ metrics microprofile + ml msf4j mustache @@ -1120,6 +1122,7 @@ core-java-modules/core-java-collections core-java-modules/core-java-collections-list core-java-modules/core-java-collections-list-2 + core-java-modules/core-java-collections-list-3 core-java-modules/core-java-collections-array-list core-java-modules/core-java-collections-set core-java-modules/core-java-concurrency-basic @@ -1253,6 +1256,7 @@ metrics microprofile + ml msf4j mustache diff --git a/spring-5-reactive-client/pom.xml b/spring-5-reactive-client/pom.xml index 1b71815eb4..70771f6832 100644 --- a/spring-5-reactive-client/pom.xml +++ b/spring-5-reactive-client/pom.xml @@ -56,7 +56,6 @@ - org.springframework.boot spring-boot-devtools @@ -89,6 +88,14 @@ org.projectlombok lombok + + + + org.eclipse.jetty + jetty-reactive-httpclient + ${jetty-reactive-httpclient.version} + test + @@ -110,6 +117,7 @@ 1.0 1.0 4.1 + 1.0.3 diff --git a/spring-5-reactive-client/src/test/java/com/baeldung/reactive/logging/WebClientLoggingIntegrationTest.java b/spring-5-reactive-client/src/test/java/com/baeldung/reactive/logging/WebClientLoggingIntegrationTest.java new file mode 100644 index 0000000000..95c63f267f --- /dev/null +++ b/spring-5-reactive-client/src/test/java/com/baeldung/reactive/logging/WebClientLoggingIntegrationTest.java @@ -0,0 +1,154 @@ +package com.baeldung.reactive.logging; + +import ch.qos.logback.classic.spi.LoggingEvent; +import ch.qos.logback.core.Appender; +import com.baeldung.reactive.logging.filters.LogFilters; +import com.baeldung.reactive.logging.netty.CustomLogger; +import com.fasterxml.jackson.databind.ObjectMapper; +import java.net.URI; +import lombok.AllArgsConstructor; +import lombok.Data; +import org.eclipse.jetty.client.api.Request; +import org.eclipse.jetty.util.ssl.SslContextFactory; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.slf4j.LoggerFactory; +import org.springframework.http.client.reactive.JettyClientHttpConnector; +import org.springframework.http.client.reactive.ReactorClientHttpConnector; +import org.springframework.web.reactive.function.BodyInserters; +import org.springframework.web.reactive.function.client.WebClient; +import reactor.netty.channel.BootstrapHandlers; +import reactor.netty.http.client.HttpClient; + +import static com.baeldung.reactive.logging.jetty.RequestLogEnhancer.enhance; +import static org.mockito.ArgumentMatchers.argThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + + +public class WebClientLoggingIntegrationTest { + + @AllArgsConstructor + @Data + class Post { + private String title; + private String body; + private int userId; + } + + private Appender jettyAppender; + private Appender nettyAppender; + private Appender mockAppender; + private String sampleUrl = "https://jsonplaceholder.typicode.com/posts"; + + private Post post; + private String sampleResponseBody; + + @BeforeEach + private void setup() throws Exception { + + post = new Post("Learn WebClient logging with Baeldung!", "", 1); + sampleResponseBody = new ObjectMapper().writeValueAsString(post); + + ch.qos.logback.classic.Logger jetty = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger("com.baeldung.reactive.logging.jetty"); + jettyAppender = mock(Appender.class); + when(jettyAppender.getName()).thenReturn("com.baeldung.reactive.logging.jetty"); + jetty.addAppender(jettyAppender); + + ch.qos.logback.classic.Logger netty = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger("reactor.netty.http.client"); + nettyAppender = mock(Appender.class); + when(nettyAppender.getName()).thenReturn("reactor.netty.http.client"); + netty.addAppender(nettyAppender); + + ch.qos.logback.classic.Logger test = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger("com.baeldung.reactive"); + mockAppender = mock(Appender.class); + when(mockAppender.getName()).thenReturn("com.baeldung.reactive"); + test.addAppender(mockAppender); + + } + + @Test + public void givenJettyHttpClient_whenEndpointIsConsumed_thenRequestAndResponseBodyLogged() { + SslContextFactory.Client sslContextFactory = new SslContextFactory.Client(); + org.eclipse.jetty.client.HttpClient httpClient = new org.eclipse.jetty.client.HttpClient(sslContextFactory) { + @Override + public Request newRequest(URI uri) { + Request request = super.newRequest(uri); + return enhance(request); + } + }; + + WebClient + .builder() + .clientConnector(new JettyClientHttpConnector(httpClient)) + .build() + .post() + .uri(sampleUrl) + .body(BodyInserters.fromObject(post)) + .retrieve() + .bodyToMono(String.class) + .block(); + + verify(jettyAppender).doAppend(argThat(argument -> (((LoggingEvent) argument).getFormattedMessage()).contains(sampleResponseBody))); + } + + @Test + public void givenNettyHttpClientWithWiretap_whenEndpointIsConsumed_thenRequestAndResponseBodyLogged() { + + reactor.netty.http.client.HttpClient httpClient = HttpClient + .create() + .wiretap(true); + WebClient + .builder() + .clientConnector(new ReactorClientHttpConnector(httpClient)) + .build() + .post() + .uri(sampleUrl) + .body(BodyInserters.fromObject(post)) + .exchange() + .block(); + + verify(nettyAppender).doAppend(argThat(argument -> (((LoggingEvent) argument).getFormattedMessage()).contains("00000300"))); + } + + @Test + public void givenNettyHttpClientWithCustomLogger_whenEndpointIsConsumed_thenRequestAndResponseBodyLogged() { + + reactor.netty.http.client.HttpClient httpClient = HttpClient + .create() + .tcpConfiguration( + tc -> tc.bootstrap( + b -> BootstrapHandlers.updateLogSupport(b, new CustomLogger(HttpClient.class)))); + WebClient + .builder() + .clientConnector(new ReactorClientHttpConnector(httpClient)) + .build() + .post() + .uri(sampleUrl) + .body(BodyInserters.fromObject(post)) + .exchange() + .block(); + + verify(nettyAppender).doAppend(argThat(argument -> (((LoggingEvent) argument).getFormattedMessage()).contains(sampleResponseBody))); + } + + @Test + public void givenDefaultHttpClientWithFilter_whenEndpointIsConsumed_thenRequestAndResponseLogged() { + WebClient + .builder() + .filters(exchangeFilterFunctions -> { + exchangeFilterFunctions.addAll(LogFilters.prepareFilters()); + }) + .build() + .post() + .uri(sampleUrl) + .body(BodyInserters.fromObject(post)) + .exchange() + .block(); + + verify(mockAppender).doAppend(argThat(argument -> (((LoggingEvent) argument).getFormattedMessage()).contains("domain=.typicode.com;"))); + } + + +} diff --git a/spring-5-reactive-client/src/test/java/com/baeldung/reactive/logging/filters/LogFilters.java b/spring-5-reactive-client/src/test/java/com/baeldung/reactive/logging/filters/LogFilters.java new file mode 100644 index 0000000000..c1c3d3e895 --- /dev/null +++ b/spring-5-reactive-client/src/test/java/com/baeldung/reactive/logging/filters/LogFilters.java @@ -0,0 +1,54 @@ +package com.baeldung.reactive.logging.filters; + +import java.util.Arrays; +import java.util.List; +import lombok.extern.slf4j.Slf4j; +import org.springframework.web.reactive.function.client.ExchangeFilterFunction; +import reactor.core.publisher.Mono; + +@Slf4j +public class LogFilters { + public static List prepareFilters() { + return Arrays.asList(logRequest(), logResponse()); + } + + private static ExchangeFilterFunction logRequest() { + return ExchangeFilterFunction.ofRequestProcessor(clientRequest -> { + if (log.isDebugEnabled()) { + StringBuilder sb = new StringBuilder("Request: \n") + .append(clientRequest.method()) + .append(" ") + .append(clientRequest.url()); + clientRequest + .headers() + .forEach((name, values) -> values.forEach(value -> sb + .append("\n") + .append(name) + .append(":") + .append(value))); + log.debug(sb.toString()); + } + return Mono.just(clientRequest); + }); + } + + private static ExchangeFilterFunction logResponse() { + return ExchangeFilterFunction.ofResponseProcessor(clientResponse -> { + if (log.isDebugEnabled()) { + StringBuilder sb = new StringBuilder("Response: \n") + .append("Status: ") + .append(clientResponse.rawStatusCode()); + clientResponse + .headers() + .asHttpHeaders() + .forEach((key, value1) -> value1.forEach(value -> sb + .append("\n") + .append(key) + .append(":") + .append(value))); + log.debug(sb.toString()); + } + return Mono.just(clientResponse); + }); + } +} diff --git a/spring-5-reactive-client/src/test/java/com/baeldung/reactive/logging/jetty/RequestLogEnhancer.java b/spring-5-reactive-client/src/test/java/com/baeldung/reactive/logging/jetty/RequestLogEnhancer.java new file mode 100644 index 0000000000..43e3660743 --- /dev/null +++ b/spring-5-reactive-client/src/test/java/com/baeldung/reactive/logging/jetty/RequestLogEnhancer.java @@ -0,0 +1,93 @@ +package com.baeldung.reactive.logging.jetty; + +import java.nio.ByteBuffer; +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; +import java.util.Locale; +import lombok.extern.slf4j.Slf4j; +import org.eclipse.jetty.client.api.Request; +import org.eclipse.jetty.http.HttpField; +import org.eclipse.jetty.http.HttpFields; +import org.eclipse.jetty.http.HttpHeader; + +@Slf4j +public class RequestLogEnhancer { + + public static Request enhance(Request request) { + StringBuilder group = new StringBuilder(); + request.onRequestBegin(theRequest -> group + .append("Request ") + .append(theRequest.getMethod()) + .append(" ") + .append(theRequest.getURI()) + .append("\n")); + request.onRequestHeaders(theRequest -> { + for (HttpField header : theRequest.getHeaders()) + group + .append(header) + .append("\n"); + }); + request.onRequestContent((theRequest, content) -> { + group.append(toString(content, getCharset(theRequest.getHeaders()))); + }); + request.onRequestSuccess(theRequest -> { + log.debug(group.toString()); + group.delete(0, group.length()); + }); + group.append("\n"); + request.onResponseBegin(theResponse -> { + group + .append("Response \n") + .append(theResponse.getVersion()) + .append(" ") + .append(theResponse.getStatus()); + if (theResponse.getReason() != null) { + group + .append(" ") + .append(theResponse.getReason()); + } + group.append("\n"); + }); + request.onResponseHeaders(theResponse -> { + for (HttpField header : theResponse.getHeaders()) + group + .append(header) + .append("\n"); + }); + request.onResponseContent((theResponse, content) -> { + group.append(toString(content, getCharset(theResponse.getHeaders()))); + }); + request.onResponseSuccess(theResponse -> { + log.debug(group.toString()); + }); + return request; + } + + private static String toString(ByteBuffer buffer, Charset charset) { + byte[] bytes; + if (buffer.hasArray()) { + bytes = new byte[buffer.capacity()]; + System.arraycopy(buffer.array(), 0, bytes, 0, buffer.capacity()); + } else { + bytes = new byte[buffer.remaining()]; + buffer.get(bytes, 0, bytes.length); + } + return new String(bytes, charset); + } + + private static Charset getCharset(HttpFields headers) { + String contentType = headers.get(HttpHeader.CONTENT_TYPE); + if (contentType != null) { + String[] tokens = contentType + .toLowerCase(Locale.US) + .split("charset="); + if (tokens.length == 2) { + String encoding = tokens[1].replaceAll("[;\"]", ""); + return Charset.forName(encoding); + } + } + return StandardCharsets.UTF_8; + } + +} + diff --git a/spring-5-reactive-client/src/test/java/com/baeldung/reactive/logging/netty/CustomLogger.java b/spring-5-reactive-client/src/test/java/com/baeldung/reactive/logging/netty/CustomLogger.java new file mode 100644 index 0000000000..9f2a4d127f --- /dev/null +++ b/spring-5-reactive-client/src/test/java/com/baeldung/reactive/logging/netty/CustomLogger.java @@ -0,0 +1,42 @@ +package com.baeldung.reactive.logging.netty; + +import io.netty.buffer.ByteBuf; +import io.netty.channel.ChannelHandlerContext; +import io.netty.handler.logging.LoggingHandler; +import java.nio.charset.Charset; + +import static io.netty.util.internal.PlatformDependent.allocateUninitializedArray; +import static java.lang.Math.max; +import static java.nio.charset.Charset.defaultCharset; + +public class CustomLogger extends LoggingHandler { + public CustomLogger(Class clazz) { + super(clazz); + } + + @Override + protected String format(ChannelHandlerContext ctx, String event, Object arg) { + if (arg instanceof ByteBuf) { + ByteBuf msg = (ByteBuf) arg; + return decode(msg, msg.readerIndex(), msg.readableBytes(), defaultCharset()); + } + return super.format(ctx, event, arg); + } + + private String decode(ByteBuf src, int readerIndex, int len, Charset charset) { + if (len != 0) { + byte[] array; + int offset; + if (src.hasArray()) { + array = src.array(); + offset = src.arrayOffset() + readerIndex; + } else { + array = allocateUninitializedArray(max(len, 1024)); + offset = 0; + src.getBytes(readerIndex, array, 0, len); + } + return new String(array, offset, len, charset); + } + return ""; + } +} diff --git a/spring-5-reactive-client/src/test/resources/logback-test.xml b/spring-5-reactive-client/src/test/resources/logback-test.xml index 7072369b8d..42cb0865c5 100644 --- a/spring-5-reactive-client/src/test/resources/logback-test.xml +++ b/spring-5-reactive-client/src/test/resources/logback-test.xml @@ -6,11 +6,15 @@ %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n - + + + + + - + \ No newline at end of file diff --git a/spring-boot-ops/pom.xml b/spring-boot-ops/pom.xml index f36434b682..f578a24163 100644 --- a/spring-boot-ops/pom.xml +++ b/spring-boot-ops/pom.xml @@ -85,11 +85,17 @@ ${jquery.version} - + org.springframework.cloud spring-cloud-context ${springcloud.version} + + + org.apache.httpcomponents + httpclient + ${httpclient.version} + @@ -190,7 +196,8 @@ 2.2 18.0 3.1.7 - 2.0.2.RELEASE + 2.0.2.RELEASE + 4.5.8 diff --git a/spring-boot-ops/src/main/java/com/baeldung/properties/ConfProperties.java b/spring-boot-ops/src/main/java/com/baeldung/properties/ConfProperties.java index 0b6041bb06..c6413324f3 100644 --- a/spring-boot-ops/src/main/java/com/baeldung/properties/ConfProperties.java +++ b/spring-boot-ops/src/main/java/com/baeldung/properties/ConfProperties.java @@ -6,13 +6,13 @@ import org.springframework.stereotype.Component; @Component public class ConfProperties { - @Value("${url}") + @Value("${db.url}") private String url; - @Value("${username}") + @Value("${db.username}") private String username; - @Value("${password}") + @Value("${db.password}") private String password; public String getUrl() { diff --git a/spring-boot-ops/src/main/resources/external/conf.properties b/spring-boot-ops/src/main/resources/external/conf.properties index cfcd23dc76..944b31cc5c 100644 --- a/spring-boot-ops/src/main/resources/external/conf.properties +++ b/spring-boot-ops/src/main/resources/external/conf.properties @@ -1,4 +1,4 @@ -url=jdbc:postgresql://localhost:5432/ -username=admin -password=root +db.url=jdbc:postgresql://localhost:5432/ +db.username=admin +db.password=root spring.main.allow-bean-definition-overriding=true \ No newline at end of file diff --git a/spring-boot-ops/src/test/java/com/baeldung/restart/RestartApplicationIntegrationTest.java b/spring-boot-ops/src/test/java/com/baeldung/restart/RestartApplicationManualTest.java similarity index 84% rename from spring-boot-ops/src/test/java/com/baeldung/restart/RestartApplicationIntegrationTest.java rename to spring-boot-ops/src/test/java/com/baeldung/restart/RestartApplicationManualTest.java index 14e80c3ac7..35b488f9d8 100644 --- a/spring-boot-ops/src/test/java/com/baeldung/restart/RestartApplicationIntegrationTest.java +++ b/spring-boot-ops/src/test/java/com/baeldung/restart/RestartApplicationManualTest.java @@ -2,17 +2,16 @@ package com.baeldung.restart; import static org.junit.Assert.assertEquals; -import org.junit.After; -import org.junit.Before; import org.junit.Test; import org.springframework.boot.test.web.client.TestRestTemplate; -import org.springframework.boot.web.client.RestTemplateBuilder; import org.springframework.http.HttpMethod; import org.springframework.http.ResponseEntity; -import java.time.Duration; - -public class RestartApplicationIntegrationTest { +/** + * We have to make sure that while running this test, 8080 and 8090 ports are free. + * Otherwise it will fail. + */ +public class RestartApplicationManualTest { private TestRestTemplate restTemplate = new TestRestTemplate(); diff --git a/spring-cloud-bus/spring-cloud-config-client/src/test/java/org/baeldung/SpringContextIntegrationTest.java b/spring-cloud-bus/spring-cloud-config-client/src/test/java/org/baeldung/SpringContextIntegrationTest.java deleted file mode 100644 index c0298daeb1..0000000000 --- a/spring-cloud-bus/spring-cloud-config-client/src/test/java/org/baeldung/SpringContextIntegrationTest.java +++ /dev/null @@ -1,18 +0,0 @@ -package org.baeldung; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.test.context.junit4.SpringRunner; - -import com.baeldung.SpringCloudConfigClientApplication; - -@RunWith(SpringRunner.class) -@SpringBootTest(classes = SpringCloudConfigClientApplication.class) -public class SpringContextIntegrationTest { - - @Test - public void contextLoads() { - } - -} diff --git a/spring-cloud-bus/spring-cloud-config-client/src/test/java/org/baeldung/SpringContextLiveTest.java b/spring-cloud-bus/spring-cloud-config-client/src/test/java/org/baeldung/SpringContextLiveTest.java index 8a33efef43..a401d41e1e 100644 --- a/spring-cloud-bus/spring-cloud-config-client/src/test/java/org/baeldung/SpringContextLiveTest.java +++ b/spring-cloud-bus/spring-cloud-config-client/src/test/java/org/baeldung/SpringContextLiveTest.java @@ -7,12 +7,27 @@ import org.springframework.test.context.junit4.SpringRunner; import com.baeldung.SpringCloudConfigClientApplication; +/** + * This LiveTest requires: + * 1- The 'spring-cloud-bus/spring-cloud-config-server' service running, with valid Spring Cloud Config properties, for instance: + * 1.1- `spring.cloud.config.server.git.uri` pointing to a valid URI, which has `user.role` and `user.password` properties configured in a properties file. For example, to achieve this in a dev environment: + * 1.1.1- `cd my/custom/path` + * 1.1.2- `git init .` + * 1.1.3- `echo user.role=Programmer >> application.properties` + * 1.1.4- `echo user.password=d3v3L >> application.properties` + * 1.1.5- `git add .` + * 1.1.6- `git commit -m 'inital commit'` + * 1.1.7- 'spring-cloud-bus/spring-cloud-config-server' with property `spring.cloud.config.server.git.uri=my/custom/path` + * + * Note: This is enough to run the ContextTest successfully, but to get the full functionality shown in the related article, we'll need also a RabbitMQ instance running (e.g. `docker run -d --hostname my-rabbit --name some-rabbit -p 15672:15672 -p 5672:5672 rabbitmq:3-management`) + * + */ @RunWith(SpringRunner.class) @SpringBootTest(classes = SpringCloudConfigClientApplication.class) public class SpringContextLiveTest { - @Test - public void contextLoads() { - } + @Test + public void contextLoads() { + } } diff --git a/spring-cloud-data-flow/spring-cloud-data-flow-stream-processing/data-flow-server/pom.xml b/spring-cloud-data-flow/spring-cloud-data-flow-stream-processing/data-flow-server/pom.xml index cf583c216a..7166b0fd32 100644 --- a/spring-cloud-data-flow/spring-cloud-data-flow-stream-processing/data-flow-server/pom.xml +++ b/spring-cloud-data-flow/spring-cloud-data-flow-stream-processing/data-flow-server/pom.xml @@ -1,5 +1,6 @@ - 4.0.0 data-flow-server @@ -12,7 +13,7 @@ parent-boot-1 com.baeldung 0.0.1-SNAPSHOT - ../../parent-boot-1 + ../../../parent-boot-1 @@ -20,6 +21,16 @@ org.springframework.cloud spring-cloud-starter-dataflow-server-local + + org.hibernate + hibernate-core + ${hibernate.compatible.version} + + + org.hibernate + hibernate-entitymanager + ${hibernate.compatible.version} + @@ -42,8 +53,9 @@ - 1.1.0.RELEASE - Brixton.SR7 + 1.3.1.RELEASE + Edgware.SR6 + 5.2.12.Final diff --git a/spring-cloud-data-flow/spring-cloud-data-flow-stream-processing/data-flow-server/src/main/resources/application.properties b/spring-cloud-data-flow/spring-cloud-data-flow-stream-processing/data-flow-server/src/main/resources/application.properties new file mode 100644 index 0000000000..20ad5d5bcb --- /dev/null +++ b/spring-cloud-data-flow/spring-cloud-data-flow-stream-processing/data-flow-server/src/main/resources/application.properties @@ -0,0 +1,2 @@ +#spring.datasource.url=jdbc:h2:mem:dataflow +spring.jpa.database-platform=org.hibernate.dialect.H2Dialect \ No newline at end of file diff --git a/spring-cloud-data-flow/spring-cloud-data-flow-stream-processing/data-flow-server/src/test/java/org/baeldung/SpringContextLiveTest.java b/spring-cloud-data-flow/spring-cloud-data-flow-stream-processing/data-flow-server/src/test/java/org/baeldung/SpringContextLiveTest.java deleted file mode 100644 index 980c096f5e..0000000000 --- a/spring-cloud-data-flow/spring-cloud-data-flow-stream-processing/data-flow-server/src/test/java/org/baeldung/SpringContextLiveTest.java +++ /dev/null @@ -1,16 +0,0 @@ -package org.baeldung; - -import org.baeldung.spring.cloud.DataFlowServerApplication; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.test.context.junit4.SpringRunner; - -@RunWith(SpringRunner.class) -@SpringBootTest(classes = DataFlowServerApplication.class) -public class SpringContextLiveTest { - - @Test - public void whenSpringContextIsBootstrapped_thenNoExceptions() { - } -} diff --git a/spring-cloud-data-flow/spring-cloud-data-flow-stream-processing/data-flow-server/src/test/resources/application.properties b/spring-cloud-data-flow/spring-cloud-data-flow-stream-processing/data-flow-server/src/test/resources/application.properties new file mode 100644 index 0000000000..70e3e5c65b --- /dev/null +++ b/spring-cloud-data-flow/spring-cloud-data-flow-stream-processing/data-flow-server/src/test/resources/application.properties @@ -0,0 +1 @@ +spring.datasource.url=jdbc:h2:mem:dataflow \ No newline at end of file diff --git a/spring-cloud-data-flow/spring-cloud-data-flow-stream-processing/data-flow-shell/pom.xml b/spring-cloud-data-flow/spring-cloud-data-flow-stream-processing/data-flow-shell/pom.xml index 52cb204201..9c379624d3 100644 --- a/spring-cloud-data-flow/spring-cloud-data-flow-stream-processing/data-flow-shell/pom.xml +++ b/spring-cloud-data-flow/spring-cloud-data-flow-stream-processing/data-flow-shell/pom.xml @@ -12,7 +12,7 @@ parent-boot-1 com.baeldung 0.0.1-SNAPSHOT - ../../parent-boot-1 + ../../../parent-boot-1 diff --git a/spring-cloud-data-flow/spring-cloud-data-flow-stream-processing/log-sink/pom.xml b/spring-cloud-data-flow/spring-cloud-data-flow-stream-processing/log-sink/pom.xml index ecabb91a98..73fb82d79f 100644 --- a/spring-cloud-data-flow/spring-cloud-data-flow-stream-processing/log-sink/pom.xml +++ b/spring-cloud-data-flow/spring-cloud-data-flow-stream-processing/log-sink/pom.xml @@ -12,7 +12,7 @@ parent-boot-1 com.baeldung 0.0.1-SNAPSHOT - ../../parent-boot-1 + ../../../parent-boot-1 diff --git a/spring-cloud-data-flow/spring-cloud-data-flow-stream-processing/time-processor/pom.xml b/spring-cloud-data-flow/spring-cloud-data-flow-stream-processing/time-processor/pom.xml index de354ca698..28a9d3e6e0 100644 --- a/spring-cloud-data-flow/spring-cloud-data-flow-stream-processing/time-processor/pom.xml +++ b/spring-cloud-data-flow/spring-cloud-data-flow-stream-processing/time-processor/pom.xml @@ -12,7 +12,7 @@ parent-boot-1 com.baeldung 0.0.1-SNAPSHOT - ../../parent-boot-1 + ../../../parent-boot-1 diff --git a/spring-cloud-data-flow/spring-cloud-data-flow-stream-processing/time-source/pom.xml b/spring-cloud-data-flow/spring-cloud-data-flow-stream-processing/time-source/pom.xml index a4d06e13d2..3b748fd7df 100644 --- a/spring-cloud-data-flow/spring-cloud-data-flow-stream-processing/time-source/pom.xml +++ b/spring-cloud-data-flow/spring-cloud-data-flow-stream-processing/time-source/pom.xml @@ -12,7 +12,7 @@ parent-boot-1 com.baeldung 0.0.1-SNAPSHOT - ../../parent-boot-1 + ../../../parent-boot-1 diff --git a/spring-cloud/spring-cloud-task/springcloudtaskbatch/pom.xml b/spring-cloud/spring-cloud-task/springcloudtaskbatch/pom.xml index 89c9318c4e..b6b5a1cf0f 100644 --- a/spring-cloud/spring-cloud-task/springcloudtaskbatch/pom.xml +++ b/spring-cloud/spring-cloud-task/springcloudtaskbatch/pom.xml @@ -1,4 +1,5 @@ - 4.0.0 org.baeldung.cloud @@ -43,6 +44,11 @@ org.springframework.cloud spring-cloud-task-batch + + com.h2database + h2 + test + diff --git a/spring-cloud/spring-cloud-task/springcloudtaskbatch/src/main/java/com/baeldung/task/TaskDemo.java b/spring-cloud/spring-cloud-task/springcloudtaskbatch/src/main/java/com/baeldung/task/TaskDemo.java index be2a173589..30e17cb956 100644 --- a/spring-cloud/spring-cloud-task/springcloudtaskbatch/src/main/java/com/baeldung/task/TaskDemo.java +++ b/spring-cloud/spring-cloud-task/springcloudtaskbatch/src/main/java/com/baeldung/task/TaskDemo.java @@ -14,6 +14,13 @@ import org.springframework.cloud.task.configuration.EnableTask; import org.springframework.context.annotation.Bean; import org.springframework.stereotype.Component; +/** + * This Application requires: + * * a MySql instance running, that allows a root user with no password, and with a database named + * + * (e.g. with the following command `docker run -p 3306:3306 --name bael-mysql -e MYSQL_ALLOW_EMPTY_PASSWORD=true -e MYSQL_DATABASE=springcloud mysql:latest`) + * + */ @SpringBootApplication @EnableTask @EnableBatchProcessing diff --git a/spring-cloud/spring-cloud-task/springcloudtaskbatch/src/test/java/org/baeldung/SpringContextLiveTest.java b/spring-cloud/spring-cloud-task/springcloudtaskbatch/src/test/java/org/baeldung/SpringContextIntegrationTest.java similarity index 60% rename from spring-cloud/spring-cloud-task/springcloudtaskbatch/src/test/java/org/baeldung/SpringContextLiveTest.java rename to spring-cloud/spring-cloud-task/springcloudtaskbatch/src/test/java/org/baeldung/SpringContextIntegrationTest.java index ddbcbf65ea..0caa626c14 100644 --- a/spring-cloud/spring-cloud-task/springcloudtaskbatch/src/test/java/org/baeldung/SpringContextLiveTest.java +++ b/spring-cloud/spring-cloud-task/springcloudtaskbatch/src/test/java/org/baeldung/SpringContextIntegrationTest.java @@ -10,18 +10,10 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.baeldung.task.JobConfiguration; import com.baeldung.task.TaskDemo; -/** - * This Live Test requires: - * * a MySql instance running, that allows a root user with no password, and with a database named - * - * (e.g. with the following command `docker run -p 3306:3306 --name bael-mysql -e MYSQL_ALLOW_EMPTY_PASSWORD=true -e MYSQL_DATABASE=springcloud mysql:latest`) - * - */ @RunWith(SpringJUnit4ClassRunner.class) @SpringBootApplication -@ContextConfiguration(classes = { JobConfiguration.class, TaskDemo.class }, initializers = { - ConfigFileApplicationContextInitializer.class }) -public class SpringContextLiveTest { +@ContextConfiguration(classes = { JobConfiguration.class, TaskDemo.class }, initializers = { ConfigFileApplicationContextInitializer.class }) +public class SpringContextIntegrationTest { @Test public void whenSpringContextIsBootstrapped_thenNoExceptions() { diff --git a/spring-cloud/spring-cloud-task/springcloudtaskbatch/src/test/resources/application.yml b/spring-cloud/spring-cloud-task/springcloudtaskbatch/src/test/resources/application.yml new file mode 100644 index 0000000000..794ac4d247 --- /dev/null +++ b/spring-cloud/spring-cloud-task/springcloudtaskbatch/src/test/resources/application.yml @@ -0,0 +1,11 @@ +spring: + datasource: + url: jdbc:h2:mem:springcloud + username: sa + password: + jpa: + hibernate: + ddl-auto: create-drop + properties: + hibernate: + dialect: org.hibernate.dialect.H2Dialect \ No newline at end of file diff --git a/spring-rest-compress/src/main/java/org/baeldung/spring/rest/compress/GzipUtils.java b/spring-rest-compress/src/main/java/org/baeldung/spring/rest/compress/GzipUtils.java index f3cb8bd631..75420ca6d8 100644 --- a/spring-rest-compress/src/main/java/org/baeldung/spring/rest/compress/GzipUtils.java +++ b/spring-rest-compress/src/main/java/org/baeldung/spring/rest/compress/GzipUtils.java @@ -30,15 +30,11 @@ public class GzipUtils { * @throws IOException */ public static byte[] compress(byte[] body) throws IOException { - ByteArrayOutputStream output = new ByteArrayOutputStream(); - try { - try (GZIPOutputStream gzipOutputStream = new GZIPOutputStream(output)) { - gzipOutputStream.write(body); - } - } finally { - output.close(); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + try (GZIPOutputStream gzipOutputStream = new GZIPOutputStream(baos)) { + gzipOutputStream.write(body); } - return output.toByteArray(); + return baos.toByteArray(); } /** diff --git a/testing-modules/README.md b/testing-modules/README.md index d534e5cea6..21fc87a67f 100644 --- a/testing-modules/README.md +++ b/testing-modules/README.md @@ -1,5 +1,4 @@ ## Testing Modules -This is a aggregator module containing several modules focused on testing libraries. - +This is a aggregator module containing several modules focused on testing libraries. diff --git a/testing-modules/assertion-libraries/README.md b/testing-modules/assertion-libraries/README.md new file mode 100644 index 0000000000..d69457fdeb --- /dev/null +++ b/testing-modules/assertion-libraries/README.md @@ -0,0 +1,13 @@ + +## Relevant Articles + +- [AssertJ’s Java 8 Features](http://www.baeldung.com/assertJ-java-8-features) +- [AssertJ for Guava](http://www.baeldung.com/assertJ-for-guava) +- [Introduction to AssertJ](http://www.baeldung.com/introduction-to-assertj) +- [Testing with Google Truth](http://www.baeldung.com/google-truth) +- [Testing with JGoTesting](http://www.baeldung.com/jgotesting) +- [Guide to JSpec](http://www.baeldung.com/jspec) +- [Custom Assertions with AssertJ](http://www.baeldung.com/assertj-custom-assertion) +- [Using Conditions with AssertJ Assertions](http://www.baeldung.com/assertj-conditions) +- [AssertJ Exception Assertions](http://www.baeldung.com/assertj-exception-assertion) + diff --git a/testing-modules/assertion-libraries/pom.xml b/testing-modules/assertion-libraries/pom.xml new file mode 100644 index 0000000000..9d60d42533 --- /dev/null +++ b/testing-modules/assertion-libraries/pom.xml @@ -0,0 +1,75 @@ + + 4.0.0 + com.baeldung + assertion-libraries + 0.1-SNAPSHOT + assertion-libraries + + + com.baeldung + testing-modules + 1.0.0-SNAPSHOT + + + + + com.google.truth + truth + ${truth.version} + + + com.google.truth.extensions + truth-java8-extension + ${truth.version} + test + + + org.assertj + assertj-guava + ${assertj-guava.version} + + + org.assertj + assertj-core + ${assertj-core.version} + test + + + org.javalite + javalite-common + ${javalite.version} + + + org.jgotesting + jgotesting + ${jgotesting.version} + test + + + + + + + org.assertj + assertj-assertions-generator-maven-plugin + ${assertj-generator.version} + + + com.baeldung.testing.assertj.custom.Person + + + + + + + + 0.32 + 3.1.0 + 3.9.0 + 2.1.0 + 1.4.13 + 0.12 + + + diff --git a/testing-modules/testing/src/main/java/com/baeldung/testing/assertj/Dog.java b/testing-modules/assertion-libraries/src/main/java/com/baeldung/assertj/Dog.java similarity index 88% rename from testing-modules/testing/src/main/java/com/baeldung/testing/assertj/Dog.java rename to testing-modules/assertion-libraries/src/main/java/com/baeldung/assertj/Dog.java index 7a1c158500..6c049e1a10 100644 --- a/testing-modules/testing/src/main/java/com/baeldung/testing/assertj/Dog.java +++ b/testing-modules/assertion-libraries/src/main/java/com/baeldung/assertj/Dog.java @@ -1,4 +1,4 @@ -package com.baeldung.testing.assertj; +package com.baeldung.assertj; public class Dog { private String name; diff --git a/testing-modules/testing/src/main/java/com/baeldung/testing/assertj/Member.java b/testing-modules/assertion-libraries/src/main/java/com/baeldung/assertj/Member.java similarity index 88% rename from testing-modules/testing/src/main/java/com/baeldung/testing/assertj/Member.java rename to testing-modules/assertion-libraries/src/main/java/com/baeldung/assertj/Member.java index a0b3d0daac..baf3c2df52 100644 --- a/testing-modules/testing/src/main/java/com/baeldung/testing/assertj/Member.java +++ b/testing-modules/assertion-libraries/src/main/java/com/baeldung/assertj/Member.java @@ -1,4 +1,4 @@ -package com.baeldung.testing.assertj; +package com.baeldung.assertj; public class Member { private String name; diff --git a/testing-modules/testing/src/main/java/com/baeldung/testing/assertj/Person.java b/testing-modules/assertion-libraries/src/main/java/com/baeldung/assertj/Person.java similarity index 88% rename from testing-modules/testing/src/main/java/com/baeldung/testing/assertj/Person.java rename to testing-modules/assertion-libraries/src/main/java/com/baeldung/assertj/Person.java index 43e9ff8884..09b16b4f5b 100644 --- a/testing-modules/testing/src/main/java/com/baeldung/testing/assertj/Person.java +++ b/testing-modules/assertion-libraries/src/main/java/com/baeldung/assertj/Person.java @@ -1,4 +1,4 @@ -package com.baeldung.testing.assertj; +package com.baeldung.assertj; public class Person { private String name; diff --git a/testing-modules/testing/src/main/java/com/baeldung/testing/assertj/custom/Person.java b/testing-modules/assertion-libraries/src/main/java/com/baeldung/assertj/custom/Person.java similarity index 93% rename from testing-modules/testing/src/main/java/com/baeldung/testing/assertj/custom/Person.java rename to testing-modules/assertion-libraries/src/main/java/com/baeldung/assertj/custom/Person.java index 34afc480e4..5506a56b51 100644 --- a/testing-modules/testing/src/main/java/com/baeldung/testing/assertj/custom/Person.java +++ b/testing-modules/assertion-libraries/src/main/java/com/baeldung/assertj/custom/Person.java @@ -1,4 +1,4 @@ -package com.baeldung.testing.assertj.custom; +package com.baeldung.assertj.custom; import java.util.ArrayList; import java.util.List; diff --git a/testing-modules/testing/src/main/java/com/baeldung/jspec/Animal.java b/testing-modules/assertion-libraries/src/main/java/com/baeldung/jspec/Animal.java similarity index 100% rename from testing-modules/testing/src/main/java/com/baeldung/jspec/Animal.java rename to testing-modules/assertion-libraries/src/main/java/com/baeldung/jspec/Animal.java diff --git a/testing-modules/testing/src/main/java/com/baeldung/jspec/Cage.java b/testing-modules/assertion-libraries/src/main/java/com/baeldung/jspec/Cage.java similarity index 100% rename from testing-modules/testing/src/main/java/com/baeldung/jspec/Cage.java rename to testing-modules/assertion-libraries/src/main/java/com/baeldung/jspec/Cage.java diff --git a/testing-modules/testing/src/main/java/com/baeldung/jspec/Cat.java b/testing-modules/assertion-libraries/src/main/java/com/baeldung/jspec/Cat.java similarity index 100% rename from testing-modules/testing/src/main/java/com/baeldung/jspec/Cat.java rename to testing-modules/assertion-libraries/src/main/java/com/baeldung/jspec/Cat.java diff --git a/testing-modules/testing/src/main/java/com/baeldung/jspec/Dog.java b/testing-modules/assertion-libraries/src/main/java/com/baeldung/jspec/Dog.java similarity index 100% rename from testing-modules/testing/src/main/java/com/baeldung/jspec/Dog.java rename to testing-modules/assertion-libraries/src/main/java/com/baeldung/jspec/Dog.java diff --git a/testing-modules/testing/src/main/java/com/baeldung/testing/truth/User.java b/testing-modules/assertion-libraries/src/main/java/com/baeldung/truth/User.java similarity index 97% rename from testing-modules/testing/src/main/java/com/baeldung/testing/truth/User.java rename to testing-modules/assertion-libraries/src/main/java/com/baeldung/truth/User.java index fe857dd265..2b5ffde19f 100644 --- a/testing-modules/testing/src/main/java/com/baeldung/testing/truth/User.java +++ b/testing-modules/assertion-libraries/src/main/java/com/baeldung/truth/User.java @@ -1,4 +1,4 @@ -package com.baeldung.testing.truth; +package com.baeldung.truth; import java.util.Arrays; import java.util.List; diff --git a/testing-modules/testing/src/main/java/com/baeldung/testing/truth/UserSubject.java b/testing-modules/assertion-libraries/src/main/java/com/baeldung/truth/UserSubject.java similarity index 97% rename from testing-modules/testing/src/main/java/com/baeldung/testing/truth/UserSubject.java rename to testing-modules/assertion-libraries/src/main/java/com/baeldung/truth/UserSubject.java index 2fd84085a0..b478043ad8 100644 --- a/testing-modules/testing/src/main/java/com/baeldung/testing/truth/UserSubject.java +++ b/testing-modules/assertion-libraries/src/main/java/com/baeldung/truth/UserSubject.java @@ -1,4 +1,4 @@ -package com.baeldung.testing.truth; +package com.baeldung.truth; import com.google.common.truth.ComparableSubject; import com.google.common.truth.FailureStrategy; diff --git a/testing-modules/testing/src/test/java/com/baeldung/testing/assertj/AssertJConditionUnitTest.java b/testing-modules/assertion-libraries/src/test/java/com/baeldung/assertj/AssertJConditionUnitTest.java similarity index 98% rename from testing-modules/testing/src/test/java/com/baeldung/testing/assertj/AssertJConditionUnitTest.java rename to testing-modules/assertion-libraries/src/test/java/com/baeldung/assertj/AssertJConditionUnitTest.java index 6fa09d8dfc..ec2d93500f 100644 --- a/testing-modules/testing/src/test/java/com/baeldung/testing/assertj/AssertJConditionUnitTest.java +++ b/testing-modules/assertion-libraries/src/test/java/com/baeldung/assertj/AssertJConditionUnitTest.java @@ -1,4 +1,4 @@ -package com.baeldung.testing.assertj; +package com.baeldung.assertj; import static org.assertj.core.api.Assertions.allOf; import static org.assertj.core.api.Assertions.anyOf; diff --git a/testing-modules/testing/src/test/java/com/baeldung/testing/assertj/AssertJCoreUnitTest.java b/testing-modules/assertion-libraries/src/test/java/com/baeldung/assertj/AssertJCoreUnitTest.java similarity index 98% rename from testing-modules/testing/src/test/java/com/baeldung/testing/assertj/AssertJCoreUnitTest.java rename to testing-modules/assertion-libraries/src/test/java/com/baeldung/assertj/AssertJCoreUnitTest.java index 6836bb79c5..73b9b373a1 100644 --- a/testing-modules/testing/src/test/java/com/baeldung/testing/assertj/AssertJCoreUnitTest.java +++ b/testing-modules/assertion-libraries/src/test/java/com/baeldung/assertj/AssertJCoreUnitTest.java @@ -1,4 +1,4 @@ -package com.baeldung.testing.assertj; +package com.baeldung.assertj; import org.assertj.core.util.Maps; import org.junit.Ignore; diff --git a/testing-modules/testing/src/test/java/com/baeldung/testing/assertj/AssertJGuavaUnitTest.java b/testing-modules/assertion-libraries/src/test/java/com/baeldung/assertj/AssertJGuavaUnitTest.java similarity index 98% rename from testing-modules/testing/src/test/java/com/baeldung/testing/assertj/AssertJGuavaUnitTest.java rename to testing-modules/assertion-libraries/src/test/java/com/baeldung/assertj/AssertJGuavaUnitTest.java index 2056d7e61a..6a552aee78 100644 --- a/testing-modules/testing/src/test/java/com/baeldung/testing/assertj/AssertJGuavaUnitTest.java +++ b/testing-modules/assertion-libraries/src/test/java/com/baeldung/assertj/AssertJGuavaUnitTest.java @@ -1,4 +1,4 @@ -package com.baeldung.testing.assertj; +package com.baeldung.assertj; import com.google.common.base.Optional; import com.google.common.collect.ArrayListMultimap; diff --git a/testing-modules/testing/src/test/java/com/baeldung/testing/assertj/AssertJJava8UnitTest.java b/testing-modules/assertion-libraries/src/test/java/com/baeldung/assertj/AssertJJava8UnitTest.java similarity index 98% rename from testing-modules/testing/src/test/java/com/baeldung/testing/assertj/AssertJJava8UnitTest.java rename to testing-modules/assertion-libraries/src/test/java/com/baeldung/assertj/AssertJJava8UnitTest.java index 7ec6d0caa7..a2f58d677d 100644 --- a/testing-modules/testing/src/test/java/com/baeldung/testing/assertj/AssertJJava8UnitTest.java +++ b/testing-modules/assertion-libraries/src/test/java/com/baeldung/assertj/AssertJJava8UnitTest.java @@ -1,4 +1,4 @@ -package com.baeldung.testing.assertj; +package com.baeldung.assertj; import org.junit.Test; diff --git a/testing-modules/testing/src/test/java/com/baeldung/testing/assertj/custom/AssertJCustomAssertionsUnitTest.java b/testing-modules/assertion-libraries/src/test/java/com/baeldung/assertj/custom/AssertJCustomAssertionsUnitTest.java similarity index 91% rename from testing-modules/testing/src/test/java/com/baeldung/testing/assertj/custom/AssertJCustomAssertionsUnitTest.java rename to testing-modules/assertion-libraries/src/test/java/com/baeldung/assertj/custom/AssertJCustomAssertionsUnitTest.java index 4c09311bac..98f50568a8 100644 --- a/testing-modules/testing/src/test/java/com/baeldung/testing/assertj/custom/AssertJCustomAssertionsUnitTest.java +++ b/testing-modules/assertion-libraries/src/test/java/com/baeldung/assertj/custom/AssertJCustomAssertionsUnitTest.java @@ -1,6 +1,6 @@ -package com.baeldung.testing.assertj.custom; +package com.baeldung.assertj.custom; -import static com.baeldung.testing.assertj.custom.Assertions.assertThat; +import static com.baeldung.assertj.custom.Assertions.assertThat; import static org.junit.Assert.fail; import org.junit.Rule; diff --git a/testing-modules/testing/src/test/java/com/baeldung/testing/assertj/custom/Assertions.java b/testing-modules/assertion-libraries/src/test/java/com/baeldung/assertj/custom/Assertions.java similarity index 81% rename from testing-modules/testing/src/test/java/com/baeldung/testing/assertj/custom/Assertions.java rename to testing-modules/assertion-libraries/src/test/java/com/baeldung/assertj/custom/Assertions.java index fcffb8fc6c..3e1021851e 100644 --- a/testing-modules/testing/src/test/java/com/baeldung/testing/assertj/custom/Assertions.java +++ b/testing-modules/assertion-libraries/src/test/java/com/baeldung/assertj/custom/Assertions.java @@ -1,4 +1,4 @@ -package com.baeldung.testing.assertj.custom; +package com.baeldung.assertj.custom; public class Assertions { public static PersonAssert assertThat(Person actual) { diff --git a/testing-modules/testing/src/test/java/com/baeldung/testing/assertj/custom/PersonAssert.java b/testing-modules/assertion-libraries/src/test/java/com/baeldung/assertj/custom/PersonAssert.java similarity index 95% rename from testing-modules/testing/src/test/java/com/baeldung/testing/assertj/custom/PersonAssert.java rename to testing-modules/assertion-libraries/src/test/java/com/baeldung/assertj/custom/PersonAssert.java index d6cc585e96..5be093644a 100644 --- a/testing-modules/testing/src/test/java/com/baeldung/testing/assertj/custom/PersonAssert.java +++ b/testing-modules/assertion-libraries/src/test/java/com/baeldung/assertj/custom/PersonAssert.java @@ -1,4 +1,4 @@ -package com.baeldung.testing.assertj.custom; +package com.baeldung.assertj.custom; import org.assertj.core.api.AbstractAssert; diff --git a/testing-modules/testing/src/test/java/com/baeldung/testing/assertj/exceptions/Java7StyleAssertions.java b/testing-modules/assertion-libraries/src/test/java/com/baeldung/assertj/exceptions/Java7StyleAssertions.java similarity index 94% rename from testing-modules/testing/src/test/java/com/baeldung/testing/assertj/exceptions/Java7StyleAssertions.java rename to testing-modules/assertion-libraries/src/test/java/com/baeldung/assertj/exceptions/Java7StyleAssertions.java index 07a5be1118..ab93f8eac7 100644 --- a/testing-modules/testing/src/test/java/com/baeldung/testing/assertj/exceptions/Java7StyleAssertions.java +++ b/testing-modules/assertion-libraries/src/test/java/com/baeldung/assertj/exceptions/Java7StyleAssertions.java @@ -1,4 +1,4 @@ -package com.baeldung.testing.assertj.exceptions; +package com.baeldung.assertj.exceptions; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.fail; diff --git a/testing-modules/testing/src/test/java/com/baeldung/testing/assertj/exceptions/Java8StyleAssertions.java b/testing-modules/assertion-libraries/src/test/java/com/baeldung/assertj/exceptions/Java8StyleAssertions.java similarity index 97% rename from testing-modules/testing/src/test/java/com/baeldung/testing/assertj/exceptions/Java8StyleAssertions.java rename to testing-modules/assertion-libraries/src/test/java/com/baeldung/assertj/exceptions/Java8StyleAssertions.java index 973b921654..4d4e2aedfc 100644 --- a/testing-modules/testing/src/test/java/com/baeldung/testing/assertj/exceptions/Java8StyleAssertions.java +++ b/testing-modules/assertion-libraries/src/test/java/com/baeldung/assertj/exceptions/Java8StyleAssertions.java @@ -1,4 +1,4 @@ -package com.baeldung.testing.assertj.exceptions; +package com.baeldung.assertj.exceptions; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.assertj.core.api.Assertions.assertThat; diff --git a/testing-modules/testing/src/test/java/com/baeldung/testing/jgotesting/JGoTestingUnitTest.java b/testing-modules/assertion-libraries/src/test/java/com/baeldung/jgotesting/JGoTestingUnitTest.java similarity index 100% rename from testing-modules/testing/src/test/java/com/baeldung/testing/jgotesting/JGoTestingUnitTest.java rename to testing-modules/assertion-libraries/src/test/java/com/baeldung/jgotesting/JGoTestingUnitTest.java diff --git a/testing-modules/testing/src/test/java/com/baeldung/jspec/CageUnitTest.java b/testing-modules/assertion-libraries/src/test/java/com/baeldung/jspec/CageUnitTest.java similarity index 100% rename from testing-modules/testing/src/test/java/com/baeldung/jspec/CageUnitTest.java rename to testing-modules/assertion-libraries/src/test/java/com/baeldung/jspec/CageUnitTest.java diff --git a/testing-modules/testing/src/test/java/com/baeldung/jspec/JSpecUnitTest.java b/testing-modules/assertion-libraries/src/test/java/com/baeldung/jspec/JSpecUnitTest.java similarity index 100% rename from testing-modules/testing/src/test/java/com/baeldung/jspec/JSpecUnitTest.java rename to testing-modules/assertion-libraries/src/test/java/com/baeldung/jspec/JSpecUnitTest.java diff --git a/testing-modules/testing/src/test/java/com/baeldung/testing/truth/GoogleTruthUnitTest.java b/testing-modules/assertion-libraries/src/test/java/com/baeldung/truth/GoogleTruthUnitTest.java similarity index 99% rename from testing-modules/testing/src/test/java/com/baeldung/testing/truth/GoogleTruthUnitTest.java rename to testing-modules/assertion-libraries/src/test/java/com/baeldung/truth/GoogleTruthUnitTest.java index 5be27db9db..b7919a29ca 100644 --- a/testing-modules/testing/src/test/java/com/baeldung/testing/truth/GoogleTruthUnitTest.java +++ b/testing-modules/assertion-libraries/src/test/java/com/baeldung/truth/GoogleTruthUnitTest.java @@ -1,4 +1,4 @@ -package com.baeldung.testing.truth; +package com.baeldung.truth; import com.google.common.collect.ArrayListMultimap; import com.google.common.collect.Multimap; @@ -6,7 +6,7 @@ import com.google.common.collect.Range; import com.google.common.collect.Table; import com.google.common.collect.TreeBasedTable; import com.google.common.collect.TreeMultiset; -import static com.baeldung.testing.truth.UserSubject.*; +import static com.baeldung.truth.UserSubject.*; import static com.google.common.truth.Truth.*; import static com.google.common.truth.Truth8.*; import java.math.BigDecimal; diff --git a/testing-modules/gatling/pom.xml b/testing-modules/gatling/pom.xml index e708d939e4..37693ebfee 100644 --- a/testing-modules/gatling/pom.xml +++ b/testing-modules/gatling/pom.xml @@ -1,136 +1,136 @@ - - - 4.0.0 - org.baeldung - gatling - 1.0-SNAPSHOT - gatling - - - com.baeldung - parent-modules - 1.0.0-SNAPSHOT - ../../ - - - - - - io.gatling - gatling-app - ${gatling.version} - - - io.gatling - gatling-recorder - ${gatling.version} - - - io.gatling.highcharts - gatling-charts-highcharts - ${gatling.version} - - - org.scala-lang - scala-library - ${scala.version} - - - - - - - io.gatling.highcharts - gatling-charts-highcharts - - - io.gatling - gatling-app - - - io.gatling - gatling-recorder - - - org.scala-lang - scala-library - - - - - src/test/scala - - - - net.alchim31.maven - scala-maven-plugin - ${scala-maven-plugin.version} - - - - - - net.alchim31.maven - scala-maven-plugin - - - - testCompile - - - - - -Ydelambdafy:method - -target:jvm-1.8 - -deprecation - -feature - -unchecked - -language:implicitConversions - -language:postfixOps - - - - - - - - - - - simulation - - - - io.gatling - gatling-maven-plugin - ${gatling-maven-plugin.version} - - - test - - execute - - - true - - - - - - - - - - - 1.8 - 1.8 - UTF-8 - 2.12.6 - 2.3.1 - 3.2.2 - 2.2.4 - - - + + + 4.0.0 + org.baeldung + gatling + 1.0-SNAPSHOT + gatling + + + com.baeldung + parent-modules + 1.0.0-SNAPSHOT + ../../ + + + + + + io.gatling + gatling-app + ${gatling.version} + + + io.gatling + gatling-recorder + ${gatling.version} + + + io.gatling.highcharts + gatling-charts-highcharts + ${gatling.version} + + + org.scala-lang + scala-library + ${scala.version} + + + + + + + io.gatling.highcharts + gatling-charts-highcharts + + + io.gatling + gatling-app + + + io.gatling + gatling-recorder + + + org.scala-lang + scala-library + + + + + src/test/scala + + + + net.alchim31.maven + scala-maven-plugin + ${scala-maven-plugin.version} + + + + + + net.alchim31.maven + scala-maven-plugin + + + + testCompile + + + + + -Ydelambdafy:method + -target:jvm-1.8 + -deprecation + -feature + -unchecked + -language:implicitConversions + -language:postfixOps + + + + + + + + + + + simulation + + + + io.gatling + gatling-maven-plugin + ${gatling-maven-plugin.version} + + + test + + execute + + + true + + + + + + + + + + + 1.8 + 1.8 + UTF-8 + 2.12.6 + 2.3.1 + 3.2.2 + 2.2.4 + + + diff --git a/testing-modules/gatling/src/test/scala/Engine.scala b/testing-modules/gatling/src/test/scala/Engine.scala index 32c85fbe45..c2884fc218 100644 --- a/testing-modules/gatling/src/test/scala/Engine.scala +++ b/testing-modules/gatling/src/test/scala/Engine.scala @@ -1,13 +1,13 @@ -import io.gatling.app.Gatling -import io.gatling.core.config.GatlingPropertiesBuilder - -object Engine extends App { - - val props = new GatlingPropertiesBuilder - props.dataDirectory(IDEPathHelper.dataDirectory.toString) - props.resultsDirectory(IDEPathHelper.resultsDirectory.toString) - props.bodiesDirectory(IDEPathHelper.bodiesDirectory.toString) - props.binariesDirectory(IDEPathHelper.mavenBinariesDirectory.toString) - - Gatling.fromMap(props.build) -} +import io.gatling.app.Gatling +import io.gatling.core.config.GatlingPropertiesBuilder + +object Engine extends App { + + val props = new GatlingPropertiesBuilder + props.dataDirectory(IDEPathHelper.dataDirectory.toString) + props.resultsDirectory(IDEPathHelper.resultsDirectory.toString) + props.bodiesDirectory(IDEPathHelper.bodiesDirectory.toString) + props.binariesDirectory(IDEPathHelper.mavenBinariesDirectory.toString) + + Gatling.fromMap(props.build) +} diff --git a/testing-modules/gatling/src/test/scala/IDEPathHelper.scala b/testing-modules/gatling/src/test/scala/IDEPathHelper.scala index 0abf6a42ef..9fb1d7d5c8 100644 --- a/testing-modules/gatling/src/test/scala/IDEPathHelper.scala +++ b/testing-modules/gatling/src/test/scala/IDEPathHelper.scala @@ -1,22 +1,22 @@ -import java.nio.file.Path - -import io.gatling.commons.util.PathHelper._ - -object IDEPathHelper { - - val gatlingConfUrl: Path = getClass.getClassLoader.getResource("gatling.conf").toURI - val projectRootDir = gatlingConfUrl.ancestor(3) - - val mavenSourcesDirectory = projectRootDir / "src" / "test" / "scala" - val mavenResourcesDirectory = projectRootDir / "src" / "test" / "resources" - val mavenTargetDirectory = projectRootDir / "target" - val mavenBinariesDirectory = mavenTargetDirectory / "test-classes" - - val dataDirectory = mavenResourcesDirectory / "data" - val bodiesDirectory = mavenResourcesDirectory / "bodies" - - val recorderOutputDirectory = mavenSourcesDirectory - val resultsDirectory = mavenTargetDirectory / "gatling" - - val recorderConfigFile = mavenResourcesDirectory / "recorder.conf" -} +import java.nio.file.Path + +import io.gatling.commons.util.PathHelper._ + +object IDEPathHelper { + + val gatlingConfUrl: Path = getClass.getClassLoader.getResource("gatling.conf").toURI + val projectRootDir = gatlingConfUrl.ancestor(3) + + val mavenSourcesDirectory = projectRootDir / "src" / "test" / "scala" + val mavenResourcesDirectory = projectRootDir / "src" / "test" / "resources" + val mavenTargetDirectory = projectRootDir / "target" + val mavenBinariesDirectory = mavenTargetDirectory / "test-classes" + + val dataDirectory = mavenResourcesDirectory / "data" + val bodiesDirectory = mavenResourcesDirectory / "bodies" + + val recorderOutputDirectory = mavenSourcesDirectory + val resultsDirectory = mavenTargetDirectory / "gatling" + + val recorderConfigFile = mavenResourcesDirectory / "recorder.conf" +} diff --git a/testing-modules/gatling/src/test/scala/Recorder.scala b/testing-modules/gatling/src/test/scala/Recorder.scala index 6ad320618b..9c38e52f12 100644 --- a/testing-modules/gatling/src/test/scala/Recorder.scala +++ b/testing-modules/gatling/src/test/scala/Recorder.scala @@ -1,12 +1,12 @@ -import io.gatling.recorder.GatlingRecorder -import io.gatling.recorder.config.RecorderPropertiesBuilder - -object Recorder extends App { - - val props = new RecorderPropertiesBuilder - props.simulationOutputFolder(IDEPathHelper.recorderOutputDirectory.toString) - props.simulationPackage("org.baeldung") - props.bodiesFolder(IDEPathHelper.bodiesDirectory.toString) - - GatlingRecorder.fromMap(props.build, Some(IDEPathHelper.recorderConfigFile)) -} +import io.gatling.recorder.GatlingRecorder +import io.gatling.recorder.config.RecorderPropertiesBuilder + +object Recorder extends App { + + val props = new RecorderPropertiesBuilder + props.simulationOutputFolder(IDEPathHelper.recorderOutputDirectory.toString) + props.simulationPackage("org.baeldung") + props.bodiesFolder(IDEPathHelper.bodiesDirectory.toString) + + GatlingRecorder.fromMap(props.build, Some(IDEPathHelper.recorderConfigFile)) +} diff --git a/testing-modules/groovy-spock/report-2019-03-29.json b/testing-modules/groovy-spock/report-2019-03-29.json deleted file mode 100644 index 85f0b261fb..0000000000 --- a/testing-modules/groovy-spock/report-2019-03-29.json +++ /dev/null @@ -1,402 +0,0 @@ -loadLogFile([{ - "package": "mocks", - "name": "ExampleSpockTest", - "start": 1553898111660, - "features": [ - { - "name": "should calculate character occurrences in given string", - "start": 1553898111662, - "end": 1553898111699, - "result": "passed", - "attachments": [ - - ] - } - ], - "end": 1553898111709, - "result": "passed", - "attachments": [ - - ] -}]) - -loadLogFile([{ - "package": "mocks", - "name": "ItemServiceTest", - "start": 1553898111714, - "features": [ - { - "name": "should spy on EventPublisher method call", - "start": 1553898111714, - "output": [ - "I've published: item-id\n" - ], - "end": 1553898112250, - "result": "passed", - "attachments": [ - - ] - }, - { - "name": "should return items", - "start": 1553898112250, - "end": 1553898112260, - "result": "passed", - "attachments": [ - - ] - }, - { - "name": "should publish events about new non-empty saved offers", - "start": 1553898112260, - "end": 1553898112267, - "result": "passed", - "attachments": [ - - ] - }, - { - "name": "should return different items for different ids lists", - "start": 1553898112267, - "end": 1553898112280, - "result": "passed", - "attachments": [ - - ] - }, - { - "name": "should throw ExternalItemProviderException when ItemProvider fails", - "start": 1553898112281, - "end": 1553898112294, - "result": "passed", - "attachments": [ - - ] - }, - { - "name": "should return different items on subsequent call", - "start": 1553898112294, - "narrative": "When method is called for the first time\nThen empty list is returned\nWhen method is called for the second time\nThen item with id=1 is returned\nWhen method is called for the thirdtime\nThen item with id=2 is returned", - "end": 1553898112298, - "result": "passed", - "attachments": [ - - ] - }, - { - "name": "should return items sorted by name", - "start": 1553898112299, - "end": 1553898112307, - "result": "passed", - "attachments": [ - - ] - } - ], - "end": 1553898112310, - "result": "passed", - "attachments": [ - - ] -}]) - -loadLogFile([{ - "package": "FirstSpecification", - "name": "FirstSpecification", - "start": 1553898112314, - "features": [ - { - "name": "Should verify notify was called", - "start": 1553898112314, - "end": 1553898112324, - "result": "passed", - "attachments": [ - - ] - }, - { - "name": "Should return true value for mock", - "start": 1553898112325, - "end": 1553898112344, - "result": "passed", - "attachments": [ - - ] - }, - { - "name": "Should return default value for mock", - "start": 1553898112344, - "end": 1553898112347, - "result": "passed", - "attachments": [ - - ] - }, - { - "name": "numbers to the power of two", - "start": 1553898112347, - "end": 1553898112358, - "result": "passed", - "attachments": [ - - ] - }, - { - "name": "Should get an index out of bounds when removing a non-existent item", - "start": 1553898112358, - "end": 1553898112364, - "result": "passed", - "attachments": [ - - ] - }, - { - "name": "Should be able to remove from list", - "start": 1553898112364, - "end": 1553898112366, - "result": "passed", - "attachments": [ - - ] - }, - { - "name": "two plus two should equal four", - "start": 1553898112366, - "end": 1553898112368, - "result": "passed", - "attachments": [ - - ] - }, - { - "name": "one plus one should equal two", - "start": 1553898112368, - "end": 1553898112391, - "result": "passed", - "attachments": [ - - ] - } - ], - "end": 1553898112394, - "result": "passed", - "attachments": [ - - ] -}]) - -loadLogFile([{ - "package": "extensions", - "name": "IgnoreTest", - "start": 1553898112395, - "end": 1553898112395, - "result": "skipped" -}]) - -loadLogFile([{ - "package": "extensions", - "name": "RetryTest", - "start": 1553898112403, - "end": 1553898112405, - "result": "passed", - "attachments": [ - - ] -}]) - -loadLogFile([{ - "package": "extensions", - "name": "This title is easy to read for humans", - "start": 1553898112407, - "end": 1553898112408, - "result": "passed", - "attachments": [ - - ] -}]) - -loadLogFile([{ - "package": "extensions", - "name": "SeeTest", - "start": 1553898112409, - "end": 1553898112411, - "result": "passed", - "attachments": [ - - ] -}]) - -loadLogFile([{ - "package": "extensions", - "name": "StepwiseTest", - "start": 1553898112422, - "end": 1553898112423, - "result": "passed", - "attachments": [ - - ] -}]) - -loadLogFile([{ - "package": "extensions", - "name": "NarrativeDescriptionTest", - "start": 1553898112427, - "narrative": "as a user\n i want to save favourite items \n and then get the list of them", - "end": 1553898112433, - "result": "passed", - "attachments": [ - - ] -}]) - -loadLogFile([{ - "package": "extensions", - "name": "SubjectTest", - "start": 1553898112434, - "end": 1553898112436, - "result": "passed", - "attachments": [ - - ] -}]) - -loadLogFile([{ - "package": "extensions", - "name": "IgnoreRestTest", - "start": 1553898112437, - "end": 1553898112437, - "result": "passed", - "attachments": [ - - ] -}]) - -loadLogFile([{ - "package": "extensions", - "name": "StackTraceTest", - "start": 1553898112438, - "features": [ - { - "name": "stacktrace", - "start": 1553898112438, - "exceptions": [ - "java.lang.RuntimeException: blabla\n\tat extensions.StackTraceTest.stacktrace(StackTraceTest.groovy:10)\n" - ], - "end": 1553898112455, - "result": "failed", - "attachments": [ - - ] - } - ], - "end": 1553898112470, - "result": "failed", - "attachments": [ - - ] -}]) - -loadLogFile([{ - "package": "extensions", - "name": "IgnoreIfTest", - "start": 1553898112471, - "end": 1553898112472, - "result": "passed", - "attachments": [ - - ] -}]) - -loadLogFile([{ - "package": "extensions", - "name": "RequiresTest", - "start": 1553898112473, - "features": [ - { - "name": "I will run only on Windows", - "start": 1553898112474, - "end": 1553898112474, - "result": "skipped" - } - ], - "end": 1553898112476, - "result": "passed", - "attachments": [ - - ] -}]) - -loadLogFile([{ - "package": "extensions", - "name": "IssueTest", - "start": 1553898112477, - "features": [ - { - "name": "I'm using Spock configuration file", - "start": 1553898112477, - "tags": [ - { - "name": "Bug LO-1000", - "key": "issue", - "value": "LO-1000", - "url": "http:\/\/jira.org\/issues\/LO-1000" - } - ], - "end": 1553898112489, - "result": "passed", - "attachments": [ - - ] - } - ], - "end": 1553898112490, - "result": "passed", - "attachments": [ - - ] -}]) - -loadLogFile([{ - "package": "extensions", - "name": "TimeoutTest", - "start": 1553898112491, - "features": [ - { - "name": "I will fail after 200 millis", - "start": 1553898112491, - "end": 1553898112514, - "result": "passed", - "attachments": [ - - ] - } - ], - "end": 1553898112517, - "result": "passed", - "attachments": [ - - ] -}]) - -loadLogFile([{ - "package": "extensions", - "name": "RestoreSystemPropertiesTest", - "start": 1553898112518, - "features": [ - { - "name": "all environment variables will be saved before execution and restored after tests", - "start": 1553898112518, - "end": 1553898112532, - "result": "passed", - "attachments": [ - - ] - } - ], - "end": 1553898112539, - "result": "passed", - "attachments": [ - - ] -}]) - diff --git a/testing-modules/junit-4/README.md b/testing-modules/junit-4/README.md index 4d4588912b..dd975daf58 100644 --- a/testing-modules/junit-4/README.md +++ b/testing-modules/junit-4/README.md @@ -1,3 +1,5 @@ ### Relevant Articles - [Guide to JUnit 4 Rules](https://www.baeldung.com/junit-4-rules) +- [Custom JUnit 4 Test Runners](http://www.baeldung.com/junit-4-custom-runners) +- [Introduction to JUnitParams](http://www.baeldung.com/junit-params) diff --git a/testing-modules/junit-4/pom.xml b/testing-modules/junit-4/pom.xml index 272a9380b5..be0f51ea23 100644 --- a/testing-modules/junit-4/pom.xml +++ b/testing-modules/junit-4/pom.xml @@ -15,4 +15,17 @@ ../../ + + + pl.pragmatists + JUnitParams + ${jUnitParams.version} + test + + + + + 1.1.0 + + diff --git a/testing-modules/testing/src/main/java/com/baeldung/junit/Calculator.java b/testing-modules/junit-4/src/main/java/com/baeldung/junit/Calculator.java similarity index 100% rename from testing-modules/testing/src/main/java/com/baeldung/junit/Calculator.java rename to testing-modules/junit-4/src/main/java/com/baeldung/junit/Calculator.java diff --git a/testing-modules/testing/src/main/java/com/baeldung/junitparams/SafeAdditionUtil.java b/testing-modules/junit-4/src/main/java/com/baeldung/junitparams/SafeAdditionUtil.java similarity index 100% rename from testing-modules/testing/src/main/java/com/baeldung/junitparams/SafeAdditionUtil.java rename to testing-modules/junit-4/src/main/java/com/baeldung/junitparams/SafeAdditionUtil.java diff --git a/testing-modules/testing/src/test/java/com/baeldung/junit/AdditionUnitTest.java b/testing-modules/junit-4/src/test/java/com/baeldung/junit/AdditionUnitTest.java similarity index 100% rename from testing-modules/testing/src/test/java/com/baeldung/junit/AdditionUnitTest.java rename to testing-modules/junit-4/src/test/java/com/baeldung/junit/AdditionUnitTest.java diff --git a/testing-modules/testing/src/test/java/com/baeldung/junit/AssertionsUnitTest.java b/testing-modules/junit-4/src/test/java/com/baeldung/junit/AssertionsUnitTest.java similarity index 100% rename from testing-modules/testing/src/test/java/com/baeldung/junit/AssertionsUnitTest.java rename to testing-modules/junit-4/src/test/java/com/baeldung/junit/AssertionsUnitTest.java diff --git a/testing-modules/testing/src/test/java/com/baeldung/junit/BlockingTestRunner.java b/testing-modules/junit-4/src/test/java/com/baeldung/junit/BlockingTestRunner.java similarity index 100% rename from testing-modules/testing/src/test/java/com/baeldung/junit/BlockingTestRunner.java rename to testing-modules/junit-4/src/test/java/com/baeldung/junit/BlockingTestRunner.java diff --git a/testing-modules/testing/src/test/java/com/baeldung/junit/CalculatorUnitTest.java b/testing-modules/junit-4/src/test/java/com/baeldung/junit/CalculatorUnitTest.java similarity index 100% rename from testing-modules/testing/src/test/java/com/baeldung/junit/CalculatorUnitTest.java rename to testing-modules/junit-4/src/test/java/com/baeldung/junit/CalculatorUnitTest.java diff --git a/testing-modules/testing/src/test/java/com/baeldung/junit/SubstractionUnitTest.java b/testing-modules/junit-4/src/test/java/com/baeldung/junit/SubstractionUnitTest.java similarity index 100% rename from testing-modules/testing/src/test/java/com/baeldung/junit/SubstractionUnitTest.java rename to testing-modules/junit-4/src/test/java/com/baeldung/junit/SubstractionUnitTest.java diff --git a/testing-modules/testing/src/test/java/com/baeldung/junit/SuiteUnitTest.java b/testing-modules/junit-4/src/test/java/com/baeldung/junit/SuiteUnitTest.java similarity index 100% rename from testing-modules/testing/src/test/java/com/baeldung/junit/SuiteUnitTest.java rename to testing-modules/junit-4/src/test/java/com/baeldung/junit/SuiteUnitTest.java diff --git a/testing-modules/testing/src/test/java/com/baeldung/junit/TestRunner.java b/testing-modules/junit-4/src/test/java/com/baeldung/junit/TestRunner.java similarity index 100% rename from testing-modules/testing/src/test/java/com/baeldung/junit/TestRunner.java rename to testing-modules/junit-4/src/test/java/com/baeldung/junit/TestRunner.java diff --git a/testing-modules/testing/src/test/java/com/baeldung/junitparams/SafeAdditionUtilUnitTest.java b/testing-modules/junit-4/src/test/java/com/baeldung/junitparams/SafeAdditionUtilUnitTest.java similarity index 100% rename from testing-modules/testing/src/test/java/com/baeldung/junitparams/SafeAdditionUtilUnitTest.java rename to testing-modules/junit-4/src/test/java/com/baeldung/junitparams/SafeAdditionUtilUnitTest.java diff --git a/testing-modules/testing/src/test/java/com/baeldung/junitparams/TestDataProvider.java b/testing-modules/junit-4/src/test/java/com/baeldung/junitparams/TestDataProvider.java similarity index 100% rename from testing-modules/testing/src/test/java/com/baeldung/junitparams/TestDataProvider.java rename to testing-modules/junit-4/src/test/java/com/baeldung/junitparams/TestDataProvider.java diff --git a/testing-modules/testing/src/test/java/com/baeldung/runfromjava/FirstUnitTest.java b/testing-modules/junit-4/src/test/java/com/baeldung/runfromjava/FirstUnitTest.java similarity index 100% rename from testing-modules/testing/src/test/java/com/baeldung/runfromjava/FirstUnitTest.java rename to testing-modules/junit-4/src/test/java/com/baeldung/runfromjava/FirstUnitTest.java diff --git a/testing-modules/testing/src/test/java/com/baeldung/runfromjava/MyTestSuite.java b/testing-modules/junit-4/src/test/java/com/baeldung/runfromjava/MyTestSuite.java similarity index 100% rename from testing-modules/testing/src/test/java/com/baeldung/runfromjava/MyTestSuite.java rename to testing-modules/junit-4/src/test/java/com/baeldung/runfromjava/MyTestSuite.java diff --git a/testing-modules/testing/src/test/java/com/baeldung/runfromjava/RunJUnit4TestsFromJava.java b/testing-modules/junit-4/src/test/java/com/baeldung/runfromjava/RunJUnit4TestsFromJava.java similarity index 100% rename from testing-modules/testing/src/test/java/com/baeldung/runfromjava/RunJUnit4TestsFromJava.java rename to testing-modules/junit-4/src/test/java/com/baeldung/runfromjava/RunJUnit4TestsFromJava.java diff --git a/testing-modules/testing/src/test/java/com/baeldung/runfromjava/SecondUnitTest.java b/testing-modules/junit-4/src/test/java/com/baeldung/runfromjava/SecondUnitTest.java similarity index 100% rename from testing-modules/testing/src/test/java/com/baeldung/runfromjava/SecondUnitTest.java rename to testing-modules/junit-4/src/test/java/com/baeldung/runfromjava/SecondUnitTest.java diff --git a/testing-modules/testing/src/test/resources/JunitParamsTestParameters.csv b/testing-modules/junit-4/src/test/resources/JunitParamsTestParameters.csv similarity index 100% rename from testing-modules/testing/src/test/resources/JunitParamsTestParameters.csv rename to testing-modules/junit-4/src/test/resources/JunitParamsTestParameters.csv diff --git a/testing-modules/junit-5/src/test/java/com/baeldung/junit5/mockito/UserServiceUnitTest.java b/testing-modules/junit-5/src/test/java/com/baeldung/junit5/mockito/UserServiceUnitTest.java index d4195e3b12..e13b4e24be 100644 --- a/testing-modules/junit-5/src/test/java/com/baeldung/junit5/mockito/UserServiceUnitTest.java +++ b/testing-modules/junit-5/src/test/java/com/baeldung/junit5/mockito/UserServiceUnitTest.java @@ -1,126 +1,126 @@ -package com.baeldung.junit5.mockito; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.fail; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.*; - -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.junit.platform.runner.JUnitPlatform; -import org.junit.runner.RunWith; -import org.mockito.Mock; -import org.mockito.Mockito; -import org.mockito.invocation.InvocationOnMock; -import org.mockito.junit.jupiter.MockitoExtension; -import org.mockito.stubbing.Answer; - -import com.baeldung.junit5.mockito.repository.MailClient; -import com.baeldung.junit5.mockito.repository.SettingRepository; -import com.baeldung.junit5.mockito.repository.UserRepository; -import com.baeldung.junit5.mockito.service.DefaultUserService; -import com.baeldung.junit5.mockito.service.Errors; -import com.baeldung.junit5.mockito.service.UserService; - -@ExtendWith(MockitoExtension.class) -@RunWith(JUnitPlatform.class) -public class UserServiceUnitTest { - - UserService userService; - SettingRepository settingRepository; - @Mock UserRepository userRepository; - @Mock MailClient mailClient; - - User user; - - @BeforeEach - void init(@Mock SettingRepository settingRepository) { - userService = new DefaultUserService(userRepository, settingRepository, mailClient); - lenient().when(settingRepository.getUserMinAge()).thenReturn(10); - when(settingRepository.getUserNameMinLength()).thenReturn(4); - lenient().when(userRepository.isUsernameAlreadyExists(any(String.class))).thenReturn(false); - this.settingRepository = settingRepository; - } - - @Test - void givenValidUser_whenSaveUser_thenSucceed(@Mock MailClient mailClient) { - // Given - user = new User("Jerry", 12); - when(userRepository.insert(any(User.class))).then(new Answer() { - int sequence = 1; - - @Override - public User answer(InvocationOnMock invocation) throws Throwable { - User user = (User) invocation.getArgument(0); - user.setId(sequence++); - return user; - } - }); - - userService = new DefaultUserService(userRepository, settingRepository, mailClient); - - // When - User insertedUser = userService.register(user); - - // Then - verify(userRepository).insert(user); - Assertions.assertNotNull(user.getId()); - verify(mailClient).sendUserRegistrationMail(insertedUser); - } - - @Test - void givenShortName_whenSaveUser_thenGiveShortUsernameError() { - // Given - user = new User("tom", 12); - - // When - try { - userService.register(user); - fail("Should give an error"); - } catch(Exception ex) { - assertEquals(ex.getMessage(), Errors.USER_NAME_SHORT); - } - - // Then - verify(userRepository, never()).insert(user); - } - - @Test - void givenSmallAge_whenSaveUser_thenGiveYoungUserError() { - // Given - user = new User("jerry", 3); - - // When - try { - userService.register(user); - fail("Should give an error"); - } catch(Exception ex) { - assertEquals(ex.getMessage(), Errors.USER_AGE_YOUNG); - } - - // Then - verify(userRepository, never()).insert(user); - } - - @Test - void givenUserWithExistingName_whenSaveUser_thenGiveUsernameAlreadyExistsError() { - // Given - user = new User("jerry", 12); - Mockito.reset(userRepository); - when(userRepository.isUsernameAlreadyExists(any(String.class))).thenReturn(true); - - // When - try { - userService.register(user); - fail("Should give an error"); - } catch(Exception ex) { - assertEquals(ex.getMessage(), Errors.USER_NAME_DUPLICATE); - } - - // Then - verify(userRepository, never()).insert(user); - } - -} +package com.baeldung.junit5.mockito; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.fail; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.*; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.junit.platform.runner.JUnitPlatform; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.junit.jupiter.MockitoExtension; +import org.mockito.stubbing.Answer; + +import com.baeldung.junit5.mockito.repository.MailClient; +import com.baeldung.junit5.mockito.repository.SettingRepository; +import com.baeldung.junit5.mockito.repository.UserRepository; +import com.baeldung.junit5.mockito.service.DefaultUserService; +import com.baeldung.junit5.mockito.service.Errors; +import com.baeldung.junit5.mockito.service.UserService; + +@ExtendWith(MockitoExtension.class) +@RunWith(JUnitPlatform.class) +public class UserServiceUnitTest { + + UserService userService; + SettingRepository settingRepository; + @Mock UserRepository userRepository; + @Mock MailClient mailClient; + + User user; + + @BeforeEach + void init(@Mock SettingRepository settingRepository) { + userService = new DefaultUserService(userRepository, settingRepository, mailClient); + lenient().when(settingRepository.getUserMinAge()).thenReturn(10); + when(settingRepository.getUserNameMinLength()).thenReturn(4); + lenient().when(userRepository.isUsernameAlreadyExists(any(String.class))).thenReturn(false); + this.settingRepository = settingRepository; + } + + @Test + void givenValidUser_whenSaveUser_thenSucceed(@Mock MailClient mailClient) { + // Given + user = new User("Jerry", 12); + when(userRepository.insert(any(User.class))).then(new Answer() { + int sequence = 1; + + @Override + public User answer(InvocationOnMock invocation) throws Throwable { + User user = (User) invocation.getArgument(0); + user.setId(sequence++); + return user; + } + }); + + userService = new DefaultUserService(userRepository, settingRepository, mailClient); + + // When + User insertedUser = userService.register(user); + + // Then + verify(userRepository).insert(user); + Assertions.assertNotNull(user.getId()); + verify(mailClient).sendUserRegistrationMail(insertedUser); + } + + @Test + void givenShortName_whenSaveUser_thenGiveShortUsernameError() { + // Given + user = new User("tom", 12); + + // When + try { + userService.register(user); + fail("Should give an error"); + } catch(Exception ex) { + assertEquals(ex.getMessage(), Errors.USER_NAME_SHORT); + } + + // Then + verify(userRepository, never()).insert(user); + } + + @Test + void givenSmallAge_whenSaveUser_thenGiveYoungUserError() { + // Given + user = new User("jerry", 3); + + // When + try { + userService.register(user); + fail("Should give an error"); + } catch(Exception ex) { + assertEquals(ex.getMessage(), Errors.USER_AGE_YOUNG); + } + + // Then + verify(userRepository, never()).insert(user); + } + + @Test + void givenUserWithExistingName_whenSaveUser_thenGiveUsernameAlreadyExistsError() { + // Given + user = new User("jerry", 12); + Mockito.reset(userRepository); + when(userRepository.isUsernameAlreadyExists(any(String.class))).thenReturn(true); + + // When + try { + userService.register(user); + fail("Should give an error"); + } catch(Exception ex) { + assertEquals(ex.getMessage(), Errors.USER_NAME_DUPLICATE); + } + + // Then + verify(userRepository, never()).insert(user); + } + +} diff --git a/testing-modules/mockito/src/test/java/org/baeldung/bddmockito/BDDMockitoIntegrationTest.java b/testing-modules/mockito/src/test/java/org/baeldung/bddmockito/BDDMockitoIntegrationTest.java index e772b5e049..e0ca6f0a35 100644 --- a/testing-modules/mockito/src/test/java/org/baeldung/bddmockito/BDDMockitoIntegrationTest.java +++ b/testing-modules/mockito/src/test/java/org/baeldung/bddmockito/BDDMockitoIntegrationTest.java @@ -1,104 +1,104 @@ -package org.baeldung.bddmockito; - -import static org.junit.Assert.fail; -import static org.mockito.BDDMockito.*; - -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import org.mockito.Mockito; -import org.mockito.invocation.InvocationOnMock; - - -public class BDDMockitoIntegrationTest { - - PhoneBookService phoneBookService; - PhoneBookRepository phoneBookRepository; - - String momContactName = "Mom"; - String momPhoneNumber = "01234"; - String xContactName = "x"; - String tooLongPhoneNumber = "01111111111111"; - - @Before - public void init() { - phoneBookRepository = Mockito.mock(PhoneBookRepository.class); - phoneBookService = new PhoneBookService(phoneBookRepository); - } - - @Test - public void givenValidContactName_whenSearchInPhoneBook_thenRetunPhoneNumber() { - given(phoneBookRepository.contains(momContactName)).willReturn(true); - given(phoneBookRepository.getPhoneNumberByContactName(momContactName)) - .will((InvocationOnMock invocation) -> { - if(invocation.getArgument(0).equals(momContactName)) { - return momPhoneNumber; - } else { - return null; - } - }); - - String phoneNumber = phoneBookService.search(momContactName); - - then(phoneBookRepository).should().contains(momContactName); - then(phoneBookRepository).should().getPhoneNumberByContactName(momContactName); - Assert.assertEquals(phoneNumber, momPhoneNumber); - } - - @Test - public void givenInvalidContactName_whenSearch_thenRetunNull() { - given(phoneBookRepository.contains(xContactName)).willReturn(false); - - String phoneNumber = phoneBookService.search(xContactName); - - then(phoneBookRepository).should().contains(xContactName); - then(phoneBookRepository).should(never()).getPhoneNumberByContactName(xContactName); - Assert.assertEquals(phoneNumber, null); - } - - @Test - public void givenValidContactNameAndPhoneNumber_whenRegister_thenSucceed() { - given(phoneBookRepository.contains(momContactName)).willReturn(false); - - phoneBookService.register(momContactName, momPhoneNumber); - - verify(phoneBookRepository).insert(momContactName, momPhoneNumber); - } - - @Test - public void givenEmptyPhoneNumber_whenRegister_thenFail() { - given(phoneBookRepository.contains(momContactName)).willReturn(false); - - phoneBookService.register(xContactName, ""); - - then(phoneBookRepository).should(never()).insert(momContactName, momPhoneNumber); - } - - @Test - public void givenLongPhoneNumber_whenRegister_thenFail() { - given(phoneBookRepository.contains(xContactName)).willReturn(false); - willThrow(new RuntimeException()) - .given(phoneBookRepository).insert(any(String.class), eq(tooLongPhoneNumber)); - - try { - phoneBookService.register(xContactName, tooLongPhoneNumber); - fail("Should throw exception"); - } catch (RuntimeException ex) { } - - then(phoneBookRepository).should(never()).insert(momContactName, tooLongPhoneNumber); - } - - @Test - public void givenExistentContactName_whenRegister_thenFail() { - given(phoneBookRepository.contains(momContactName)) - .willThrow(new RuntimeException("Name already exist")); - - try { - phoneBookService.register(momContactName, momPhoneNumber); - fail("Should throw exception"); - } catch(Exception ex) { } - - then(phoneBookRepository).should(never()).insert(momContactName, momPhoneNumber); - } - -} +package org.baeldung.bddmockito; + +import static org.junit.Assert.fail; +import static org.mockito.BDDMockito.*; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mockito; +import org.mockito.invocation.InvocationOnMock; + + +public class BDDMockitoIntegrationTest { + + PhoneBookService phoneBookService; + PhoneBookRepository phoneBookRepository; + + String momContactName = "Mom"; + String momPhoneNumber = "01234"; + String xContactName = "x"; + String tooLongPhoneNumber = "01111111111111"; + + @Before + public void init() { + phoneBookRepository = Mockito.mock(PhoneBookRepository.class); + phoneBookService = new PhoneBookService(phoneBookRepository); + } + + @Test + public void givenValidContactName_whenSearchInPhoneBook_thenRetunPhoneNumber() { + given(phoneBookRepository.contains(momContactName)).willReturn(true); + given(phoneBookRepository.getPhoneNumberByContactName(momContactName)) + .will((InvocationOnMock invocation) -> { + if(invocation.getArgument(0).equals(momContactName)) { + return momPhoneNumber; + } else { + return null; + } + }); + + String phoneNumber = phoneBookService.search(momContactName); + + then(phoneBookRepository).should().contains(momContactName); + then(phoneBookRepository).should().getPhoneNumberByContactName(momContactName); + Assert.assertEquals(phoneNumber, momPhoneNumber); + } + + @Test + public void givenInvalidContactName_whenSearch_thenRetunNull() { + given(phoneBookRepository.contains(xContactName)).willReturn(false); + + String phoneNumber = phoneBookService.search(xContactName); + + then(phoneBookRepository).should().contains(xContactName); + then(phoneBookRepository).should(never()).getPhoneNumberByContactName(xContactName); + Assert.assertEquals(phoneNumber, null); + } + + @Test + public void givenValidContactNameAndPhoneNumber_whenRegister_thenSucceed() { + given(phoneBookRepository.contains(momContactName)).willReturn(false); + + phoneBookService.register(momContactName, momPhoneNumber); + + verify(phoneBookRepository).insert(momContactName, momPhoneNumber); + } + + @Test + public void givenEmptyPhoneNumber_whenRegister_thenFail() { + given(phoneBookRepository.contains(momContactName)).willReturn(false); + + phoneBookService.register(xContactName, ""); + + then(phoneBookRepository).should(never()).insert(momContactName, momPhoneNumber); + } + + @Test + public void givenLongPhoneNumber_whenRegister_thenFail() { + given(phoneBookRepository.contains(xContactName)).willReturn(false); + willThrow(new RuntimeException()) + .given(phoneBookRepository).insert(any(String.class), eq(tooLongPhoneNumber)); + + try { + phoneBookService.register(xContactName, tooLongPhoneNumber); + fail("Should throw exception"); + } catch (RuntimeException ex) { } + + then(phoneBookRepository).should(never()).insert(momContactName, tooLongPhoneNumber); + } + + @Test + public void givenExistentContactName_whenRegister_thenFail() { + given(phoneBookRepository.contains(momContactName)) + .willThrow(new RuntimeException("Name already exist")); + + try { + phoneBookService.register(momContactName, momPhoneNumber); + fail("Should throw exception"); + } catch(Exception ex) { } + + then(phoneBookRepository).should(never()).insert(momContactName, momPhoneNumber); + } + +} diff --git a/testing-modules/mockito/src/test/java/org/baeldung/bddmockito/PhoneBookRepository.java b/testing-modules/mockito/src/test/java/org/baeldung/bddmockito/PhoneBookRepository.java index b73a1d835c..94d4a90d4b 100644 --- a/testing-modules/mockito/src/test/java/org/baeldung/bddmockito/PhoneBookRepository.java +++ b/testing-modules/mockito/src/test/java/org/baeldung/bddmockito/PhoneBookRepository.java @@ -1,26 +1,26 @@ -package org.baeldung.bddmockito; - -public interface PhoneBookRepository { - - /** - * Insert phone record - * @param name Contact name - * @param phone Phone number - */ - void insert(String name, String phone); - - /** - * Search for contact phone number - * @param name Contact name - * @return phone number - */ - String getPhoneNumberByContactName(String name); - - /** - * Check if the phonebook contains this contact - * @param name Contact name - * @return true if this contact name exists - */ - boolean contains(String name); - -} +package org.baeldung.bddmockito; + +public interface PhoneBookRepository { + + /** + * Insert phone record + * @param name Contact name + * @param phone Phone number + */ + void insert(String name, String phone); + + /** + * Search for contact phone number + * @param name Contact name + * @return phone number + */ + String getPhoneNumberByContactName(String name); + + /** + * Check if the phonebook contains this contact + * @param name Contact name + * @return true if this contact name exists + */ + boolean contains(String name); + +} diff --git a/testing-modules/mockito/src/test/java/org/baeldung/bddmockito/PhoneBookService.java b/testing-modules/mockito/src/test/java/org/baeldung/bddmockito/PhoneBookService.java index 645884af02..8fc49b026d 100644 --- a/testing-modules/mockito/src/test/java/org/baeldung/bddmockito/PhoneBookService.java +++ b/testing-modules/mockito/src/test/java/org/baeldung/bddmockito/PhoneBookService.java @@ -1,34 +1,34 @@ -package org.baeldung.bddmockito; - -public class PhoneBookService { - - private PhoneBookRepository phoneBookRepository; - - public PhoneBookService(PhoneBookRepository phoneBookRepository) { - this.phoneBookRepository = phoneBookRepository; - } - - /** - * Register a contact - * @param name Contact name - * @param phone Phone number - */ - public void register(String name, String phone) { - if(!name.isEmpty() && !phone.isEmpty() && !phoneBookRepository.contains(name)) { - phoneBookRepository.insert(name, phone); - } - } - - /** - * Search for a phone number by contact name - * @param name Contact name - * @return Phone number - */ - public String search(String name) { - if(!name.isEmpty() && phoneBookRepository.contains(name)) { - return phoneBookRepository.getPhoneNumberByContactName(name); - } - return null; - } - -} +package org.baeldung.bddmockito; + +public class PhoneBookService { + + private PhoneBookRepository phoneBookRepository; + + public PhoneBookService(PhoneBookRepository phoneBookRepository) { + this.phoneBookRepository = phoneBookRepository; + } + + /** + * Register a contact + * @param name Contact name + * @param phone Phone number + */ + public void register(String name, String phone) { + if(!name.isEmpty() && !phone.isEmpty() && !phoneBookRepository.contains(name)) { + phoneBookRepository.insert(name, phone); + } + } + + /** + * Search for a phone number by contact name + * @param name Contact name + * @return Phone number + */ + public String search(String name) { + if(!name.isEmpty() && phoneBookRepository.contains(name)) { + return phoneBookRepository.getPhoneNumberByContactName(name); + } + return null; + } + +} diff --git a/testing-modules/mocks/README.md b/testing-modules/mocks/README.md index 3cb20dcf92..3baefe072b 100644 --- a/testing-modules/mocks/README.md +++ b/testing-modules/mocks/README.md @@ -2,3 +2,10 @@ - [EasyMock Argument Matchers](http://www.baeldung.com/easymock-argument-matchers) - [Mock Static Method using JMockit](https://www.baeldung.com/jmockit-static-method) +- [Mockito vs EasyMock vs JMockit](http://www.baeldung.com/mockito-vs-easymock-vs-jmockit) +- [Introduction to EasyMock](http://www.baeldung.com/easymock) +- [JMockit 101](http://www.baeldung.com/jmockit-101) +- [A Guide to JMockit Expectations](http://www.baeldung.com/jmockit-expectations) +- [JMockit Advanced Usage](http://www.baeldung.com/jmockit-advanced-usage) +- [Introduction to Jukito](http://www.baeldung.com/jukito) +- [A Guide to JavaFaker](https://www.baeldung.com/java-faker) diff --git a/testing-modules/mocks/jmockit/README.md b/testing-modules/mocks/jmockit/README.md deleted file mode 100644 index 0e44b93d6e..0000000000 --- a/testing-modules/mocks/jmockit/README.md +++ /dev/null @@ -1,9 +0,0 @@ -========= - -## JMockit related tutorials - - -### Relevant Articles: -- [JMockit 101](http://www.baeldung.com/jmockit-101) -- [A Guide to JMockit Expectations](http://www.baeldung.com/jmockit-expectations) -- [JMockit Advanced Usage](http://www.baeldung.com/jmockit-advanced-usage) diff --git a/testing-modules/mocks/jmockit/pom.xml b/testing-modules/mocks/jmockit/pom.xml deleted file mode 100644 index 12bbe6b60d..0000000000 --- a/testing-modules/mocks/jmockit/pom.xml +++ /dev/null @@ -1,37 +0,0 @@ - - 4.0.0 - jmockit - jmockit - - - com.baeldung - mocks - 1.0.0-SNAPSHOT - ../ - - - - - org.jmockit - jmockit - ${jmockit.version} - test - - - - - jmockit - - - src/main/resources - true - - - - - - 1.24 - - - \ No newline at end of file diff --git a/testing-modules/mocks/mock-comparisons/README.md b/testing-modules/mocks/mock-comparisons/README.md deleted file mode 100644 index 20c13ecd72..0000000000 --- a/testing-modules/mocks/mock-comparisons/README.md +++ /dev/null @@ -1,8 +0,0 @@ -========= - -## Mock comparison realated tutorials - - -### Relevant Articles: -- [Mockito vs EasyMock vs JMockit](http://www.baeldung.com/mockito-vs-easymock-vs-jmockit) -- [Introduction to EasyMock](http://www.baeldung.com/easymock) diff --git a/testing-modules/mocks/mock-comparisons/pom.xml b/testing-modules/mocks/mock-comparisons/pom.xml deleted file mode 100644 index e454f124ce..0000000000 --- a/testing-modules/mocks/mock-comparisons/pom.xml +++ /dev/null @@ -1,54 +0,0 @@ - - 4.0.0 - mock-comparisons - mock-comparisons - - - com.baeldung - mocks - 1.0.0-SNAPSHOT - ../ - - - - - org.mockito - mockito-core - ${mockito.version} - test - - - - org.easymock - easymock - ${easymock.version} - test - - - - org.jmockit - jmockit - ${jmockit.version} - test - - - - - - mock-comparisons - - - src/main/resources - true - - - - - - 2.21.0 - 3.5.1 - 1.41 - - - diff --git a/testing-modules/mocks/pom.xml b/testing-modules/mocks/pom.xml index ecc8b61a6d..bf75cfc3a2 100644 --- a/testing-modules/mocks/pom.xml +++ b/testing-modules/mocks/pom.xml @@ -3,7 +3,6 @@ 4.0.0 mocks mocks - pom com.baeldung @@ -12,9 +11,45 @@ ../../ - - mock-comparisons - jmockit - + + + com.github.javafaker + javafaker + ${javafaker.version} + + + org.jmockit + jmockit + ${jmockit.version} + test + + + org.jukito + jukito + ${jukito.version} + test + + + org.mockito + mockito-core + ${mockito.version} + test + + + + org.easymock + easymock + ${easymock.version} + test + + + + + 0.15 + 1.5 +2.21.0 + 3.5.1 + 1.41 + diff --git a/testing-modules/mocks/mock-comparisons/src/main/java/com/baeldung/easymock/ArticleReader.java b/testing-modules/mocks/src/main/java/com/baeldung/easymock/ArticleReader.java old mode 100755 new mode 100644 similarity index 95% rename from testing-modules/mocks/mock-comparisons/src/main/java/com/baeldung/easymock/ArticleReader.java rename to testing-modules/mocks/src/main/java/com/baeldung/easymock/ArticleReader.java index 9e4a15c27a..c4732e3452 --- a/testing-modules/mocks/mock-comparisons/src/main/java/com/baeldung/easymock/ArticleReader.java +++ b/testing-modules/mocks/src/main/java/com/baeldung/easymock/ArticleReader.java @@ -1,36 +1,36 @@ -package com.baeldung.easymock; - -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; - -import static java.util.stream.Collectors.toList; - -public class ArticleReader { - - private List articles; - private Iterator articleIter; - - public ArticleReader() { - this(new ArrayList<>()); - } - - public ArticleReader(List articles) { - this.articles = articles; - this.articleIter = this.articles.iterator(); - } - - public List ofTopic(String topic) { - return articles - .stream() - .filter(article -> article - .title() - .contains(topic)) - .collect(toList()); - } - - public BaeldungArticle next() { - return this.articleIter.next(); - } - +package com.baeldung.easymock; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +import static java.util.stream.Collectors.toList; + +public class ArticleReader { + + private List articles; + private Iterator articleIter; + + public ArticleReader() { + this(new ArrayList<>()); + } + + public ArticleReader(List articles) { + this.articles = articles; + this.articleIter = this.articles.iterator(); + } + + public List ofTopic(String topic) { + return articles + .stream() + .filter(article -> article + .title() + .contains(topic)) + .collect(toList()); + } + + public BaeldungArticle next() { + return this.articleIter.next(); + } + } \ No newline at end of file diff --git a/testing-modules/mocks/mock-comparisons/src/main/java/com/baeldung/easymock/BaeldungArticle.java b/testing-modules/mocks/src/main/java/com/baeldung/easymock/BaeldungArticle.java old mode 100755 new mode 100644 similarity index 95% rename from testing-modules/mocks/mock-comparisons/src/main/java/com/baeldung/easymock/BaeldungArticle.java rename to testing-modules/mocks/src/main/java/com/baeldung/easymock/BaeldungArticle.java index f386c980f4..9f332a9bac --- a/testing-modules/mocks/mock-comparisons/src/main/java/com/baeldung/easymock/BaeldungArticle.java +++ b/testing-modules/mocks/src/main/java/com/baeldung/easymock/BaeldungArticle.java @@ -1,25 +1,25 @@ -package com.baeldung.easymock; - -public class BaeldungArticle { - - public static BaeldungArticle simpleArticle(String title, String content) { - return new BaeldungArticle(title, content); - } - - private String title; - private String content; - - private BaeldungArticle(String title, String content) { - this.title = title; - this.content = content; - } - - public String title() { - return this.title; - } - - public String content() { - return this.content; - } - +package com.baeldung.easymock; + +public class BaeldungArticle { + + public static BaeldungArticle simpleArticle(String title, String content) { + return new BaeldungArticle(title, content); + } + + private String title; + private String content; + + private BaeldungArticle(String title, String content) { + this.title = title; + this.content = content; + } + + public String title() { + return this.title; + } + + public String content() { + return this.content; + } + } \ No newline at end of file diff --git a/testing-modules/mocks/mock-comparisons/src/main/java/com/baeldung/easymock/BaeldungReader.java b/testing-modules/mocks/src/main/java/com/baeldung/easymock/BaeldungReader.java old mode 100755 new mode 100644 similarity index 95% rename from testing-modules/mocks/mock-comparisons/src/main/java/com/baeldung/easymock/BaeldungReader.java rename to testing-modules/mocks/src/main/java/com/baeldung/easymock/BaeldungReader.java index 319dfc5d77..fc37b9f94a --- a/testing-modules/mocks/mock-comparisons/src/main/java/com/baeldung/easymock/BaeldungReader.java +++ b/testing-modules/mocks/src/main/java/com/baeldung/easymock/BaeldungReader.java @@ -1,40 +1,40 @@ -package com.baeldung.easymock; - -import java.util.List; - -public class BaeldungReader { - - private ArticleReader articleReader; - private IArticleWriter articleWriter; - - public BaeldungReader() { - } - - ; - - public BaeldungReader(ArticleReader articleReader) { - this.articleReader = articleReader; - } - - public BaeldungReader(IArticleWriter writer) { - this.articleWriter = writer; - } - - public BaeldungReader(ArticleReader articleReader, IArticleWriter writer) { - this.articleReader = articleReader; - this.articleWriter = writer; - } - - public BaeldungArticle readNext() { - return articleReader.next(); - } - - public List readTopic(String topic) { - return articleReader.ofTopic(topic); - } - - public String write(String title, String content) { - return articleWriter.write(title, content); - } - +package com.baeldung.easymock; + +import java.util.List; + +public class BaeldungReader { + + private ArticleReader articleReader; + private IArticleWriter articleWriter; + + public BaeldungReader() { + } + + ; + + public BaeldungReader(ArticleReader articleReader) { + this.articleReader = articleReader; + } + + public BaeldungReader(IArticleWriter writer) { + this.articleWriter = writer; + } + + public BaeldungReader(ArticleReader articleReader, IArticleWriter writer) { + this.articleReader = articleReader; + this.articleWriter = writer; + } + + public BaeldungArticle readNext() { + return articleReader.next(); + } + + public List readTopic(String topic) { + return articleReader.ofTopic(topic); + } + + public String write(String title, String content) { + return articleWriter.write(title, content); + } + } \ No newline at end of file diff --git a/testing-modules/mocks/mock-comparisons/src/main/java/com/baeldung/easymock/IArticleWriter.java b/testing-modules/mocks/src/main/java/com/baeldung/easymock/IArticleWriter.java old mode 100755 new mode 100644 similarity index 95% rename from testing-modules/mocks/mock-comparisons/src/main/java/com/baeldung/easymock/IArticleWriter.java rename to testing-modules/mocks/src/main/java/com/baeldung/easymock/IArticleWriter.java index 4c8b190464..26970d5eb5 --- a/testing-modules/mocks/mock-comparisons/src/main/java/com/baeldung/easymock/IArticleWriter.java +++ b/testing-modules/mocks/src/main/java/com/baeldung/easymock/IArticleWriter.java @@ -1,7 +1,7 @@ -package com.baeldung.easymock; - -public interface IArticleWriter { - - String write(String title, String content); - +package com.baeldung.easymock; + +public interface IArticleWriter { + + String write(String title, String content); + } \ No newline at end of file diff --git a/testing-modules/mocks/mock-comparisons/src/main/java/com/baeldung/easymock/IUserService.java b/testing-modules/mocks/src/main/java/com/baeldung/easymock/IUserService.java similarity index 100% rename from testing-modules/mocks/mock-comparisons/src/main/java/com/baeldung/easymock/IUserService.java rename to testing-modules/mocks/src/main/java/com/baeldung/easymock/IUserService.java diff --git a/testing-modules/mocks/mock-comparisons/src/main/java/com/baeldung/easymock/User.java b/testing-modules/mocks/src/main/java/com/baeldung/easymock/User.java similarity index 100% rename from testing-modules/mocks/mock-comparisons/src/main/java/com/baeldung/easymock/User.java rename to testing-modules/mocks/src/main/java/com/baeldung/easymock/User.java diff --git a/testing-modules/testing/src/main/java/com/baeldung/introductionjukito/Calculator.java b/testing-modules/mocks/src/main/java/com/baeldung/introductionjukito/Calculator.java similarity index 100% rename from testing-modules/testing/src/main/java/com/baeldung/introductionjukito/Calculator.java rename to testing-modules/mocks/src/main/java/com/baeldung/introductionjukito/Calculator.java diff --git a/testing-modules/testing/src/main/java/com/baeldung/introductionjukito/ScientificCalculator.java b/testing-modules/mocks/src/main/java/com/baeldung/introductionjukito/ScientificCalculator.java similarity index 100% rename from testing-modules/testing/src/main/java/com/baeldung/introductionjukito/ScientificCalculator.java rename to testing-modules/mocks/src/main/java/com/baeldung/introductionjukito/ScientificCalculator.java diff --git a/testing-modules/testing/src/main/java/com/baeldung/introductionjukito/SimpleCalculator.java b/testing-modules/mocks/src/main/java/com/baeldung/introductionjukito/SimpleCalculator.java similarity index 100% rename from testing-modules/testing/src/main/java/com/baeldung/introductionjukito/SimpleCalculator.java rename to testing-modules/mocks/src/main/java/com/baeldung/introductionjukito/SimpleCalculator.java diff --git a/testing-modules/mocks/jmockit/src/main/java/com/baeldung/mocks/jmockit/AppManager.java b/testing-modules/mocks/src/main/java/com/baeldung/mocks/jmockit/AppManager.java similarity index 100% rename from testing-modules/mocks/jmockit/src/main/java/com/baeldung/mocks/jmockit/AppManager.java rename to testing-modules/mocks/src/main/java/com/baeldung/mocks/jmockit/AppManager.java diff --git a/testing-modules/mocks/jmockit/src/main/java/org/baeldung/mocks/jmockit/AdvancedCollaborator.java b/testing-modules/mocks/src/main/java/org/baeldung/mocks/jmockit/AdvancedCollaborator.java similarity index 100% rename from testing-modules/mocks/jmockit/src/main/java/org/baeldung/mocks/jmockit/AdvancedCollaborator.java rename to testing-modules/mocks/src/main/java/org/baeldung/mocks/jmockit/AdvancedCollaborator.java diff --git a/testing-modules/mocks/jmockit/src/main/java/org/baeldung/mocks/jmockit/Collaborator.java b/testing-modules/mocks/src/main/java/org/baeldung/mocks/jmockit/Collaborator.java similarity index 100% rename from testing-modules/mocks/jmockit/src/main/java/org/baeldung/mocks/jmockit/Collaborator.java rename to testing-modules/mocks/src/main/java/org/baeldung/mocks/jmockit/Collaborator.java diff --git a/testing-modules/mocks/jmockit/src/main/java/org/baeldung/mocks/jmockit/ExpectationsCollaborator.java b/testing-modules/mocks/src/main/java/org/baeldung/mocks/jmockit/ExpectationsCollaborator.java similarity index 100% rename from testing-modules/mocks/jmockit/src/main/java/org/baeldung/mocks/jmockit/ExpectationsCollaborator.java rename to testing-modules/mocks/src/main/java/org/baeldung/mocks/jmockit/ExpectationsCollaborator.java diff --git a/testing-modules/mocks/jmockit/src/main/java/org/baeldung/mocks/jmockit/Model.java b/testing-modules/mocks/src/main/java/org/baeldung/mocks/jmockit/Model.java similarity index 100% rename from testing-modules/mocks/jmockit/src/main/java/org/baeldung/mocks/jmockit/Model.java rename to testing-modules/mocks/src/main/java/org/baeldung/mocks/jmockit/Model.java diff --git a/testing-modules/mocks/jmockit/src/main/java/org/baeldung/mocks/jmockit/Performer.java b/testing-modules/mocks/src/main/java/org/baeldung/mocks/jmockit/Performer.java similarity index 100% rename from testing-modules/mocks/jmockit/src/main/java/org/baeldung/mocks/jmockit/Performer.java rename to testing-modules/mocks/src/main/java/org/baeldung/mocks/jmockit/Performer.java diff --git a/testing-modules/mocks/mock-comparisons/src/main/java/org/baeldung/mocks/testCase/LoginController.java b/testing-modules/mocks/src/main/java/org/baeldung/mocks/testCase/LoginController.java similarity index 100% rename from testing-modules/mocks/mock-comparisons/src/main/java/org/baeldung/mocks/testCase/LoginController.java rename to testing-modules/mocks/src/main/java/org/baeldung/mocks/testCase/LoginController.java diff --git a/testing-modules/mocks/mock-comparisons/src/main/java/org/baeldung/mocks/testCase/LoginDao.java b/testing-modules/mocks/src/main/java/org/baeldung/mocks/testCase/LoginDao.java similarity index 100% rename from testing-modules/mocks/mock-comparisons/src/main/java/org/baeldung/mocks/testCase/LoginDao.java rename to testing-modules/mocks/src/main/java/org/baeldung/mocks/testCase/LoginDao.java diff --git a/testing-modules/mocks/mock-comparisons/src/main/java/org/baeldung/mocks/testCase/LoginService.java b/testing-modules/mocks/src/main/java/org/baeldung/mocks/testCase/LoginService.java similarity index 100% rename from testing-modules/mocks/mock-comparisons/src/main/java/org/baeldung/mocks/testCase/LoginService.java rename to testing-modules/mocks/src/main/java/org/baeldung/mocks/testCase/LoginService.java diff --git a/testing-modules/mocks/mock-comparisons/src/main/java/org/baeldung/mocks/testCase/UserForm.java b/testing-modules/mocks/src/main/java/org/baeldung/mocks/testCase/UserForm.java similarity index 100% rename from testing-modules/mocks/mock-comparisons/src/main/java/org/baeldung/mocks/testCase/UserForm.java rename to testing-modules/mocks/src/main/java/org/baeldung/mocks/testCase/UserForm.java diff --git a/testing-modules/testing/src/main/resources/logback.xml b/testing-modules/mocks/src/main/resources/logback.xml similarity index 100% rename from testing-modules/testing/src/main/resources/logback.xml rename to testing-modules/mocks/src/main/resources/logback.xml diff --git a/testing-modules/mocks/mock-comparisons/src/test/java/com/baeldung/easymock/ArgumentMatchersUnitTest.java b/testing-modules/mocks/src/test/java/com/baeldung/easymock/ArgumentMatchersUnitTest.java similarity index 100% rename from testing-modules/mocks/mock-comparisons/src/test/java/com/baeldung/easymock/ArgumentMatchersUnitTest.java rename to testing-modules/mocks/src/test/java/com/baeldung/easymock/ArgumentMatchersUnitTest.java diff --git a/testing-modules/mocks/mock-comparisons/src/test/java/com/baeldung/easymock/BaeldungReaderAnnotatedUnitTest.java b/testing-modules/mocks/src/test/java/com/baeldung/easymock/BaeldungReaderAnnotatedUnitTest.java old mode 100755 new mode 100644 similarity index 96% rename from testing-modules/mocks/mock-comparisons/src/test/java/com/baeldung/easymock/BaeldungReaderAnnotatedUnitTest.java rename to testing-modules/mocks/src/test/java/com/baeldung/easymock/BaeldungReaderAnnotatedUnitTest.java index afacd8d8ad..56a58dd6e8 --- a/testing-modules/mocks/mock-comparisons/src/test/java/com/baeldung/easymock/BaeldungReaderAnnotatedUnitTest.java +++ b/testing-modules/mocks/src/test/java/com/baeldung/easymock/BaeldungReaderAnnotatedUnitTest.java @@ -1,57 +1,57 @@ -package com.baeldung.easymock; - -import org.easymock.EasyMockRunner; -import org.easymock.Mock; -import org.easymock.TestSubject; -import org.junit.*; -import org.junit.runner.RunWith; - -import java.util.NoSuchElementException; - -import static org.easymock.EasyMock.*; - -@RunWith(EasyMockRunner.class) -public class BaeldungReaderAnnotatedUnitTest { - - @Mock ArticleReader mockArticleReader; - - @Mock IArticleWriter mockArticleWriter; - - @TestSubject BaeldungReader baeldungReader = new BaeldungReader(); - - @Test - public void givenBaeldungReader_whenReadNext_thenNextArticleRead() { - expect(mockArticleReader.next()).andReturn(null); - replay(mockArticleReader); - baeldungReader.readNext(); - verify(mockArticleReader); - } - - @Mock BaeldungReader mockBaeldungReader; - - @Test - public void givenBaeldungReader_whenWrite_thenWriterCalled() { - expect(mockArticleWriter.write("title", "content")).andReturn(null); - replay(mockArticleWriter); - baeldungReader.write("title", "content"); - verify(mockArticleWriter); - } - - @Test - public void givenArticlesInReader_whenReadTillEnd_thenThrowException() { - expect(mockArticleReader.next()) - .andReturn(null) - .times(2) - .andThrow(new NoSuchElementException()); - replay(mockArticleReader); - try { - for (int i = 0; i < 3; i++) { - baeldungReader.readNext(); - } - } catch (Exception ignored) { - } - verify(mockArticleReader); - } - -} - +package com.baeldung.easymock; + +import org.easymock.EasyMockRunner; +import org.easymock.Mock; +import org.easymock.TestSubject; +import org.junit.*; +import org.junit.runner.RunWith; + +import java.util.NoSuchElementException; + +import static org.easymock.EasyMock.*; + +@RunWith(EasyMockRunner.class) +public class BaeldungReaderAnnotatedUnitTest { + + @Mock ArticleReader mockArticleReader; + + @Mock IArticleWriter mockArticleWriter; + + @TestSubject BaeldungReader baeldungReader = new BaeldungReader(); + + @Test + public void givenBaeldungReader_whenReadNext_thenNextArticleRead() { + expect(mockArticleReader.next()).andReturn(null); + replay(mockArticleReader); + baeldungReader.readNext(); + verify(mockArticleReader); + } + + @Mock BaeldungReader mockBaeldungReader; + + @Test + public void givenBaeldungReader_whenWrite_thenWriterCalled() { + expect(mockArticleWriter.write("title", "content")).andReturn(null); + replay(mockArticleWriter); + baeldungReader.write("title", "content"); + verify(mockArticleWriter); + } + + @Test + public void givenArticlesInReader_whenReadTillEnd_thenThrowException() { + expect(mockArticleReader.next()) + .andReturn(null) + .times(2) + .andThrow(new NoSuchElementException()); + replay(mockArticleReader); + try { + for (int i = 0; i < 3; i++) { + baeldungReader.readNext(); + } + } catch (Exception ignored) { + } + verify(mockArticleReader); + } + +} + diff --git a/testing-modules/mocks/mock-comparisons/src/test/java/com/baeldung/easymock/BaeldungReaderAnnotatedWithRuleUnitTest.java b/testing-modules/mocks/src/test/java/com/baeldung/easymock/BaeldungReaderAnnotatedWithRuleUnitTest.java old mode 100755 new mode 100644 similarity index 96% rename from testing-modules/mocks/mock-comparisons/src/test/java/com/baeldung/easymock/BaeldungReaderAnnotatedWithRuleUnitTest.java rename to testing-modules/mocks/src/test/java/com/baeldung/easymock/BaeldungReaderAnnotatedWithRuleUnitTest.java index 086ed88888..5b4219d263 --- a/testing-modules/mocks/mock-comparisons/src/test/java/com/baeldung/easymock/BaeldungReaderAnnotatedWithRuleUnitTest.java +++ b/testing-modules/mocks/src/test/java/com/baeldung/easymock/BaeldungReaderAnnotatedWithRuleUnitTest.java @@ -1,57 +1,57 @@ -package com.baeldung.easymock; - -import org.easymock.EasyMockRule; -import org.easymock.Mock; -import org.easymock.TestSubject; -import org.junit.*; - -import java.util.NoSuchElementException; - -import static org.easymock.EasyMock.*; - -public class BaeldungReaderAnnotatedWithRuleUnitTest { - - @Rule public EasyMockRule mockRule = new EasyMockRule(this); - - @Mock ArticleReader mockArticleReader; - - @Mock IArticleWriter mockArticleWriter; - - @TestSubject BaeldungReader baeldungReader = new BaeldungReader(); - - @Test - public void givenBaeldungReader_whenReadNext_thenNextArticleRead() { - expect(mockArticleReader.next()).andReturn(null); - replay(mockArticleReader); - baeldungReader.readNext(); - verify(mockArticleReader); - } - - @Mock BaeldungReader mockBaeldungReader; - - @Test - public void givenBaeldungReader_whenWrite_thenWriterCalled() { - expect(mockArticleWriter.write("title", "content")).andReturn(null); - replay(mockArticleWriter); - baeldungReader.write("title", "content"); - verify(mockArticleWriter); - } - - @Test - public void givenArticlesInReader_whenReadTillEnd_thenThrowException() { - expect(mockArticleReader.next()) - .andReturn(null) - .times(2) - .andThrow(new NoSuchElementException()); - replay(mockArticleReader); - try { - for (int i = 0; i < 3; i++) { - baeldungReader.readNext(); - } - } catch (Exception ignored) { - } - verify(mockArticleReader); - } - -} - +package com.baeldung.easymock; + +import org.easymock.EasyMockRule; +import org.easymock.Mock; +import org.easymock.TestSubject; +import org.junit.*; + +import java.util.NoSuchElementException; + +import static org.easymock.EasyMock.*; + +public class BaeldungReaderAnnotatedWithRuleUnitTest { + + @Rule public EasyMockRule mockRule = new EasyMockRule(this); + + @Mock ArticleReader mockArticleReader; + + @Mock IArticleWriter mockArticleWriter; + + @TestSubject BaeldungReader baeldungReader = new BaeldungReader(); + + @Test + public void givenBaeldungReader_whenReadNext_thenNextArticleRead() { + expect(mockArticleReader.next()).andReturn(null); + replay(mockArticleReader); + baeldungReader.readNext(); + verify(mockArticleReader); + } + + @Mock BaeldungReader mockBaeldungReader; + + @Test + public void givenBaeldungReader_whenWrite_thenWriterCalled() { + expect(mockArticleWriter.write("title", "content")).andReturn(null); + replay(mockArticleWriter); + baeldungReader.write("title", "content"); + verify(mockArticleWriter); + } + + @Test + public void givenArticlesInReader_whenReadTillEnd_thenThrowException() { + expect(mockArticleReader.next()) + .andReturn(null) + .times(2) + .andThrow(new NoSuchElementException()); + replay(mockArticleReader); + try { + for (int i = 0; i < 3; i++) { + baeldungReader.readNext(); + } + } catch (Exception ignored) { + } + verify(mockArticleReader); + } + +} + diff --git a/testing-modules/mocks/mock-comparisons/src/test/java/com/baeldung/easymock/BaeldungReaderMockDelegationUnitTest.java b/testing-modules/mocks/src/test/java/com/baeldung/easymock/BaeldungReaderMockDelegationUnitTest.java old mode 100755 new mode 100644 similarity index 97% rename from testing-modules/mocks/mock-comparisons/src/test/java/com/baeldung/easymock/BaeldungReaderMockDelegationUnitTest.java rename to testing-modules/mocks/src/test/java/com/baeldung/easymock/BaeldungReaderMockDelegationUnitTest.java index 89d3a2baee..6d8b553df7 --- a/testing-modules/mocks/mock-comparisons/src/test/java/com/baeldung/easymock/BaeldungReaderMockDelegationUnitTest.java +++ b/testing-modules/mocks/src/test/java/com/baeldung/easymock/BaeldungReaderMockDelegationUnitTest.java @@ -1,27 +1,27 @@ -package com.baeldung.easymock; - -import org.easymock.*; -import org.junit.*; - -import static org.easymock.EasyMock.*; - -public class BaeldungReaderMockDelegationUnitTest { - - EasyMockSupport easyMockSupport = new EasyMockSupport(); - - @Test - public void givenBaeldungReader_whenReadAndWriteSequencially_thenWorks() { - ArticleReader mockArticleReader = easyMockSupport.createMock(ArticleReader.class); - IArticleWriter mockArticleWriter = easyMockSupport.createMock(IArticleWriter.class); - BaeldungReader baeldungReader = new BaeldungReader(mockArticleReader, mockArticleWriter); - - expect(mockArticleReader.next()).andReturn(null); - expect(mockArticleWriter.write("title", "content")).andReturn(""); - easyMockSupport.replayAll(); - - baeldungReader.readNext(); - baeldungReader.write("title", "content"); - easyMockSupport.verifyAll(); - } - +package com.baeldung.easymock; + +import org.easymock.*; +import org.junit.*; + +import static org.easymock.EasyMock.*; + +public class BaeldungReaderMockDelegationUnitTest { + + EasyMockSupport easyMockSupport = new EasyMockSupport(); + + @Test + public void givenBaeldungReader_whenReadAndWriteSequencially_thenWorks() { + ArticleReader mockArticleReader = easyMockSupport.createMock(ArticleReader.class); + IArticleWriter mockArticleWriter = easyMockSupport.createMock(IArticleWriter.class); + BaeldungReader baeldungReader = new BaeldungReader(mockArticleReader, mockArticleWriter); + + expect(mockArticleReader.next()).andReturn(null); + expect(mockArticleWriter.write("title", "content")).andReturn(""); + easyMockSupport.replayAll(); + + baeldungReader.readNext(); + baeldungReader.write("title", "content"); + easyMockSupport.verifyAll(); + } + } \ No newline at end of file diff --git a/testing-modules/mocks/mock-comparisons/src/test/java/com/baeldung/easymock/BaeldungReaderMockSupportUnitTest.java b/testing-modules/mocks/src/test/java/com/baeldung/easymock/BaeldungReaderMockSupportUnitTest.java old mode 100755 new mode 100644 similarity index 96% rename from testing-modules/mocks/mock-comparisons/src/test/java/com/baeldung/easymock/BaeldungReaderMockSupportUnitTest.java rename to testing-modules/mocks/src/test/java/com/baeldung/easymock/BaeldungReaderMockSupportUnitTest.java index cd0c906949..8b5c4d9d41 --- a/testing-modules/mocks/mock-comparisons/src/test/java/com/baeldung/easymock/BaeldungReaderMockSupportUnitTest.java +++ b/testing-modules/mocks/src/test/java/com/baeldung/easymock/BaeldungReaderMockSupportUnitTest.java @@ -1,42 +1,42 @@ -package com.baeldung.easymock; - -import org.easymock.*; -import org.junit.*; -import org.junit.runner.RunWith; - -import java.util.NoSuchElementException; - -import static org.easymock.EasyMock.*; -import static org.junit.Assert.assertEquals; - -@RunWith(EasyMockRunner.class) -public class BaeldungReaderMockSupportUnitTest extends EasyMockSupport { - - @TestSubject BaeldungReader baeldungReader = new BaeldungReader(); - @Mock ArticleReader mockArticleReader; - @Mock IArticleWriter mockArticleWriter; - - @Test - public void givenBaeldungReader_whenReadAndWriteSequencially_thenWorks() { - expect(mockArticleReader.next()) - .andReturn(null) - .times(2) - .andThrow(new NoSuchElementException()); - expect(mockArticleWriter.write("title", "content")).andReturn("BAEL-201801"); - replayAll(); - - Exception expectedException = null; - try { - for (int i = 0; i < 3; i++) { - baeldungReader.readNext(); - } - } catch (Exception exception) { - expectedException = exception; - } - String articleId = baeldungReader.write("title", "content"); - verifyAll(); - assertEquals(NoSuchElementException.class, expectedException.getClass()); - assertEquals("BAEL-201801", articleId); - } - +package com.baeldung.easymock; + +import org.easymock.*; +import org.junit.*; +import org.junit.runner.RunWith; + +import java.util.NoSuchElementException; + +import static org.easymock.EasyMock.*; +import static org.junit.Assert.assertEquals; + +@RunWith(EasyMockRunner.class) +public class BaeldungReaderMockSupportUnitTest extends EasyMockSupport { + + @TestSubject BaeldungReader baeldungReader = new BaeldungReader(); + @Mock ArticleReader mockArticleReader; + @Mock IArticleWriter mockArticleWriter; + + @Test + public void givenBaeldungReader_whenReadAndWriteSequencially_thenWorks() { + expect(mockArticleReader.next()) + .andReturn(null) + .times(2) + .andThrow(new NoSuchElementException()); + expect(mockArticleWriter.write("title", "content")).andReturn("BAEL-201801"); + replayAll(); + + Exception expectedException = null; + try { + for (int i = 0; i < 3; i++) { + baeldungReader.readNext(); + } + } catch (Exception exception) { + expectedException = exception; + } + String articleId = baeldungReader.write("title", "content"); + verifyAll(); + assertEquals(NoSuchElementException.class, expectedException.getClass()); + assertEquals("BAEL-201801", articleId); + } + } \ No newline at end of file diff --git a/testing-modules/mocks/mock-comparisons/src/test/java/com/baeldung/easymock/BaeldungReaderUnitTest.java b/testing-modules/mocks/src/test/java/com/baeldung/easymock/BaeldungReaderUnitTest.java old mode 100755 new mode 100644 similarity index 97% rename from testing-modules/mocks/mock-comparisons/src/test/java/com/baeldung/easymock/BaeldungReaderUnitTest.java rename to testing-modules/mocks/src/test/java/com/baeldung/easymock/BaeldungReaderUnitTest.java index 31f6af116c..12fce4b07a --- a/testing-modules/mocks/mock-comparisons/src/test/java/com/baeldung/easymock/BaeldungReaderUnitTest.java +++ b/testing-modules/mocks/src/test/java/com/baeldung/easymock/BaeldungReaderUnitTest.java @@ -1,105 +1,105 @@ -package com.baeldung.easymock; - -import org.junit.*; - -import java.util.NoSuchElementException; - -import static org.easymock.EasyMock.*; -import static org.junit.Assert.assertEquals; - -public class BaeldungReaderUnitTest { - - private BaeldungReader baeldungReader; - - private ArticleReader mockArticleReader; - - private IArticleWriter mockArticleWriter; - - @Test - public void givenBaeldungReader_whenReadNext_thenNextArticleRead() { - mockArticleReader = mock(ArticleReader.class); - baeldungReader = new BaeldungReader(mockArticleReader); - - expect(mockArticleReader.next()).andReturn(null); - replay(mockArticleReader); - - BaeldungArticle article = baeldungReader.readNext(); - verify(mockArticleReader); - assertEquals(null, article); - } - - @Test - public void givenBaeldungReader_whenReadNextAndSkimTopics_thenAllAllowed() { - mockArticleReader = strictMock(ArticleReader.class); - baeldungReader = new BaeldungReader(mockArticleReader); - - expect(mockArticleReader.next()).andReturn(null); - expect(mockArticleReader.ofTopic("easymock")).andReturn(null); - replay(mockArticleReader); - - baeldungReader.readNext(); - baeldungReader.readTopic("easymock"); - verify(mockArticleReader); - } - - @Test - public void givenBaeldungReader_whenReadNextAndOthers_thenAllowed() { - mockArticleReader = niceMock(ArticleReader.class); - baeldungReader = new BaeldungReader(mockArticleReader); - - expect(mockArticleReader.next()).andReturn(null); - replay(mockArticleReader); - - baeldungReader.readNext(); - baeldungReader.readTopic("easymock"); - verify(mockArticleReader); - } - - @Test - public void givenBaeldungReader_whenWriteMaliciousContent_thenArgumentIllegal() { - mockArticleWriter = mock(IArticleWriter.class); - baeldungReader = new BaeldungReader(mockArticleWriter); - expect(mockArticleWriter.write("easymock", "")).andThrow(new IllegalArgumentException()); - replay(mockArticleWriter); - - Exception expectedException = null; - try { - baeldungReader.write("easymock", ""); - } catch (Exception exception) { - expectedException = exception; - } - - verify(mockArticleWriter); - assertEquals(IllegalArgumentException.class, expectedException.getClass()); - } - - @Test - public void givenBaeldungReader_whenWrite_thenWriterCalled() { - mockArticleWriter = mock(IArticleWriter.class); - baeldungReader = new BaeldungReader(mockArticleWriter); - expect(mockArticleWriter.write("title", "content")).andReturn(null); - replay(mockArticleWriter); - String articleId = baeldungReader.write("title", "content"); - verify(mockArticleWriter); - assertEquals(null, articleId); - } - - @Test - public void givenArticlesInReader_whenReadTillEnd_thenThrowException() { - ArticleReader mockArticleReader = mock(ArticleReader.class); - baeldungReader = new BaeldungReader(mockArticleReader); - expect(mockArticleReader.next()) - .andReturn(null) - .times(2) - .andThrow(new NoSuchElementException()); - replay(mockArticleReader); - try { - for (int i = 0; i < 3; i++) { - baeldungReader.readNext(); - } - } catch (Exception ignored) { - } - verify(mockArticleReader); - } - +package com.baeldung.easymock; + +import org.junit.*; + +import java.util.NoSuchElementException; + +import static org.easymock.EasyMock.*; +import static org.junit.Assert.assertEquals; + +public class BaeldungReaderUnitTest { + + private BaeldungReader baeldungReader; + + private ArticleReader mockArticleReader; + + private IArticleWriter mockArticleWriter; + + @Test + public void givenBaeldungReader_whenReadNext_thenNextArticleRead() { + mockArticleReader = mock(ArticleReader.class); + baeldungReader = new BaeldungReader(mockArticleReader); + + expect(mockArticleReader.next()).andReturn(null); + replay(mockArticleReader); + + BaeldungArticle article = baeldungReader.readNext(); + verify(mockArticleReader); + assertEquals(null, article); + } + + @Test + public void givenBaeldungReader_whenReadNextAndSkimTopics_thenAllAllowed() { + mockArticleReader = strictMock(ArticleReader.class); + baeldungReader = new BaeldungReader(mockArticleReader); + + expect(mockArticleReader.next()).andReturn(null); + expect(mockArticleReader.ofTopic("easymock")).andReturn(null); + replay(mockArticleReader); + + baeldungReader.readNext(); + baeldungReader.readTopic("easymock"); + verify(mockArticleReader); + } + + @Test + public void givenBaeldungReader_whenReadNextAndOthers_thenAllowed() { + mockArticleReader = niceMock(ArticleReader.class); + baeldungReader = new BaeldungReader(mockArticleReader); + + expect(mockArticleReader.next()).andReturn(null); + replay(mockArticleReader); + + baeldungReader.readNext(); + baeldungReader.readTopic("easymock"); + verify(mockArticleReader); + } + + @Test + public void givenBaeldungReader_whenWriteMaliciousContent_thenArgumentIllegal() { + mockArticleWriter = mock(IArticleWriter.class); + baeldungReader = new BaeldungReader(mockArticleWriter); + expect(mockArticleWriter.write("easymock", "")).andThrow(new IllegalArgumentException()); + replay(mockArticleWriter); + + Exception expectedException = null; + try { + baeldungReader.write("easymock", ""); + } catch (Exception exception) { + expectedException = exception; + } + + verify(mockArticleWriter); + assertEquals(IllegalArgumentException.class, expectedException.getClass()); + } + + @Test + public void givenBaeldungReader_whenWrite_thenWriterCalled() { + mockArticleWriter = mock(IArticleWriter.class); + baeldungReader = new BaeldungReader(mockArticleWriter); + expect(mockArticleWriter.write("title", "content")).andReturn(null); + replay(mockArticleWriter); + String articleId = baeldungReader.write("title", "content"); + verify(mockArticleWriter); + assertEquals(null, articleId); + } + + @Test + public void givenArticlesInReader_whenReadTillEnd_thenThrowException() { + ArticleReader mockArticleReader = mock(ArticleReader.class); + baeldungReader = new BaeldungReader(mockArticleReader); + expect(mockArticleReader.next()) + .andReturn(null) + .times(2) + .andThrow(new NoSuchElementException()); + replay(mockArticleReader); + try { + for (int i = 0; i < 3; i++) { + baeldungReader.readNext(); + } + } catch (Exception ignored) { + } + verify(mockArticleReader); + } + } \ No newline at end of file diff --git a/testing-modules/testing/src/test/java/com/baeldung/introductionjukito/CalculatorUnitTest.java b/testing-modules/mocks/src/test/java/com/baeldung/introductionjukito/CalculatorUnitTest.java similarity index 100% rename from testing-modules/testing/src/test/java/com/baeldung/introductionjukito/CalculatorUnitTest.java rename to testing-modules/mocks/src/test/java/com/baeldung/introductionjukito/CalculatorUnitTest.java diff --git a/testing-modules/testing/src/test/java/com/baeldung/javafaker/JavaFakerUnitTest.java b/testing-modules/mocks/src/test/java/com/baeldung/javafaker/JavaFakerUnitTest.java similarity index 100% rename from testing-modules/testing/src/test/java/com/baeldung/javafaker/JavaFakerUnitTest.java rename to testing-modules/mocks/src/test/java/com/baeldung/javafaker/JavaFakerUnitTest.java diff --git a/testing-modules/mocks/jmockit/src/test/java/com/baeldung/mocks/jmockit/AppManagerUnitTest.java b/testing-modules/mocks/src/test/java/com/baeldung/mocks/jmockit/AppManagerUnitTest.java similarity index 59% rename from testing-modules/mocks/jmockit/src/test/java/com/baeldung/mocks/jmockit/AppManagerUnitTest.java rename to testing-modules/mocks/src/test/java/com/baeldung/mocks/jmockit/AppManagerUnitTest.java index 2c5aa2a3ed..ecdc6e441e 100644 --- a/testing-modules/mocks/jmockit/src/test/java/com/baeldung/mocks/jmockit/AppManagerUnitTest.java +++ b/testing-modules/mocks/src/test/java/com/baeldung/mocks/jmockit/AppManagerUnitTest.java @@ -28,17 +28,4 @@ public class AppManagerUnitTest { Assertions.assertFalse(appManager.managerResponse("Why are you coming late?")); } - - @Test - public void givenAppManager_whenPrivateStaticMethod_thenValidateExpectedResponse() { - final int response = Deencapsulation.invoke(AppManager.class, "stringToInteger", "110"); - Assertions.assertEquals(110, response); - } - - @Test - public void givenAppManager_whenPrivateStaticMethod_thenExpectException() { - Assertions.assertThrows(IllegalArgumentException.class, () -> { - Deencapsulation.invoke(AppManager.class, "stringToInteger", "11r"); - }); - } } diff --git a/testing-modules/mocks/mock-comparisons/src/test/java/org/baeldung/mocks/easymock/LoginControllerIntegrationTest.java b/testing-modules/mocks/src/test/java/org/baeldung/mocks/easymock/LoginControllerIntegrationTest.java similarity index 100% rename from testing-modules/mocks/mock-comparisons/src/test/java/org/baeldung/mocks/easymock/LoginControllerIntegrationTest.java rename to testing-modules/mocks/src/test/java/org/baeldung/mocks/easymock/LoginControllerIntegrationTest.java diff --git a/testing-modules/mocks/jmockit/src/test/java/org/baeldung/mocks/jmockit/AdvancedCollaboratorIntegrationTest.java b/testing-modules/mocks/src/test/java/org/baeldung/mocks/jmockit/AdvancedCollaboratorIntegrationTest.java similarity index 77% rename from testing-modules/mocks/jmockit/src/test/java/org/baeldung/mocks/jmockit/AdvancedCollaboratorIntegrationTest.java rename to testing-modules/mocks/src/test/java/org/baeldung/mocks/jmockit/AdvancedCollaboratorIntegrationTest.java index 5cde912277..3a30342eb9 100644 --- a/testing-modules/mocks/jmockit/src/test/java/org/baeldung/mocks/jmockit/AdvancedCollaboratorIntegrationTest.java +++ b/testing-modules/mocks/src/test/java/org/baeldung/mocks/jmockit/AdvancedCollaboratorIntegrationTest.java @@ -1,12 +1,10 @@ package org.baeldung.mocks.jmockit; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; import java.util.ArrayList; import java.util.List; -import org.baeldung.mocks.jmockit.AdvancedCollaborator.InnerAdvancedCollaborator; import org.junit.Test; import org.junit.runner.RunWith; @@ -52,12 +50,6 @@ public class AdvancedCollaboratorIntegrationTest assertEquals(1, coll.i); } - @Test - public void testToCallPrivateMethodsDirectly() { - Object value = Deencapsulation.invoke(mock, "privateMethod"); - assertEquals("default:", value); - } - @Test public void testToSetPrivateFieldDirectly() { Deencapsulation.setField(mock, "privateField", 10); @@ -70,18 +62,6 @@ public class AdvancedCollaboratorIntegrationTest assertEquals(5, value); } - @Test - public void testToCreateNewInstanceDirectly() { - AdvancedCollaborator coll = Deencapsulation.newInstance(AdvancedCollaborator.class, "foo"); - assertEquals(3, coll.i); - } - - @Test - public void testToCreateNewInnerClassInstanceDirectly() { - InnerAdvancedCollaborator innerCollaborator = Deencapsulation.newInnerInstance(InnerAdvancedCollaborator.class, mock); - assertNotNull(innerCollaborator); - } - @Test @SuppressWarnings("unchecked") public void testMultipleInterfacesWholeTest() { diff --git a/testing-modules/mocks/jmockit/src/test/java/org/baeldung/mocks/jmockit/ExpectationsIntegrationTest.java b/testing-modules/mocks/src/test/java/org/baeldung/mocks/jmockit/ExpectationsIntegrationTest.java similarity index 98% rename from testing-modules/mocks/jmockit/src/test/java/org/baeldung/mocks/jmockit/ExpectationsIntegrationTest.java rename to testing-modules/mocks/src/test/java/org/baeldung/mocks/jmockit/ExpectationsIntegrationTest.java index c176cdc182..8b85af6243 100644 --- a/testing-modules/mocks/jmockit/src/test/java/org/baeldung/mocks/jmockit/ExpectationsIntegrationTest.java +++ b/testing-modules/mocks/src/test/java/org/baeldung/mocks/jmockit/ExpectationsIntegrationTest.java @@ -3,7 +3,6 @@ package org.baeldung.mocks.jmockit; import mockit.Delegate; import mockit.Expectations; import mockit.Mocked; -import mockit.StrictExpectations; import mockit.Verifications; import mockit.integration.junit4.JMockit; import org.hamcrest.BaseMatcher; @@ -106,7 +105,7 @@ public class ExpectationsIntegrationTest { @Test public void testResultAndReturns(@Mocked ExpectationsCollaborator mock) { - new StrictExpectations() {{ + new Expectations() {{ mock.methodReturnsString(); result = "foo"; result = new Exception(); diff --git a/testing-modules/mocks/mock-comparisons/src/test/java/org/baeldung/mocks/jmockit/LoginControllerIntegrationTest.java b/testing-modules/mocks/src/test/java/org/baeldung/mocks/jmockit/LoginControllerIntegrationTest.java similarity index 100% rename from testing-modules/mocks/mock-comparisons/src/test/java/org/baeldung/mocks/jmockit/LoginControllerIntegrationTest.java rename to testing-modules/mocks/src/test/java/org/baeldung/mocks/jmockit/LoginControllerIntegrationTest.java diff --git a/testing-modules/mocks/jmockit/src/test/java/org/baeldung/mocks/jmockit/PerformerIntegrationTest.java b/testing-modules/mocks/src/test/java/org/baeldung/mocks/jmockit/PerformerIntegrationTest.java similarity index 100% rename from testing-modules/mocks/jmockit/src/test/java/org/baeldung/mocks/jmockit/PerformerIntegrationTest.java rename to testing-modules/mocks/src/test/java/org/baeldung/mocks/jmockit/PerformerIntegrationTest.java diff --git a/testing-modules/mocks/jmockit/src/test/java/org/baeldung/mocks/jmockit/ReusingIntegrationTest.java b/testing-modules/mocks/src/test/java/org/baeldung/mocks/jmockit/ReusingIntegrationTest.java similarity index 100% rename from testing-modules/mocks/jmockit/src/test/java/org/baeldung/mocks/jmockit/ReusingIntegrationTest.java rename to testing-modules/mocks/src/test/java/org/baeldung/mocks/jmockit/ReusingIntegrationTest.java diff --git a/testing-modules/mocks/mock-comparisons/src/test/java/org/baeldung/mocks/mockito/LoginControllerIntegrationTest.java b/testing-modules/mocks/src/test/java/org/baeldung/mocks/mockito/LoginControllerIntegrationTest.java similarity index 100% rename from testing-modules/mocks/mock-comparisons/src/test/java/org/baeldung/mocks/mockito/LoginControllerIntegrationTest.java rename to testing-modules/mocks/src/test/java/org/baeldung/mocks/mockito/LoginControllerIntegrationTest.java diff --git a/testing-modules/pom.xml b/testing-modules/pom.xml index e15fdb4a37..c0eb59f15e 100644 --- a/testing-modules/pom.xml +++ b/testing-modules/pom.xml @@ -14,6 +14,7 @@ + assertion-libraries easy-random gatling groovy-spock @@ -31,12 +32,12 @@ selenium-junit-testng spring-testing test-containers - testing testng junit-5-basics easymock junit-5-advanced xmlunit-2 junit-4 + testing-libraries diff --git a/testing-modules/rest-assured/src/test/java/com/baeldung/restassured/Odd.java b/testing-modules/rest-assured/src/test/java/com/baeldung/restassured/Odd.java index f60f1764c6..c3f82f0836 100644 --- a/testing-modules/rest-assured/src/test/java/com/baeldung/restassured/Odd.java +++ b/testing-modules/rest-assured/src/test/java/com/baeldung/restassured/Odd.java @@ -1,49 +1,49 @@ -package com.baeldung.restassured; - -public class Odd { - - float price; - int status; - float ck; - String name; - - Odd(float price, int status, float ck, String name) { - this.price = price; - this.status = status; - this.ck = ck; - this.name = name; - } - - public float getPrice() { - return price; - } - - public void setPrice(float price) { - this.price = price; - } - - public int getStatus() { - return status; - } - - public void setStatus(int status) { - this.status = status; - } - - public float getCk() { - return ck; - } - - public void setCk(float ck) { - this.ck = ck; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - -} +package com.baeldung.restassured; + +public class Odd { + + float price; + int status; + float ck; + String name; + + Odd(float price, int status, float ck, String name) { + this.price = price; + this.status = status; + this.ck = ck; + this.name = name; + } + + public float getPrice() { + return price; + } + + public void setPrice(float price) { + this.price = price; + } + + public int getStatus() { + return status; + } + + public void setStatus(int status) { + this.status = status; + } + + public float getCk() { + return ck; + } + + public void setCk(float ck) { + this.ck = ck; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + +} diff --git a/testing-modules/selenium-junit-testng/geckodriver.mac b/testing-modules/selenium-junit-testng/geckodriver.mac old mode 100755 new mode 100644 diff --git a/testing-modules/spring-testing/src/main/java/org/baeldung/reflectiontestutils/repository/Employee.java b/testing-modules/spring-testing/src/main/java/org/baeldung/reflectiontestutils/repository/Employee.java index 0677b05d66..8d4eeb57c4 100644 --- a/testing-modules/spring-testing/src/main/java/org/baeldung/reflectiontestutils/repository/Employee.java +++ b/testing-modules/spring-testing/src/main/java/org/baeldung/reflectiontestutils/repository/Employee.java @@ -1,23 +1,23 @@ -package org.baeldung.reflectiontestutils.repository; - -public class Employee { - private Integer id; - private String name; - - public Integer getId() { - return id; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - private String employeeToString() { - return "id: " + getId() + "; name: " + getName(); - } - -} +package org.baeldung.reflectiontestutils.repository; + +public class Employee { + private Integer id; + private String name; + + public Integer getId() { + return id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + private String employeeToString() { + return "id: " + getId() + "; name: " + getName(); + } + +} diff --git a/testing-modules/spring-testing/src/main/java/org/baeldung/reflectiontestutils/repository/EmployeeService.java b/testing-modules/spring-testing/src/main/java/org/baeldung/reflectiontestutils/repository/EmployeeService.java index 699ec3236c..d28745e2ab 100644 --- a/testing-modules/spring-testing/src/main/java/org/baeldung/reflectiontestutils/repository/EmployeeService.java +++ b/testing-modules/spring-testing/src/main/java/org/baeldung/reflectiontestutils/repository/EmployeeService.java @@ -1,14 +1,14 @@ -package org.baeldung.reflectiontestutils.repository; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; - -@Component -public class EmployeeService { - @Autowired - private HRService hrService; - - public String findEmployeeStatus(Integer employeeId) { - return "Employee " + employeeId + " status: " + hrService.getEmployeeStatus(employeeId); - } -} +package org.baeldung.reflectiontestutils.repository; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +@Component +public class EmployeeService { + @Autowired + private HRService hrService; + + public String findEmployeeStatus(Integer employeeId) { + return "Employee " + employeeId + " status: " + hrService.getEmployeeStatus(employeeId); + } +} diff --git a/testing-modules/spring-testing/src/main/java/org/baeldung/reflectiontestutils/repository/HRService.java b/testing-modules/spring-testing/src/main/java/org/baeldung/reflectiontestutils/repository/HRService.java index e693aca764..13a5973ee8 100644 --- a/testing-modules/spring-testing/src/main/java/org/baeldung/reflectiontestutils/repository/HRService.java +++ b/testing-modules/spring-testing/src/main/java/org/baeldung/reflectiontestutils/repository/HRService.java @@ -1,11 +1,11 @@ -package org.baeldung.reflectiontestutils.repository; - -import org.springframework.stereotype.Component; - -@Component -public class HRService { - - public String getEmployeeStatus(Integer employeeId) { - return "Inactive"; - } -} +package org.baeldung.reflectiontestutils.repository; + +import org.springframework.stereotype.Component; + +@Component +public class HRService { + + public String getEmployeeStatus(Integer employeeId) { + return "Inactive"; + } +} diff --git a/testing-modules/spring-testing/src/test/java/org/baeldung/reflectiontestutils/ReflectionTestUtilsUnitTest.java b/testing-modules/spring-testing/src/test/java/org/baeldung/reflectiontestutils/ReflectionTestUtilsUnitTest.java index 64c7ca19ef..36e5428b58 100644 --- a/testing-modules/spring-testing/src/test/java/org/baeldung/reflectiontestutils/ReflectionTestUtilsUnitTest.java +++ b/testing-modules/spring-testing/src/test/java/org/baeldung/reflectiontestutils/ReflectionTestUtilsUnitTest.java @@ -1,46 +1,46 @@ -package org.baeldung.reflectiontestutils; - -import static org.junit.Assert.*; -import static org.mockito.Mockito.mock; - -import org.baeldung.reflectiontestutils.repository.Employee; -import org.baeldung.reflectiontestutils.repository.EmployeeService; -import org.baeldung.reflectiontestutils.repository.HRService; -import org.junit.Test; -import org.springframework.test.util.ReflectionTestUtils; - -import static org.mockito.Mockito.when; - -public class ReflectionTestUtilsUnitTest { - - @Test - public void whenNonPublicField_thenReflectionTestUtilsSetField() { - Employee employee = new Employee(); - ReflectionTestUtils.setField(employee, "id", 1); - assertTrue(employee.getId().equals(1)); - - } - - @Test - public void whenNonPublicMethod_thenReflectionTestUtilsInvokeMethod() { - Employee employee = new Employee(); - ReflectionTestUtils.setField(employee, "id", 1); - employee.setName("Smith, John"); - assertTrue(ReflectionTestUtils.invokeMethod(employee, "employeeToString").equals("id: 1; name: Smith, John")); - } - - @Test - public void whenInjectingMockOfDependency_thenReflectionTestUtilsSetField() { - Employee employee = new Employee(); - ReflectionTestUtils.setField(employee, "id", 1); - employee.setName("Smith, John"); - - HRService hrService = mock(HRService.class); - when(hrService.getEmployeeStatus(employee.getId())).thenReturn("Active"); - EmployeeService employeeService = new EmployeeService(); - - // Inject mock into the private field - ReflectionTestUtils.setField(employeeService, "hrService", hrService); - assertEquals("Employee " + employee.getId() + " status: Active", employeeService.findEmployeeStatus(employee.getId())); - } -} +package org.baeldung.reflectiontestutils; + +import static org.junit.Assert.*; +import static org.mockito.Mockito.mock; + +import org.baeldung.reflectiontestutils.repository.Employee; +import org.baeldung.reflectiontestutils.repository.EmployeeService; +import org.baeldung.reflectiontestutils.repository.HRService; +import org.junit.Test; +import org.springframework.test.util.ReflectionTestUtils; + +import static org.mockito.Mockito.when; + +public class ReflectionTestUtilsUnitTest { + + @Test + public void whenNonPublicField_thenReflectionTestUtilsSetField() { + Employee employee = new Employee(); + ReflectionTestUtils.setField(employee, "id", 1); + assertTrue(employee.getId().equals(1)); + + } + + @Test + public void whenNonPublicMethod_thenReflectionTestUtilsInvokeMethod() { + Employee employee = new Employee(); + ReflectionTestUtils.setField(employee, "id", 1); + employee.setName("Smith, John"); + assertTrue(ReflectionTestUtils.invokeMethod(employee, "employeeToString").equals("id: 1; name: Smith, John")); + } + + @Test + public void whenInjectingMockOfDependency_thenReflectionTestUtilsSetField() { + Employee employee = new Employee(); + ReflectionTestUtils.setField(employee, "id", 1); + employee.setName("Smith, John"); + + HRService hrService = mock(HRService.class); + when(hrService.getEmployeeStatus(employee.getId())).thenReturn("Active"); + EmployeeService employeeService = new EmployeeService(); + + // Inject mock into the private field + ReflectionTestUtils.setField(employeeService, "hrService", hrService); + assertEquals("Employee " + employee.getId() + " status: Active", employeeService.findEmployeeStatus(employee.getId())); + } +} diff --git a/testing-modules/testing-libraries/README.md b/testing-modules/testing-libraries/README.md new file mode 100644 index 0000000000..0cd9ca7f2e --- /dev/null +++ b/testing-modules/testing-libraries/README.md @@ -0,0 +1,10 @@ + +## Relevant Articles + +- [Mutation Testing with PITest](http://www.baeldung.com/java-mutation-testing-with-pitest) +- [Intro to JaCoCo](http://www.baeldung.com/jacoco) +- [Cucumber and Scenario Outline](http://www.baeldung.com/cucumber-scenario-outline) +- [Cucumber Java 8 Support](http://www.baeldung.com/cucumber-java-8-support) +- [Introduction to Lambda Behave](http://www.baeldung.com/lambda-behave) +- [Running JUnit Tests Programmatically, from a Java Application](https://www.baeldung.com/junit-tests-run-programmatically-from-java) + diff --git a/testing-modules/testing-libraries/pom.xml b/testing-modules/testing-libraries/pom.xml new file mode 100644 index 0000000000..7891ebbf6d --- /dev/null +++ b/testing-modules/testing-libraries/pom.xml @@ -0,0 +1,46 @@ + + + 4.0.0 + testing-libraries + testing-libraries + + + com.baeldung + testing-modules + 1.0.0-SNAPSHOT + + + + + com.insightfullogic + lambda-behave + ${lambda-behave.version} + + + info.cukes + cucumber-junit + ${cucumber.version} + test + + + info.cukes + cucumber-java + ${cucumber.version} + test + + + info.cukes + cucumber-java8 + ${cucumber.version} + test + + + + + 0.4 + 1.2.5 + + + + diff --git a/testing-modules/testing/src/main/java/com/baeldung/cucumber/Calculator.java b/testing-modules/testing-libraries/src/main/java/com/baeldung/cucumber/Calculator.java similarity index 100% rename from testing-modules/testing/src/main/java/com/baeldung/cucumber/Calculator.java rename to testing-modules/testing-libraries/src/main/java/com/baeldung/cucumber/Calculator.java diff --git a/testing-modules/testing/src/main/java/com/baeldung/lambdabehave/Calculator.java b/testing-modules/testing-libraries/src/main/java/com/baeldung/lambdabehave/Calculator.java similarity index 100% rename from testing-modules/testing/src/main/java/com/baeldung/lambdabehave/Calculator.java rename to testing-modules/testing-libraries/src/main/java/com/baeldung/lambdabehave/Calculator.java diff --git a/testing-modules/testing/src/main/java/com/baeldung/testing/mutation/Palindrome.java b/testing-modules/testing-libraries/src/main/java/com/baeldung/mutation/Palindrome.java similarity index 92% rename from testing-modules/testing/src/main/java/com/baeldung/testing/mutation/Palindrome.java rename to testing-modules/testing-libraries/src/main/java/com/baeldung/mutation/Palindrome.java index e264a4c759..5e6fb7e4da 100644 --- a/testing-modules/testing/src/main/java/com/baeldung/testing/mutation/Palindrome.java +++ b/testing-modules/testing-libraries/src/main/java/com/baeldung/mutation/Palindrome.java @@ -1,4 +1,4 @@ -package com.baeldung.testing.mutation; +package com.baeldung.mutation; public class Palindrome { diff --git a/testing-modules/testing/src/test/java/com/baeldung/testing/calculator/CalculatorIntegrationTest.java b/testing-modules/testing-libraries/src/test/java/com/baeldung/calculator/CalculatorIntegrationTest.java similarity index 91% rename from testing-modules/testing/src/test/java/com/baeldung/testing/calculator/CalculatorIntegrationTest.java rename to testing-modules/testing-libraries/src/test/java/com/baeldung/calculator/CalculatorIntegrationTest.java index 20bd62396c..00f666db2d 100644 --- a/testing-modules/testing/src/test/java/com/baeldung/testing/calculator/CalculatorIntegrationTest.java +++ b/testing-modules/testing-libraries/src/test/java/com/baeldung/calculator/CalculatorIntegrationTest.java @@ -1,4 +1,4 @@ -package com.baeldung.testing.calculator; +package com.baeldung.calculator; import cucumber.api.CucumberOptions; import cucumber.api.junit.Cucumber; diff --git a/testing-modules/testing/src/test/java/com/baeldung/testing/calculator/CalculatorRunSteps.java b/testing-modules/testing-libraries/src/test/java/com/baeldung/calculator/CalculatorRunSteps.java similarity index 95% rename from testing-modules/testing/src/test/java/com/baeldung/testing/calculator/CalculatorRunSteps.java rename to testing-modules/testing-libraries/src/test/java/com/baeldung/calculator/CalculatorRunSteps.java index ef6dff9617..7eda618566 100644 --- a/testing-modules/testing/src/test/java/com/baeldung/testing/calculator/CalculatorRunSteps.java +++ b/testing-modules/testing-libraries/src/test/java/com/baeldung/calculator/CalculatorRunSteps.java @@ -1,4 +1,4 @@ -package com.baeldung.testing.calculator; +package com.baeldung.calculator; import com.baeldung.cucumber.Calculator; import cucumber.api.java.Before; diff --git a/testing-modules/testing/src/test/java/com/baeldung/lambdabehave/CalculatorUnitTest.java b/testing-modules/testing-libraries/src/test/java/com/baeldung/lambdabehave/CalculatorUnitTest.java similarity index 100% rename from testing-modules/testing/src/test/java/com/baeldung/lambdabehave/CalculatorUnitTest.java rename to testing-modules/testing-libraries/src/test/java/com/baeldung/lambdabehave/CalculatorUnitTest.java diff --git a/testing-modules/testing/src/test/java/com/baeldung/mutation/test/PalindromeUnitTest.java b/testing-modules/testing-libraries/src/test/java/com/baeldung/mutation/PalindromeUnitTest.java similarity index 90% rename from testing-modules/testing/src/test/java/com/baeldung/mutation/test/PalindromeUnitTest.java rename to testing-modules/testing-libraries/src/test/java/com/baeldung/mutation/PalindromeUnitTest.java index 5321735469..cb4830a6fb 100644 --- a/testing-modules/testing/src/test/java/com/baeldung/mutation/test/PalindromeUnitTest.java +++ b/testing-modules/testing-libraries/src/test/java/com/baeldung/mutation/PalindromeUnitTest.java @@ -1,11 +1,11 @@ -package com.baeldung.mutation.test; +package com.baeldung.mutation; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import org.junit.Test; -import com.baeldung.testing.mutation.Palindrome; +import com.baeldung.mutation.Palindrome; public class PalindromeUnitTest { @Test diff --git a/testing-modules/testing/src/test/java/com/baeldung/testing/shopping/ShoppingIntegrationTest.java b/testing-modules/testing-libraries/src/test/java/com/baeldung/shopping/ShoppingIntegrationTest.java similarity index 86% rename from testing-modules/testing/src/test/java/com/baeldung/testing/shopping/ShoppingIntegrationTest.java rename to testing-modules/testing-libraries/src/test/java/com/baeldung/shopping/ShoppingIntegrationTest.java index 7bf8641ed6..20fd65b02a 100644 --- a/testing-modules/testing/src/test/java/com/baeldung/testing/shopping/ShoppingIntegrationTest.java +++ b/testing-modules/testing-libraries/src/test/java/com/baeldung/shopping/ShoppingIntegrationTest.java @@ -1,4 +1,4 @@ -package com.baeldung.testing.shopping; +package com.baeldung.shopping; import org.junit.runner.RunWith; diff --git a/testing-modules/testing/src/test/java/com/baeldung/testing/shopping/ShoppingStepsDef.java b/testing-modules/testing-libraries/src/test/java/com/baeldung/shopping/ShoppingStepsDef.java similarity index 92% rename from testing-modules/testing/src/test/java/com/baeldung/testing/shopping/ShoppingStepsDef.java rename to testing-modules/testing-libraries/src/test/java/com/baeldung/shopping/ShoppingStepsDef.java index 2c4bf2eeae..c56ec95883 100644 --- a/testing-modules/testing/src/test/java/com/baeldung/testing/shopping/ShoppingStepsDef.java +++ b/testing-modules/testing-libraries/src/test/java/com/baeldung/shopping/ShoppingStepsDef.java @@ -1,4 +1,4 @@ -package com.baeldung.testing.shopping; +package com.baeldung.shopping; import static org.junit.Assert.assertEquals; import cucumber.api.java8.En; diff --git a/testing-modules/testing/src/test/resources/features/calculator-scenario-outline.feature b/testing-modules/testing-libraries/src/test/resources/features/calculator-scenario-outline.feature similarity index 100% rename from testing-modules/testing/src/test/resources/features/calculator-scenario-outline.feature rename to testing-modules/testing-libraries/src/test/resources/features/calculator-scenario-outline.feature diff --git a/testing-modules/testing/src/test/resources/features/calculator.feature b/testing-modules/testing-libraries/src/test/resources/features/calculator.feature similarity index 100% rename from testing-modules/testing/src/test/resources/features/calculator.feature rename to testing-modules/testing-libraries/src/test/resources/features/calculator.feature diff --git a/testing-modules/testing/src/test/resources/features/shopping.feature b/testing-modules/testing-libraries/src/test/resources/features/shopping.feature similarity index 100% rename from testing-modules/testing/src/test/resources/features/shopping.feature rename to testing-modules/testing-libraries/src/test/resources/features/shopping.feature diff --git a/testing-modules/testing/README.md b/testing-modules/testing/README.md deleted file mode 100644 index 135ef1054a..0000000000 --- a/testing-modules/testing/README.md +++ /dev/null @@ -1,24 +0,0 @@ -========= - -## Mutation Testing - -### Relevant Articles: -- [Mutation Testing with PITest](http://www.baeldung.com/java-mutation-testing-with-pitest) -- [Intro to JaCoCo](http://www.baeldung.com/jacoco) -- [AssertJ’s Java 8 Features](http://www.baeldung.com/assertJ-java-8-features) -- [AssertJ for Guava](http://www.baeldung.com/assertJ-for-guava) -- [Introduction to AssertJ](http://www.baeldung.com/introduction-to-assertj) -- [Cucumber and Scenario Outline](http://www.baeldung.com/cucumber-scenario-outline) -- [Testing with Google Truth](http://www.baeldung.com/google-truth) -- [Testing with JGoTesting](http://www.baeldung.com/jgotesting) -- [Introduction to JUnitParams](http://www.baeldung.com/junit-params) -- [Cucumber Java 8 Support](http://www.baeldung.com/cucumber-java-8-support) -- [Introduction to Lambda Behave](http://www.baeldung.com/lambda-behave) -- [Introduction to Jukito](http://www.baeldung.com/jukito) -- [Custom JUnit 4 Test Runners](http://www.baeldung.com/junit-4-custom-runners) -- [Guide to JSpec](http://www.baeldung.com/jspec) -- [Custom Assertions with AssertJ](http://www.baeldung.com/assertj-custom-assertion) -- [Using Conditions with AssertJ Assertions](http://www.baeldung.com/assertj-conditions) -- [A Guide to JavaFaker](https://www.baeldung.com/java-faker) -- [Running JUnit Tests Programmatically, from a Java Application](https://www.baeldung.com/junit-tests-run-programmatically-from-java) -- [AssertJ Exception Assertions](http://www.baeldung.com/assertj-exception-assertion) diff --git a/testing-modules/testing/pom.xml b/testing-modules/testing/pom.xml deleted file mode 100644 index ccfa1070d1..0000000000 --- a/testing-modules/testing/pom.xml +++ /dev/null @@ -1,183 +0,0 @@ - - 4.0.0 - com.baeldung - testing - 0.1-SNAPSHOT - testing - - - com.baeldung - parent-java - 0.0.1-SNAPSHOT - ../../parent-java - - - - - com.insightfullogic - lambda-behave - ${lambda-behave.version} - - - org.assertj - assertj-guava - ${assertj-guava.version} - - - org.assertj - assertj-core - ${assertj-core.version} - test - - - info.cukes - cucumber-junit - ${cucumber.version} - test - - - info.cukes - cucumber-java - ${cucumber.version} - test - - - info.cukes - cucumber-java8 - ${cucumber.version} - test - - - org.pitest - pitest-parent - ${pitest.version} - pom - - - com.google.truth - truth - ${truth.version} - - - com.google.truth.extensions - truth-java8-extension - ${truth.version} - test - - - pl.pragmatists - JUnitParams - ${jUnitParams.version} - test - - - org.jgotesting - jgotesting - ${jgotesting.version} - test - - - org.jukito - jukito - ${jukito.version} - test - - - org.javalite - javalite-common - ${javalite.version} - - - com.github.javafaker - javafaker - ${javafaker.version} - - - - - - - org.pitest - pitest-maven - ${pitest.version} - - - com.baeldung.testing.mutation.* - - - com.baeldung.mutation.test.* - - - - - org.jacoco - jacoco-maven-plugin - ${jacoco.version} - - - - prepare-agent - - - - report - prepare-package - - report - - - - jacoco-check - - check - - - - - PACKAGE - - - LINE - COVEREDRATIO - 0 - - - - - - - - - - org.assertj - assertj-assertions-generator-maven-plugin - ${assertj-generator.version} - - - com.baeldung.testing.assertj.custom.Person - - - - - - - - 1.2.5 - 1.1.10 - 0.7.7.201606060606 - 21.0 - 3.1.0 - 3.9.0 - 2.1.0 - 0.32 - 1.1.0 - 0.12 - 1.4.13 - 0.4 - 3.0.0 - 1.5 - 0.15 - - - diff --git a/testing-modules/testng/pom.xml b/testing-modules/testng/pom.xml index 8389604717..5d0c9f126b 100644 --- a/testing-modules/testng/pom.xml +++ b/testing-modules/testng/pom.xml @@ -1,48 +1,48 @@ - - - 4.0.0 - testng - 0.1.0-SNAPSHOT - testng - jar - - - com.baeldung - parent-modules - 1.0.0-SNAPSHOT - ../../ - - - - - - org.testng - testng - ${testng.version} - test - - - - - testng - - - src/main/resources - true - - - - - src/main/resources - true - - - - - - - 6.10 - - + + + 4.0.0 + testng + 0.1.0-SNAPSHOT + testng + jar + + + com.baeldung + parent-modules + 1.0.0-SNAPSHOT + ../../ + + + + + + org.testng + testng + ${testng.version} + test + + + + + testng + + + src/main/resources + true + + + + + src/main/resources + true + + + + + + + 6.10 + + \ No newline at end of file diff --git a/testing-modules/testng/src/test/java/com/baeldung/reports/CustomisedListener.java b/testing-modules/testng/src/test/java/com/baeldung/reports/CustomisedListener.java index 1a0ff190e3..669d6a1a4c 100644 --- a/testing-modules/testng/src/test/java/com/baeldung/reports/CustomisedListener.java +++ b/testing-modules/testng/src/test/java/com/baeldung/reports/CustomisedListener.java @@ -1,67 +1,67 @@ -package com.baeldung.reports; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.testng.ITestContext; -import org.testng.ITestListener; -import org.testng.ITestResult; - -public class CustomisedListener implements ITestListener { - private static final Logger LOGGER = LoggerFactory.getLogger("CUSTOM_LOGS"); - - @Override - public void onFinish(ITestContext context) { - LOGGER.info("PASSED TEST CASES"); - context.getPassedTests() - .getAllResults() - .forEach(result -> { - LOGGER.info(result.getName()); - }); - LOGGER.info("FAILED TEST CASES"); - context.getFailedTests() - .getAllResults() - .forEach(result -> { - LOGGER.info(result.getName()); - }); - LOGGER.info("Test completed on: " + context.getEndDate() - .toString()); - } - - @Override - public void onStart(ITestContext arg0) { - LOGGER.info("Started testing on: " + arg0.getStartDate() - .toString()); - } - - @Override - public void onTestFailedButWithinSuccessPercentage(ITestResult arg0) { - // TODO Auto-generated method stub - - } - - @Override - public void onTestFailure(ITestResult arg0) { - LOGGER.info("Failed : " + arg0.getName()); - - } - - @Override - public void onTestSkipped(ITestResult arg0) { - LOGGER.info("Skipped Test: " + arg0.getName()); - - } - - @Override - public void onTestStart(ITestResult arg0) { - LOGGER.info("Testing: " + arg0.getName()); - - } - - @Override - public void onTestSuccess(ITestResult arg0) { - long timeTaken = ((arg0.getEndMillis() - arg0.getStartMillis())); - LOGGER.info("Tested: " + arg0.getName() + " Time taken:" + timeTaken + " ms"); - - } - -} +package com.baeldung.reports; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.testng.ITestContext; +import org.testng.ITestListener; +import org.testng.ITestResult; + +public class CustomisedListener implements ITestListener { + private static final Logger LOGGER = LoggerFactory.getLogger("CUSTOM_LOGS"); + + @Override + public void onFinish(ITestContext context) { + LOGGER.info("PASSED TEST CASES"); + context.getPassedTests() + .getAllResults() + .forEach(result -> { + LOGGER.info(result.getName()); + }); + LOGGER.info("FAILED TEST CASES"); + context.getFailedTests() + .getAllResults() + .forEach(result -> { + LOGGER.info(result.getName()); + }); + LOGGER.info("Test completed on: " + context.getEndDate() + .toString()); + } + + @Override + public void onStart(ITestContext arg0) { + LOGGER.info("Started testing on: " + arg0.getStartDate() + .toString()); + } + + @Override + public void onTestFailedButWithinSuccessPercentage(ITestResult arg0) { + // TODO Auto-generated method stub + + } + + @Override + public void onTestFailure(ITestResult arg0) { + LOGGER.info("Failed : " + arg0.getName()); + + } + + @Override + public void onTestSkipped(ITestResult arg0) { + LOGGER.info("Skipped Test: " + arg0.getName()); + + } + + @Override + public void onTestStart(ITestResult arg0) { + LOGGER.info("Testing: " + arg0.getName()); + + } + + @Override + public void onTestSuccess(ITestResult arg0) { + long timeTaken = ((arg0.getEndMillis() - arg0.getStartMillis())); + LOGGER.info("Tested: " + arg0.getName() + " Time taken:" + timeTaken + " ms"); + + } + +} diff --git a/testing-modules/testng/src/test/java/com/baeldung/reports/CustomisedReports.java b/testing-modules/testng/src/test/java/com/baeldung/reports/CustomisedReports.java index 2c57993e0b..6d53043918 100644 --- a/testing-modules/testng/src/test/java/com/baeldung/reports/CustomisedReports.java +++ b/testing-modules/testng/src/test/java/com/baeldung/reports/CustomisedReports.java @@ -1,111 +1,111 @@ -package com.baeldung.reports; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.testng.*; -import org.testng.xml.XmlSuite; - -import java.io.*; -import java.nio.file.Files; -import java.nio.file.Paths; -import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.function.Function; -import java.util.stream.Collectors; -import java.util.stream.Stream; - -import static java.util.stream.Collectors.toList; - -public class CustomisedReports implements IReporter { - private static final Logger LOGGER = LoggerFactory.getLogger(CustomisedReports.class); - - private static final String ROW_TEMPLATE = "%s%s%s%s%s"; - - public void generateReport(List xmlSuites, List suites, String outputDirectory) { - String reportTemplate = initReportTemplate(); - - final String body = suites - .stream() - .flatMap(suiteToResults()) - .collect(Collectors.joining()); - - saveReportTemplate(outputDirectory, reportTemplate.replaceFirst("", String.format("%s", body))); - } - - private Function> suiteToResults() { - return suite -> suite.getResults().entrySet() - .stream() - .flatMap(resultsToRows(suite)); - } - - private Function, Stream> resultsToRows(ISuite suite) { - return e -> { - ITestContext testContext = e.getValue().getTestContext(); - - Set failedTests = testContext - .getFailedTests() - .getAllResults(); - Set passedTests = testContext - .getPassedTests() - .getAllResults(); - Set skippedTests = testContext - .getSkippedTests() - .getAllResults(); - - String suiteName = suite.getName(); - - return Stream - .of(failedTests, passedTests, skippedTests) - .flatMap(results -> generateReportRows(e.getKey(), suiteName, results).stream()); - }; - } - - private List generateReportRows(String testName, String suiteName, Set allTestResults) { - return allTestResults.stream() - .map(testResultToResultRow(testName, suiteName)) - .collect(toList()); - } - - private Function testResultToResultRow(String testName, String suiteName) { - return testResult -> { - switch (testResult.getStatus()) { - case ITestResult.FAILURE: - return String.format(ROW_TEMPLATE, "danger", suiteName, testName, testResult.getName(), "FAILED", "NA"); - - case ITestResult.SUCCESS: - return String.format(ROW_TEMPLATE, "success", suiteName, testName, testResult.getName(), "PASSED", String.valueOf(testResult.getEndMillis() - testResult.getStartMillis())); - - case ITestResult.SKIP: - return String.format(ROW_TEMPLATE, "warning", suiteName, testName, testResult.getName(), "SKIPPED", "NA"); - - default: - return ""; - } - }; - } - - private String initReportTemplate() { - String template = null; - byte[] reportTemplate; - try { - reportTemplate = Files.readAllBytes(Paths.get("src/test/resources/reportTemplate.html")); - template = new String(reportTemplate, "UTF-8"); - } catch (IOException e) { - LOGGER.error("Problem initializing template", e); - } - return template; - } - - private void saveReportTemplate(String outputDirectory, String reportTemplate) { - new File(outputDirectory).mkdirs(); - try { - PrintWriter reportWriter = new PrintWriter(new BufferedWriter(new FileWriter(new File(outputDirectory, "my-report.html")))); - reportWriter.println(reportTemplate); - reportWriter.flush(); - reportWriter.close(); - } catch (IOException e) { - LOGGER.error("Problem saving template", e); - } - } -} +package com.baeldung.reports; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.testng.*; +import org.testng.xml.XmlSuite; + +import java.io.*; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.function.Function; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import static java.util.stream.Collectors.toList; + +public class CustomisedReports implements IReporter { + private static final Logger LOGGER = LoggerFactory.getLogger(CustomisedReports.class); + + private static final String ROW_TEMPLATE = "%s%s%s%s%s"; + + public void generateReport(List xmlSuites, List suites, String outputDirectory) { + String reportTemplate = initReportTemplate(); + + final String body = suites + .stream() + .flatMap(suiteToResults()) + .collect(Collectors.joining()); + + saveReportTemplate(outputDirectory, reportTemplate.replaceFirst("", String.format("%s", body))); + } + + private Function> suiteToResults() { + return suite -> suite.getResults().entrySet() + .stream() + .flatMap(resultsToRows(suite)); + } + + private Function, Stream> resultsToRows(ISuite suite) { + return e -> { + ITestContext testContext = e.getValue().getTestContext(); + + Set failedTests = testContext + .getFailedTests() + .getAllResults(); + Set passedTests = testContext + .getPassedTests() + .getAllResults(); + Set skippedTests = testContext + .getSkippedTests() + .getAllResults(); + + String suiteName = suite.getName(); + + return Stream + .of(failedTests, passedTests, skippedTests) + .flatMap(results -> generateReportRows(e.getKey(), suiteName, results).stream()); + }; + } + + private List generateReportRows(String testName, String suiteName, Set allTestResults) { + return allTestResults.stream() + .map(testResultToResultRow(testName, suiteName)) + .collect(toList()); + } + + private Function testResultToResultRow(String testName, String suiteName) { + return testResult -> { + switch (testResult.getStatus()) { + case ITestResult.FAILURE: + return String.format(ROW_TEMPLATE, "danger", suiteName, testName, testResult.getName(), "FAILED", "NA"); + + case ITestResult.SUCCESS: + return String.format(ROW_TEMPLATE, "success", suiteName, testName, testResult.getName(), "PASSED", String.valueOf(testResult.getEndMillis() - testResult.getStartMillis())); + + case ITestResult.SKIP: + return String.format(ROW_TEMPLATE, "warning", suiteName, testName, testResult.getName(), "SKIPPED", "NA"); + + default: + return ""; + } + }; + } + + private String initReportTemplate() { + String template = null; + byte[] reportTemplate; + try { + reportTemplate = Files.readAllBytes(Paths.get("src/test/resources/reportTemplate.html")); + template = new String(reportTemplate, "UTF-8"); + } catch (IOException e) { + LOGGER.error("Problem initializing template", e); + } + return template; + } + + private void saveReportTemplate(String outputDirectory, String reportTemplate) { + new File(outputDirectory).mkdirs(); + try { + PrintWriter reportWriter = new PrintWriter(new BufferedWriter(new FileWriter(new File(outputDirectory, "my-report.html")))); + reportWriter.println(reportTemplate); + reportWriter.flush(); + reportWriter.close(); + } catch (IOException e) { + LOGGER.error("Problem saving template", e); + } + } +} diff --git a/testing-modules/testng/src/test/resources/logback.xml b/testing-modules/testng/src/test/resources/logback.xml index 035520aa15..ec0dc2469a 100644 --- a/testing-modules/testng/src/test/resources/logback.xml +++ b/testing-modules/testng/src/test/resources/logback.xml @@ -1,19 +1,19 @@ - - - - - web - %date [%thread] %-5level %logger{36} - %message%n - - - - - - - - - - - - - + + + + + web - %date [%thread] %-5level %logger{36} - %message%n + + + + + + + + + + + + + \ No newline at end of file diff --git a/testing-modules/testng/src/test/resources/parametrized_testng.xml b/testing-modules/testng/src/test/resources/parametrized_testng.xml index d3a9a6dc51..1a9266dd59 100644 --- a/testing-modules/testng/src/test/resources/parametrized_testng.xml +++ b/testing-modules/testng/src/test/resources/parametrized_testng.xml @@ -1,13 +1,13 @@ - - - - - - - - - - - - + + + + + + + + + + + + \ No newline at end of file diff --git a/testing-modules/testng/src/test/resources/test_group.xml b/testing-modules/testng/src/test/resources/test_group.xml index 3f51c039d6..34a825855c 100644 --- a/testing-modules/testng/src/test/resources/test_group.xml +++ b/testing-modules/testng/src/test/resources/test_group.xml @@ -1,13 +1,13 @@ - - - - - - - - - - - - + + + + + + + + + + + + \ No newline at end of file diff --git a/testing-modules/testng/src/test/resources/test_setup.xml b/testing-modules/testng/src/test/resources/test_setup.xml index dea9d9bf5a..8e91349b0a 100644 --- a/testing-modules/testng/src/test/resources/test_setup.xml +++ b/testing-modules/testng/src/test/resources/test_setup.xml @@ -1,17 +1,17 @@ - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/testing-modules/testng/src/test/resources/test_suite.xml b/testing-modules/testng/src/test/resources/test_suite.xml index 7a01f1af08..8b66799c4e 100644 --- a/testing-modules/testng/src/test/resources/test_suite.xml +++ b/testing-modules/testng/src/test/resources/test_suite.xml @@ -1,13 +1,13 @@ - - - - - - - - - - - - + + + + + + + + + + + + \ No newline at end of file