diff --git a/.gitignore b/.gitignore index 08f570ad06..693a35c176 100644 --- a/.gitignore +++ b/.gitignore @@ -43,3 +43,4 @@ spring-call-getters-using-reflection/.mvn/wrapper/maven-wrapper.properties spring-check-if-a-property-is-null/.mvn/wrapper/maven-wrapper.properties *.springBeans +20171220-JMeter.csv diff --git a/algorithms/src/main/java/com/baeldung/algorithms/sudoku/BacktrackingAlgorithm.java b/algorithms/src/main/java/com/baeldung/algorithms/sudoku/BacktrackingAlgorithm.java index ff426cbe68..4b37558aab 100644 --- a/algorithms/src/main/java/com/baeldung/algorithms/sudoku/BacktrackingAlgorithm.java +++ b/algorithms/src/main/java/com/baeldung/algorithms/sudoku/BacktrackingAlgorithm.java @@ -40,15 +40,15 @@ public class BacktrackingAlgorithm { } private boolean solve(int[][] board) { - for (int r = BOARD_START_INDEX; r < BOARD_SIZE; r++) { - for (int c = BOARD_START_INDEX; c < BOARD_SIZE; c++) { - if (board[r][c] == NO_VALUE) { + for (int row = BOARD_START_INDEX; row < BOARD_SIZE; row++) { + for (int column = BOARD_START_INDEX; column < BOARD_SIZE; column++) { + if (board[row][column] == NO_VALUE) { for (int k = MIN_VALUE; k <= MAX_VALUE; k++) { - board[r][c] = k; - if (isValid(board, r, c) && solve(board)) { + board[row][column] = k; + if (isValid(board, row, column) && solve(board)) { return true; } - board[r][c] = NO_VALUE; + board[row][column] = NO_VALUE; } return false; } @@ -57,44 +57,44 @@ public class BacktrackingAlgorithm { return true; } - private boolean isValid(int[][] board, int r, int c) { - return rowConstraint(board, r) && - columnConstraint(board, c) && - subsectionConstraint(board, r, c); + private boolean isValid(int[][] board, int row, int column) { + return rowConstraint(board, row) && + columnConstraint(board, column) && + subsectionConstraint(board, row, column); } - private boolean subsectionConstraint(int[][] board, int r, int c) { + private boolean subsectionConstraint(int[][] board, int row, int column) { boolean[] constraint = new boolean[BOARD_SIZE]; - int subsectionRowStart = (r / SUBSECTION_SIZE) * SUBSECTION_SIZE; + int subsectionRowStart = (row / SUBSECTION_SIZE) * SUBSECTION_SIZE; int subsectionRowEnd = subsectionRowStart + SUBSECTION_SIZE; - int subsectionColumnStart = (c / SUBSECTION_SIZE) * SUBSECTION_SIZE; + int subsectionColumnStart = (column / SUBSECTION_SIZE) * SUBSECTION_SIZE; int subsectionColumnEnd = subsectionColumnStart + SUBSECTION_SIZE; - for (int i = subsectionRowStart; i < subsectionRowEnd; i++) { - for (int j = subsectionColumnStart; j < subsectionColumnEnd; j++) { - if (!checkConstraint(board, i, constraint, j)) return false; + for (int r = subsectionRowStart; r < subsectionRowEnd; r++) { + for (int c = subsectionColumnStart; c < subsectionColumnEnd; c++) { + if (!checkConstraint(board, r, constraint, c)) return false; } } return true; } - private boolean columnConstraint(int[][] board, int c) { + private boolean columnConstraint(int[][] board, int column) { boolean[] constraint = new boolean[BOARD_SIZE]; return IntStream.range(BOARD_START_INDEX, BOARD_SIZE) - .allMatch(i -> checkConstraint(board, i, constraint, c)); + .allMatch(row -> checkConstraint(board, row, constraint, column)); } - private boolean rowConstraint(int[][] board, int r) { + private boolean rowConstraint(int[][] board, int row) { boolean[] constraint = new boolean[BOARD_SIZE]; return IntStream.range(BOARD_START_INDEX, BOARD_SIZE) - .allMatch(i -> checkConstraint(board, r, constraint, i)); + .allMatch(column -> checkConstraint(board, row, constraint, column)); } - private boolean checkConstraint(int[][] board, int r, boolean[] constraint, int c) { - if (board[r][c] != NO_VALUE) { - if (!constraint[board[r][c] - 1]) { - constraint[board[r][c] - 1] = true; + private boolean checkConstraint(int[][] board, int row, boolean[] constraint, int column) { + if (board[row][column] != NO_VALUE) { + if (!constraint[board[row][column] - 1]) { + constraint[board[row][column] - 1] = true; } else { return false; } diff --git a/algorithms/src/main/java/com/baeldung/algorithms/sudoku/DancingLinksAlgorithm.java b/algorithms/src/main/java/com/baeldung/algorithms/sudoku/DancingLinksAlgorithm.java index 76b686afa6..df02ff3d11 100644 --- a/algorithms/src/main/java/com/baeldung/algorithms/sudoku/DancingLinksAlgorithm.java +++ b/algorithms/src/main/java/com/baeldung/algorithms/sudoku/DancingLinksAlgorithm.java @@ -34,8 +34,8 @@ public class DancingLinksAlgorithm { dlx.runSolver(); } - private int getIndex(int row, int col, int num) { - return (row - 1) * BOARD_SIZE * BOARD_SIZE + (col - 1) * BOARD_SIZE + (num - 1); + private int getIndex(int row, int column, int num) { + return (row - 1) * BOARD_SIZE * BOARD_SIZE + (column - 1) * BOARD_SIZE + (num - 1); } private boolean[][] createExactCoverBoard() { @@ -51,12 +51,12 @@ public class DancingLinksAlgorithm { } private int checkSubsectionConstraint(boolean[][] coverBoard, int hBase) { - for (int br = COVER_START_INDEX; br <= BOARD_SIZE; br += SUBSECTION_SIZE) { - for (int bc = COVER_START_INDEX; bc <= BOARD_SIZE; bc += SUBSECTION_SIZE) { + for (int row = COVER_START_INDEX; row <= BOARD_SIZE; row += SUBSECTION_SIZE) { + for (int column = COVER_START_INDEX; column <= BOARD_SIZE; column += SUBSECTION_SIZE) { for (int n = COVER_START_INDEX; n <= BOARD_SIZE; n++, hBase++) { - for (int rDelta = 0; rDelta < SUBSECTION_SIZE; rDelta++) { - for (int cDelta = 0; cDelta < SUBSECTION_SIZE; cDelta++) { - int index = getIndex(br + rDelta, bc + cDelta, n); + for (int rowDelta = 0; rowDelta < SUBSECTION_SIZE; rowDelta++) { + for (int columnDelta = 0; columnDelta < SUBSECTION_SIZE; columnDelta++) { + int index = getIndex(row + rowDelta, column + columnDelta, n); coverBoard[index][hBase] = true; } } @@ -67,10 +67,10 @@ public class DancingLinksAlgorithm { } private int checkColumnConstraint(boolean[][] coverBoard, int hBase) { - for (int c = COVER_START_INDEX; c <= BOARD_SIZE; c++) { + for (int column = COVER_START_INDEX; column <= BOARD_SIZE; column++) { for (int n = COVER_START_INDEX; n <= BOARD_SIZE; n++, hBase++) { - for (int r1 = COVER_START_INDEX; r1 <= BOARD_SIZE; r1++) { - int index = getIndex(r1, c, n); + for (int row = COVER_START_INDEX; row <= BOARD_SIZE; row++) { + int index = getIndex(row, column, n); coverBoard[index][hBase] = true; } } @@ -79,10 +79,10 @@ public class DancingLinksAlgorithm { } private int checkRowConstraint(boolean[][] coverBoard, int hBase) { - for (int r = COVER_START_INDEX; r <= BOARD_SIZE; r++) { + for (int row = COVER_START_INDEX; row <= BOARD_SIZE; row++) { for (int n = COVER_START_INDEX; n <= BOARD_SIZE; n++, hBase++) { - for (int c1 = COVER_START_INDEX; c1 <= BOARD_SIZE; c1++) { - int index = getIndex(r, c1, n); + for (int column = COVER_START_INDEX; column <= BOARD_SIZE; column++) { + int index = getIndex(row, column, n); coverBoard[index][hBase] = true; } } @@ -91,10 +91,10 @@ public class DancingLinksAlgorithm { } private int checkCellConstraint(boolean[][] coverBoard, int hBase) { - for (int r = COVER_START_INDEX; r <= BOARD_SIZE; r++) { - for (int c = COVER_START_INDEX; c <= BOARD_SIZE; c++, hBase++) { + for (int row = COVER_START_INDEX; row <= BOARD_SIZE; row++) { + for (int column = COVER_START_INDEX; column <= BOARD_SIZE; column++, hBase++) { for (int n = COVER_START_INDEX; n <= BOARD_SIZE; n++) { - int index = getIndex(r, c, n); + int index = getIndex(row, column, n); coverBoard[index][hBase] = true; } } @@ -104,13 +104,13 @@ public class DancingLinksAlgorithm { private boolean[][] initializeExactCoverBoard(int[][] board) { boolean[][] coverBoard = createExactCoverBoard(); - for (int i = COVER_START_INDEX; i <= BOARD_SIZE; i++) { - for (int j = COVER_START_INDEX; j <= BOARD_SIZE; j++) { - int n = board[i - 1][j - 1]; + for (int row = COVER_START_INDEX; row <= BOARD_SIZE; row++) { + for (int column = COVER_START_INDEX; column <= BOARD_SIZE; column++) { + int n = board[row - 1][column - 1]; if (n != NO_VALUE) { for (int num = MIN_VALUE; num <= MAX_VALUE; num++) { if (num != n) { - Arrays.fill(coverBoard[getIndex(i, j, num)], false); + Arrays.fill(coverBoard[getIndex(row, column, num)], false); } } } diff --git a/algorithms/src/main/java/com/baeldung/algorithms/sudoku/DancingNode.java b/algorithms/src/main/java/com/baeldung/algorithms/sudoku/DancingNode.java index b494eba9ef..2422ff0dff 100644 --- a/algorithms/src/main/java/com/baeldung/algorithms/sudoku/DancingNode.java +++ b/algorithms/src/main/java/com/baeldung/algorithms/sudoku/DancingNode.java @@ -4,21 +4,21 @@ class DancingNode { DancingNode L, R, U, D; ColumnNode C; - DancingNode hookDown(DancingNode n1) { - assert (this.C == n1.C); - n1.D = this.D; - n1.D.U = n1; - n1.U = this; - this.D = n1; - return n1; + DancingNode hookDown(DancingNode node) { + assert (this.C == node.C); + node.D = this.D; + node.D.U = node; + node.U = this; + this.D = node; + return node; } - DancingNode hookRight(DancingNode n1) { - n1.R = this.R; - n1.R.L = n1; - n1.L = this; - this.R = n1; - return n1; + DancingNode hookRight(DancingNode node) { + node.R = this.R; + node.R.L = node; + node.L = this; + this.R = node; + return node; } void unlinkLR() { diff --git a/core-java-9/src/test/java/com/baeldung/java9/httpclient/SimpleHttpRequestsUnitTest.java b/core-java-9/src/test/java/com/baeldung/java9/httpclient/SimpleHttpRequestsUnitTest.java deleted file mode 100644 index 4a704139a1..0000000000 --- a/core-java-9/src/test/java/com/baeldung/java9/httpclient/SimpleHttpRequestsUnitTest.java +++ /dev/null @@ -1,126 +0,0 @@ -package com.baeldung.java9.httpclient; - -import static java.net.HttpURLConnection.HTTP_OK; -import static org.junit.Assert.assertTrue; - -import java.io.IOException; -import java.net.CookieManager; -import java.net.CookiePolicy; -import java.net.URI; -import java.net.URISyntaxException; -import java.net.http.HttpClient; -import java.net.http.HttpHeaders; -import java.net.http.HttpRequest; -import java.net.http.HttpResponse; -import java.security.NoSuchAlgorithmException; -import java.util.List; -import java.util.Map; -import java.util.concurrent.CompletableFuture; -import java.util.concurrent.ExecutionException; - -import javax.net.ssl.SSLContext; -import javax.net.ssl.SSLParameters; - -import org.junit.Before; -import org.junit.Test; - -public class SimpleHttpRequestsUnitTest { - - private URI httpURI; - - @Before - public void init() throws URISyntaxException { - httpURI = new URI("http://www.baeldung.com/"); - } - - @Test - public void quickGet() throws IOException, InterruptedException, URISyntaxException { - HttpRequest request = HttpRequest.create(httpURI).GET(); - HttpResponse response = request.response(); - int responseStatusCode = response.statusCode(); - String responseBody = response.body(HttpResponse.asString()); - assertTrue("Get response status code is bigger then 400", responseStatusCode < 400); - } - - @Test - public void asynchronousGet() throws URISyntaxException, IOException, InterruptedException, ExecutionException { - HttpRequest request = HttpRequest.create(httpURI).GET(); - long before = System.currentTimeMillis(); - CompletableFuture futureResponse = request.responseAsync(); - futureResponse.thenAccept(response -> { - String responseBody = response.body(HttpResponse.asString()); - }); - HttpResponse resp = futureResponse.get(); - HttpHeaders hs = resp.headers(); - assertTrue("There should be more then 1 header.", hs.map().size() > 1); - } - - @Test - public void postMehtod() throws URISyntaxException, IOException, InterruptedException { - HttpRequest.Builder requestBuilder = HttpRequest.create(httpURI); - requestBuilder.body(HttpRequest.fromString("param1=foo,param2=bar")).followRedirects(HttpClient.Redirect.SECURE); - HttpRequest request = requestBuilder.POST(); - HttpResponse response = request.response(); - int statusCode = response.statusCode(); - assertTrue("HTTP return code", statusCode == HTTP_OK); - } - - @Test - public void configureHttpClient() throws NoSuchAlgorithmException, URISyntaxException, IOException, InterruptedException { - CookieManager cManager = new CookieManager(); - cManager.setCookiePolicy(CookiePolicy.ACCEPT_ORIGINAL_SERVER); - - SSLParameters sslParam = new SSLParameters(new String[] { "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256" }, new String[] { "TLSv1.2" }); - - HttpClient.Builder hcBuilder = HttpClient.create(); - hcBuilder.cookieManager(cManager).sslContext(SSLContext.getDefault()).sslParameters(sslParam); - HttpClient httpClient = hcBuilder.build(); - HttpRequest.Builder reqBuilder = httpClient.request(new URI("https://www.facebook.com")); - - HttpRequest request = reqBuilder.followRedirects(HttpClient.Redirect.ALWAYS).GET(); - HttpResponse response = request.response(); - int statusCode = response.statusCode(); - assertTrue("HTTP return code", statusCode == HTTP_OK); - } - - SSLParameters getDefaultSSLParameters() throws NoSuchAlgorithmException { - SSLParameters sslP1 = SSLContext.getDefault().getSupportedSSLParameters(); - String[] proto = sslP1.getApplicationProtocols(); - String[] cifers = sslP1.getCipherSuites(); - System.out.println(printStringArr(proto)); - System.out.println(printStringArr(cifers)); - return sslP1; - } - - String printStringArr(String... args) { - if (args == null) { - return null; - } - StringBuilder sb = new StringBuilder(); - for (String s : args) { - sb.append(s); - sb.append("\n"); - } - return sb.toString(); - } - - String printHeaders(HttpHeaders h) { - if (h == null) { - return null; - } - - StringBuilder sb = new StringBuilder(); - Map> hMap = h.map(); - for (String k : hMap.keySet()) { - sb.append(k).append(":"); - List l = hMap.get(k); - if (l != null) { - l.forEach(s -> { - sb.append(s).append(","); - }); - } - sb.append("\n"); - } - return sb.toString(); - } -} diff --git a/core-java-concurrency/src/main/java/com/baeldung/concurrent/prioritytaskexecution/Job.java b/core-java-concurrency/src/main/java/com/baeldung/concurrent/prioritytaskexecution/Job.java index a70041ed7d..9900d1c63d 100644 --- a/core-java-concurrency/src/main/java/com/baeldung/concurrent/prioritytaskexecution/Job.java +++ b/core-java-concurrency/src/main/java/com/baeldung/concurrent/prioritytaskexecution/Job.java @@ -16,7 +16,8 @@ public class Job implements Runnable { @Override public void run() { try { - System.out.println("Job:" + jobName + " Priority:" + jobPriority); + System.out.println("Job:" + jobName + + " Priority:" + jobPriority); Thread.sleep(1000); } catch (InterruptedException ignored) { } diff --git a/core-java-concurrency/src/main/java/com/baeldung/concurrent/prioritytaskexecution/PriorityJobScheduler.java b/core-java-concurrency/src/main/java/com/baeldung/concurrent/prioritytaskexecution/PriorityJobScheduler.java index 70fd1710c0..ba55696d5a 100644 --- a/core-java-concurrency/src/main/java/com/baeldung/concurrent/prioritytaskexecution/PriorityJobScheduler.java +++ b/core-java-concurrency/src/main/java/com/baeldung/concurrent/prioritytaskexecution/PriorityJobScheduler.java @@ -9,21 +9,21 @@ import java.util.concurrent.TimeUnit; public class PriorityJobScheduler { private ExecutorService priorityJobPoolExecutor; - private ExecutorService priorityJobScheduler; - private PriorityBlockingQueue priorityQueue; + private ExecutorService priorityJobScheduler = + Executors.newSingleThreadExecutor(); + private PriorityBlockingQueue priorityQueue; public PriorityJobScheduler(Integer poolSize, Integer queueSize) { priorityJobPoolExecutor = Executors.newFixedThreadPool(poolSize); - Comparator jobComparator = Comparator.comparing(Job::getJobPriority); - priorityQueue = new PriorityBlockingQueue(queueSize, - (Comparator) jobComparator); + priorityQueue = new PriorityBlockingQueue(queueSize, + Comparator.comparing(Job::getJobPriority)); - priorityJobScheduler = Executors.newSingleThreadExecutor(); priorityJobScheduler.execute(()->{ while (true) { try { priorityJobPoolExecutor.execute(priorityQueue.take()); } catch (InterruptedException e) { + // exception needs special handling break; } } diff --git a/core-java-concurrency/src/main/java/com/baeldung/concurrent/threadlifecycle/BlockedState.java b/core-java-concurrency/src/main/java/com/baeldung/concurrent/threadlifecycle/BlockedState.java new file mode 100644 index 0000000000..19c6d08c2d --- /dev/null +++ b/core-java-concurrency/src/main/java/com/baeldung/concurrent/threadlifecycle/BlockedState.java @@ -0,0 +1,31 @@ +package com.baeldung.concurrent.threadlifecycle; + +public class BlockedState { + public static void main(String[] args) throws InterruptedException { + Thread t1 = new Thread(new DemoThreadB()); + Thread t2 = new Thread(new DemoThreadB()); + + t1.start(); + t2.start(); + + Thread.sleep(1000); + + System.out.println(t2.getState()); + System.exit(0); + } +} + +class DemoThreadB implements Runnable { + @Override + public void run() { + commonResource(); + } + + public static synchronized void commonResource() { + while(true) { + // Infinite loop to mimic heavy processing + // Thread 't1' won't leave this method + // when Thread 't2' enters this + } + } +} \ No newline at end of file diff --git a/core-java-concurrency/src/main/java/com/baeldung/concurrent/threadlifecycle/NewState.java b/core-java-concurrency/src/main/java/com/baeldung/concurrent/threadlifecycle/NewState.java new file mode 100644 index 0000000000..b524cc5ec7 --- /dev/null +++ b/core-java-concurrency/src/main/java/com/baeldung/concurrent/threadlifecycle/NewState.java @@ -0,0 +1,14 @@ +package com.baeldung.concurrent.threadlifecycle; + +public class NewState implements Runnable { + public static void main(String[] args) { + Runnable runnable = new NewState(); + Thread t = new Thread(runnable); + System.out.println(t.getState()); + } + + @Override + public void run() { + + } +} \ No newline at end of file diff --git a/core-java-concurrency/src/main/java/com/baeldung/concurrent/threadlifecycle/RunnableState.java b/core-java-concurrency/src/main/java/com/baeldung/concurrent/threadlifecycle/RunnableState.java new file mode 100644 index 0000000000..4aa8b36a76 --- /dev/null +++ b/core-java-concurrency/src/main/java/com/baeldung/concurrent/threadlifecycle/RunnableState.java @@ -0,0 +1,15 @@ +package com.baeldung.concurrent.threadlifecycle; + +public class RunnableState implements Runnable { + public static void main(String[] args) { + Runnable runnable = new NewState(); + Thread t = new Thread(runnable); + t.start(); + System.out.println(t.getState()); + } + + @Override + public void run() { + + } +} \ No newline at end of file diff --git a/core-java-concurrency/src/main/java/com/baeldung/concurrent/threadlifecycle/TerminatedState.java b/core-java-concurrency/src/main/java/com/baeldung/concurrent/threadlifecycle/TerminatedState.java new file mode 100644 index 0000000000..7c8884dc22 --- /dev/null +++ b/core-java-concurrency/src/main/java/com/baeldung/concurrent/threadlifecycle/TerminatedState.java @@ -0,0 +1,15 @@ +package com.baeldung.concurrent.threadlifecycle; + +public class TerminatedState implements Runnable { + public static void main(String[] args) throws InterruptedException { + Thread t1 = new Thread(new TerminatedState()); + t1.start(); + Thread.sleep(1000); + System.out.println(t1.getState()); + } + + @Override + public void run() { + // No processing in this block + } +} \ No newline at end of file diff --git a/core-java-concurrency/src/main/java/com/baeldung/concurrent/threadlifecycle/TimedWaitingState.java b/core-java-concurrency/src/main/java/com/baeldung/concurrent/threadlifecycle/TimedWaitingState.java new file mode 100644 index 0000000000..8d005352eb --- /dev/null +++ b/core-java-concurrency/src/main/java/com/baeldung/concurrent/threadlifecycle/TimedWaitingState.java @@ -0,0 +1,25 @@ +package com.baeldung.concurrent.threadlifecycle; + +public class TimedWaitingState { + public static void main(String[] args) throws InterruptedException { + DemoThread obj1 = new DemoThread(); + Thread t1 = new Thread(obj1); + t1.start(); + // The following sleep will give enough time for ThreadScheduler + // to start processing of thread t1 + Thread.sleep(1000); + System.out.println(t1.getState()); + } +} + +class DemoThread implements Runnable { + @Override + public void run() { + try { + Thread.sleep(5000); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + e.printStackTrace(); + } + } +} \ No newline at end of file diff --git a/core-java-concurrency/src/main/java/com/baeldung/concurrent/threadlifecycle/WaitingState.java b/core-java-concurrency/src/main/java/com/baeldung/concurrent/threadlifecycle/WaitingState.java new file mode 100644 index 0000000000..98a6844309 --- /dev/null +++ b/core-java-concurrency/src/main/java/com/baeldung/concurrent/threadlifecycle/WaitingState.java @@ -0,0 +1,35 @@ +package com.baeldung.concurrent.threadlifecycle; + +public class WaitingState implements Runnable { + public static Thread t1; + + public static void main(String[] args) { + t1 = new Thread(new WaitingState()); + t1.start(); + } + + public void run() { + Thread t2 = new Thread(new DemoThreadWS()); + t2.start(); + + try { + t2.join(); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + e.printStackTrace(); + } + } +} + +class DemoThreadWS implements Runnable { + public void run() { + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + e.printStackTrace(); + } + + System.out.println(WaitingState.t1.getState()); + } +} \ No newline at end of file diff --git a/core-java-concurrency/src/test/java/com/baeldung/concurrent/prioritytaskexecution/PriorityJobSchedulerUnitTest.java b/core-java-concurrency/src/test/java/com/baeldung/concurrent/prioritytaskexecution/PriorityJobSchedulerUnitTest.java index 902bada3a2..1e67fe45c1 100644 --- a/core-java-concurrency/src/test/java/com/baeldung/concurrent/prioritytaskexecution/PriorityJobSchedulerUnitTest.java +++ b/core-java-concurrency/src/test/java/com/baeldung/concurrent/prioritytaskexecution/PriorityJobSchedulerUnitTest.java @@ -30,7 +30,9 @@ public class PriorityJobSchedulerUnitTest { // delay to avoid job sleep (added for demo) being interrupted try { Thread.sleep(2000); - } catch (InterruptedException ignored) { + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + throw new RuntimeException(e); } pjs.closeScheduler(); diff --git a/core-java/src/main/java/com/baeldung/methodoverloadingoverriding/application/Application.java b/core-java/src/main/java/com/baeldung/methodoverloadingoverriding/application/Application.java new file mode 100644 index 0000000000..6e023e9fa8 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/methodoverloadingoverriding/application/Application.java @@ -0,0 +1,26 @@ +package com.baeldung.methodoverloadingoverriding.application; + +import com.baeldung.methodoverloadingoverriding.model.Car; +import com.baeldung.methodoverloadingoverriding.model.Vehicle; +import com.baeldung.methodoverloadingoverriding.util.Multiplier; + +public class Application { + + public static void main(String[] args) { + Multiplier multiplier = new Multiplier(); + System.out.println(multiplier.multiply(10, 10)); + System.out.println(multiplier.multiply(10, 10, 10)); + System.out.println(multiplier.multiply(10, 10.5)); + System.out.println(multiplier.multiply(10.5, 10.5)); + + Vehicle vehicle = new Vehicle(); + System.out.println(vehicle.accelerate(100)); + System.out.println(vehicle.run()); + System.out.println(vehicle.stop()); + + Vehicle car = new Car(); + System.out.println(car.accelerate(80)); + System.out.println(car.run()); + System.out.println(car.stop()); + } +} diff --git a/core-java/src/main/java/com/baeldung/methodoverloadingoverriding/model/Car.java b/core-java/src/main/java/com/baeldung/methodoverloadingoverriding/model/Car.java new file mode 100644 index 0000000000..aa5bdcaec9 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/methodoverloadingoverriding/model/Car.java @@ -0,0 +1,9 @@ +package com.baeldung.methodoverloadingoverriding.model; + +public class Car extends Vehicle { + + @Override + public String accelerate(long mph) { + return "The car accelerates at : " + mph + " MPH."; + } +} diff --git a/core-java/src/main/java/com/baeldung/methodoverloadingoverriding/model/Vehicle.java b/core-java/src/main/java/com/baeldung/methodoverloadingoverriding/model/Vehicle.java new file mode 100644 index 0000000000..eb59e8ca23 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/methodoverloadingoverriding/model/Vehicle.java @@ -0,0 +1,16 @@ +package com.baeldung.methodoverloadingoverriding.model; + +public class Vehicle { + + public String accelerate(long mph) { + return "The vehicle accelerates at : " + mph + " MPH."; + } + + public String stop() { + return "The vehicle has stopped."; + } + + public String run() { + return "The vehicle is running."; + } +} diff --git a/core-java/src/main/java/com/baeldung/methodoverloadingoverriding/util/Multiplier.java b/core-java/src/main/java/com/baeldung/methodoverloadingoverriding/util/Multiplier.java new file mode 100644 index 0000000000..c43e61c78f --- /dev/null +++ b/core-java/src/main/java/com/baeldung/methodoverloadingoverriding/util/Multiplier.java @@ -0,0 +1,16 @@ +package com.baeldung.methodoverloadingoverriding.util; + +public class Multiplier { + + public int multiply(int a, int b) { + return a * b; + } + + public int multiply(int a, int b, int c) { + return a * b * c; + } + + public double multiply(double a, double b) { + return a * b; + } +} diff --git a/core-java/src/main/java/com/baeldung/string/Palindrome.java b/core-java/src/main/java/com/baeldung/string/Palindrome.java new file mode 100644 index 0000000000..97d4d36d07 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/string/Palindrome.java @@ -0,0 +1,66 @@ +package com.baeldung.string; + +import java.util.stream.IntStream; + +public class Palindrome { + + public boolean isPalindrome(String text) { + String clean = text.replaceAll("\\s+", "").toLowerCase(); + int length = clean.length(); + int forward = 0; + int backward = length - 1; + while (backward > forward) { + char forwardChar = clean.charAt(forward++); + char backwardChar = clean.charAt(backward--); + if (forwardChar != backwardChar) + return false; + } + return true; + } + + public boolean isPalindromeReverseTheString(String text) { + StringBuilder reverse = new StringBuilder(); + String clean = text.replaceAll("\\s+", "").toLowerCase(); + char[] plain = clean.toCharArray(); + for (int i = plain.length - 1; i >= 0; i--) + reverse.append(plain[i]); + return (reverse.toString()).equals(clean); + } + + public boolean isPalindromeUsingStringBuilder(String text) { + String clean = text.replaceAll("\\s+", "").toLowerCase(); + StringBuilder plain = new StringBuilder(clean); + StringBuilder reverse = plain.reverse(); + return (reverse.toString()).equals(clean); + } + + public boolean isPalindromeUsingStringBuffer(String text) { + String clean = text.replaceAll("\\s+", "").toLowerCase(); + StringBuffer plain = new StringBuffer(clean); + StringBuffer reverse = plain.reverse(); + return (reverse.toString()).equals(clean); + } + + public boolean isPalindromeRecursive(String text) { + String clean = text.replaceAll("\\s+", "").toLowerCase(); + return recursivePalindrome(clean, 0, clean.length() - 1); + } + + private boolean recursivePalindrome(String text, int forward, int backward) { + if (forward == backward) + return true; + if ((text.charAt(forward)) != (text.charAt(backward))) + return false; + if (forward < backward + 1) { + return recursivePalindrome(text, forward + 1, backward - 1); + } + + return true; + } + + public boolean isPalindromeUsingIntStream(String text) { + String temp = text.replaceAll("\\s+", "").toLowerCase(); + return IntStream.range(0, temp.length() / 2) + .noneMatch(i -> temp.charAt(i) != temp.charAt(temp.length() - i - 1)); + } +} diff --git a/core-java/src/test/java/com/baeldung/methodoverloadingoverriding/test/MethodOverloadingUnitTest.java b/core-java/src/test/java/com/baeldung/methodoverloadingoverriding/test/MethodOverloadingUnitTest.java new file mode 100644 index 0000000000..081a30c34a --- /dev/null +++ b/core-java/src/test/java/com/baeldung/methodoverloadingoverriding/test/MethodOverloadingUnitTest.java @@ -0,0 +1,41 @@ +package com.baeldung.methodoverloadingoverriding.test; + +import com.baeldung.methodoverloadingoverriding.util.Multiplier; +import org.junit.BeforeClass; +import org.junit.Test; +import static org.assertj.core.api.Assertions.*; + +public class MethodOverloadingUnitTest { + + private static Multiplier multiplier; + + @BeforeClass + public static void setUpMultiplierInstance() { + multiplier = new Multiplier(); + } + + @Test + public void givenMultiplierInstance_whenCalledMultiplyWithTwoIntegers_thenOneAssertion() { + assertThat(multiplier.multiply(10, 10)).isEqualTo(100); + } + + @Test + public void givenMultiplierInstance_whenCalledMultiplyWithTreeIntegers_thenOneAssertion() { + assertThat(multiplier.multiply(10, 10, 10)).isEqualTo(1000); + } + + @Test + public void givenMultiplierInstance_whenCalledMultiplyWithIntDouble_thenOneAssertion() { + assertThat(multiplier.multiply(10, 10.5)).isEqualTo(105.0); + } + + @Test + public void givenMultiplierInstance_whenCalledMultiplyWithDoubleDouble_thenOneAssertion() { + assertThat(multiplier.multiply(10.5, 10.5)).isEqualTo(110.25); + } + + @Test + public void givenMultiplierInstance_whenCalledMultiplyWithIntIntAndMatching_thenNoTypePromotion() { + assertThat(multiplier.multiply(10, 10)).isEqualTo(100); + } +} diff --git a/core-java/src/test/java/com/baeldung/methodoverloadingoverriding/test/MethodOverridingUnitTest.java b/core-java/src/test/java/com/baeldung/methodoverloadingoverriding/test/MethodOverridingUnitTest.java new file mode 100644 index 0000000000..554ac121bc --- /dev/null +++ b/core-java/src/test/java/com/baeldung/methodoverloadingoverriding/test/MethodOverridingUnitTest.java @@ -0,0 +1,68 @@ +package com.baeldung.methodoverloadingoverriding.test; + +import com.baeldung.methodoverloadingoverriding.model.Car; +import com.baeldung.methodoverloadingoverriding.model.Vehicle; +import org.junit.BeforeClass; +import org.junit.Test; +import static org.assertj.core.api.Assertions.*; + +public class MethodOverridingUnitTest { + + private static Vehicle vehicle; + private static Car car; + + @BeforeClass + public static void setUpVehicleInstance() { + vehicle = new Vehicle(); + } + + @BeforeClass + public static void setUpCarInstance() { + car = new Car(); + } + + @Test + public void givenVehicleInstance_whenCalledAccelerate_thenOneAssertion() { + assertThat(vehicle.accelerate(100)).isEqualTo("The vehicle accelerates at : 100 MPH."); + } + + @Test + public void givenVehicleInstance_whenCalledRun_thenOneAssertion() { + assertThat(vehicle.run()).isEqualTo("The vehicle is running."); + } + + @Test + public void givenVehicleInstance_whenCalledStop_thenOneAssertion() { + assertThat(vehicle.stop()).isEqualTo("The vehicle has stopped."); + } + + @Test + public void givenCarInstance_whenCalledAccelerate_thenOneAssertion() { + assertThat(car.accelerate(80)).isEqualTo("The car accelerates at : 80 MPH."); + } + + @Test + public void givenCarInstance_whenCalledRun_thenOneAssertion() { + assertThat(car.run()).isEqualTo("The vehicle is running."); + } + + @Test + public void givenCarInstance_whenCalledStop_thenOneAssertion() { + assertThat(car.stop()).isEqualTo("The vehicle has stopped."); + } + + @Test + public void givenVehicleCarInstances_whenCalledAccelerateWithSameArgument_thenNotEqual() { + assertThat(vehicle.accelerate(100)).isNotEqualTo(car.accelerate(100)); + } + + @Test + public void givenVehicleCarInstances_whenCalledRun_thenEqual() { + assertThat(vehicle.run()).isEqualTo(car.run()); + } + + @Test + public void givenVehicleCarInstances_whenCalledStop_thenEqual() { + assertThat(vehicle.stop()).isEqualTo(car.stop()); + } +} diff --git a/core-java/src/test/java/com/baeldung/string/PalindromeTest.java b/core-java/src/test/java/com/baeldung/string/PalindromeTest.java new file mode 100644 index 0000000000..bc6fee2cd9 --- /dev/null +++ b/core-java/src/test/java/com/baeldung/string/PalindromeTest.java @@ -0,0 +1,97 @@ +package com.baeldung.string; + +import static org.junit.Assert.*; +import org.junit.Test; + +public class PalindromeTest { + + private String[] words = { + "Anna", + "Civic", + "Kayak", + "Level", + "Madam", + }; + + private String[] sentences = { + "Sore was I ere I saw Eros", + "Euston saw I was not Sue", + "Too hot to hoot", + "No mists or frost Simon", + "Stella won no wallets" + }; + + private Palindrome palindrome = new Palindrome(); + + @Test + public void whenWord_shouldBePalindrome() { + for (String word : words) + assertTrue(palindrome.isPalindrome(word)); + } + + @Test + public void whenSentence_shouldBePalindrome() { + for (String sentence : sentences) + assertTrue(palindrome.isPalindrome(sentence)); + } + + @Test + public void whenReverseWord_shouldBePalindrome() { + for (String word : words) + assertTrue(palindrome.isPalindromeReverseTheString(word)); + } + + @Test + public void whenReverseSentence_shouldBePalindrome() { + for (String sentence : sentences) + assertTrue(palindrome.isPalindromeReverseTheString(sentence)); + } + + @Test + public void whenStringBuilderWord_shouldBePalindrome() { + for (String word : words) + assertTrue(palindrome.isPalindromeUsingStringBuilder(word)); + } + + @Test + public void whenStringBuilderSentence_shouldBePalindrome() { + for (String sentence : sentences) + assertTrue(palindrome.isPalindromeUsingStringBuilder(sentence)); + } + + @Test + public void whenStringBufferWord_shouldBePalindrome() { + for (String word : words) + assertTrue(palindrome.isPalindromeUsingStringBuffer(word)); + } + + @Test + public void whenStringBufferSentence_shouldBePalindrome() { + for (String sentence : sentences) + assertTrue(palindrome.isPalindromeUsingStringBuffer(sentence)); + } + + @Test + public void whenPalindromeRecursive_wordShouldBePalindrome() { + for (String word : words) + assertTrue(palindrome.isPalindromeRecursive(word)); + } + + @Test + public void whenPalindromeRecursive_sentenceShouldBePalindrome() { + for (String sentence : sentences) + assertTrue(palindrome.isPalindromeRecursive(sentence)); + } + + @Test + public void whenPalindromeStreams_wordShouldBePalindrome() { + for (String word : words) + assertTrue(palindrome.isPalindromeUsingIntStream(word)); + } + + @Test + public void whenPalindromeStreams_sentenceShouldBePalindrome() { + for (String sentence : sentences) + assertTrue(palindrome.isPalindromeUsingIntStream(sentence)); + } +} diff --git a/core-kotlin/src/test/kotlin/com/baeldung/kotlin/InfixFunctionsTest.kt b/core-kotlin/src/test/kotlin/com/baeldung/kotlin/InfixFunctionsTest.kt new file mode 100644 index 0000000000..fc4286460a --- /dev/null +++ b/core-kotlin/src/test/kotlin/com/baeldung/kotlin/InfixFunctionsTest.kt @@ -0,0 +1,51 @@ +package com.baeldung.kotlin + +import org.junit.Assert +import org.junit.Test + +class InfixFunctionsTest { + @Test + fun testColours() { + val color = 0x123456 + val red = (color and 0xff0000) shr 16 + val green = (color and 0x00ff00) shr 8 + val blue = (color and 0x0000ff) shr 0 + + Assert.assertEquals(0x12, red) + Assert.assertEquals(0x34, green) + Assert.assertEquals(0x56, blue) + } + + @Test + fun testNewAssertions() { + class Assertion(private val target: T) { + infix fun isEqualTo(other: T) { + Assert.assertEquals(other, target) + } + + infix fun isDifferentFrom(other: T) { + Assert.assertNotEquals(other, target) + } + } + + val result = Assertion(5) + + result isEqualTo 5 + + // The following two lines are expected to fail + // result isEqualTo 6 + // result isDifferentFrom 5 + } + + @Test + fun testNewStringMethod() { + infix fun String.substringMatches(r: Regex) : List { + return r.findAll(this) + .map { it.value } + .toList() + } + + val matches = "a bc def" substringMatches ".*? ".toRegex() + Assert.assertEquals(listOf("a ", "bc "), matches) + } +} diff --git a/core-kotlin/src/test/kotlin/com/baeldung/kotlin/stdlib/RegexTest.kt b/core-kotlin/src/test/kotlin/com/baeldung/kotlin/stdlib/RegexTest.kt new file mode 100644 index 0000000000..eeb587ee22 --- /dev/null +++ b/core-kotlin/src/test/kotlin/com/baeldung/kotlin/stdlib/RegexTest.kt @@ -0,0 +1,128 @@ +package com.baeldung.kotlin.stdlib + +import org.junit.Test +import java.beans.ExceptionListener +import java.beans.XMLEncoder +import java.io.* +import java.lang.Exception +import kotlin.test.* +import kotlin.text.RegexOption.* + +class RegexTest { + + @Test + fun whenRegexIsInstantiated_thenIsEqualToToRegexMethod() { + val pattern = """a([bc]+)d?\\""" + + assertEquals(Regex.fromLiteral(pattern).pattern, pattern) + assertEquals(pattern, Regex(pattern).pattern) + assertEquals(pattern, pattern.toRegex().pattern) + } + + @Test + fun whenRegexMatches_thenResultIsTrue() { + val regex = """a([bc]+)d?""".toRegex() + + assertTrue(regex.containsMatchIn("xabcdy")) + assertTrue(regex.matches("abcd")) + assertFalse(regex matches "xabcdy") + } + + @Test + fun givenCompletelyMatchingRegex_whenMatchResult_thenDestructuring() { + val regex = """a([bc]+)d?""".toRegex() + + assertNull(regex.matchEntire("xabcdy")) + + val matchResult = regex.matchEntire("abbccbbd") + + assertNotNull(matchResult) + assertEquals(matchResult!!.value, matchResult.groupValues[0]) + assertEquals(matchResult.destructured.toList(), matchResult.groupValues.drop(1)) + assertEquals("bbccbb", matchResult.destructured.component1()) + assertNull(matchResult.next()) + } + + @Test + fun givenPartiallyMatchingRegex_whenMatchResult_thenGroups() { + val regex = """a([bc]+)d?""".toRegex() + var matchResult = regex.find("abcb abbd") + + assertNotNull(matchResult) + assertEquals(matchResult!!.value, matchResult.groupValues[0]) + assertEquals("abcb", matchResult.value) + assertEquals(IntRange(0, 3), matchResult.range) + assertEquals(listOf("abcb", "bcb"), matchResult.groupValues) + assertEquals(matchResult.destructured.toList(), matchResult.groupValues.drop(1)) + + matchResult = matchResult.next() + + assertNotNull(matchResult) + assertEquals("abbd", matchResult!!.value) + assertEquals("bb", matchResult.groupValues[1]) + + matchResult = matchResult.next() + + assertNull(matchResult) + } + + @Test + fun givenPartiallyMatchingRegex_whenMatchResult_thenDestructuring() { + val regex = """([\w\s]+) is (\d+) years old""".toRegex() + val matchResult = regex.find("Mickey Mouse is 95 years old") + val (name, age) = matchResult!!.destructured + + assertEquals("Mickey Mouse", name) + assertEquals("95", age) + } + + @Test + fun givenNonMatchingRegex_whenFindCalled_thenNull() { + val regex = """a([bc]+)d?""".toRegex() + val matchResult = regex.find("foo") + + assertNull(matchResult) + } + + @Test + fun givenNonMatchingRegex_whenFindAllCalled_thenEmptySet() { + val regex = """a([bc]+)d?""".toRegex() + val matchResults = regex.findAll("foo") + + assertNotNull(matchResults) + assertTrue(matchResults.none()) + } + + @Test + fun whenReplace_thenReplacement() { + val regex = """(red|green|blue)""".toRegex() + val beautiful = "Roses are red, Violets are blue" + val grim = regex.replace(beautiful, "dark") + val shiny = regex.replaceFirst(beautiful, "rainbow") + + assertEquals("Roses are dark, Violets are dark", grim) + assertEquals("Roses are rainbow, Violets are blue", shiny) + } + + @Test + fun whenComplexReplace_thenReplacement() { + val regex = """(red|green|blue)""".toRegex() + val beautiful = "Roses are red, Violets are blue" + val reallyBeautiful = regex.replace(beautiful) { + matchResult -> matchResult.value.toUpperCase() + "!" + } + + assertEquals("Roses are RED!, Violets are BLUE!", reallyBeautiful) + } + + @Test + fun whenSplit_thenList() { + val regex = """\W+""".toRegex() + val beautiful = "Roses are red, Violets are blue" + + assertEquals(listOf("Roses", "are", "red", "Violets", "are", "blue"), regex.split(beautiful)) + assertEquals(listOf("Roses", "are", "red", "Violets are blue"), regex.split(beautiful, 4)) + assertEquals(regex.toPattern().split(beautiful).asList(), regex.split(beautiful)) + } + +} \ No newline at end of file diff --git a/jackson/pom.xml b/jackson/pom.xml index 001fde5021..2587e61ac1 100644 --- a/jackson/pom.xml +++ b/jackson/pom.xml @@ -129,7 +129,7 @@ - 2.9.2 + 2.9.4 19.0 diff --git a/javax-servlets/src/main/java/com/baeldung/controller/StudentServlet.java b/javax-servlets/src/main/java/com/baeldung/controller/StudentServlet.java new file mode 100644 index 0000000000..3c893eab1a --- /dev/null +++ b/javax-servlets/src/main/java/com/baeldung/controller/StudentServlet.java @@ -0,0 +1,43 @@ +package com.baeldung.controller; + +import java.io.IOException; + +import javax.servlet.RequestDispatcher; +import javax.servlet.ServletException; +import javax.servlet.annotation.WebServlet; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import com.baeldung.service.StudentService; + +/** + * + * @author haseeb + * + */ +@WebServlet(name = "StudentServlet", urlPatterns = "/student-record") +public class StudentServlet extends HttpServlet { + + protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + StudentService studentService = new StudentService(); + String studentID = request.getParameter("id"); + if (studentID != null) { + int id = Integer.parseInt(studentID); + request.setAttribute("studentRecord", studentService.getStudent(id)); + } + + RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/jsp/student-record.jsp"); + dispatcher.forward(request, response); + } + + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + processRequest(request, response); + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + processRequest(request, response); + } +} diff --git a/javax-servlets/src/main/java/com/baeldung/model/Student.java b/javax-servlets/src/main/java/com/baeldung/model/Student.java new file mode 100644 index 0000000000..55afe6bc2a --- /dev/null +++ b/javax-servlets/src/main/java/com/baeldung/model/Student.java @@ -0,0 +1,62 @@ +package com.baeldung.model; + +/** + * + * @author haseeb + * + */ +public class Student { + + private int id; + private String firstName; + private String lastName; + + public Student(int id, String firstName, String lastName) { + super(); + this.id = id; + this.firstName = firstName; + this.lastName = lastName; + } + + /** + * @return the id + */ + public int getId() { + return id; + } + + /** + * @param id the id to set + */ + public void setId(int id) { + this.id = id; + } + + /** + * @return the firstName + */ + public String getFirstName() { + return firstName; + } + + /** + * @param firstName the firstName to set + */ + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + /** + * @return the lastName + */ + public String getLastName() { + return lastName; + } + + /** + * @param lastName the lastName to set + */ + public void setLastName(String lastName) { + this.lastName = lastName; + } +} diff --git a/javax-servlets/src/main/java/com/baeldung/service/StudentService.java b/javax-servlets/src/main/java/com/baeldung/service/StudentService.java new file mode 100644 index 0000000000..1666850d50 --- /dev/null +++ b/javax-servlets/src/main/java/com/baeldung/service/StudentService.java @@ -0,0 +1,34 @@ +package com.baeldung.service; + +import com.baeldung.model.Student; + +/** + * + * @author haseeb + * + */ +public class StudentService { + + /** + * + * @param id + * @return + */ + public Student getStudent(int id) { + + Student student = null; + + switch (id) { + case 1: + student = new Student(1, "John", "Doe"); + break; + case 2: + student = new Student(2, "Jane", "Goodall"); + break; + case 3: + student = new Student(3, "Max", "Born"); + break; + } + return student; + } +} diff --git a/javax-servlets/src/main/java/com/baeldung/servlets/FormServlet.java b/javax-servlets/src/main/java/com/baeldung/servlets/FormServlet.java index fcd9143dff..04c5fec42d 100644 --- a/javax-servlets/src/main/java/com/baeldung/servlets/FormServlet.java +++ b/javax-servlets/src/main/java/com/baeldung/servlets/FormServlet.java @@ -25,7 +25,7 @@ public class FormServlet extends HttpServlet { response.setHeader("Test", "Success"); response.setHeader("BMI", String.valueOf(bmi)); - RequestDispatcher dispatcher = request.getRequestDispatcher("index.jsp"); + RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/jsp/index.jsp"); dispatcher.forward(request, response); } catch (Exception e) { diff --git a/javax-servlets/web/index.jsp b/javax-servlets/src/main/webapp/WEB-INF/jsp/index.jsp similarity index 100% rename from javax-servlets/web/index.jsp rename to javax-servlets/src/main/webapp/WEB-INF/jsp/index.jsp diff --git a/javax-servlets/src/main/webapp/WEB-INF/jsp/student-record.jsp b/javax-servlets/src/main/webapp/WEB-INF/jsp/student-record.jsp new file mode 100644 index 0000000000..4b9d9b378f --- /dev/null +++ b/javax-servlets/src/main/webapp/WEB-INF/jsp/student-record.jsp @@ -0,0 +1,36 @@ +<%@ page language="java" contentType="text/html; charset=ISO-8859-1" + pageEncoding="ISO-8859-1"%> + +<%@ page import="com.baeldung.model.Student"%> + + + + +Student Record + + + <% + if (request.getAttribute("studentRecord") != null) { + Student student = (Student) request.getAttribute("studentRecord"); + %> + +

Student Record

+
+ ID: + <%=student.getId()%>
+
+ First Name: + <%=student.getFirstName()%>
+
+ Last Name: + <%=student.getLastName()%>
+ + <% + } else { + %> +

No student record found.

+ <% + } + %> + + \ No newline at end of file diff --git a/javax-servlets/web/WEB-INF/web.xml b/javax-servlets/src/main/webapp/WEB-INF/web.xml similarity index 100% rename from javax-servlets/web/WEB-INF/web.xml rename to javax-servlets/src/main/webapp/WEB-INF/web.xml diff --git a/libraries/pom.xml b/libraries/pom.xml index fa1839010c..64cc9c6a84 100644 --- a/libraries/pom.xml +++ b/libraries/pom.xml @@ -649,6 +649,11 @@ google-http-client-gson ${googleclient.version} + + org.infinispan + infinispan-core + ${infinispan.version} + @@ -811,5 +816,6 @@ 3.0.14 8.5.24 2.2.0 + 9.1.5.Final
- \ No newline at end of file + diff --git a/libraries/src/main/java/com/baeldung/infinispan/CacheConfiguration.java b/libraries/src/main/java/com/baeldung/infinispan/CacheConfiguration.java new file mode 100644 index 0000000000..bf214458f3 --- /dev/null +++ b/libraries/src/main/java/com/baeldung/infinispan/CacheConfiguration.java @@ -0,0 +1,85 @@ +package com.baeldung.infinispan; + +import com.baeldung.infinispan.listener.CacheListener; +import org.infinispan.Cache; +import org.infinispan.configuration.cache.Configuration; +import org.infinispan.configuration.cache.ConfigurationBuilder; +import org.infinispan.eviction.EvictionType; +import org.infinispan.manager.DefaultCacheManager; +import org.infinispan.transaction.LockingMode; +import org.infinispan.transaction.TransactionMode; + +import java.util.concurrent.TimeUnit; + +public class CacheConfiguration { + + public static final String SIMPLE_HELLO_WORLD_CACHE = "simple-hello-world-cache"; + public static final String EXPIRING_HELLO_WORLD_CACHE = "expiring-hello-world-cache"; + public static final String EVICTING_HELLO_WORLD_CACHE = "evicting-hello-world-cache"; + public static final String PASSIVATING_HELLO_WORLD_CACHE = "passivating-hello-world-cache"; + public static final String TRANSACTIONAL_CACHE = "transactional-cache"; + + public DefaultCacheManager cacheManager() { + DefaultCacheManager cacheManager = new DefaultCacheManager(); + return cacheManager; + } + + public Cache transactionalCache(DefaultCacheManager cacheManager, CacheListener listener) { + return this.buildCache(TRANSACTIONAL_CACHE, cacheManager, listener, transactionalConfiguration()); + } + + public Cache simpleHelloWorldCache(DefaultCacheManager cacheManager, CacheListener listener) { + return this.buildCache(SIMPLE_HELLO_WORLD_CACHE, cacheManager, listener, new ConfigurationBuilder().build()); + } + + public Cache expiringHelloWorldCache(DefaultCacheManager cacheManager, CacheListener listener) { + return this.buildCache(EXPIRING_HELLO_WORLD_CACHE, cacheManager, listener, expiringConfiguration()); + } + + public Cache evictingHelloWorldCache(DefaultCacheManager cacheManager, CacheListener listener) { + return this.buildCache(EVICTING_HELLO_WORLD_CACHE, cacheManager, listener, evictingConfiguration()); + } + + public Cache passivatingHelloWorldCache(DefaultCacheManager cacheManager, CacheListener listener) { + return this.buildCache(PASSIVATING_HELLO_WORLD_CACHE, cacheManager, listener, passivatingConfiguration()); + } + + private Cache buildCache(String cacheName, DefaultCacheManager cacheManager, + CacheListener listener, Configuration configuration) { + + cacheManager.defineConfiguration(cacheName, configuration); + Cache cache = cacheManager.getCache(cacheName); + cache.addListener(listener); + return cache; + } + + private Configuration expiringConfiguration() { + return new ConfigurationBuilder().expiration().lifespan(1, TimeUnit.SECONDS) + .build(); + } + + private Configuration evictingConfiguration() { + return new ConfigurationBuilder() + .memory().evictionType(EvictionType.COUNT).size(1) + .build(); + } + + private Configuration passivatingConfiguration() { + return new ConfigurationBuilder() + .memory().evictionType(EvictionType.COUNT).size(1) + .persistence() + .passivation(true) + .addSingleFileStore() + .purgeOnStartup(true) + .location(System.getProperty("java.io.tmpdir")) + .build(); + } + + private Configuration transactionalConfiguration() { + return new ConfigurationBuilder() + .transaction().transactionMode(TransactionMode.TRANSACTIONAL) + .lockingMode(LockingMode.PESSIMISTIC) + .build(); + } + +} diff --git a/libraries/src/main/java/com/baeldung/infinispan/listener/CacheListener.java b/libraries/src/main/java/com/baeldung/infinispan/listener/CacheListener.java new file mode 100644 index 0000000000..2f6536ad87 --- /dev/null +++ b/libraries/src/main/java/com/baeldung/infinispan/listener/CacheListener.java @@ -0,0 +1,55 @@ +package com.baeldung.infinispan.listener; + +import org.infinispan.notifications.Listener; +import org.infinispan.notifications.cachelistener.annotation.*; +import org.infinispan.notifications.cachelistener.event.*; + +@Listener +public class CacheListener { + + @CacheEntryCreated + public void entryCreated(CacheEntryCreatedEvent event) { + this.printLog("Adding key '" + event.getKey() + "' to cache", event); + } + + @CacheEntryExpired + public void entryExpired(CacheEntryExpiredEvent event) { + this.printLog("Expiring key '" + event.getKey() + "' from cache", event); + } + + @CacheEntryVisited + public void entryVisited(CacheEntryVisitedEvent event) { + this.printLog("Key '" + event.getKey() + "' was visited", event); + } + + @CacheEntryActivated + public void entryActivated(CacheEntryActivatedEvent event) { + this.printLog("Activating key '" + event.getKey() + "' on cache", event); + } + + @CacheEntryPassivated + public void entryPassivated(CacheEntryPassivatedEvent event) { + this.printLog("Passivating key '" + event.getKey() + "' from cache", event); + } + + @CacheEntryLoaded + public void entryLoaded(CacheEntryLoadedEvent event) { + this.printLog("Loading key '" + event.getKey() + "' to cache", event); + } + + @CacheEntriesEvicted + public void entriesEvicted(CacheEntriesEvictedEvent event) { + final StringBuilder builder = new StringBuilder(); + event.getEntries().entrySet().forEach((e) -> + builder.append(e.getKey() + ", ") + ); + System.out.println("Evicting following entries from cache: " + builder.toString()); + } + + private void printLog(String log, CacheEntryEvent event) { + if (!event.isPre()) { + System.out.println(log); + } + } + +} diff --git a/libraries/src/main/java/com/baeldung/infinispan/repository/HelloWorldRepository.java b/libraries/src/main/java/com/baeldung/infinispan/repository/HelloWorldRepository.java new file mode 100644 index 0000000000..85c0d539a3 --- /dev/null +++ b/libraries/src/main/java/com/baeldung/infinispan/repository/HelloWorldRepository.java @@ -0,0 +1,15 @@ +package com.baeldung.infinispan.repository; + +public class HelloWorldRepository { + + public String getHelloWorld() { + try { + System.out.println("Executing some heavy query"); + Thread.sleep(1000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + return "Hello World!"; + } + +} diff --git a/libraries/src/main/java/com/baeldung/infinispan/service/HelloWorldService.java b/libraries/src/main/java/com/baeldung/infinispan/service/HelloWorldService.java new file mode 100644 index 0000000000..0d1ffb4168 --- /dev/null +++ b/libraries/src/main/java/com/baeldung/infinispan/service/HelloWorldService.java @@ -0,0 +1,90 @@ +package com.baeldung.infinispan.service; + +import com.baeldung.infinispan.listener.CacheListener; +import com.baeldung.infinispan.repository.HelloWorldRepository; +import org.infinispan.Cache; + +import java.util.concurrent.TimeUnit; + +public class HelloWorldService { + + private final HelloWorldRepository repository; + + private final Cache simpleHelloWorldCache; + private final Cache expiringHelloWorldCache; + private final Cache evictingHelloWorldCache; + private final Cache passivatingHelloWorldCache; + + public HelloWorldService(HelloWorldRepository repository, CacheListener listener, + Cache simpleHelloWorldCache, + Cache expiringHelloWorldCache, + Cache evictingHelloWorldCache, + Cache passivatingHelloWorldCache) { + + this.repository = repository; + + this.simpleHelloWorldCache = simpleHelloWorldCache; + this.expiringHelloWorldCache = expiringHelloWorldCache; + this.evictingHelloWorldCache = evictingHelloWorldCache; + this.passivatingHelloWorldCache = passivatingHelloWorldCache; + } + + public String findSimpleHelloWorld() { + String cacheKey = "simple-hello"; + String helloWorld = simpleHelloWorldCache.get(cacheKey); + if (helloWorld == null) { + helloWorld = repository.getHelloWorld(); + simpleHelloWorldCache.put(cacheKey, helloWorld); + } + return helloWorld; + } + + public String findExpiringHelloWorld() { + String cacheKey = "expiring-hello"; + String helloWorld = simpleHelloWorldCache.get(cacheKey); + if (helloWorld == null) { + helloWorld = repository.getHelloWorld(); + simpleHelloWorldCache.put(cacheKey, helloWorld, 1, TimeUnit.SECONDS); + } + return helloWorld; + } + + public String findIdleHelloWorld() { + String cacheKey = "idle-hello"; + String helloWorld = simpleHelloWorldCache.get(cacheKey); + if (helloWorld == null) { + helloWorld = repository.getHelloWorld(); + simpleHelloWorldCache.put(cacheKey, helloWorld, -1, TimeUnit.SECONDS, 10, TimeUnit.SECONDS); + } + return helloWorld; + } + + public String findSimpleHelloWorldInExpiringCache() { + String cacheKey = "simple-hello"; + String helloWorld = expiringHelloWorldCache.get(cacheKey); + if (helloWorld == null) { + helloWorld = repository.getHelloWorld(); + expiringHelloWorldCache.put(cacheKey, helloWorld); + } + return helloWorld; + } + + public String findEvictingHelloWorld(String key) { + String value = evictingHelloWorldCache.get(key); + if(value == null) { + value = repository.getHelloWorld(); + evictingHelloWorldCache.put(key, value); + } + return value; + } + + public String findPassivatingHelloWorld(String key) { + String value = passivatingHelloWorldCache.get(key); + if(value == null) { + value = repository.getHelloWorld(); + passivatingHelloWorldCache.put(key, value); + } + return value; + } + +} diff --git a/libraries/src/main/java/com/baeldung/infinispan/service/TransactionalService.java b/libraries/src/main/java/com/baeldung/infinispan/service/TransactionalService.java new file mode 100644 index 0000000000..b0dbf5475f --- /dev/null +++ b/libraries/src/main/java/com/baeldung/infinispan/service/TransactionalService.java @@ -0,0 +1,57 @@ +package com.baeldung.infinispan.service; + +import org.infinispan.Cache; +import org.springframework.util.StopWatch; + +import javax.transaction.TransactionManager; + +public class TransactionalService { + + private final Cache transactionalCache; + + private static final String KEY = "key"; + + public TransactionalService(Cache transactionalCache) { + this.transactionalCache = transactionalCache; + + transactionalCache.put(KEY, 0); + } + + public Integer getQuickHowManyVisits() { + try { + TransactionManager tm = transactionalCache.getAdvancedCache().getTransactionManager(); + tm.begin(); + Integer howManyVisits = transactionalCache.get(KEY); + howManyVisits++; + System.out.println("Ill try to set HowManyVisits to " + howManyVisits); + StopWatch watch = new StopWatch(); + watch.start(); + transactionalCache.put(KEY, howManyVisits); + watch.stop(); + System.out.println("I was able to set HowManyVisits to " + howManyVisits + + " after waiting " + watch.getTotalTimeSeconds() + " seconds"); + + tm.commit(); + return howManyVisits; + } catch (Exception e) { + e.printStackTrace(); + return 0; + } + } + + public void startBackgroundBatch() { + try { + TransactionManager tm = transactionalCache.getAdvancedCache().getTransactionManager(); + tm.begin(); + transactionalCache.put(KEY, 1000); + System.out.println("HowManyVisits should now be 1000, " + + "but we are holding the transaction"); + Thread.sleep(1000L); + tm.rollback(); + System.out.println("The slow batch suffered a rollback"); + } catch (Exception e) { + e.printStackTrace(); + } + } + +} diff --git a/libraries/src/test/java/com/baeldung/infinispan/ConfigurationTest.java b/libraries/src/test/java/com/baeldung/infinispan/ConfigurationTest.java new file mode 100644 index 0000000000..906a887e1a --- /dev/null +++ b/libraries/src/test/java/com/baeldung/infinispan/ConfigurationTest.java @@ -0,0 +1,69 @@ +package com.baeldung.infinispan; + +import com.baeldung.infinispan.listener.CacheListener; +import com.baeldung.infinispan.repository.HelloWorldRepository; +import com.baeldung.infinispan.service.HelloWorldService; +import com.baeldung.infinispan.service.TransactionalService; +import org.infinispan.Cache; +import org.infinispan.manager.DefaultCacheManager; +import org.junit.After; +import org.junit.Before; + +import java.util.concurrent.Callable; + +public class ConfigurationTest { + + private DefaultCacheManager cacheManager; + + private HelloWorldRepository repository = new HelloWorldRepository(); + + protected HelloWorldService helloWorldService; + protected TransactionalService transactionalService; + + @Before + public void setup() { + CacheConfiguration configuration = new CacheConfiguration(); + CacheListener listener = new CacheListener(); + + cacheManager = configuration.cacheManager(); + + Cache transactionalCache = + configuration.transactionalCache(cacheManager, listener); + + Cache simpleHelloWorldCache = + configuration.simpleHelloWorldCache(cacheManager, listener); + + Cache expiringHelloWorldCache = + configuration.expiringHelloWorldCache(cacheManager, listener); + + Cache evictingHelloWorldCache = + configuration.evictingHelloWorldCache(cacheManager, listener); + + Cache passivatingHelloWorldCache = + configuration.passivatingHelloWorldCache(cacheManager, listener); + + this.helloWorldService = new HelloWorldService(repository, + listener, simpleHelloWorldCache, expiringHelloWorldCache, evictingHelloWorldCache, + passivatingHelloWorldCache); + + this.transactionalService = new TransactionalService(transactionalCache); + + } + + @After + public void tearDown() { + cacheManager.stop(); + } + + protected long timeThis(Callable callable) { + try { + long milis = System.currentTimeMillis(); + callable.call(); + return System.currentTimeMillis() - milis; + } catch (Exception e) { + e.printStackTrace(); + } + return 0l; + } + +} diff --git a/libraries/src/test/java/com/baeldung/infinispan/service/HelloWorldServiceUnitTest.java b/libraries/src/test/java/com/baeldung/infinispan/service/HelloWorldServiceUnitTest.java new file mode 100644 index 0000000000..c9ecd57995 --- /dev/null +++ b/libraries/src/test/java/com/baeldung/infinispan/service/HelloWorldServiceUnitTest.java @@ -0,0 +1,71 @@ +package com.baeldung.infinispan.service; + +import com.baeldung.infinispan.ConfigurationTest; +import org.junit.Test; + +import java.util.concurrent.Callable; +import java.util.function.Consumer; + +import static org.assertj.core.api.Java6Assertions.assertThat; + +public class HelloWorldServiceUnitTest extends ConfigurationTest { + + @Test + public void whenGetIsCalledTwoTimes_thenTheSecondShouldHitTheCache() { + assertThat(timeThis(() -> helloWorldService.findSimpleHelloWorld())) + .isGreaterThanOrEqualTo(1000); + + assertThat(timeThis(() -> helloWorldService.findSimpleHelloWorld())) + .isLessThan(100); + } + + @Test + public void whenGetIsCalledTwoTimesQuickly_thenTheSecondShouldHitTheCache() { + assertThat(timeThis(() -> helloWorldService.findExpiringHelloWorld())) + .isGreaterThanOrEqualTo(1000); + + assertThat(timeThis(() -> helloWorldService.findExpiringHelloWorld())) + .isLessThan(100); + } + + @Test + public void whenGetIsCalledTwoTimesSparsely_thenNeitherShouldHitTheCache() + throws InterruptedException { + + assertThat(timeThis(() -> helloWorldService.findExpiringHelloWorld())) + .isGreaterThanOrEqualTo(1000); + + Thread.sleep(1100); + + assertThat(timeThis(() -> helloWorldService.findExpiringHelloWorld())) + .isGreaterThanOrEqualTo(1000); + } + + @Test + public void givenOneEntryIsConfigured_whenTwoAreAdded_thenFirstShouldntBeAvailable() { + + assertThat(timeThis(() -> helloWorldService.findEvictingHelloWorld("key 1"))) + .isGreaterThanOrEqualTo(1000); + + assertThat(timeThis(() -> helloWorldService.findEvictingHelloWorld("key 2"))) + .isGreaterThanOrEqualTo(1000); + + assertThat(timeThis(() -> helloWorldService.findEvictingHelloWorld("key 1"))) + .isGreaterThanOrEqualTo(1000); + } + + @Test + public void givenOneEntryIsConfigured_whenTwoAreAdded_thenTheFirstShouldBeAvailable() { + + assertThat(timeThis(() -> helloWorldService.findPassivatingHelloWorld("key 1"))) + .isGreaterThanOrEqualTo(1000); + + assertThat(timeThis(() -> helloWorldService.findPassivatingHelloWorld("key 2"))) + .isGreaterThanOrEqualTo(1000); + + assertThat(timeThis(() -> helloWorldService.findPassivatingHelloWorld("key 1"))) + .isLessThan(100); + + } + +} diff --git a/libraries/src/test/java/com/baeldung/infinispan/service/TransactionalServiceUnitTest.java b/libraries/src/test/java/com/baeldung/infinispan/service/TransactionalServiceUnitTest.java new file mode 100644 index 0000000000..99efacd18a --- /dev/null +++ b/libraries/src/test/java/com/baeldung/infinispan/service/TransactionalServiceUnitTest.java @@ -0,0 +1,22 @@ +package com.baeldung.infinispan.service; + +import com.baeldung.infinispan.ConfigurationTest; +import org.junit.Test; + +import static org.assertj.core.api.Java6Assertions.assertThat; + +public class TransactionalServiceUnitTest extends ConfigurationTest { + + @Test + public void whenLockingAnEntry_thenItShouldBeInaccessible() throws InterruptedException { + Runnable backGroundJob = () -> transactionalService.startBackgroundBatch(); + Thread backgroundThread = new Thread(backGroundJob); + transactionalService.getQuickHowManyVisits(); + backgroundThread.start(); + Thread.sleep(100); //lets wait our thread warm up + + assertThat(timeThis(() -> transactionalService.getQuickHowManyVisits())) + .isGreaterThan(500).isLessThan(1000); + } + +} diff --git a/persistence-modules/spring-data-dynamodb/pom.xml b/persistence-modules/spring-data-dynamodb/pom.xml index bf90779c29..0b78aac10e 100644 --- a/persistence-modules/spring-data-dynamodb/pom.xml +++ b/persistence-modules/spring-data-dynamodb/pom.xml @@ -23,6 +23,10 @@ 4.4.1 1.11.64 3.3.7-1 + 1.0.392 + 1.11.106 + 1.11.86 + https://s3.eu-central-1.amazonaws.com/dynamodb-local-frankfurt/release @@ -103,6 +107,57 @@ httpclient ${httpclient.version} + + + + + com.amazonaws + DynamoDBLocal + ${dynamodblocal.version} + test + + + com.almworks.sqlite4java + sqlite4java + ${sqlite4java.version} + test + + + com.almworks.sqlite4java + sqlite4java-win32-x86 + ${sqlite4java.version} + dll + test + + + com.almworks.sqlite4java + sqlite4java-win32-x64 + ${sqlite4java.version} + dll + test + + + com.almworks.sqlite4java + libsqlite4java-osx + ${sqlite4java.version} + dylib + test + + + com.almworks.sqlite4java + libsqlite4java-linux-i386 + ${sqlite4java.version} + so + test + + + com.almworks.sqlite4java + libsqlite4java-linux-amd64 + ${sqlite4java.version} + so + test + + @@ -120,6 +175,25 @@ org.apache.maven.plugins maven-war-plugin + + org.apache.maven.plugins + maven-dependency-plugin + 2.10 + + + copy-dependencies + test-compile + + copy-dependencies + + + test + so,dll,dylib + ${project.basedir}/native-libs + + + + @@ -142,6 +216,11 @@ false + + dynamodb-local + DynamoDB Local Release Repository + ${dynamodblocal.repository.url} + diff --git a/persistence-modules/spring-data-dynamodb/src/test/java/com/baeldung/spring/data/dynamodb/repository/ProductInfoRepositoryIntegrationTest.java b/persistence-modules/spring-data-dynamodb/src/test/java/com/baeldung/spring/data/dynamodb/repository/ProductInfoRepositoryIntegrationTest.java index 2ff418b4ec..8052aba3df 100644 --- a/persistence-modules/spring-data-dynamodb/src/test/java/com/baeldung/spring/data/dynamodb/repository/ProductInfoRepositoryIntegrationTest.java +++ b/persistence-modules/spring-data-dynamodb/src/test/java/com/baeldung/spring/data/dynamodb/repository/ProductInfoRepositoryIntegrationTest.java @@ -8,8 +8,9 @@ import com.amazonaws.services.dynamodbv2.model.ResourceInUseException; import com.baeldung.Application; import com.baeldung.spring.data.dynamodb.model.ProductInfo; import com.baeldung.spring.data.dynamodb.repositories.ProductInfoRepository; +import com.baeldung.spring.data.dynamodb.repository.rule.LocalDynamoDBCreationRule; import org.junit.Before; -import org.junit.Ignore; +import org.junit.ClassRule; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; @@ -21,7 +22,10 @@ import org.springframework.test.context.web.WebAppConfiguration; import java.util.List; -import static org.junit.Assert.assertTrue; +import static org.hamcrest.Matchers.greaterThan; +import static org.hamcrest.core.Is.is; +import static org.hamcrest.core.IsEqual.equalTo; +import static org.junit.Assert.assertThat; @RunWith(SpringJUnit4ClassRunner.class) @SpringBootTest(classes = Application.class) @@ -30,6 +34,9 @@ import static org.junit.Assert.assertTrue; @TestPropertySource(properties = { "amazon.dynamodb.endpoint=http://localhost:8000/", "amazon.aws.accesskey=test1", "amazon.aws.secretkey=test231" }) public class ProductInfoRepositoryIntegrationTest { + @ClassRule + public static LocalDynamoDBCreationRule dynamoDB = new LocalDynamoDBCreationRule(); + private DynamoDBMapper dynamoDBMapper; @Autowired @@ -42,7 +49,6 @@ public class ProductInfoRepositoryIntegrationTest { private static final String EXPECTED_PRICE = "50"; @Before - @Ignore // TODO Remove Ignore annotations when running locally with Local DynamoDB instance public void setup() throws Exception { try { @@ -57,19 +63,18 @@ public class ProductInfoRepositoryIntegrationTest { // Do nothing, table already created } - // TODO How to handle different environments. i.e. AVOID deleting all entries in ProductInfoion table + // TODO How to handle different environments. i.e. AVOID deleting all entries in ProductInfo on table dynamoDBMapper.batchDelete((List) repository.findAll()); } @Test - @Ignore // TODO Remove Ignore annotations when running locally with Local DynamoDB instance public void givenItemWithExpectedCost_whenRunFindAll_thenItemIsFound() { ProductInfo productInfo = new ProductInfo(EXPECTED_COST, EXPECTED_PRICE); repository.save(productInfo); List result = (List) repository.findAll(); - assertTrue("Not empty", result.size() > 0); - assertTrue("Contains item with expected cost", result.get(0).getCost().equals(EXPECTED_COST)); + assertThat(result.size(), is(greaterThan(0))); + assertThat(result.get(0).getCost(), is(equalTo(EXPECTED_COST))); } } diff --git a/persistence-modules/spring-data-dynamodb/src/test/java/com/baeldung/spring/data/dynamodb/repository/rule/LocalDynamoDBCreationRule.java b/persistence-modules/spring-data-dynamodb/src/test/java/com/baeldung/spring/data/dynamodb/repository/rule/LocalDynamoDBCreationRule.java new file mode 100644 index 0000000000..62334b6d00 --- /dev/null +++ b/persistence-modules/spring-data-dynamodb/src/test/java/com/baeldung/spring/data/dynamodb/repository/rule/LocalDynamoDBCreationRule.java @@ -0,0 +1,37 @@ +package com.baeldung.spring.data.dynamodb.repository.rule; + +import com.amazonaws.services.dynamodbv2.local.main.ServerRunner; +import com.amazonaws.services.dynamodbv2.local.server.DynamoDBProxyServer; +import org.junit.rules.ExternalResource; + +import java.util.Optional; + +public class LocalDynamoDBCreationRule extends ExternalResource { + + protected DynamoDBProxyServer server; + + public LocalDynamoDBCreationRule() { + System.setProperty("sqlite4java.library.path", "native-libs"); + } + + @Override + protected void before() throws Exception { + String port = "8000"; + this.server = ServerRunner.createServerFromCommandLineArgs(new String[]{"-inMemory", "-port", port}); + server.start(); + } + + @Override + protected void after() { + Optional.ofNullable(server).ifPresent(this::stopUnchecked); + } + + protected void stopUnchecked(DynamoDBProxyServer dynamoDbServer) { + try { + dynamoDbServer.stop(); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + +} diff --git a/persistence-modules/spring-jpa/pom.xml b/persistence-modules/spring-jpa/pom.xml index 04c64fafc3..bc0b2381f3 100644 --- a/persistence-modules/spring-jpa/pom.xml +++ b/persistence-modules/spring-jpa/pom.xml @@ -13,7 +13,7 @@ com.baeldung parent-modules 1.0.0-SNAPSHOT - ../ + ../../ diff --git a/persistence-modules/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/dao/StudentRepository.java b/persistence-modules/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/dao/StudentRepository.java index bfcf6f5cdc..f856b78c52 100644 --- a/persistence-modules/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/dao/StudentRepository.java +++ b/persistence-modules/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/dao/StudentRepository.java @@ -2,6 +2,17 @@ package org.baeldung.inmemory.persistence.dao; import org.baeldung.inmemory.persistence.model.Student; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; + +import java.util.List; public interface StudentRepository extends JpaRepository { + + @Query("SELECT s FROM Student s JOIN s.tags t WHERE t = LOWER(:tag)") + List retrieveByTag(@Param("tag") String tag); + + @Query("SELECT s FROM Student s JOIN s.tags t WHERE s.name = LOWER(:name) AND t = LOWER(:tag)") + List retrieveByNameFilterByTag(@Param("name") String name, @Param("tag") String tag); + } diff --git a/persistence-modules/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/model/Student.java b/persistence-modules/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/model/Student.java index b50fe9122e..2e4e3ea2cb 100644 --- a/persistence-modules/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/model/Student.java +++ b/persistence-modules/spring-jpa/src/main/java/org/baeldung/inmemory/persistence/model/Student.java @@ -1,7 +1,10 @@ package org.baeldung.inmemory.persistence.model; +import javax.persistence.ElementCollection; import javax.persistence.Entity; import javax.persistence.Id; +import java.util.ArrayList; +import java.util.List; @Entity public class Student { @@ -10,6 +13,9 @@ public class Student { private long id; private String name; + @ElementCollection + private List tags = new ArrayList<>(); + public Student() { } @@ -35,4 +41,11 @@ public class Student { this.name = name; } + public List getTags() { + return tags; + } + + public void setTags(List tags) { + this.tags.addAll(tags); + } } diff --git a/persistence-modules/spring-jpa/src/test/java/org/baeldung/persistence/repository/InMemoryDBIntegrationTest.java b/persistence-modules/spring-jpa/src/test/java/org/baeldung/persistence/repository/InMemoryDBIntegrationTest.java index 8380ab5434..28d7e3772c 100644 --- a/persistence-modules/spring-jpa/src/test/java/org/baeldung/persistence/repository/InMemoryDBIntegrationTest.java +++ b/persistence-modules/spring-jpa/src/test/java/org/baeldung/persistence/repository/InMemoryDBIntegrationTest.java @@ -12,6 +12,9 @@ import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; +import java.util.Arrays; +import java.util.List; + import static org.junit.Assert.assertEquals; @RunWith(SpringJUnit4ClassRunner.class) @@ -34,4 +37,46 @@ public class InMemoryDBIntegrationTest { assertEquals("name incorrect", NAME, student2.getName()); } + @Test + public void givenStudentWithTags_whenSave_thenGetByTagOk(){ + Student student = new Student(ID, NAME); + student.setTags(Arrays.asList("full time", "computer science")); + studentRepository.save(student); + + Student student2 = studentRepository.retrieveByTag("full time").get(0); + assertEquals("name incorrect", NAME, student2.getName()); + } + + @Test + public void givenMultipleStudentsWithTags_whenSave_thenGetByTagReturnsCorrectCount(){ + Student student = new Student(0, "Larry"); + student.setTags(Arrays.asList("full time", "computer science")); + studentRepository.save(student); + + Student student2 = new Student(1, "Curly"); + student2.setTags(Arrays.asList("part time", "rocket science")); + studentRepository.save(student2); + + Student student3 = new Student(2, "Moe"); + student3.setTags(Arrays.asList("full time", "philosophy")); + studentRepository.save(student3); + + Student student4 = new Student(3, "Shemp"); + student4.setTags(Arrays.asList("part time", "mathematics")); + studentRepository.save(student4); + + List students = studentRepository.retrieveByTag("full time"); + assertEquals("size incorrect", 2, students.size()); + } + + @Test + public void givenStudentWithTags_whenSave_thenGetByNameAndTagOk(){ + Student student = new Student(ID, NAME); + student.setTags(Arrays.asList("full time", "computer science")); + studentRepository.save(student); + + Student student2 = studentRepository.retrieveByNameFilterByTag("John", "full time").get(0); + assertEquals("name incorrect", NAME, student2.getName()); + } + } diff --git a/pom.xml b/pom.xml index ca6d4afe82..fc0c8f8ba7 100644 --- a/pom.xml +++ b/pom.xml @@ -49,7 +49,9 @@ core-java core-java-io core-java-8 + couchbase deltaspike diff --git a/reactor-core/pom.xml b/reactor-core/pom.xml index 12f481c96f..d387471d56 100644 --- a/reactor-core/pom.xml +++ b/reactor-core/pom.xml @@ -26,11 +26,17 @@ ${assertj.version} test - + + + io.projectreactor + reactor-test + ${reactor-core.version} + test + - 3.0.5.RELEASE + 3.1.3.RELEASE 3.6.1 diff --git a/reactor-core/src/test/java/com/baeldung/reactor/core/CombiningPublishersTest.java b/reactor-core/src/test/java/com/baeldung/reactor/core/CombiningPublishersTest.java new file mode 100644 index 0000000000..9d5d094875 --- /dev/null +++ b/reactor-core/src/test/java/com/baeldung/reactor/core/CombiningPublishersTest.java @@ -0,0 +1,182 @@ +package com.baeldung.reactor.core; + +import java.time.Duration; + +import org.junit.Test; + +import reactor.core.publisher.Flux; +import reactor.test.StepVerifier; + +public class CombiningPublishersTest { + + private static Integer min = 1; + private static Integer max = 5; + + private static Flux evenNumbers = Flux.range(min, max).filter(x -> x % 2 == 0); + private static Flux oddNumbers = Flux.range(min, max).filter(x -> x % 2 > 0); + + + @Test + public void testMerge() { + Flux fluxOfIntegers = Flux.merge( + evenNumbers, + oddNumbers); + + StepVerifier.create(fluxOfIntegers) + .expectNext(2) + .expectNext(4) + .expectNext(1) + .expectNext(3) + .expectNext(5) + .expectComplete() + .verify(); + } + + @Test + public void testMergeWithDelayedElements() { + Flux fluxOfIntegers = Flux.merge( + evenNumbers.delayElements(Duration.ofMillis(500L)), + oddNumbers.delayElements(Duration.ofMillis(300L))); + + StepVerifier.create(fluxOfIntegers) + .expectNext(1) + .expectNext(2) + .expectNext(3) + .expectNext(5) + .expectNext(4) + .expectComplete() + .verify(); + } + + @Test + public void testConcat() { + Flux fluxOfIntegers = Flux.concat( + evenNumbers.delayElements(Duration.ofMillis(500L)), + oddNumbers.delayElements(Duration.ofMillis(300L))); + + StepVerifier.create(fluxOfIntegers) + .expectNext(2) + .expectNext(4) + .expectNext(1) + .expectNext(3) + .expectNext(5) + .expectComplete() + .verify(); + } + + @Test + public void testConcatWith() { + Flux fluxOfIntegers = evenNumbers + .concatWith(oddNumbers); + + StepVerifier.create(fluxOfIntegers) + .expectNext(2) + .expectNext(4) + .expectNext(1) + .expectNext(3) + .expectNext(5) + .expectComplete() + .verify(); + } + + @Test + public void testCombineLatest() { + Flux fluxOfIntegers = Flux.combineLatest( + evenNumbers, + oddNumbers, + (a, b) -> a + b); + + StepVerifier.create(fluxOfIntegers) + .expectNext(5) + .expectNext(7) + .expectNext(9) + .expectComplete() + .verify(); + + } + + @Test + public void testCombineLatest1() { + StepVerifier.create(Flux.combineLatest(obj -> (int) obj[1], evenNumbers, oddNumbers)) + .expectNext(1) + .expectNext(3) + .expectNext(5) + .verifyComplete(); + } + + @Test + public void testMergeSequential() { + Flux fluxOfIntegers = Flux.mergeSequential( + evenNumbers, + oddNumbers); + + StepVerifier.create(fluxOfIntegers) + .expectNext(2) + .expectNext(4) + .expectNext(1) + .expectNext(3) + .expectNext(5) + .expectComplete() + .verify(); + } + + + @Test + public void testMergeDelayError() { + Flux fluxOfIntegers = Flux.mergeDelayError(1, + evenNumbers.delayElements(Duration.ofMillis(500L)), + oddNumbers.delayElements(Duration.ofMillis(300L))); + + StepVerifier.create(fluxOfIntegers) + .expectNext(1) + .expectNext(2) + .expectNext(3) + .expectNext(5) + .expectNext(4) + .expectComplete() + .verify(); + } + + @Test + public void testMergeWith() { + Flux fluxOfIntegers = evenNumbers.mergeWith(oddNumbers); + + StepVerifier.create(fluxOfIntegers) + .expectNext(2) + .expectNext(4) + .expectNext(1) + .expectNext(3) + .expectNext(5) + .expectComplete() + .verify(); + } + + @Test + public void testZip() { + Flux fluxOfIntegers = Flux.zip( + evenNumbers, + oddNumbers, + (a, b) -> a + b); + + StepVerifier.create(fluxOfIntegers) + .expectNext(3) + .expectNext(7) + .expectComplete() + .verify(); + } + + @Test + public void testZipWith() { + Flux fluxOfIntegers = evenNumbers + .zipWith(oddNumbers, + (a, b) -> a * b); + + StepVerifier.create(fluxOfIntegers) + .expectNext(2) + .expectNext(12) + .expectComplete() + .verify(); + } + + +} diff --git a/spring-5-security/src/main/java/com/baeldung/dsl/ClientErrorLoggingDsl.java b/spring-5-security/src/main/java/com/baeldung/dsl/ClientErrorLoggingDsl.java new file mode 100644 index 0000000000..6c7c0d2717 --- /dev/null +++ b/spring-5-security/src/main/java/com/baeldung/dsl/ClientErrorLoggingDsl.java @@ -0,0 +1,32 @@ +package com.baeldung.dsl; + +import java.util.List; + +import org.springframework.http.HttpStatus; +import org.springframework.security.config.annotation.web.builders.HttpSecurity; +import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer; +import org.springframework.security.web.access.intercept.FilterSecurityInterceptor; + +public class ClientErrorLoggingDsl extends AbstractHttpConfigurer { + + private List errorCodes; + + public ClientErrorLoggingDsl(List errorCodes) { + this.errorCodes = errorCodes; + } + + public ClientErrorLoggingDsl() { + + } + + @Override + public void init(HttpSecurity http) throws Exception { + // initialization code + } + + @Override + public void configure(HttpSecurity http) throws Exception { + http.addFilterAfter(new ClientErrorLoggingFilter(errorCodes), FilterSecurityInterceptor.class); + } + +} diff --git a/spring-5-security/src/main/java/com/baeldung/dsl/ClientErrorLoggingFilter.java b/spring-5-security/src/main/java/com/baeldung/dsl/ClientErrorLoggingFilter.java new file mode 100644 index 0000000000..56f7cea3b8 --- /dev/null +++ b/spring-5-security/src/main/java/com/baeldung/dsl/ClientErrorLoggingFilter.java @@ -0,0 +1,54 @@ +package com.baeldung.dsl; + +import java.io.IOException; +import java.util.List; + +import javax.servlet.FilterChain; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; +import javax.servlet.http.HttpServletResponse; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.springframework.http.HttpStatus; +import org.springframework.security.core.Authentication; +import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.web.filter.GenericFilterBean; + +public class ClientErrorLoggingFilter extends GenericFilterBean { + + private static final Logger logger = LogManager.getLogger(ClientErrorLoggingFilter.class); + + private List errorCodes; + + public ClientErrorLoggingFilter(List errorCodes) { + this.errorCodes = errorCodes; + } + + @Override + public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { + + Authentication auth = SecurityContextHolder.getContext() + .getAuthentication(); + + if (auth != null) { + int status = ((HttpServletResponse) response).getStatus(); + if (status >= 400 && status < 500) { + if (errorCodes == null) { + logger.debug("User " + auth.getName() + " encountered error " + status); + } else { + if (errorCodes.stream() + .filter(s -> s.value() == status) + .findFirst() + .isPresent()) { + logger.debug("User " + auth.getName() + " encountered error " + status); + } + } + } + } + + chain.doFilter(request, response); + } + +} diff --git a/spring-5-security/src/main/java/com/baeldung/dsl/CustomDslApplication.java b/spring-5-security/src/main/java/com/baeldung/dsl/CustomDslApplication.java new file mode 100644 index 0000000000..3e58bccaf4 --- /dev/null +++ b/spring-5-security/src/main/java/com/baeldung/dsl/CustomDslApplication.java @@ -0,0 +1,13 @@ +package com.baeldung.dsl; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class CustomDslApplication { + + public static void main(String[] args) { + SpringApplication.run(CustomDslApplication.class, args); + } + +} diff --git a/spring-5-security/src/main/java/com/baeldung/dsl/MyController.java b/spring-5-security/src/main/java/com/baeldung/dsl/MyController.java new file mode 100644 index 0000000000..c69046afb5 --- /dev/null +++ b/spring-5-security/src/main/java/com/baeldung/dsl/MyController.java @@ -0,0 +1,14 @@ +package com.baeldung.dsl; + +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class MyController { + + @GetMapping("/admin") + public String getAdminPage() { + return "Hello Admin"; + } + +} diff --git a/spring-5-security/src/main/java/com/baeldung/dsl/SecurityConfig.java b/spring-5-security/src/main/java/com/baeldung/dsl/SecurityConfig.java new file mode 100644 index 0000000000..4494aaa131 --- /dev/null +++ b/spring-5-security/src/main/java/com/baeldung/dsl/SecurityConfig.java @@ -0,0 +1,50 @@ +package com.baeldung.dsl; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; +import org.springframework.security.config.annotation.web.builders.HttpSecurity; +import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; +import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; +import org.springframework.security.crypto.password.PasswordEncoder; + +@Configuration +public class SecurityConfig extends WebSecurityConfigurerAdapter { + + @Override + protected void configure(HttpSecurity http) throws Exception { + http.authorizeRequests() + .antMatchers("/admin*") + .hasAnyRole("ADMIN") + .anyRequest() + .authenticated() + .and() + .formLogin() + .and() + .apply(clientErrorLogging()); + } + + @Bean + public ClientErrorLoggingDsl clientErrorLogging() { + return new ClientErrorLoggingDsl(); + } + + @Override + protected void configure(AuthenticationManagerBuilder auth) throws Exception { + auth.inMemoryAuthentication() + .passwordEncoder(passwordEncoder()) + .withUser("user1") + .password(passwordEncoder().encode("user")) + .roles("USER") + .and() + .withUser("admin") + .password(passwordEncoder().encode("admin")) + .roles("ADMIN"); + } + + @Bean + public PasswordEncoder passwordEncoder() { + return new BCryptPasswordEncoder(); + } + +} diff --git a/spring-5-security/src/main/resources/application.properties b/spring-5-security/src/main/resources/application.properties index ccec014c2b..781ee76826 100644 --- a/spring-5-security/src/main/resources/application.properties +++ b/spring-5-security/src/main/resources/application.properties @@ -1,3 +1,5 @@ server.port=8081 -logging.level.root=INFO \ No newline at end of file +logging.level.root=INFO + +logging.level.com.baeldung.dsl.ClientErrorLoggingFilter=DEBUG diff --git a/spring-5/src/main/java/com/baeldung/execption/ActorController.java b/spring-5/src/main/java/com/baeldung/execption/ActorController.java new file mode 100644 index 0000000000..6c9c46253a --- /dev/null +++ b/spring-5/src/main/java/com/baeldung/execption/ActorController.java @@ -0,0 +1,41 @@ +package com.baeldung.execption; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PutMapping; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.server.ResponseStatusException; + +@RestController +public class ActorController { + + @Autowired + ActorService actorService; + + @GetMapping("/actor/{id}") + public String getActorName(@PathVariable("id") int id) { + try { + return actorService.getActor(id); + } catch (ActorNotFoundException ex) { + throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Actor Not Found", ex); + } + } + + @DeleteMapping("/actor/{id}") + public String getActor(@PathVariable("id") int id) { + return actorService.removeActor(id); + } + + @PutMapping("/actor/{id}/{name}") + public String updateActorName(@PathVariable("id") int id, @PathVariable("name") String name) { + try { + return actorService.updateActor(id, name); + } catch (ActorNotFoundException ex) { + throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Provide correct Actor Id", ex); + } + } + +} diff --git a/spring-5/src/main/java/com/baeldung/execption/ActorNotFoundException.java b/spring-5/src/main/java/com/baeldung/execption/ActorNotFoundException.java new file mode 100644 index 0000000000..642c075b5d --- /dev/null +++ b/spring-5/src/main/java/com/baeldung/execption/ActorNotFoundException.java @@ -0,0 +1,13 @@ +package com.baeldung.execption; + +import org.springframework.http.HttpStatus; +import org.springframework.web.bind.annotation.ResponseStatus; + +@ResponseStatus(code = HttpStatus.NOT_FOUND, reason = "Actor Not Found") +public class ActorNotFoundException extends Exception { + private static final long serialVersionUID = 1L; + + public ActorNotFoundException(String errorMessage) { + super(errorMessage); + } +} diff --git a/spring-5/src/main/java/com/baeldung/execption/ActorService.java b/spring-5/src/main/java/com/baeldung/execption/ActorService.java new file mode 100644 index 0000000000..956fa92015 --- /dev/null +++ b/spring-5/src/main/java/com/baeldung/execption/ActorService.java @@ -0,0 +1,35 @@ +package com.baeldung.execption; + +import java.util.Arrays; +import java.util.List; + +import org.springframework.http.HttpStatus; +import org.springframework.stereotype.Service; +import org.springframework.web.server.ResponseStatusException; + +@Service +public class ActorService { + List actors = Arrays.asList("Jack Nicholson", "Marlon Brando", "Robert De Niro", "Al Pacino", "Tom Hanks"); + + public String getActor(int index) throws ActorNotFoundException { + if (index >= actors.size()) { + throw new ActorNotFoundException("Actor Not Found in Repsoitory"); + } + return actors.get(index); + } + + public String updateActor(int index, String actorName) throws ActorNotFoundException { + if (index >= actors.size()) { + throw new ActorNotFoundException("Actor Not Found in Repsoitory"); + } + actors.set(index, actorName); + return actorName; + } + + public String removeActor(int index) { + if (index >= actors.size()) { + throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Actor Not Found in Repsoitory"); + } + return actors.remove(index); + } +} diff --git a/spring-5/src/main/java/com/baeldung/execption/SpringExceptionApplication.java b/spring-5/src/main/java/com/baeldung/execption/SpringExceptionApplication.java new file mode 100644 index 0000000000..287356256c --- /dev/null +++ b/spring-5/src/main/java/com/baeldung/execption/SpringExceptionApplication.java @@ -0,0 +1,14 @@ +package com.baeldung.execption; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration; +import org.springframework.context.annotation.ComponentScan; + +@SpringBootApplication(exclude = SecurityAutoConfiguration.class) +@ComponentScan(basePackages = { "com.baeldung.execption" }) +public class SpringExceptionApplication { + public static void main(String[] args) { + SpringApplication.run(SpringExceptionApplication.class, args); + } +} \ No newline at end of file diff --git a/spring-batch/pom.xml b/spring-batch/pom.xml index f372ebd724..f72024d32b 100644 --- a/spring-batch/pom.xml +++ b/spring-batch/pom.xml @@ -18,9 +18,10 @@ UTF-8 - 4.3.4.RELEASE - 3.0.7.RELEASE + 5.0.3.RELEASE + 4.0.0.RELEASE 3.15.1 + 4.1 @@ -51,6 +52,16 @@ spring-batch-core ${spring.batch.version} + + org.springframework.batch + spring-batch-test + ${spring.batch.version} + + + com.opencsv + opencsv + ${opencsv.version} + diff --git a/spring-batch/src/main/java/org/baeldung/taskletsvschunks/chunks/LineProcessor.java b/spring-batch/src/main/java/org/baeldung/taskletsvschunks/chunks/LineProcessor.java new file mode 100644 index 0000000000..5b6cd9add3 --- /dev/null +++ b/spring-batch/src/main/java/org/baeldung/taskletsvschunks/chunks/LineProcessor.java @@ -0,0 +1,36 @@ +package org.baeldung.taskletsvschunks.chunks; + +import org.baeldung.taskletsvschunks.model.Line; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.batch.core.ExitStatus; +import org.springframework.batch.core.StepExecution; +import org.springframework.batch.core.StepExecutionListener; +import org.springframework.batch.item.ItemProcessor; + +import java.time.LocalDate; +import java.time.temporal.ChronoUnit; + +public class LineProcessor implements ItemProcessor, StepExecutionListener { + + private final Logger logger = LoggerFactory.getLogger(LineProcessor.class); + + @Override + public void beforeStep(StepExecution stepExecution) { + logger.debug("Line Processor initialized."); + } + + @Override + public Line process(Line line) throws Exception { + long age = ChronoUnit.YEARS.between(line.getDob(), LocalDate.now()); + logger.debug("Calculated age " + age + " for line " + line.toString()); + line.setAge(age); + return line; + } + + @Override + public ExitStatus afterStep(StepExecution stepExecution) { + logger.debug("Line Processor ended."); + return ExitStatus.COMPLETED; + } +} diff --git a/spring-batch/src/main/java/org/baeldung/taskletsvschunks/chunks/LineReader.java b/spring-batch/src/main/java/org/baeldung/taskletsvschunks/chunks/LineReader.java new file mode 100644 index 0000000000..5f6b6de218 --- /dev/null +++ b/spring-batch/src/main/java/org/baeldung/taskletsvschunks/chunks/LineReader.java @@ -0,0 +1,36 @@ +package org.baeldung.taskletsvschunks.chunks; + +import org.baeldung.taskletsvschunks.model.Line; +import org.baeldung.taskletsvschunks.utils.FileUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.batch.core.ExitStatus; +import org.springframework.batch.core.StepExecution; +import org.springframework.batch.core.StepExecutionListener; +import org.springframework.batch.item.ItemReader; + +public class LineReader implements ItemReader, StepExecutionListener { + + private final Logger logger = LoggerFactory.getLogger(LineReader.class); + private FileUtils fu; + + @Override + public void beforeStep(StepExecution stepExecution) { + fu = new FileUtils("taskletsvschunks/input/tasklets-vs-chunks.csv"); + logger.debug("Line Reader initialized."); + } + + @Override + public Line read() throws Exception { + Line line = fu.readLine(); + if (line != null) logger.debug("Read line: " + line.toString()); + return line; + } + + @Override + public ExitStatus afterStep(StepExecution stepExecution) { + fu.closeReader(); + logger.debug("Line Reader ended."); + return ExitStatus.COMPLETED; + } +} diff --git a/spring-batch/src/main/java/org/baeldung/taskletsvschunks/chunks/LinesWriter.java b/spring-batch/src/main/java/org/baeldung/taskletsvschunks/chunks/LinesWriter.java new file mode 100644 index 0000000000..66f9103a56 --- /dev/null +++ b/spring-batch/src/main/java/org/baeldung/taskletsvschunks/chunks/LinesWriter.java @@ -0,0 +1,39 @@ +package org.baeldung.taskletsvschunks.chunks; + +import org.baeldung.taskletsvschunks.model.Line; +import org.baeldung.taskletsvschunks.utils.FileUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.batch.core.ExitStatus; +import org.springframework.batch.core.StepExecution; +import org.springframework.batch.core.StepExecutionListener; +import org.springframework.batch.item.ItemWriter; + +import java.util.List; + +public class LinesWriter implements ItemWriter, StepExecutionListener { + + private final Logger logger = LoggerFactory.getLogger(LinesWriter.class); + private FileUtils fu; + + @Override + public void beforeStep(StepExecution stepExecution) { + fu = new FileUtils("output.csv"); + logger.debug("Line Writer initialized."); + } + + @Override + public ExitStatus afterStep(StepExecution stepExecution) { + fu.closeWriter(); + logger.debug("Line Writer ended."); + return ExitStatus.COMPLETED; + } + + @Override + public void write(List lines) throws Exception { + for (Line line : lines) { + fu.writeLine(line); + logger.debug("Wrote line " + line.toString()); + } + } +} diff --git a/spring-batch/src/main/java/org/baeldung/taskletsvschunks/model/Line.java b/spring-batch/src/main/java/org/baeldung/taskletsvschunks/model/Line.java new file mode 100644 index 0000000000..fee6fd31a6 --- /dev/null +++ b/spring-batch/src/main/java/org/baeldung/taskletsvschunks/model/Line.java @@ -0,0 +1,55 @@ +package org.baeldung.taskletsvschunks.model; + +import java.io.Serializable; +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; + +public class Line implements Serializable { + + private String name; + private LocalDate dob; + private Long age; + + public Line(String name, LocalDate dob) { + this.name = name; + this.dob = dob; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public LocalDate getDob() { + return dob; + } + + public void setDob(LocalDate dob) { + this.dob = dob; + } + + public Long getAge() { + return age; + } + + public void setAge(Long age) { + this.age = age; + } + + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("["); + sb.append(this.name); + sb.append(","); + sb.append(this.dob.format(DateTimeFormatter.ofPattern("MM/dd/yyyy"))); + if (this.age != null) { + sb.append(","); + sb.append(this.age); + } + sb.append("]"); + return sb.toString(); + } +} diff --git a/spring-batch/src/main/java/org/baeldung/taskletsvschunks/tasklets/LinesProcessor.java b/spring-batch/src/main/java/org/baeldung/taskletsvschunks/tasklets/LinesProcessor.java new file mode 100644 index 0000000000..ba7a3088d5 --- /dev/null +++ b/spring-batch/src/main/java/org/baeldung/taskletsvschunks/tasklets/LinesProcessor.java @@ -0,0 +1,49 @@ +package org.baeldung.taskletsvschunks.tasklets; + +import org.baeldung.taskletsvschunks.model.Line; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.batch.core.ExitStatus; +import org.springframework.batch.core.StepContribution; +import org.springframework.batch.core.StepExecution; +import org.springframework.batch.core.StepExecutionListener; +import org.springframework.batch.core.scope.context.ChunkContext; +import org.springframework.batch.core.step.tasklet.Tasklet; +import org.springframework.batch.item.ExecutionContext; +import org.springframework.batch.repeat.RepeatStatus; + +import java.time.LocalDate; +import java.time.temporal.ChronoUnit; +import java.util.List; + +public class LinesProcessor implements Tasklet, StepExecutionListener { + + private final Logger logger = LoggerFactory.getLogger(LinesProcessor.class); + + private List lines; + + @Override + public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception { + for (Line line : lines) { + long age = ChronoUnit.YEARS.between(line.getDob(), LocalDate.now()); + logger.debug("Calculated age " + age + " for line " + line.toString()); + line.setAge(age); + } + return RepeatStatus.FINISHED; + } + + @Override + public void beforeStep(StepExecution stepExecution) { + ExecutionContext executionContext = stepExecution + .getJobExecution() + .getExecutionContext(); + this.lines = (List) executionContext.get("lines"); + logger.debug("Lines Processor initialized."); + } + + @Override + public ExitStatus afterStep(StepExecution stepExecution) { + logger.debug("Lines Processor ended."); + return ExitStatus.COMPLETED; + } +} diff --git a/spring-batch/src/main/java/org/baeldung/taskletsvschunks/tasklets/LinesReader.java b/spring-batch/src/main/java/org/baeldung/taskletsvschunks/tasklets/LinesReader.java new file mode 100644 index 0000000000..9905ee8f8a --- /dev/null +++ b/spring-batch/src/main/java/org/baeldung/taskletsvschunks/tasklets/LinesReader.java @@ -0,0 +1,53 @@ +package org.baeldung.taskletsvschunks.tasklets; + +import org.baeldung.taskletsvschunks.model.Line; +import org.baeldung.taskletsvschunks.utils.FileUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.batch.core.ExitStatus; +import org.springframework.batch.core.StepContribution; +import org.springframework.batch.core.StepExecution; +import org.springframework.batch.core.StepExecutionListener; +import org.springframework.batch.core.scope.context.ChunkContext; +import org.springframework.batch.core.step.tasklet.Tasklet; +import org.springframework.batch.repeat.RepeatStatus; + +import java.util.ArrayList; +import java.util.List; + +public class LinesReader implements Tasklet, StepExecutionListener { + + private final Logger logger = LoggerFactory.getLogger(LinesReader.class); + + private List lines; + private FileUtils fu; + + @Override + public void beforeStep(StepExecution stepExecution) { + lines = new ArrayList(); + fu = new FileUtils("taskletsvschunks/input/tasklets-vs-chunks.csv"); + logger.debug("Lines Reader initialized."); + } + + @Override + public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception { + Line line = fu.readLine(); + while (line != null) { + lines.add(line); + logger.debug("Read line: " + line.toString()); + line = fu.readLine(); + } + return RepeatStatus.FINISHED; + } + + @Override + public ExitStatus afterStep(StepExecution stepExecution) { + fu.closeReader(); + stepExecution + .getJobExecution() + .getExecutionContext() + .put("lines", this.lines); + logger.debug("Lines Reader ended."); + return ExitStatus.COMPLETED; + } +} diff --git a/spring-batch/src/main/java/org/baeldung/taskletsvschunks/tasklets/LinesWriter.java b/spring-batch/src/main/java/org/baeldung/taskletsvschunks/tasklets/LinesWriter.java new file mode 100644 index 0000000000..937288a890 --- /dev/null +++ b/spring-batch/src/main/java/org/baeldung/taskletsvschunks/tasklets/LinesWriter.java @@ -0,0 +1,50 @@ +package org.baeldung.taskletsvschunks.tasklets; + +import org.baeldung.taskletsvschunks.model.Line; +import org.baeldung.taskletsvschunks.utils.FileUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.batch.core.ExitStatus; +import org.springframework.batch.core.StepContribution; +import org.springframework.batch.core.StepExecution; +import org.springframework.batch.core.StepExecutionListener; +import org.springframework.batch.core.scope.context.ChunkContext; +import org.springframework.batch.core.step.tasklet.Tasklet; +import org.springframework.batch.item.ExecutionContext; +import org.springframework.batch.repeat.RepeatStatus; + +import java.util.List; + +public class LinesWriter implements Tasklet, StepExecutionListener { + + private final Logger logger = LoggerFactory.getLogger(LinesWriter.class); + + private List lines; + private FileUtils fu; + + @Override + public void beforeStep(StepExecution stepExecution) { + ExecutionContext executionContext = stepExecution + .getJobExecution() + .getExecutionContext(); + this.lines = (List) executionContext.get("lines"); + fu = new FileUtils("output.csv"); + logger.debug("Lines Writer initialized."); + } + + @Override + public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception { + for (Line line : lines) { + fu.writeLine(line); + logger.debug("Wrote line " + line.toString()); + } + return RepeatStatus.FINISHED; + } + + @Override + public ExitStatus afterStep(StepExecution stepExecution) { + fu.closeWriter(); + logger.debug("Lines Writer ended."); + return ExitStatus.COMPLETED; + } +} diff --git a/spring-batch/src/main/java/org/baeldung/taskletsvschunks/utils/FileUtils.java b/spring-batch/src/main/java/org/baeldung/taskletsvschunks/utils/FileUtils.java new file mode 100644 index 0000000000..df36d5c756 --- /dev/null +++ b/spring-batch/src/main/java/org/baeldung/taskletsvschunks/utils/FileUtils.java @@ -0,0 +1,95 @@ +package org.baeldung.taskletsvschunks.utils; + +import com.opencsv.CSVReader; +import com.opencsv.CSVWriter; +import org.baeldung.taskletsvschunks.model.Line; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; + +public class FileUtils { + + private final Logger logger = LoggerFactory.getLogger(FileUtils.class); + + private String fileName; + private CSVReader CSVReader; + private CSVWriter CSVWriter; + private FileReader fileReader; + private FileWriter fileWriter; + private File file; + + public FileUtils(String fileName) { + this.fileName = fileName; + } + + public Line readLine() { + try { + if (CSVReader == null) initReader(); + String[] line = CSVReader.readNext(); + if (line == null) return null; + return new Line(line[0], LocalDate.parse(line[1], DateTimeFormatter.ofPattern("MM/dd/yyyy"))); + } catch (Exception e) { + logger.error("Error while reading line in file: " + this.fileName); + return null; + } + } + + public void writeLine(Line line) { + try { + if (CSVWriter == null) initWriter(); + String[] lineStr = new String[2]; + lineStr[0] = line.getName(); + lineStr[1] = line + .getAge() + .toString(); + CSVWriter.writeNext(lineStr); + } catch (Exception e) { + logger.error("Error while writing line in file: " + this.fileName); + } + } + + private void initReader() throws Exception { + ClassLoader classLoader = this + .getClass() + .getClassLoader(); + if (file == null) file = new File(classLoader + .getResource(fileName) + .getFile()); + if (fileReader == null) fileReader = new FileReader(file); + if (CSVReader == null) CSVReader = new CSVReader(fileReader); + } + + private void initWriter() throws Exception { + if (file == null) { + file = new File(fileName); + file.createNewFile(); + } + if (fileWriter == null) fileWriter = new FileWriter(file, true); + if (CSVWriter == null) CSVWriter = new CSVWriter(fileWriter); + } + + public void closeWriter() { + try { + CSVWriter.close(); + fileWriter.close(); + } catch (IOException e) { + logger.error("Error while closing writer."); + } + } + + public void closeReader() { + try { + CSVReader.close(); + fileReader.close(); + } catch (IOException e) { + logger.error("Error while closing reader."); + } + } + +} diff --git a/spring-batch/src/main/resources/logback.xml b/spring-batch/src/main/resources/logback.xml new file mode 100644 index 0000000000..dc9218caf3 --- /dev/null +++ b/spring-batch/src/main/resources/logback.xml @@ -0,0 +1,33 @@ + + + + C:\Users\admin\Desktop\Baeldung\repo\magkrause\tutorials\spring-batch\src\main\resources\taskletsvschunks\logs.log + + + %d{yyyy-MM-dd HH:mm:ss} [%thread] %level %logger{35} - %msg%n + + + + + + ${DEV_HOME}/archived/debug.%d{yyyy-MM-dd}.%i.log + + + 10MB + + + + + + + + + + + + + + \ No newline at end of file diff --git a/spring-batch/src/main/resources/taskletsvschunks/chunks.xml b/spring-batch/src/main/resources/taskletsvschunks/chunks.xml new file mode 100644 index 0000000000..f4b77ac10c --- /dev/null +++ b/spring-batch/src/main/resources/taskletsvschunks/chunks.xml @@ -0,0 +1,39 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/spring-batch/src/main/resources/taskletsvschunks/input/tasklets-vs-chunks.csv b/spring-batch/src/main/resources/taskletsvschunks/input/tasklets-vs-chunks.csv new file mode 100644 index 0000000000..214bd3cb70 --- /dev/null +++ b/spring-batch/src/main/resources/taskletsvschunks/input/tasklets-vs-chunks.csv @@ -0,0 +1,6 @@ +Mae Hodges,10/22/1972 +Gary Potter,02/22/1953 +Betty Wise,02/17/1968 +Wayne Rose,04/06/1977 +Adam Caldwell,09/27/1995 +Lucille Phillips,05/14/1992 \ No newline at end of file diff --git a/spring-batch/src/main/resources/taskletsvschunks/tasklets.xml b/spring-batch/src/main/resources/taskletsvschunks/tasklets.xml new file mode 100644 index 0000000000..34ce2944bc --- /dev/null +++ b/spring-batch/src/main/resources/taskletsvschunks/tasklets.xml @@ -0,0 +1,47 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/spring-batch/src/test/java/org/baeldung/taskletsvschunks/chunks/ChunksTest.java b/spring-batch/src/test/java/org/baeldung/taskletsvschunks/chunks/ChunksTest.java new file mode 100644 index 0000000000..34b6315dc4 --- /dev/null +++ b/spring-batch/src/test/java/org/baeldung/taskletsvschunks/chunks/ChunksTest.java @@ -0,0 +1,24 @@ +package org.baeldung.taskletsvschunks.chunks; + +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.batch.core.ExitStatus; +import org.springframework.batch.core.JobExecution; +import org.springframework.batch.test.JobLauncherTestUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(locations = "classpath:/taskletsvschunks/chunks.xml") +public class ChunksTest { + + @Autowired private JobLauncherTestUtils jobLauncherTestUtils; + + @Test + public void givenChunksJob_WhenJobEnds_ThenStatusCompleted() throws Exception { + JobExecution jobExecution = jobLauncherTestUtils.launchJob(); + Assert.assertEquals(ExitStatus.COMPLETED, jobExecution.getExitStatus()); + } +} \ No newline at end of file diff --git a/spring-batch/src/test/java/org/baeldung/taskletsvschunks/tasklets/TaskletsTest.java b/spring-batch/src/test/java/org/baeldung/taskletsvschunks/tasklets/TaskletsTest.java new file mode 100644 index 0000000000..53731feed4 --- /dev/null +++ b/spring-batch/src/test/java/org/baeldung/taskletsvschunks/tasklets/TaskletsTest.java @@ -0,0 +1,24 @@ +package org.baeldung.taskletsvschunks.tasklets; + +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.batch.core.ExitStatus; +import org.springframework.batch.core.JobExecution; +import org.springframework.batch.test.JobLauncherTestUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(locations = "classpath:/taskletsvschunks/tasklets.xml") +public class TaskletsTest { + + @Autowired private JobLauncherTestUtils jobLauncherTestUtils; + + @Test + public void givenTaskletsJob_WhenJobEnds_ThenStatusCompleted() throws Exception { + JobExecution jobExecution = jobLauncherTestUtils.launchJob(); + Assert.assertEquals(ExitStatus.COMPLETED, jobExecution.getExitStatus()); + } +} \ No newline at end of file diff --git a/spring-cloud/spring-cloud-security/oauth2client/pom.xml b/spring-cloud/spring-cloud-security/auth-client/pom.xml similarity index 97% rename from spring-cloud/spring-cloud-security/oauth2client/pom.xml rename to spring-cloud/spring-cloud-security/auth-client/pom.xml index fd1c6964e1..5213b93f9a 100644 --- a/spring-cloud/spring-cloud-security/oauth2client/pom.xml +++ b/spring-cloud/spring-cloud-security/auth-client/pom.xml @@ -3,11 +3,11 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 com.baeldung - oauth2client + auth-client 0.0.1-SNAPSHOT jar - oauth2client + auth-client Demo project for Spring Boot diff --git a/spring-cloud/spring-cloud-security/oauth2client/src/main/java/com/baeldung/CloudSite.java b/spring-cloud/spring-cloud-security/auth-client/src/main/java/com/baeldung/CloudSite.java similarity index 100% rename from spring-cloud/spring-cloud-security/oauth2client/src/main/java/com/baeldung/CloudSite.java rename to spring-cloud/spring-cloud-security/auth-client/src/main/java/com/baeldung/CloudSite.java diff --git a/spring-cloud/spring-cloud-security/oauth2client/src/main/java/com/baeldung/config/SiteSecurityConfigurer.java b/spring-cloud/spring-cloud-security/auth-client/src/main/java/com/baeldung/config/SiteSecurityConfigurer.java similarity index 100% rename from spring-cloud/spring-cloud-security/oauth2client/src/main/java/com/baeldung/config/SiteSecurityConfigurer.java rename to spring-cloud/spring-cloud-security/auth-client/src/main/java/com/baeldung/config/SiteSecurityConfigurer.java diff --git a/spring-cloud/spring-cloud-security/oauth2client/src/main/java/com/baeldung/controller/CloudSiteController.java b/spring-cloud/spring-cloud-security/auth-client/src/main/java/com/baeldung/controller/CloudSiteController.java similarity index 100% rename from spring-cloud/spring-cloud-security/oauth2client/src/main/java/com/baeldung/controller/CloudSiteController.java rename to spring-cloud/spring-cloud-security/auth-client/src/main/java/com/baeldung/controller/CloudSiteController.java diff --git a/spring-cloud/spring-cloud-security/oauth2client/src/main/java/com/baeldung/filters/SimpleFilter.java b/spring-cloud/spring-cloud-security/auth-client/src/main/java/com/baeldung/filters/SimpleFilter.java similarity index 100% rename from spring-cloud/spring-cloud-security/oauth2client/src/main/java/com/baeldung/filters/SimpleFilter.java rename to spring-cloud/spring-cloud-security/auth-client/src/main/java/com/baeldung/filters/SimpleFilter.java diff --git a/spring-cloud/spring-cloud-security/oauth2client/src/main/resources/application.properties b/spring-cloud/spring-cloud-security/auth-client/src/main/resources/application.properties similarity index 100% rename from spring-cloud/spring-cloud-security/oauth2client/src/main/resources/application.properties rename to spring-cloud/spring-cloud-security/auth-client/src/main/resources/application.properties diff --git a/spring-cloud/spring-cloud-security/oauth2client/src/main/resources/application.yml b/spring-cloud/spring-cloud-security/auth-client/src/main/resources/application.yml similarity index 100% rename from spring-cloud/spring-cloud-security/oauth2client/src/main/resources/application.yml rename to spring-cloud/spring-cloud-security/auth-client/src/main/resources/application.yml diff --git a/spring-cloud/spring-cloud-security/oauth2client/src/main/resources/templates/personinfo.html b/spring-cloud/spring-cloud-security/auth-client/src/main/resources/templates/personinfo.html similarity index 100% rename from spring-cloud/spring-cloud-security/oauth2client/src/main/resources/templates/personinfo.html rename to spring-cloud/spring-cloud-security/auth-client/src/main/resources/templates/personinfo.html diff --git a/spring-cloud/spring-cloud-security/oauth2client/src/test/java/com/example/springoath2/Springoath2ApplicationTests.java b/spring-cloud/spring-cloud-security/auth-client/src/test/java/com/example/springoath2/Springoath2ApplicationTests.java similarity index 100% rename from spring-cloud/spring-cloud-security/oauth2client/src/test/java/com/example/springoath2/Springoath2ApplicationTests.java rename to spring-cloud/spring-cloud-security/auth-client/src/test/java/com/example/springoath2/Springoath2ApplicationTests.java diff --git a/spring-cloud/spring-cloud-security/personresource/pom.xml b/spring-cloud/spring-cloud-security/auth-resource/pom.xml similarity index 96% rename from spring-cloud/spring-cloud-security/personresource/pom.xml rename to spring-cloud/spring-cloud-security/auth-resource/pom.xml index ca1ff82515..2c54d24e7d 100644 --- a/spring-cloud/spring-cloud-security/personresource/pom.xml +++ b/spring-cloud/spring-cloud-security/auth-resource/pom.xml @@ -4,11 +4,11 @@ 4.0.0 com.baeldung - personresource + auth-resource 0.0.1-SNAPSHOT jar - personresource + auth-resource Demo project for Spring Boot diff --git a/spring-cloud/spring-cloud-security/personresource/src/main/java/com/baeldung/Application.java b/spring-cloud/spring-cloud-security/auth-resource/src/main/java/com/baeldung/Application.java similarity index 100% rename from spring-cloud/spring-cloud-security/personresource/src/main/java/com/baeldung/Application.java rename to spring-cloud/spring-cloud-security/auth-resource/src/main/java/com/baeldung/Application.java diff --git a/spring-cloud/spring-cloud-security/personresource/src/main/java/com/baeldung/config/ResourceConfigurer.java b/spring-cloud/spring-cloud-security/auth-resource/src/main/java/com/baeldung/config/ResourceConfigurer.java similarity index 100% rename from spring-cloud/spring-cloud-security/personresource/src/main/java/com/baeldung/config/ResourceConfigurer.java rename to spring-cloud/spring-cloud-security/auth-resource/src/main/java/com/baeldung/config/ResourceConfigurer.java diff --git a/spring-cloud/spring-cloud-security/personresource/src/main/java/com/baeldung/controller/PersonInfoController.java b/spring-cloud/spring-cloud-security/auth-resource/src/main/java/com/baeldung/controller/PersonInfoController.java similarity index 100% rename from spring-cloud/spring-cloud-security/personresource/src/main/java/com/baeldung/controller/PersonInfoController.java rename to spring-cloud/spring-cloud-security/auth-resource/src/main/java/com/baeldung/controller/PersonInfoController.java diff --git a/spring-cloud/spring-cloud-security/personresource/src/main/java/com/baeldung/model/Person.java b/spring-cloud/spring-cloud-security/auth-resource/src/main/java/com/baeldung/model/Person.java similarity index 100% rename from spring-cloud/spring-cloud-security/personresource/src/main/java/com/baeldung/model/Person.java rename to spring-cloud/spring-cloud-security/auth-resource/src/main/java/com/baeldung/model/Person.java diff --git a/spring-cloud/spring-cloud-security/personresource/src/main/resources/application.yml b/spring-cloud/spring-cloud-security/auth-resource/src/main/resources/application.yml similarity index 100% rename from spring-cloud/spring-cloud-security/personresource/src/main/resources/application.yml rename to spring-cloud/spring-cloud-security/auth-resource/src/main/resources/application.yml diff --git a/spring-cloud/spring-cloud-security/personresource/src/test/java/com/baeldung/service/personservice/PersonserviceApplicationTests.java b/spring-cloud/spring-cloud-security/auth-resource/src/test/java/com/baeldung/service/personservice/PersonserviceApplicationTests.java similarity index 100% rename from spring-cloud/spring-cloud-security/personresource/src/test/java/com/baeldung/service/personservice/PersonserviceApplicationTests.java rename to spring-cloud/spring-cloud-security/auth-resource/src/test/java/com/baeldung/service/personservice/PersonserviceApplicationTests.java diff --git a/spring-cloud/spring-cloud-security/authserver/pom.xml b/spring-cloud/spring-cloud-security/auth-server/pom.xml similarity index 96% rename from spring-cloud/spring-cloud-security/authserver/pom.xml rename to spring-cloud/spring-cloud-security/auth-server/pom.xml index ed88ac046b..ab30f3f2ec 100644 --- a/spring-cloud/spring-cloud-security/authserver/pom.xml +++ b/spring-cloud/spring-cloud-security/auth-server/pom.xml @@ -4,7 +4,7 @@ 4.0.0 com.baeldung - authserver + auth-server 0.0.1-SNAPSHOT diff --git a/spring-cloud/spring-cloud-security/authserver/src/main/java/com/baeldung/AuthServer.java b/spring-cloud/spring-cloud-security/auth-server/src/main/java/com/baeldung/AuthServer.java similarity index 100% rename from spring-cloud/spring-cloud-security/authserver/src/main/java/com/baeldung/AuthServer.java rename to spring-cloud/spring-cloud-security/auth-server/src/main/java/com/baeldung/AuthServer.java diff --git a/spring-cloud/spring-cloud-security/authserver/src/main/java/com/baeldung/config/AuthServerConfigurer.java b/spring-cloud/spring-cloud-security/auth-server/src/main/java/com/baeldung/config/AuthServerConfigurer.java similarity index 100% rename from spring-cloud/spring-cloud-security/authserver/src/main/java/com/baeldung/config/AuthServerConfigurer.java rename to spring-cloud/spring-cloud-security/auth-server/src/main/java/com/baeldung/config/AuthServerConfigurer.java diff --git a/spring-cloud/spring-cloud-security/authserver/src/main/java/com/baeldung/config/ResourceServerConfigurer.java b/spring-cloud/spring-cloud-security/auth-server/src/main/java/com/baeldung/config/ResourceServerConfigurer.java similarity index 100% rename from spring-cloud/spring-cloud-security/authserver/src/main/java/com/baeldung/config/ResourceServerConfigurer.java rename to spring-cloud/spring-cloud-security/auth-server/src/main/java/com/baeldung/config/ResourceServerConfigurer.java diff --git a/spring-cloud/spring-cloud-security/authserver/src/main/java/com/baeldung/config/WebMvcConfigurer.java b/spring-cloud/spring-cloud-security/auth-server/src/main/java/com/baeldung/config/WebMvcConfigurer.java similarity index 100% rename from spring-cloud/spring-cloud-security/authserver/src/main/java/com/baeldung/config/WebMvcConfigurer.java rename to spring-cloud/spring-cloud-security/auth-server/src/main/java/com/baeldung/config/WebMvcConfigurer.java diff --git a/spring-cloud/spring-cloud-security/authserver/src/main/java/com/baeldung/config/WebSecurityConfigurer.java b/spring-cloud/spring-cloud-security/auth-server/src/main/java/com/baeldung/config/WebSecurityConfigurer.java similarity index 100% rename from spring-cloud/spring-cloud-security/authserver/src/main/java/com/baeldung/config/WebSecurityConfigurer.java rename to spring-cloud/spring-cloud-security/auth-server/src/main/java/com/baeldung/config/WebSecurityConfigurer.java diff --git a/spring-cloud/spring-cloud-security/authserver/src/main/java/com/baeldung/controller/ResourceController.java b/spring-cloud/spring-cloud-security/auth-server/src/main/java/com/baeldung/controller/ResourceController.java similarity index 100% rename from spring-cloud/spring-cloud-security/authserver/src/main/java/com/baeldung/controller/ResourceController.java rename to spring-cloud/spring-cloud-security/auth-server/src/main/java/com/baeldung/controller/ResourceController.java diff --git a/spring-cloud/spring-cloud-security/authserver/src/main/resources/application.yml b/spring-cloud/spring-cloud-security/auth-server/src/main/resources/application.yml similarity index 100% rename from spring-cloud/spring-cloud-security/authserver/src/main/resources/application.yml rename to spring-cloud/spring-cloud-security/auth-server/src/main/resources/application.yml diff --git a/spring-cloud/spring-cloud-security/authserver/src/main/resources/certificate/mykeystore.jks b/spring-cloud/spring-cloud-security/auth-server/src/main/resources/certificate/mykeystore.jks similarity index 100% rename from spring-cloud/spring-cloud-security/authserver/src/main/resources/certificate/mykeystore.jks rename to spring-cloud/spring-cloud-security/auth-server/src/main/resources/certificate/mykeystore.jks diff --git a/spring-cloud/spring-cloud-security/authserver/src/main/resources/templates/login.html b/spring-cloud/spring-cloud-security/auth-server/src/main/resources/templates/login.html similarity index 100% rename from spring-cloud/spring-cloud-security/authserver/src/main/resources/templates/login.html rename to spring-cloud/spring-cloud-security/auth-server/src/main/resources/templates/login.html diff --git a/spring-dispatcher-servlet/src/main/java/com/baeldung/springdispatcherservlet/controller/MultipartController.java b/spring-dispatcher-servlet/src/main/java/com/baeldung/springdispatcherservlet/controller/MultipartController.java index 1cc8261f7c..a693bf039f 100644 --- a/spring-dispatcher-servlet/src/main/java/com/baeldung/springdispatcherservlet/controller/MultipartController.java +++ b/spring-dispatcher-servlet/src/main/java/com/baeldung/springdispatcherservlet/controller/MultipartController.java @@ -25,14 +25,20 @@ public class MultipartController { try { InputStream in = file.getInputStream(); String path = new File(".").getAbsolutePath(); - FileOutputStream f = new FileOutputStream(path.substring(0, path.length()-1)+ "/uploads/" + file.getOriginalFilename()); - int ch; - while ((ch = in.read()) != -1) { - f.write(ch); + FileOutputStream f = new FileOutputStream(path.substring(0, path.length() - 1) + "/uploads/" + file.getOriginalFilename()); + try { + int ch; + while ((ch = in.read()) != -1) { + f.write(ch); + } + modelAndView.getModel().put("message", "File uploaded successfully!"); + } catch (Exception e) { + System.out.println("Exception uploading multipart: " + e); + } finally { + f.flush(); + f.close(); + in.close(); } - f.flush(); - f.close(); - modelAndView.getModel().put("message", "File uploaded successfully!"); } catch (Exception e) { System.out.println("Exception uploading multipart: " + e); } diff --git a/spring-integration/pom.xml b/spring-integration/pom.xml index 27cc7381e3..43e45dfd17 100644 --- a/spring-integration/pom.xml +++ b/spring-integration/pom.xml @@ -128,12 +128,6 @@ spring-security-test ${spring.version} test - - - org.springframework - spring-test - ${spring.version} - test junit diff --git a/spring-integration/src/test/java/com/baeldung/si/TestSpringIntegrationSecurity.java b/spring-integration/src/test/java/com/baeldung/si/TestSpringIntegrationSecurity.java index 45dcb1e836..9ae82af2dc 100644 --- a/spring-integration/src/test/java/com/baeldung/si/TestSpringIntegrationSecurity.java +++ b/spring-integration/src/test/java/com/baeldung/si/TestSpringIntegrationSecurity.java @@ -1,8 +1,11 @@ package com.baeldung.si; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import org.hamcrest.core.IsInstanceOf; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.messaging.SubscribableChannel; @@ -21,6 +24,9 @@ import com.baeldung.si.security.SecurityConfig; @ContextConfiguration(classes = { SecurityConfig.class, SecuredDirectChannel.class, MessageConsumer.class }) public class TestSpringIntegrationSecurity { + @Rule + public ExpectedException expectedException = ExpectedException.none(); + @Autowired SubscribableChannel startDirectChannel; @@ -34,34 +40,28 @@ public class TestSpringIntegrationSecurity { startDirectChannel.send(new GenericMessage(DIRECT_CHANNEL_MESSAGE)); } - @Test(expected = AccessDeniedException.class) + @Test @WithMockUser(username = "jane", roles = { "LOGGER" }) - public void givenRoleLogger_whenSendMessageToDirectChannel_thenAccessDenied() throws Throwable { - try { - startDirectChannel.send(new GenericMessage(DIRECT_CHANNEL_MESSAGE)); - } catch (Exception e) { - throw e.getCause(); - } + public void givenRoleLogger_whenSendMessageToDirectChannel_thenAccessDenied() { + expectedException.expectCause(IsInstanceOf. instanceOf(AccessDeniedException.class)); + + startDirectChannel.send(new GenericMessage(DIRECT_CHANNEL_MESSAGE)); } - @Test(expected = AccessDeniedException.class) + @Test @WithMockUser(username = "jane") - public void givenJane_whenSendMessageToDirectChannel_thenAccessDenied() throws Throwable { - try { - startDirectChannel.send(new GenericMessage(DIRECT_CHANNEL_MESSAGE)); - } catch (Exception e) { - throw e.getCause(); - } + public void givenJane_whenSendMessageToDirectChannel_thenAccessDenied() { + expectedException.expectCause(IsInstanceOf. instanceOf(AccessDeniedException.class)); + + startDirectChannel.send(new GenericMessage(DIRECT_CHANNEL_MESSAGE)); } - @Test(expected = AccessDeniedException.class) + @Test @WithMockUser(roles = { "VIEWER" }) - public void givenRoleViewer_whenSendToDirectChannel_thenAccessDenied() throws Throwable { - try { - startDirectChannel.send(new GenericMessage(DIRECT_CHANNEL_MESSAGE)); - } catch (Exception e) { - throw e.getCause(); - } + public void givenRoleViewer_whenSendToDirectChannel_thenAccessDenied() { + expectedException.expectCause(IsInstanceOf. instanceOf(AccessDeniedException.class)); + + startDirectChannel.send(new GenericMessage(DIRECT_CHANNEL_MESSAGE)); } @Test @@ -78,4 +78,4 @@ public class TestSpringIntegrationSecurity { assertEquals(DIRECT_CHANNEL_MESSAGE, messageConsumer.getMessageContent()); } -} +} \ No newline at end of file diff --git a/spring-integration/src/test/resources/logback-test.xml b/spring-integration/src/test/resources/logback-test.xml new file mode 100644 index 0000000000..a807be0ca2 --- /dev/null +++ b/spring-integration/src/test/resources/logback-test.xml @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/spring-jinq/README.md b/spring-jinq/README.md new file mode 100644 index 0000000000..10d7903f49 --- /dev/null +++ b/spring-jinq/README.md @@ -0,0 +1,4 @@ +## Relevant articles: + +- [Introduction to Jinq with Spring](http://www.baeldung.com/spring-jinq) + diff --git a/spring-mvc-java/pom.xml b/spring-mvc-java/pom.xml index b939f0496d..9d90ba2dbf 100644 --- a/spring-mvc-java/pom.xml +++ b/spring-mvc-java/pom.xml @@ -297,7 +297,7 @@ 4.3.4.RELEASE 4.2.0.RELEASE 2.1.5.RELEASE - 2.8.5 + 2.9.4 5.2.5.Final diff --git a/spring-mvc-simple/src/main/java/com/baeldung/spring/configuration/ApplicationConfiguration.java b/spring-mvc-simple/src/main/java/com/baeldung/spring/configuration/ApplicationConfiguration.java index b9a8336bf2..69c45d90b3 100644 --- a/spring-mvc-simple/src/main/java/com/baeldung/spring/configuration/ApplicationConfiguration.java +++ b/spring-mvc-simple/src/main/java/com/baeldung/spring/configuration/ApplicationConfiguration.java @@ -1,14 +1,21 @@ package com.baeldung.spring.configuration; +import com.baeldung.spring.controller.rss.ArticleRssFeedViewResolver; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.web.multipart.MultipartResolver; import org.springframework.web.multipart.commons.CommonsMultipartResolver; +import org.springframework.web.servlet.ViewResolver; import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; +import org.springframework.web.servlet.view.ContentNegotiatingViewResolver; import org.springframework.web.servlet.view.InternalResourceViewResolver; +import org.springframework.web.accept.ContentNegotiationManager; + +import java.util.List; +import java.util.ArrayList; @Configuration @EnableWebMvc @@ -21,11 +28,19 @@ class ApplicationConfiguration implements WebMvcConfigurer { } @Bean - public InternalResourceViewResolver jspViewResolver() { - InternalResourceViewResolver bean = new InternalResourceViewResolver(); - bean.setPrefix("/WEB-INF/views/"); - bean.setSuffix(".jsp"); - return bean; + public ContentNegotiatingViewResolver viewResolver(ContentNegotiationManager cnManager) { + ContentNegotiatingViewResolver cnvResolver = new ContentNegotiatingViewResolver(); + cnvResolver.setContentNegotiationManager(cnManager); + List resolvers = new ArrayList<>(); + + InternalResourceViewResolver bean = new InternalResourceViewResolver("/WEB-INF/views/",".jsp"); + ArticleRssFeedViewResolver articleRssFeedViewResolver = new ArticleRssFeedViewResolver(); + + resolvers.add(bean); + resolvers.add(articleRssFeedViewResolver); + + cnvResolver.setViewResolvers(resolvers); + return cnvResolver; } @Bean diff --git a/spring-mvc-simple/src/main/java/com/baeldung/spring/controller/rss/ArticleRssController.java b/spring-mvc-simple/src/main/java/com/baeldung/spring/controller/rss/ArticleRssController.java index 1f51b238ac..8f23076e8e 100644 --- a/spring-mvc-simple/src/main/java/com/baeldung/spring/controller/rss/ArticleRssController.java +++ b/spring-mvc-simple/src/main/java/com/baeldung/spring/controller/rss/ArticleRssController.java @@ -1,14 +1,59 @@ package com.baeldung.spring.controller.rss; +import com.rometools.rome.feed.synd.*; +import com.rometools.rome.io.FeedException; +import com.rometools.rome.io.SyndFeedOutput; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.ResponseBody; + +import java.util.ArrayList; +import java.util.Date; +import java.util.List; @Controller public class ArticleRssController { - @GetMapping(value = "/rss", produces = "application/*") - public String articleFeed() { + @GetMapping(value = "/rssMvc") + public String articleMvcFeed() { return "articleFeedView"; } + @GetMapping(value = "/rssRest", produces = "application/rss+xml") + @ResponseBody + public String articleRestFeed() throws FeedException { + SyndFeed feed = new SyndFeedImpl(); + feed.setFeedType("rss_2.0"); + feed.setLink("http://localhost:8080/spring-mvc-simple/rss"); + feed.setTitle("Article Feed"); + feed.setDescription("Article Feed Description"); + feed.setPublishedDate(new Date()); + + List list = new ArrayList(); + + SyndEntry item1 = new SyndEntryImpl(); + item1.setLink("http://www.baeldung.com/netty-exception-handling"); + item1.setTitle("Exceptions in Netty"); + SyndContent description1 = new SyndContentImpl(); + description1.setValue("In this quick article, we’ll be looking at exception handling in Netty."); + item1.setDescription(description1); + item1.setPublishedDate(new Date()); + item1.setAuthor("Carlos"); + + SyndEntry item2 = new SyndEntryImpl(); + item2.setLink("http://www.baeldung.com/cockroachdb-java"); + item2.setTitle("Guide to CockroachDB in Java"); + SyndContent description2 = new SyndContentImpl(); + description2.setValue("This tutorial is an introductory guide to using CockroachDB with Java."); + item2.setDescription(description2); + item2.setPublishedDate(new Date()); + item2.setAuthor("Baeldung"); + + list.add(item1); + list.add(item2); + feed.setEntries(list); + + return new SyndFeedOutput().outputString(feed); + } + } diff --git a/spring-mvc-simple/src/main/java/com/baeldung/spring/controller/rss/ArticleRssFeedViewResolver.java b/spring-mvc-simple/src/main/java/com/baeldung/spring/controller/rss/ArticleRssFeedViewResolver.java new file mode 100644 index 0000000000..6be06c4812 --- /dev/null +++ b/spring-mvc-simple/src/main/java/com/baeldung/spring/controller/rss/ArticleRssFeedViewResolver.java @@ -0,0 +1,15 @@ +package com.baeldung.spring.controller.rss; + +import org.springframework.web.servlet.View; +import org.springframework.web.servlet.ViewResolver; + +import java.util.Locale; + +public class ArticleRssFeedViewResolver implements ViewResolver { + + @Override + public View resolveViewName(String s, Locale locale) throws Exception { + ArticleFeedView articleFeedView = new ArticleFeedView(); + return articleFeedView; + } +} diff --git a/testing-modules/testing/src/test/java/com/baeldung/testing/assertj/exceptions/Java8StyleAssertions.java b/testing-modules/testing/src/test/java/com/baeldung/testing/assertj/exceptions/Java8StyleAssertions.java index 53f192bb2f..973b921654 100644 --- a/testing-modules/testing/src/test/java/com/baeldung/testing/assertj/exceptions/Java8StyleAssertions.java +++ b/testing-modules/testing/src/test/java/com/baeldung/testing/assertj/exceptions/Java8StyleAssertions.java @@ -5,19 +5,43 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatExceptionOfType; import static org.assertj.core.api.Assertions.catchThrowable; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; import org.junit.Test; public class Java8StyleAssertions { @Test - public void whenDividingByZero_thenArithmeticException() { + public void whenGettingOutOfBoundsItem_thenIndexOutOfBoundsException() { assertThatThrownBy(() -> { - int numerator = 10; - int denominator = 0; - int quotient = numerator / denominator; - }).isInstanceOf(ArithmeticException.class) - .hasMessageContaining("/ by zero"); + ArrayList myStringList = new ArrayList(Arrays.asList("Strine one", "String two")); + myStringList.get(2); + }).isInstanceOf(IndexOutOfBoundsException.class) + .hasMessageStartingWith("Index: 2") + .hasMessageContaining("2") + .hasMessageEndingWith("Size: 2") + .hasMessageContaining("Index: 2, Size: 2") + .hasMessage("Index: %s, Size: %s", 2, 2) + .hasMessageMatching("Index: \\d+, Size: \\d+") + .hasNoCause(); + } + @Test + public void whenWrappingException_thenCauseInstanceOfWrappedExceptionType() { + assertThatThrownBy(() -> { + try { + throw new IOException(); + } catch (IOException e) { + throw new RuntimeException(e); + } + }).isInstanceOf(RuntimeException.class) + .hasCauseInstanceOf(IOException.class) + .hasStackTraceContaining("IOException"); + } + + @Test + public void whenDividingByZero_thenArithmeticException() { assertThatExceptionOfType(ArithmeticException.class).isThrownBy(() -> { int numerator = 10; int denominator = 0; @@ -25,7 +49,7 @@ public class Java8StyleAssertions { }) .withMessageContaining("/ by zero"); - // BDD style: + // Alternatively: // when Throwable thrown = catchThrowable(() -> {