diff --git a/apache-solrj/README.md b/apache-solrj/README.md new file mode 100644 index 0000000000..7a32becb64 --- /dev/null +++ b/apache-solrj/README.md @@ -0,0 +1,4 @@ +## Apache Solrj Tutorials Project + +### Relevant Articles +- [Guide to Solr in Java with Apache Solrj](http://www.baeldung.com/apache-solrj) diff --git a/apache-solrj/src/main/java/com/baeldung/solrjava/ProductBean.java b/apache-solrj/src/main/java/com/baeldung/solrjava/ProductBean.java new file mode 100644 index 0000000000..14eea8f2f9 --- /dev/null +++ b/apache-solrj/src/main/java/com/baeldung/solrjava/ProductBean.java @@ -0,0 +1,44 @@ +package com.baeldung.solrjava; + +import org.apache.solr.client.solrj.beans.Field; + +public class ProductBean { + + String id; + String name; + String price; + + public ProductBean(String id, String name, String price) { + super(); + this.id = id; + this.name = name; + this.price = price; + } + + public String getId() { + return id; + } + + @Field("id") + protected void setId(String id) { + this.id = id; + } + + public String getName() { + return name; + } + + @Field("name") + protected void setName(String name) { + this.name = name; + } + + public String getPrice() { + return price; + } + + @Field("price") + protected void setPrice(String price) { + this.price = price; + } +} diff --git a/apache-solrj/src/main/java/com/baeldung/solrjava/SolrJavaIntegration.java b/apache-solrj/src/main/java/com/baeldung/solrjava/SolrJavaIntegration.java index f2d21f0993..c55e1c9ada 100644 --- a/apache-solrj/src/main/java/com/baeldung/solrjava/SolrJavaIntegration.java +++ b/apache-solrj/src/main/java/com/baeldung/solrjava/SolrJavaIntegration.java @@ -17,6 +17,12 @@ public class SolrJavaIntegration { solrClient.setParser(new XMLResponseParser()); } + public void addProductBean(ProductBean pBean) throws IOException, SolrServerException { + + solrClient.addBean(pBean); + solrClient.commit(); + } + public void addSolrDocument(String documentId, String itemName, String itemPrice) throws SolrServerException, IOException { SolrInputDocument document = new SolrInputDocument(); @@ -27,12 +33,18 @@ public class SolrJavaIntegration { solrClient.commit(); } - public void deleteSolrDocument(String documentId) throws SolrServerException, IOException { + public void deleteSolrDocumentById(String documentId) throws SolrServerException, IOException { solrClient.deleteById(documentId); solrClient.commit(); } + public void deleteSolrDocumentByQuery(String query) throws SolrServerException, IOException { + + solrClient.deleteByQuery(query); + solrClient.commit(); + } + protected HttpSolrClient getSolrClient() { return solrClient; } @@ -40,4 +52,5 @@ public class SolrJavaIntegration { protected void setSolrClient(HttpSolrClient solrClient) { this.solrClient = solrClient; } + } diff --git a/apache-solrj/src/test/java/com/baeldung/solrjava/SolrJavaIntegrationTest.java b/apache-solrj/src/test/java/com/baeldung/solrjava/SolrJavaIntegrationTest.java index 22f9eae8ee..8b5fe77c6f 100644 --- a/apache-solrj/src/test/java/com/baeldung/solrjava/SolrJavaIntegrationTest.java +++ b/apache-solrj/src/test/java/com/baeldung/solrjava/SolrJavaIntegrationTest.java @@ -24,7 +24,7 @@ public class SolrJavaIntegrationTest { } @Test - public void whenAdd_thenVerifyAdded() throws SolrServerException, IOException { + public void whenAdd_thenVerifyAddedByQueryOnId() throws SolrServerException, IOException { SolrQuery query = new SolrQuery(); query.set("q", "id:123456"); @@ -33,18 +33,68 @@ public class SolrJavaIntegrationTest { response = solrJavaIntegration.getSolrClient().query(query); SolrDocumentList docList = response.getResults(); - assertEquals(docList.getNumFound(), 1); + assertEquals(1, docList.getNumFound()); for (SolrDocument doc : docList) { - assertEquals((String) doc.getFieldValue("id"), "123456"); - assertEquals((Double) doc.getFieldValue("price"), (Double) 599.99); + assertEquals("Kenmore Dishwasher", (String) doc.getFieldValue("name")); + assertEquals((Double) 599.99, (Double) doc.getFieldValue("price")); } } @Test - public void whenDelete_thenVerifyDeleted() throws SolrServerException, IOException { + public void whenAdd_thenVerifyAddedByQueryOnPrice() throws SolrServerException, IOException { - solrJavaIntegration.deleteSolrDocument("123456"); + SolrQuery query = new SolrQuery(); + query.set("q", "price:599.99"); + QueryResponse response = null; + + response = solrJavaIntegration.getSolrClient().query(query); + + SolrDocumentList docList = response.getResults(); + assertEquals(1, docList.getNumFound()); + + for (SolrDocument doc : docList) { + assertEquals("123456", (String) doc.getFieldValue("id")); + assertEquals((Double) 599.99, (Double) doc.getFieldValue("price")); + } + } + + @Test + public void whenAdd_thenVerifyAddedByQuery() throws SolrServerException, IOException { + + SolrDocument doc = solrJavaIntegration.getSolrClient().getById("123456"); + assertEquals("Kenmore Dishwasher", (String) doc.getFieldValue("name")); + assertEquals((Double) 599.99, (Double) doc.getFieldValue("price")); + } + + @Test + public void whenAddBean_thenVerifyAddedByQuery() throws SolrServerException, IOException { + + ProductBean pBean = new ProductBean("888", "Apple iPhone 6s", "299.99"); + solrJavaIntegration.addProductBean(pBean); + + SolrDocument doc = solrJavaIntegration.getSolrClient().getById("888"); + assertEquals("Apple iPhone 6s", (String) doc.getFieldValue("name")); + assertEquals((Double) 299.99, (Double) doc.getFieldValue("price")); + } + + @Test + public void whenDeleteById_thenVerifyDeleted() throws SolrServerException, IOException { + + solrJavaIntegration.deleteSolrDocumentById("123456"); + + SolrQuery query = new SolrQuery(); + query.set("q", "id:123456"); + QueryResponse response = solrJavaIntegration.getSolrClient().query(query); + + SolrDocumentList docList = response.getResults(); + assertEquals(0, docList.getNumFound()); + } + + @Test + public void whenDeleteByQuery_thenVerifyDeleted() throws SolrServerException, IOException { + + solrJavaIntegration.deleteSolrDocumentByQuery("name:Kenmore Dishwasher"); SolrQuery query = new SolrQuery(); query.set("q", "id:123456"); @@ -53,6 +103,6 @@ public class SolrJavaIntegrationTest { response = solrJavaIntegration.getSolrClient().query(query); SolrDocumentList docList = response.getResults(); - assertEquals(docList.getNumFound(), 0); + assertEquals(0, docList.getNumFound()); } } diff --git a/core-java/src/main/java/com/baeldung/algorithms/RunAlgorithm.java b/core-java/src/main/java/com/baeldung/algorithms/RunAlgorithm.java index 3f7c8bf4b3..22c5776293 100644 --- a/core-java/src/main/java/com/baeldung/algorithms/RunAlgorithm.java +++ b/core-java/src/main/java/com/baeldung/algorithms/RunAlgorithm.java @@ -2,7 +2,8 @@ package com.baeldung.algorithms; import java.util.Scanner; -import com.baeldung.algorithms.annealing.SimulatedAnnealing; +import com.baeldung.algorithms.ga.annealing.SimulatedAnnealing; +import com.baeldung.algorithms.ga.ant_colony.AntColonyOptimization; import com.baeldung.algorithms.ga.binary.SimpleGeneticAlgorithm; import com.baeldung.algorithms.slope_one.SlopeOne; @@ -14,6 +15,7 @@ public class RunAlgorithm { System.out.println("1 - Simulated Annealing"); System.out.println("2 - Slope One"); System.out.println("3 - Simple Genetic Algorithm"); + System.out.println("4 - Ant Colony"); int decision = in.nextInt(); switch (decision) { case 1: @@ -27,6 +29,10 @@ public class RunAlgorithm { SimpleGeneticAlgorithm ga = new SimpleGeneticAlgorithm(); ga.runAlgorithm(50, "1011000100000100010000100000100111001000000100000100000000001111"); break; + case 4: + AntColonyOptimization antColony = new AntColonyOptimization(21); + antColony.startAntOptimization(); + break; default: System.out.println("Unknown option"); break; diff --git a/core-java/src/main/java/com/baeldung/algorithms/annealing/City.java b/core-java/src/main/java/com/baeldung/algorithms/ga/annealing/City.java similarity index 90% rename from core-java/src/main/java/com/baeldung/algorithms/annealing/City.java rename to core-java/src/main/java/com/baeldung/algorithms/ga/annealing/City.java index 77e8652df0..cb5647f4d2 100644 --- a/core-java/src/main/java/com/baeldung/algorithms/annealing/City.java +++ b/core-java/src/main/java/com/baeldung/algorithms/ga/annealing/City.java @@ -1,4 +1,4 @@ -package com.baeldung.algorithms.annealing; +package com.baeldung.algorithms.ga.annealing; import lombok.Data; diff --git a/core-java/src/main/java/com/baeldung/algorithms/annealing/SimulatedAnnealing.java b/core-java/src/main/java/com/baeldung/algorithms/ga/annealing/SimulatedAnnealing.java similarity index 96% rename from core-java/src/main/java/com/baeldung/algorithms/annealing/SimulatedAnnealing.java rename to core-java/src/main/java/com/baeldung/algorithms/ga/annealing/SimulatedAnnealing.java index a7dc974e97..bff64fc239 100644 --- a/core-java/src/main/java/com/baeldung/algorithms/annealing/SimulatedAnnealing.java +++ b/core-java/src/main/java/com/baeldung/algorithms/ga/annealing/SimulatedAnnealing.java @@ -1,4 +1,4 @@ -package com.baeldung.algorithms.annealing; +package com.baeldung.algorithms.ga.annealing; public class SimulatedAnnealing { diff --git a/core-java/src/main/java/com/baeldung/algorithms/annealing/Travel.java b/core-java/src/main/java/com/baeldung/algorithms/ga/annealing/Travel.java similarity index 97% rename from core-java/src/main/java/com/baeldung/algorithms/annealing/Travel.java rename to core-java/src/main/java/com/baeldung/algorithms/ga/annealing/Travel.java index 9bf341fbbe..3139b49586 100644 --- a/core-java/src/main/java/com/baeldung/algorithms/annealing/Travel.java +++ b/core-java/src/main/java/com/baeldung/algorithms/ga/annealing/Travel.java @@ -1,4 +1,4 @@ -package com.baeldung.algorithms.annealing; +package com.baeldung.algorithms.ga.annealing; import java.util.ArrayList; import java.util.Collections; diff --git a/core-java/src/main/java/com/baeldung/algorithms/ga/ant_colony/Ant.java b/core-java/src/main/java/com/baeldung/algorithms/ga/ant_colony/Ant.java new file mode 100644 index 0000000000..4ea23b799f --- /dev/null +++ b/core-java/src/main/java/com/baeldung/algorithms/ga/ant_colony/Ant.java @@ -0,0 +1,37 @@ +package com.baeldung.algorithms.ga.ant_colony; + +public class Ant { + + protected int trailSize; + protected int trail[]; + protected boolean visited[]; + + public Ant(int tourSize) { + this.trailSize = tourSize; + this.trail = new int[tourSize]; + this.visited = new boolean[tourSize]; + } + + protected void visitCity(int currentIndex, int city) { + trail[currentIndex + 1] = city; + visited[city] = true; + } + + protected boolean visited(int i) { + return visited[i]; + } + + protected double trailLength(double graph[][]) { + double length = graph[trail[trailSize - 1]][trail[0]]; + for (int i = 0; i < trailSize - 1; i++) { + length += graph[trail[i]][trail[i + 1]]; + } + return length; + } + + protected void clear() { + for (int i = 0; i < trailSize; i++) + visited[i] = false; + } + +} diff --git a/core-java/src/main/java/com/baeldung/algorithms/ga/ant_colony/AntColonyOptimization.java b/core-java/src/main/java/com/baeldung/algorithms/ga/ant_colony/AntColonyOptimization.java new file mode 100644 index 0000000000..e46ac77e84 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/algorithms/ga/ant_colony/AntColonyOptimization.java @@ -0,0 +1,212 @@ +package com.baeldung.algorithms.ga.ant_colony; + +import java.util.Arrays; +import java.util.Random; + +public class AntColonyOptimization { + + private double c = 1.0; + private double alpha = 1; + private double beta = 5; + private double evaporation = 0.5; + private double Q = 500; + private double antFactor = 0.8; + private double randomFactor = 0.01; + + private int maxIterations = 1000; + + public int numberOfCities; + public int numberOfAnts; + private double graph[][]; + private double trails[][]; + private Ant ants[]; + private Random random = new Random(); + private double probabilities[]; + + private int currentIndex; + + public int[] bestTourOrder; + public double bestTourLength; + + public AntColonyOptimization(int noOfCities) { + graph = generateRandomMatrix(noOfCities); + numberOfCities = graph.length; + numberOfAnts = (int) (numberOfCities * antFactor); + + trails = new double[numberOfCities][numberOfCities]; + probabilities = new double[numberOfCities]; + ants = new Ant[numberOfAnts]; + for (int j = 0; j < numberOfAnts; j++) { + ants[j] = new Ant(numberOfCities); + } + } + + /** + * Generate initial solution + * @param n + * @return + */ + public double[][] generateRandomMatrix(int n) { + double[][] randomMatrix = new double[n][n]; + random.setSeed(System.currentTimeMillis()); + for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) { + Integer r = random.nextInt(100) + 1; + randomMatrix[i][j] = Math.abs(r); + } + } + return randomMatrix; + } + + /** + * Perform ant optimization + * @return + */ + public int[] startAntOptimization() { + int[] finalResult = null; + for (int i = 1; i <= 3; i++) { + System.out.println("Attempt #" + i); + finalResult = solve(); + } + return finalResult; + } + + /** + * Use this method to run the main logic + * @return + */ + private int[] solve() { + setupAnts(); + clearTrails(); + int iteration = 0; + while (iteration < maxIterations) { + moveAnts(); + updateTrails(); + updateBest(); + iteration++; + } + System.out.println("Best tour length: " + (bestTourLength - numberOfCities)); + System.out.println("Best tour order: " + Arrays.toString(bestTourOrder)); + return bestTourOrder.clone(); + } + + /** + * Prepare ants for the simulation + */ + private void setupAnts() { + currentIndex = -1; + for (int i = 0; i < numberOfAnts; i++) { + ants[i].clear(); + ants[i].visitCity(currentIndex, random.nextInt(numberOfCities)); + } + currentIndex++; + } + + /** + * At each iteration, move ants + */ + private void moveAnts() { + while (currentIndex < numberOfCities - 1) { + for (Ant a : ants) + a.visitCity(currentIndex, selectNextCity(a)); + currentIndex++; + } + } + + /** + * Select next city for each ant + * @param ant + * @return + */ + private int selectNextCity(Ant ant) { + if (random.nextDouble() < randomFactor) { + int t = random.nextInt(numberOfCities - currentIndex); + int j = -1; + for (int i = 0; i < numberOfCities; i++) { + if (!ant.visited(i)) { + j++; + } + if (j == t) { + return i; + } + } + } + calculateProbabilities(ant); + double r = random.nextDouble(); + double total = 0; + for (int i = 0; i < numberOfCities; i++) { + total += probabilities[i]; + if (total >= r) { + return i; + } + } + + throw new RuntimeException("There are no other cities"); + } + + /** + * Calculate the next city picks probabilites + * @param ant + */ + private void calculateProbabilities(Ant ant) { + int i = ant.trail[currentIndex]; + double pheromone = 0.0; + for (int l = 0; l < numberOfCities; l++) { + if (!ant.visited(l)) { + pheromone += Math.pow(trails[i][l], alpha) * Math.pow(1.0 / graph[i][l], beta); + } + } + for (int j = 0; j < numberOfCities; j++) { + if (ant.visited(j)) { + probabilities[j] = 0.0; + } else { + double numerator = Math.pow(trails[i][j], alpha) * Math.pow(1.0 / graph[i][j], beta); + probabilities[j] = numerator / pheromone; + } + } + } + + /** + * Update trails that ants used + */ + private void updateTrails() { + for (int i = 0; i < numberOfCities; i++) { + for (int j = 0; j < numberOfCities; j++) { + trails[i][j] *= evaporation; + } + } + for (Ant a : ants) { + double contribution = Q / a.trailLength(graph); + for (int i = 0; i < numberOfCities - 1; i++) { + trails[a.trail[i]][a.trail[i + 1]] += contribution; + } + trails[a.trail[numberOfCities - 1]][a.trail[0]] += contribution; + } + } + + /** + * Update the best solution + */ + private void updateBest() { + if (bestTourOrder == null) { + bestTourOrder = ants[0].trail; + bestTourLength = ants[0].trailLength(graph); + } + for (Ant a : ants) { + if (a.trailLength(graph) < bestTourLength) { + bestTourLength = a.trailLength(graph); + bestTourOrder = a.trail.clone(); + } + } + } + + /** + * Clear trails after simulation + */ + private void clearTrails() { + for (int i = 0; i < numberOfCities; i++) + for (int j = 0; j < numberOfCities; j++) + trails[i][j] = c; + } + +} diff --git a/core-java/src/test/java/com/baeldung/algorithms/AntColonyOptimizationTest.java b/core-java/src/test/java/com/baeldung/algorithms/AntColonyOptimizationTest.java new file mode 100644 index 0000000000..cd8efaa106 --- /dev/null +++ b/core-java/src/test/java/com/baeldung/algorithms/AntColonyOptimizationTest.java @@ -0,0 +1,22 @@ +package com.baeldung.algorithms; + +import org.junit.Assert; +import org.junit.Test; + +import com.baeldung.algorithms.ga.ant_colony.AntColonyOptimization; + +public class AntColonyOptimizationTest { + + @Test + public void testGenerateRandomMatrix() { + AntColonyOptimization antTSP = new AntColonyOptimization(5); + Assert.assertNotNull(antTSP.generateRandomMatrix(5)); + } + + @Test + public void testStartAntOptimization() { + AntColonyOptimization antTSP = new AntColonyOptimization(5); + Assert.assertNotNull(antTSP.startAntOptimization()); + } + +} diff --git a/core-java/src/test/java/com/baeldung/algorithms/SimulatedAnnealingTest.java b/core-java/src/test/java/com/baeldung/algorithms/SimulatedAnnealingTest.java index 2fc7ea9b92..6822bae990 100644 --- a/core-java/src/test/java/com/baeldung/algorithms/SimulatedAnnealingTest.java +++ b/core-java/src/test/java/com/baeldung/algorithms/SimulatedAnnealingTest.java @@ -3,7 +3,7 @@ package com.baeldung.algorithms; import org.junit.Assert; import org.junit.Test; -import com.baeldung.algorithms.annealing.SimulatedAnnealing; +import com.baeldung.algorithms.ga.annealing.SimulatedAnnealing; public class SimulatedAnnealingTest { diff --git a/core-java/src/test/java/com/baeldung/list/listoflist/ListOfListsTest.java b/core-java/src/test/java/com/baeldung/list/listoflist/ListOfListsTest.java index ce24ff24bc..674a2f89bc 100644 --- a/core-java/src/test/java/com/baeldung/list/listoflist/ListOfListsTest.java +++ b/core-java/src/test/java/com/baeldung/list/listoflist/ListOfListsTest.java @@ -39,7 +39,8 @@ public class ListOfListsTest { @SuppressWarnings("unchecked") @Test public void givenListOfLists_whenRemovingElements_thenCheckNames() { - ((ArrayList) listOfLists.get(1)).remove(0); + + ((ArrayList) listOfLists.get(1)).remove(0); listOfLists.remove(1); assertEquals("Rubber 1", ((Rubber) listOfLists.get(1) .get(0)).getName()); @@ -47,4 +48,29 @@ public class ListOfListsTest { assertEquals("Rubber 1", ((Rubber) listOfLists.get(0) .get(0)).getName()); } + + @Test + public void givenThreeList_whenCombineIntoOneList_thenCheckList() { + ArrayList pens = new ArrayList<>(); + pens.add(new Pen("Pen 1")); + pens.add(new Pen("Pen 2")); + ArrayList pencils = new ArrayList<>(); + pencils.add(new Pencil("Pencil 1")); + pencils.add(new Pencil("Pencil 2")); + ArrayList rubbers = new ArrayList<>(); + rubbers.add(new Rubber("Rubber 1")); + rubbers.add(new Rubber("Rubber 2")); + + List> list = new ArrayList>(); + list.add(pens); + list.add(pencils); + list.add(rubbers); + + assertEquals("Pen 1", ((Pen) list.get(0) + .get(0)).getName()); + assertEquals("Pencil 1", ((Pencil) list.get(1) + .get(0)).getName()); + assertEquals("Rubber 1", ((Rubber) list.get(2) + .get(0)).getName()); + } } diff --git a/guava/src/test/java/org/baeldung/guava/GuavaReflectionUtilsTest.java b/guava/src/test/java/org/baeldung/guava/GuavaReflectionUtilsTest.java new file mode 100644 index 0000000000..e59a682e08 --- /dev/null +++ b/guava/src/test/java/org/baeldung/guava/GuavaReflectionUtilsTest.java @@ -0,0 +1,150 @@ +package org.baeldung.guava; + + +import com.google.common.collect.Lists; +import com.google.common.reflect.Invokable; +import com.google.common.reflect.TypeToken; +import org.junit.Test; + +import java.lang.reflect.Method; +import java.lang.reflect.Modifier; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.function.Function; + +import static junit.framework.TestCase.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +public class GuavaReflectionUtilsTest { + + @Test + public void givenTwoGenericList_whenCheckIsAssignableFrom_thenReturnTrueDueToTypeErasure() { + //given + ArrayList stringList = Lists.newArrayList(); + ArrayList intList = Lists.newArrayList(); + + //when + boolean result = stringList.getClass().isAssignableFrom(intList.getClass()); + + //then + assertTrue(result); + } + + @Test + public void givenTypeToken_whenResolveType_thenShouldResolveProperType() { + //given + TypeToken> stringListToken = new TypeToken>() { + }; + TypeToken> integerListToken = new TypeToken>() { + }; + TypeToken> numberTypeToken = new TypeToken>() { + }; + + //then + assertFalse(stringListToken.isSubtypeOf(integerListToken)); + assertFalse(numberTypeToken.isSubtypeOf(integerListToken)); + assertTrue(integerListToken.isSubtypeOf(numberTypeToken)); + } + + @Test + public void givenCustomClass_whenCaptureGeneric_thenReturnTypeAtRuntime() { + //given + ParametrizedClass parametrizedClass = new ParametrizedClass() { + }; + + //then + assertEquals(parametrizedClass.type, TypeToken.of(String.class)); + } + + @Test + public void givenComplexType_whenGetTypeArgument_thenShouldReturnTypeAtRuntime() { + //given + TypeToken> funToken = new TypeToken>() { + }; + + //when + TypeToken funResultToken = funToken.resolveType(Function.class.getTypeParameters()[1]); + + //then + assertEquals(funResultToken, TypeToken.of(String.class)); + } + + + @Test + public void givenMapType_whenGetTypeArgumentOfEntry_thenShouldReturnTypeAtRuntime() throws NoSuchMethodException { + //given + TypeToken> mapToken = new TypeToken>() { + }; + + //when + TypeToken entrySetToken = mapToken.resolveType(Map.class.getMethod("entrySet").getGenericReturnType()); + + //then + assertEquals(entrySetToken, new TypeToken>>() { + }); + } + + @Test + public void givenInvokable_whenCheckPublicMethod_shouldReturnTrue() throws NoSuchMethodException { + //given + Method method = CustomClass.class.getMethod("somePublicMethod"); + Invokable invokable = new TypeToken() { + }.method(method); + + //when + boolean isPublicStandradJava = Modifier.isPublic(method.getModifiers()); + boolean isPublicGuava = invokable.isPublic(); + //then + assertTrue(isPublicStandradJava); + assertTrue(isPublicGuava); + } + + @Test + public void givenInvokable_whenCheckFinalMethod_shouldReturnFalseForIsOverridable() throws NoSuchMethodException { + //given + Method method = CustomClass.class.getMethod("notOverridablePublicMethod"); + Invokable invokable = new TypeToken() { + }.method(method); + + //when + boolean isOverridableStandardJava = (!(Modifier.isFinal(method.getModifiers()) || Modifier.isPrivate(method.getModifiers()) + || Modifier.isStatic(method.getModifiers()) + || Modifier.isFinal(method.getDeclaringClass().getModifiers()))); + boolean isOverridableFinalGauava = invokable.isOverridable(); + + //then + assertFalse(isOverridableStandardJava); + assertFalse(isOverridableFinalGauava); + } + + @Test + public void givenListOfType_whenGetReturnRype_shouldCaptureTypeAtRuntime() throws NoSuchMethodException { + //given + Method getMethod = List.class.getMethod("get", int.class); + + //when + Invokable, ?> invokable = new TypeToken>() { + }.method(getMethod); + + //then + assertEquals(TypeToken.of(Integer.class), invokable.getReturnType()); // Not Object.class! + } + + + abstract class ParametrizedClass { + TypeToken type = new TypeToken(getClass()) { + }; + } + + class CustomClass { + public void somePublicMethod() { + } + + public final void notOverridablePublicMethod() { + + } + } +} diff --git a/guice-intro/pom.xml b/guice-intro/pom.xml new file mode 100644 index 0000000000..1f0d7679b7 --- /dev/null +++ b/guice-intro/pom.xml @@ -0,0 +1,34 @@ + + + 4.0.0 + com.baeldung.examples.guice + guice-intro + 1.0-SNAPSHOT + jar + + + com.google.inject + guice + ${guice.version} + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.8 + 1.8 + + + + + + UTF-8 + 1.8 + 1.8 + 4.1.0 + + guice-intro + diff --git a/guice-intro/src/main/java/com/baeldung/examples/RunGuice.java b/guice-intro/src/main/java/com/baeldung/examples/RunGuice.java new file mode 100644 index 0000000000..b4b3e8571e --- /dev/null +++ b/guice-intro/src/main/java/com/baeldung/examples/RunGuice.java @@ -0,0 +1,36 @@ + +package com.baeldung.examples; + +import com.baeldung.examples.guice.Communication; +import com.baeldung.examples.guice.binding.AOPModule; +import com.baeldung.examples.guice.modules.BasicModule; +import com.google.inject.Guice; +import com.google.inject.Injector; +import java.util.Scanner; + +/** + * + * @author Baeldung + */ +public class RunGuice { + + public static void main(String[] args) { + Injector injector = Guice.createInjector(new BasicModule(), new AOPModule()); + Communication comms = injector.getInstance(Communication.class); + Scanner scanner = new Scanner(System.in); + System.out.println("Enter your message to be sent; press Q to quit and P to print the message log"); + while (true) { + String input = scanner.nextLine(); + if (input.equalsIgnoreCase("q")) { + System.exit(0); + } + if (input.equalsIgnoreCase("p")) { + comms.print(); + } else { + comms.sendMessage(input); + } + + } + + } +} diff --git a/guice-intro/src/main/java/com/baeldung/examples/guice/Communication.java b/guice-intro/src/main/java/com/baeldung/examples/guice/Communication.java new file mode 100644 index 0000000000..c4b17b57d2 --- /dev/null +++ b/guice-intro/src/main/java/com/baeldung/examples/guice/Communication.java @@ -0,0 +1,57 @@ + +package com.baeldung.examples.guice; + +import com.google.inject.Inject; +import com.google.inject.name.Named; +import java.util.Date; +import java.util.LinkedList; +import java.util.Queue; +import java.util.logging.Logger; + +/** + * + * @author Baeldung + */ +public class Communication { + + final Date start = new Date(); + + @Inject + private Logger logger; + + private Queue messageLog; + + @Named("CommsUUID") + private String commsID; + + @Inject + private DefaultCommunicator communicator; + + public Communication(Boolean keepRecords) { + if (keepRecords) { + messageLog = new LinkedList(); + } + } + + public boolean sendMessage(String message) { + if (!message.isEmpty() && messageLog != null) { + messageLog.add(message); + } + return communicator.sendMessage(message); + } + + public void print() { + if (messageLog != null) { + for (String message : messageLog) { + logger.info(message); + } + } else { + logger.info("Message logging wasn't enabled"); + } + } + + public DefaultCommunicator getCommunicator() { + return this.communicator; + } + +} diff --git a/guice-intro/src/main/java/com/baeldung/examples/guice/CommunicationMode.java b/guice-intro/src/main/java/com/baeldung/examples/guice/CommunicationMode.java new file mode 100644 index 0000000000..444b775478 --- /dev/null +++ b/guice-intro/src/main/java/com/baeldung/examples/guice/CommunicationMode.java @@ -0,0 +1,12 @@ + +package com.baeldung.examples.guice; + +import com.baeldung.examples.guice.constant.CommunicationModel; + +public interface CommunicationMode { + + public CommunicationModel getMode(); + + public boolean sendMessage(String message); + +} diff --git a/guice-intro/src/main/java/com/baeldung/examples/guice/DefaultCommunicator.java b/guice-intro/src/main/java/com/baeldung/examples/guice/DefaultCommunicator.java new file mode 100644 index 0000000000..423c24f789 --- /dev/null +++ b/guice-intro/src/main/java/com/baeldung/examples/guice/DefaultCommunicator.java @@ -0,0 +1,48 @@ + +package com.baeldung.examples.guice; + +import com.baeldung.examples.guice.marker.Communicator; +import com.google.inject.Inject; +import com.google.inject.name.Named; + + +public class DefaultCommunicator implements Communicator { + + private CommunicationMode defaultCommsMode; + @Inject + @Named("SMSComms") + CommunicationMode smsCommsMode; + @Inject + @Named("EmailComms") + CommunicationMode emailCommsMode; + @Inject + @Named("IMComms") + CommunicationMode imCommsMode; + + protected DefaultCommunicator(CommunicationMode defaultComms) { + this.defaultCommsMode = defaultComms; + } + + public DefaultCommunicator() { + + } + + public boolean sendMessage(String message) { + boolean sent = false; + if (defaultCommsMode != null) { + sent = sendMessageByDefault(message); + } else { + sent = smsCommsMode.sendMessage(message); + } + return sent; + } + + private boolean sendMessageByDefault(String message) { + boolean sent = false; + if (message != null && !message.trim().equals("")) { + return defaultCommsMode.sendMessage(message); + } + return sent; + } + +} diff --git a/guice-intro/src/main/java/com/baeldung/examples/guice/EmailCommunicationMode.java b/guice-intro/src/main/java/com/baeldung/examples/guice/EmailCommunicationMode.java new file mode 100644 index 0000000000..642ee7ace0 --- /dev/null +++ b/guice-intro/src/main/java/com/baeldung/examples/guice/EmailCommunicationMode.java @@ -0,0 +1,23 @@ +package com.baeldung.examples.guice; + +import com.baeldung.examples.guice.aop.MessageSentLoggable; +import com.baeldung.examples.guice.constant.CommunicationModel; + +/** + * + * @author Baekdung + */ +public class EmailCommunicationMode implements CommunicationMode { + + @Override + public CommunicationModel getMode() { + return CommunicationModel.EMAIL; + } + + @Override + @MessageSentLoggable + public boolean sendMessage(String Message) { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + +} diff --git a/guice-intro/src/main/java/com/baeldung/examples/guice/IMCommunicationMode.java b/guice-intro/src/main/java/com/baeldung/examples/guice/IMCommunicationMode.java new file mode 100644 index 0000000000..9f34e9a241 --- /dev/null +++ b/guice-intro/src/main/java/com/baeldung/examples/guice/IMCommunicationMode.java @@ -0,0 +1,30 @@ + +package com.baeldung.examples.guice; + +import com.baeldung.examples.guice.aop.MessageSentLoggable; +import com.baeldung.examples.guice.constant.CommunicationModel; +import com.google.inject.Inject; +import java.util.logging.Logger; + +/** + * + * @author Baeldung + */ +public class IMCommunicationMode implements CommunicationMode { + + @Inject + private Logger logger; + + @Override + public CommunicationModel getMode() { + return CommunicationModel.IM; + } + + @Override + @MessageSentLoggable + public boolean sendMessage(String message) { + logger.info("IM Message Sent"); + return true; + } + +} diff --git a/guice-intro/src/main/java/com/baeldung/examples/guice/SMSCommunicationMode.java b/guice-intro/src/main/java/com/baeldung/examples/guice/SMSCommunicationMode.java new file mode 100644 index 0000000000..251e249971 --- /dev/null +++ b/guice-intro/src/main/java/com/baeldung/examples/guice/SMSCommunicationMode.java @@ -0,0 +1,29 @@ +package com.baeldung.examples.guice; + +import com.baeldung.examples.guice.aop.MessageSentLoggable; +import com.baeldung.examples.guice.constant.CommunicationModel; +import com.google.inject.Inject; +import java.util.logging.Logger; + +/** + * + * @author Baeldung + */ +public class SMSCommunicationMode implements CommunicationMode { + + @Inject + private Logger logger; + + @Override + public CommunicationModel getMode() { + return CommunicationModel.SMS; + } + + @Override + @MessageSentLoggable + public boolean sendMessage(String message) { + logger.info("SMS message sent"); + return true; + } + +} diff --git a/guice-intro/src/main/java/com/baeldung/examples/guice/aop/MessageLogger.java b/guice-intro/src/main/java/com/baeldung/examples/guice/aop/MessageLogger.java new file mode 100644 index 0000000000..8926dfa714 --- /dev/null +++ b/guice-intro/src/main/java/com/baeldung/examples/guice/aop/MessageLogger.java @@ -0,0 +1,22 @@ +package com.baeldung.examples.guice.aop; + +import java.util.logging.Logger; +import org.aopalliance.intercept.MethodInterceptor; +import org.aopalliance.intercept.MethodInvocation; + +/** + * + * @author Baeldung + */ +public class MessageLogger implements MethodInterceptor { + + @Override + public Object invoke(MethodInvocation invocation) throws Throwable { + Object[] objectArray = invocation.getArguments(); + int i = 0; + for (Object object : objectArray) { + Logger.getAnonymousLogger().info("Sending message: " + object.toString()); + } + return invocation.proceed(); + } +} diff --git a/guice-intro/src/main/java/com/baeldung/examples/guice/aop/MessageSentLoggable.java b/guice-intro/src/main/java/com/baeldung/examples/guice/aop/MessageSentLoggable.java new file mode 100644 index 0000000000..cacf3bde7c --- /dev/null +++ b/guice-intro/src/main/java/com/baeldung/examples/guice/aop/MessageSentLoggable.java @@ -0,0 +1,16 @@ +package com.baeldung.examples.guice.aop; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * + * @author Baeldung + */ +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.METHOD) +public @interface MessageSentLoggable { + +} diff --git a/guice-intro/src/main/java/com/baeldung/examples/guice/binding/AOPModule.java b/guice-intro/src/main/java/com/baeldung/examples/guice/binding/AOPModule.java new file mode 100644 index 0000000000..dc9d258efa --- /dev/null +++ b/guice-intro/src/main/java/com/baeldung/examples/guice/binding/AOPModule.java @@ -0,0 +1,22 @@ +package com.baeldung.examples.guice.binding; + +import com.baeldung.examples.guice.aop.MessageLogger; +import com.baeldung.examples.guice.aop.MessageSentLoggable; +import com.google.inject.AbstractModule; +import com.google.inject.matcher.Matchers; + +/** + * + * @author Baeldung + */ +public class AOPModule extends AbstractModule { + + @Override + protected void configure() { + bindInterceptor(Matchers.any(), + Matchers.annotatedWith(MessageSentLoggable.class), + new MessageLogger() + ); + } + +} diff --git a/guice-intro/src/main/java/com/baeldung/examples/guice/binding/BasicModule.java b/guice-intro/src/main/java/com/baeldung/examples/guice/binding/BasicModule.java new file mode 100644 index 0000000000..9168195130 --- /dev/null +++ b/guice-intro/src/main/java/com/baeldung/examples/guice/binding/BasicModule.java @@ -0,0 +1,36 @@ +package com.baeldung.examples.guice.binding; + +import com.baeldung.examples.guice.Communication; +import com.baeldung.examples.guice.CommunicationMode; +import com.baeldung.examples.guice.DefaultCommunicator; +import com.baeldung.examples.guice.EmailCommunicationMode; +import com.baeldung.examples.guice.IMCommunicationMode; +import com.baeldung.examples.guice.SMSCommunicationMode; +import com.google.inject.AbstractModule; +import com.google.inject.name.Names; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * + * @author Baeldung + */ +public class BasicModule extends AbstractModule { + + @Override + protected void configure() { + try { + bind(Communication.class).toConstructor(Communication.class.getConstructor(Boolean.TYPE)); + } catch (NoSuchMethodException ex) { + Logger.getLogger(BasicModule.class.getName()).log(Level.SEVERE, null, ex); + } catch (SecurityException ex) { + Logger.getLogger(BasicModule.class.getName()).log(Level.SEVERE, null, ex); + } + bind(DefaultCommunicator.class).annotatedWith(Names.named("AnotherCommunicator")).to(DefaultCommunicator.class).asEagerSingleton(); + + bind(CommunicationMode.class).annotatedWith(Names.named("IMComms")).to(IMCommunicationMode.class); + bind(CommunicationMode.class).annotatedWith(Names.named("EmailComms")).to(EmailCommunicationMode.class); + bind(CommunicationMode.class).annotatedWith(Names.named("SMSComms")).to(SMSCommunicationMode.class); + } + +} diff --git a/guice-intro/src/main/java/com/baeldung/examples/guice/constant/CommunicationModel.java b/guice-intro/src/main/java/com/baeldung/examples/guice/constant/CommunicationModel.java new file mode 100644 index 0000000000..b9fa604a32 --- /dev/null +++ b/guice-intro/src/main/java/com/baeldung/examples/guice/constant/CommunicationModel.java @@ -0,0 +1,16 @@ +package com.baeldung.examples.guice.constant; + +/** + * + * @author Baeldung + */ +public enum CommunicationModel { + + EMAIL("Email"), SMS("SMS"), IM("IM"), PHONE("Phone"); + + final String name; + + CommunicationModel(String name) { + this.name = name; + } +} diff --git a/guice-intro/src/main/java/com/baeldung/examples/guice/marker/Communicator.java b/guice-intro/src/main/java/com/baeldung/examples/guice/marker/Communicator.java new file mode 100644 index 0000000000..239666b6ab --- /dev/null +++ b/guice-intro/src/main/java/com/baeldung/examples/guice/marker/Communicator.java @@ -0,0 +1,11 @@ +package com.baeldung.examples.guice.marker; + +/** + * + * @author Baeldung + */ +public interface Communicator { + + public boolean sendMessage(String message); + +} diff --git a/guice-intro/src/main/java/com/baeldung/examples/guice/modules/BasicModule.java b/guice-intro/src/main/java/com/baeldung/examples/guice/modules/BasicModule.java new file mode 100644 index 0000000000..47b3e2e573 --- /dev/null +++ b/guice-intro/src/main/java/com/baeldung/examples/guice/modules/BasicModule.java @@ -0,0 +1,37 @@ +package com.baeldung.examples.guice.modules; + +import com.baeldung.examples.guice.Communication; +import com.baeldung.examples.guice.CommunicationMode; +import com.baeldung.examples.guice.DefaultCommunicator; +import com.baeldung.examples.guice.EmailCommunicationMode; +import com.baeldung.examples.guice.IMCommunicationMode; +import com.baeldung.examples.guice.SMSCommunicationMode; +import com.google.inject.AbstractModule; +import com.google.inject.name.Names; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * + * @author Baeldung + */ +public class BasicModule extends AbstractModule { + + @Override + protected void configure() { + try { + bind(Communication.class).toConstructor(Communication.class.getConstructor(Boolean.class)); + bind(Boolean.class).toInstance(true); + } catch (NoSuchMethodException ex) { + Logger.getLogger(com.baeldung.examples.guice.binding.BasicModule.class.getName()).log(Level.SEVERE, null, ex); + } catch (SecurityException ex) { + Logger.getLogger(com.baeldung.examples.guice.binding.BasicModule.class.getName()).log(Level.SEVERE, null, ex); + } + bind(DefaultCommunicator.class).annotatedWith(Names.named("AnotherCommunicator")).to(DefaultCommunicator.class).asEagerSingleton(); + + bind(CommunicationMode.class).annotatedWith(Names.named("IMComms")).to(IMCommunicationMode.class); + bind(CommunicationMode.class).annotatedWith(Names.named("EmailComms")).to(EmailCommunicationMode.class); + bind(CommunicationMode.class).annotatedWith(Names.named("SMSComms")).to(SMSCommunicationMode.class); + } + +} diff --git a/jasypt/pom.xml b/jasypt/pom.xml new file mode 100644 index 0000000000..7e0c51f0b9 --- /dev/null +++ b/jasypt/pom.xml @@ -0,0 +1,34 @@ + + + + parent-modules + com.baeldung + 1.0.0-SNAPSHOT + + 4.0.0 + + jasypt + + + + org.jasypt + jasypt + ${jasypt.version} + + + junit + junit + ${junit.version} + test + + + + + 1.9.2 + 4.12 + + + + \ No newline at end of file diff --git a/jasypt/src/test/java/org/baeldung/jasypt/JasyptTest.java b/jasypt/src/test/java/org/baeldung/jasypt/JasyptTest.java new file mode 100644 index 0000000000..c4bed5de83 --- /dev/null +++ b/jasypt/src/test/java/org/baeldung/jasypt/JasyptTest.java @@ -0,0 +1,98 @@ +package org.baeldung.jasypt; + + +import org.jasypt.encryption.pbe.PooledPBEStringEncryptor; +import org.jasypt.encryption.pbe.StandardPBEStringEncryptor; +import org.jasypt.util.password.BasicPasswordEncryptor; +import org.jasypt.util.text.BasicTextEncryptor; +import org.junit.Ignore; +import org.junit.Test; + +import static junit.framework.Assert.assertFalse; +import static junit.framework.Assert.assertNotSame; +import static junit.framework.Assert.assertTrue; +import static junit.framework.TestCase.assertEquals; + +public class JasyptTest { + + @Test + public void givenTextPassword_whenDecrypt_thenCompareToEncrypted() { + //given + BasicTextEncryptor textEncryptor = new BasicTextEncryptor(); + String password = "secret-pass"; + textEncryptor.setPasswordCharArray("some-random-password".toCharArray()); + + //when + String myEncryptedText = textEncryptor.encrypt(password); + assertNotSame(password, myEncryptedText); //myEncryptedText can be save in db + + //then + String plainText = textEncryptor.decrypt(myEncryptedText); + assertEquals(plainText, password); + } + + @Test + public void givenTextPassword_whenOneWayEncryption_thenCompareEncryptedPasswordsShouldBeSame(){ + String password = "secret-pass"; + BasicPasswordEncryptor passwordEncryptor = new BasicPasswordEncryptor(); + String encryptedPassword = passwordEncryptor.encryptPassword(password); + + //when + boolean result = passwordEncryptor.checkPassword("secret-pass", encryptedPassword); + + //then + assertTrue(result); + } + + @Test + public void givenTextPassword_whenOneWayEncryption_thenCompareEncryptedPasswordsShouldNotBeSame(){ + String password = "secret-pass"; + BasicPasswordEncryptor passwordEncryptor = new BasicPasswordEncryptor(); + String encryptedPassword = passwordEncryptor.encryptPassword(password); + + //when + boolean result = passwordEncryptor.checkPassword("secret-pass-not-same", encryptedPassword); + + //then + assertFalse(result); + } + + + + @Test + @Ignore("should have installed local_policy.jar") + public void givenTextPassword_whenDecrypt_thenCompareToEncryptedWithCustomAlgorithm() { + //given + StandardPBEStringEncryptor encryptor = new StandardPBEStringEncryptor(); + String password = "secret-pass"; + encryptor.setPassword("secret-pass"); + encryptor.setAlgorithm("PBEWithMD5AndTripleDES"); + + //when + String encryptedText = encryptor.encrypt("secret-pass"); + assertNotSame(password, encryptedText); + + //then + String plainText = encryptor.decrypt(encryptedText); + assertEquals(plainText, password); + } + + @Test + @Ignore("should have installed local_policy.jar") + public void givenTextPassword_whenDecryptOnHighPerformance_thenDecrypt(){ + //given + String password = "secret-pass"; + PooledPBEStringEncryptor encryptor = new PooledPBEStringEncryptor(); + encryptor.setPoolSize(4); + encryptor.setPassword(password); + encryptor.setAlgorithm("PBEWithMD5AndTripleDES"); + + //when + String encryptedText = encryptor.encrypt(password); + assertNotSame(password, encryptedText); + + //then + String plainText = encryptor.decrypt(encryptedText); + assertEquals(plainText, password); + } +} diff --git a/log4j2/src/test/java/com/baeldung/logging/log4j2/tests/CustomLoggingTest.java b/log4j2/src/test/java/com/baeldung/logging/log4j2/tests/CustomLoggingTest.java index 1562b67068..333b4f5e78 100644 --- a/log4j2/src/test/java/com/baeldung/logging/log4j2/tests/CustomLoggingTest.java +++ b/log4j2/src/test/java/com/baeldung/logging/log4j2/tests/CustomLoggingTest.java @@ -31,78 +31,85 @@ public class CustomLoggingTest { } @Test - public void givenLoggerWithDefaultConfig_shouldLogToConsole() throws Exception { + public void givenLoggerWithDefaultConfig_whenLogToConsole_thanOK() throws Exception { Logger logger = LogManager.getLogger(getClass()); Exception e = new RuntimeException("This is only a test!"); + logger.info("This is a simple message at INFO level. " + "It will be hidden."); logger.error("This is a simple message at ERROR level. " + "This is the minimum visible level.", e); } @Test - public void givenLoggerWithConsoleConfig_shouldLogToConsoleInColors() throws Exception { + public void givenLoggerWithConsoleConfig_whenLogToConsoleInColors_thanOK() throws Exception { Logger logger = LogManager.getLogger("CONSOLE_PATTERN_APPENDER_MARKER"); + Exception e = new RuntimeException("This is only a test!"); + logger.trace("This is a colored message at TRACE level."); logger.debug("This is a colored message at DEBUG level. " + "This is the minimum visible level."); logger.info("This is a colored message at INFO level."); logger.warn("This is a colored message at WARN level."); - Exception e = new RuntimeException("This is only a test!"); logger.error("This is a colored message at ERROR level.", e); logger.fatal("This is a colored message at FATAL level."); } @Test - public void givenLoggerWithConsoleConfig_shouldFilterByMarker() throws Exception { + public void givenLoggerWithConsoleConfig_whenFilterByMarker_thanOK() throws Exception { Logger logger = LogManager.getLogger("CONSOLE_PATTERN_APPENDER_MARKER"); Marker appError = MarkerManager.getMarker("APP_ERROR"); - logger.error(appError, "This marker message at ERROR level should be hidden."); Marker connectionTrace = MarkerManager.getMarker("CONN_TRACE"); + + logger.error(appError, "This marker message at ERROR level should be hidden."); logger.trace(connectionTrace, "This is a marker message at TRACE level."); } @Test - public void givenLoggerWithConsoleConfig_shouldFilterByThreadContext() throws Exception { + public void givenLoggerWithConsoleConfig_whenFilterByThreadContext_thanOK() throws Exception { Logger logger = LogManager.getLogger("CONSOLE_PATTERN_APPENDER_THREAD_CONTEXT"); ThreadContext.put("userId", "1000"); logger.info("This is a log-visible user login. Maybe from an admin account?"); ThreadContext.put("userId", "1001"); logger.info("This is a log-invisible user login."); - } @Test - public void givenLoggerWithAsyncConfig_shouldLogToJsonFile() throws Exception { + public void givenLoggerWithAsyncConfig_whenLogToJsonFile_thanOK() throws Exception { Logger logger = LogManager.getLogger("ASYNC_JSON_FILE_APPENDER"); + final int count = 88; for (int i = 0; i < count; i++) { logger.info("This is async JSON message #{} at INFO level.", count); } + long logEventsCount = Files.lines(Paths.get("target/logfile.json")) .count(); assertTrue(logEventsCount > 0 && logEventsCount <= count); } @Test - public void givenLoggerWithFailoverConfig_shouldLog() throws Exception { + public void givenLoggerWithFailoverConfig_whenLog_thanOK() throws Exception { Logger logger = LogManager.getLogger("FAIL_OVER_SYSLOG_APPENDER"); + Exception e = new RuntimeException("This is only a test!"); + logger.trace("This is a syslog message at TRACE level."); logger.debug("This is a syslog message at DEBUG level."); logger.info("This is a syslog message at INFO level. This is the minimum visible level."); logger.warn("This is a syslog message at WARN level."); - Exception e = new RuntimeException("This is only a test!"); logger.error("This is a syslog message at ERROR level.", e); logger.fatal("This is a syslog message at FATAL level."); } @Test - public void givenLoggerWithJdbcConfig_shouldLogToDataSource() throws Exception { + public void givenLoggerWithJdbcConfig_whenLogToDataSource_thanOK() throws Exception { Logger logger = LogManager.getLogger("JDBC_APPENDER"); + final int count = 88; for (int i = 0; i < count; i++) { logger.info("This is JDBC message #{} at INFO level.", count); } Connection connection = ConnectionFactory.getConnection(); ResultSet resultSet = connection.createStatement() - .executeQuery("SELECT COUNT(*) AS ROW_COUNT FROM logs"); + .executeQuery("SELECT COUNT(*) AS ROW_COUNT FROM logs"); + int logCount = 0; if (resultSet.next()) { logCount = resultSet.getInt("ROW_COUNT"); @@ -111,8 +118,9 @@ public class CustomLoggingTest { } @Test - public void givenLoggerWithRollingFileConfig_shouldLogToXMLFile() throws Exception { + public void givenLoggerWithRollingFileConfig_whenLogToXMLFile_thanOK() throws Exception { Logger logger = LogManager.getLogger("XML_ROLLING_FILE_APPENDER"); + final int count = 88; for (int i = 0; i < count; i++) { logger.info("This is rolling file XML message #{} at INFO level.", i); diff --git a/log4j2/src/test/resources/log4j2.xml b/log4j2/src/test/resources/log4j2.xml index 83c1184f1f..21fd1da446 100644 --- a/log4j2/src/test/resources/log4j2.xml +++ b/log4j2/src/test/resources/log4j2.xml @@ -19,12 +19,14 @@ - + @@ -53,9 +55,11 @@ + diff --git a/mockito2/.gitignore b/mockito2/.gitignore new file mode 100644 index 0000000000..7f300600e6 --- /dev/null +++ b/mockito2/.gitignore @@ -0,0 +1,14 @@ +*.class + +.settings +.project + +#folders# +/target +/src/main/webapp/WEB-INF/classes +*/META-INF/* + +# Packaged files # +*.jar +*.war +*.ear diff --git a/mockito2/README.md b/mockito2/README.md new file mode 100644 index 0000000000..587f1341bb --- /dev/null +++ b/mockito2/README.md @@ -0,0 +1,5 @@ +========= + +## Mockito 2 and Java 8 Tips + +Examples on how to leverage Java 8 new features with Mockito version 2 diff --git a/mockito2/pom.xml b/mockito2/pom.xml new file mode 100644 index 0000000000..e116647009 --- /dev/null +++ b/mockito2/pom.xml @@ -0,0 +1,98 @@ + + + 4.0.0 + com.baeldung + mockito2 + 0.0.1-SNAPSHOT + jar + mockito-2-with-java8 + + + + + junit + junit + ${junit.version} + test + + + + org.mockito + mockito-core + ${mockito.version} + test + + + + + + + maven-compiler-plugin + ${maven-compiler-plugin.version} + + 1.8 + 1.8 + + + + org.apache.maven.plugins + maven-surefire-plugin + ${maven-surefire-plugin.version} + + + **/*IntegrationTest.java + **/*LiveTest.java + + + + + + + + + integration + + + + org.apache.maven.plugins + maven-surefire-plugin + + + integration-test + + test + + + + **/*LiveTest.java + + + **/*IntegrationTest.java + + + + + + + json + + + + + + + + + + UTF-8 + + + 4.12 + 2.7.5 + + + 3.6.0 + 2.19.1 + + diff --git a/mockito2/src/main/java/com/baeldung/mockito/java8/JobPosition.java b/mockito2/src/main/java/com/baeldung/mockito/java8/JobPosition.java new file mode 100644 index 0000000000..c7712fa47e --- /dev/null +++ b/mockito2/src/main/java/com/baeldung/mockito/java8/JobPosition.java @@ -0,0 +1,20 @@ +package com.baeldung.mockito.java8; + +public class JobPosition { + private String title; + + public JobPosition() {} + + public JobPosition(String title) { + super(); + this.title = title; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } +} diff --git a/mockito2/src/main/java/com/baeldung/mockito/java8/JobService.java b/mockito2/src/main/java/com/baeldung/mockito/java8/JobService.java new file mode 100644 index 0000000000..85a59b18a4 --- /dev/null +++ b/mockito2/src/main/java/com/baeldung/mockito/java8/JobService.java @@ -0,0 +1,20 @@ +package com.baeldung.mockito.java8; + +import java.util.Optional; +import java.util.stream.Stream; + +public interface JobService { + Optional findCurrentJobPosition(Person person); + + default boolean assignJobPosition(Person person, JobPosition jobPosition) { + if (!findCurrentJobPosition(person).isPresent()) { + person.setCurrentJobPosition(jobPosition); + + return true; + } else { + return false; + } + } + + Stream listJobs(Person person); +} diff --git a/mockito2/src/main/java/com/baeldung/mockito/java8/Person.java b/mockito2/src/main/java/com/baeldung/mockito/java8/Person.java new file mode 100644 index 0000000000..4f7af49f9d --- /dev/null +++ b/mockito2/src/main/java/com/baeldung/mockito/java8/Person.java @@ -0,0 +1,28 @@ +package com.baeldung.mockito.java8; + +public class Person { + private String name; + private JobPosition currentJobPosition; + + public Person() {} + + public Person(String name) { + this.name = name; + } + + public JobPosition getCurrentJobPosition() { + return currentJobPosition; + } + + public void setCurrentJobPosition(JobPosition currentJobPosition) { + this.currentJobPosition = currentJobPosition; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/mockito2/src/main/java/com/baeldung/mockito/java8/UnemploymentService.java b/mockito2/src/main/java/com/baeldung/mockito/java8/UnemploymentService.java new file mode 100644 index 0000000000..5859ab31b0 --- /dev/null +++ b/mockito2/src/main/java/com/baeldung/mockito/java8/UnemploymentService.java @@ -0,0 +1,11 @@ +package com.baeldung.mockito.java8; + +import java.util.Optional; + +public interface UnemploymentService { + + boolean personIsEntitledToUnemploymentSupport(Person person); + + Optional searchJob(Person person, String searchString); + +} diff --git a/mockito2/src/main/java/com/baeldung/mockito/java8/UnemploymentServiceImpl.java b/mockito2/src/main/java/com/baeldung/mockito/java8/UnemploymentServiceImpl.java new file mode 100644 index 0000000000..6f189b46ea --- /dev/null +++ b/mockito2/src/main/java/com/baeldung/mockito/java8/UnemploymentServiceImpl.java @@ -0,0 +1,26 @@ +package com.baeldung.mockito.java8; + +import java.util.Optional; +import java.util.stream.Stream; + +public class UnemploymentServiceImpl implements UnemploymentService { + private final JobService jobService; + + public UnemploymentServiceImpl(JobService jobService) { + this.jobService = jobService; + } + + @Override + public boolean personIsEntitledToUnemploymentSupport(Person person) { + Optional optional = jobService.findCurrentJobPosition(person); + + return !optional.isPresent(); + } + + @Override + public Optional searchJob(Person person, String searchString) { + Stream stream = jobService.listJobs(person); + + return stream.filter((j) -> j.getTitle().contains(searchString)).findFirst(); + } +} diff --git a/mockito2/src/test/java/com/baeldung/mockito/java8/ArgumentMatcherWithLambdaUnitTest.java b/mockito2/src/test/java/com/baeldung/mockito/java8/ArgumentMatcherWithLambdaUnitTest.java new file mode 100644 index 0000000000..2efac513b7 --- /dev/null +++ b/mockito2/src/test/java/com/baeldung/mockito/java8/ArgumentMatcherWithLambdaUnitTest.java @@ -0,0 +1,44 @@ +package com.baeldung.mockito.java8; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.when; + +import java.util.Optional; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.ArgumentMatchers; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + + +public class ArgumentMatcherWithLambdaUnitTest { + + @InjectMocks + private UnemploymentServiceImpl unemploymentService; + + @Mock + private JobService jobService; + + @Test + public void whenPersonWithJob_thenIsNotEntitled() { + Person peter = new Person("Peter"); + Person linda = new Person("Linda"); + + JobPosition teacher = new JobPosition("Teacher"); + + when(jobService.findCurrentJobPosition( + ArgumentMatchers.argThat((p) -> p.getName().equals("Peter"))) + ).thenReturn(Optional.of(teacher)); + + assertTrue(unemploymentService.personIsEntitledToUnemploymentSupport(linda)); + assertFalse(unemploymentService.personIsEntitledToUnemploymentSupport(peter)); + } + + @Before + public void init() { + MockitoAnnotations.initMocks(this); + } +} diff --git a/mockito2/src/test/java/com/baeldung/mockito/java8/ArgumentMatcherWithoutLambdaUnitTest.java b/mockito2/src/test/java/com/baeldung/mockito/java8/ArgumentMatcherWithoutLambdaUnitTest.java new file mode 100644 index 0000000000..f4e8fb4cf5 --- /dev/null +++ b/mockito2/src/test/java/com/baeldung/mockito/java8/ArgumentMatcherWithoutLambdaUnitTest.java @@ -0,0 +1,55 @@ +package com.baeldung.mockito.java8; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.when; + +import java.util.Optional; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.ArgumentMatchers; +import org.mockito.InjectMocks; +import org.mockito.ArgumentMatcher; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + + +public class ArgumentMatcherWithoutLambdaUnitTest { + @InjectMocks + private UnemploymentServiceImpl unemploymentService; + + @Mock + private JobService jobService; + + @Test + public void whenPersonWithJob_thenIsNotEntitled() { + Person peter = new Person("Peter"); + Person linda = new Person("Linda"); + + JobPosition teacher = new JobPosition("Teacher"); + + when(jobService.findCurrentJobPosition( + ArgumentMatchers.argThat(new PeterArgumentMatcher())) + ).thenReturn(Optional.of(teacher)); + + assertTrue(unemploymentService.personIsEntitledToUnemploymentSupport(linda)); + assertFalse(unemploymentService.personIsEntitledToUnemploymentSupport(peter)); + } + + private class PeterArgumentMatcher implements ArgumentMatcher { + @Override + public boolean matches(Person p) { + + if (p.getName().equals("Peter")) { + return true; + } + return false; + } + } + + @Before + public void init() { + MockitoAnnotations.initMocks(this); + } +} diff --git a/mockito2/src/test/java/com/baeldung/mockito/java8/CustomAnswerWithLambdaUnitTest.java b/mockito2/src/test/java/com/baeldung/mockito/java8/CustomAnswerWithLambdaUnitTest.java new file mode 100644 index 0000000000..b4e5abdc6c --- /dev/null +++ b/mockito2/src/test/java/com/baeldung/mockito/java8/CustomAnswerWithLambdaUnitTest.java @@ -0,0 +1,45 @@ +package com.baeldung.mockito.java8; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.when; + +import java.util.stream.Stream; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +public class CustomAnswerWithLambdaUnitTest { + @InjectMocks + private UnemploymentServiceImpl unemploymentService; + + @Mock + private JobService jobService; + + @Test + public void whenPersonWithJobHistory_thenSearchReturnsValue() { + Person peter = new Person("Peter"); + + assertEquals("Teacher", unemploymentService.searchJob(peter, "").get().getTitle()); + } + + @Test + public void whenPersonWithNoJobHistory_thenSearchReturnsEmpty() { + Person linda = new Person("Linda"); + + assertFalse(unemploymentService.searchJob(linda, "").isPresent()); + } + + @Before + public void init() { + MockitoAnnotations.initMocks(this); + + when(jobService.listJobs(any(Person.class))).then((i) -> { + return ((Person) i.getArgument(0)).getName().equals("Peter") ? Stream.of(new JobPosition("Teacher")) : Stream.empty(); + }); + } +} diff --git a/mockito2/src/test/java/com/baeldung/mockito/java8/CustomAnswerWithoutLambdaUnitTest.java b/mockito2/src/test/java/com/baeldung/mockito/java8/CustomAnswerWithoutLambdaUnitTest.java new file mode 100644 index 0000000000..28922bb303 --- /dev/null +++ b/mockito2/src/test/java/com/baeldung/mockito/java8/CustomAnswerWithoutLambdaUnitTest.java @@ -0,0 +1,59 @@ +package com.baeldung.mockito.java8; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.when; + +import java.util.stream.Stream; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; + + +public class CustomAnswerWithoutLambdaUnitTest { + @InjectMocks + private UnemploymentServiceImpl unemploymentService; + + @Mock + private JobService jobService; + + @Test + public void whenPersonWithJobHistory_thenSearchReturnsValue() { + Person peter = new Person("Peter"); + + assertEquals("Teacher", unemploymentService.searchJob(peter, "").get().getTitle()); + } + + @Test + public void whenPersonWithNoJobHistory_thenSearchReturnsEmpty() { + Person linda = new Person("Linda"); + + assertFalse(unemploymentService.searchJob(linda, "").isPresent()); + } + + private class PersonAnswer implements Answer> { + @Override + public Stream answer(InvocationOnMock invocation) throws Throwable { + Person person = invocation.getArgument(0); + + if(person.getName().equals("Peter")) { + return Stream.builder().add(new JobPosition("Teacher")).build(); + } + + return Stream.empty(); + } + } + + @Before + public void init() { + MockitoAnnotations.initMocks(this); + + when(jobService.listJobs(any(Person.class))).then(new PersonAnswer()); + } +} diff --git a/mockito2/src/test/java/com/baeldung/mockito/java8/JobServiceUnitTest.java b/mockito2/src/test/java/com/baeldung/mockito/java8/JobServiceUnitTest.java new file mode 100644 index 0000000000..9ea5c1db47 --- /dev/null +++ b/mockito2/src/test/java/com/baeldung/mockito/java8/JobServiceUnitTest.java @@ -0,0 +1,44 @@ +package com.baeldung.mockito.java8; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.doCallRealMethod; +import static org.mockito.Mockito.when; + +import java.util.Optional; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; + +public class JobServiceUnitTest { + @Mock + private JobService jobService; + + @Test + public void givenDefaultMethod_whenCallRealMethod_thenNoExceptionIsRaised() { + Person person = new Person(); + + when(jobService.findCurrentJobPosition(person)).thenReturn(Optional.of(new JobPosition())); + doCallRealMethod().when(jobService).assignJobPosition(Mockito.any(Person.class), Mockito.any(JobPosition.class)); + + assertFalse(jobService.assignJobPosition(person, new JobPosition())); + } + + @Test + public void givenReturnIsOfTypeOptional_whenDefaultValueIsReturned_thenValueIsEmpty() { + Person person = new Person(); + + when(jobService.findCurrentJobPosition(person)).thenReturn(Optional.empty()); + doCallRealMethod().when(jobService).assignJobPosition(Mockito.any(Person.class), Mockito.any(JobPosition.class)); + + assertTrue(jobService.assignJobPosition(person, new JobPosition())); + } + + @Before + public void init() { + MockitoAnnotations.initMocks(this); + } +} diff --git a/mockito2/src/test/java/com/baeldung/mockito/java8/UnemploymentServiceImplUnitTest.java b/mockito2/src/test/java/com/baeldung/mockito/java8/UnemploymentServiceImplUnitTest.java new file mode 100644 index 0000000000..b3b71e5bf9 --- /dev/null +++ b/mockito2/src/test/java/com/baeldung/mockito/java8/UnemploymentServiceImplUnitTest.java @@ -0,0 +1,62 @@ +package com.baeldung.mockito.java8; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.when; + +import java.util.Optional; +import java.util.stream.Stream; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +public class UnemploymentServiceImplUnitTest { + @Mock + private JobService jobService; + + @InjectMocks + private UnemploymentServiceImpl unemploymentService; + + @Test + public void givenReturnIsOfTypeOptional_whenMocked_thenValueIsEmpty() { + Person person = new Person(); + + when(jobService.findCurrentJobPosition(any(Person.class))).thenReturn(Optional.empty()); + + assertTrue(unemploymentService.personIsEntitledToUnemploymentSupport(person)); + } + + @Test + public void givenReturnIsOfTypeOptional_whenDefaultValueIsReturned_thenValueIsEmpty() { + Person person = new Person(); + + // This will fail when Mockito 1 is used + assertTrue(unemploymentService.personIsEntitledToUnemploymentSupport(person)); + } + + @Test + public void givenReturnIsOfTypeStream_whenMocked_thenValueIsEmpty() { + Person person = new Person(); + + when(jobService.listJobs(any(Person.class))).thenReturn(Stream.empty()); + + assertFalse(unemploymentService.searchJob(person, "").isPresent()); + } + + @Test + public void givenReturnIsOfTypeStream_whenDefaultValueIsReturned_thenValueIsEmpty() { + Person person = new Person(); + + // This will fail when Mockito 1 is used + assertFalse(unemploymentService.searchJob(person, "").isPresent()); + } + + @Before + public void init() { + MockitoAnnotations.initMocks(this); + } +} diff --git a/pom.xml b/pom.xml index 014e4016c5..72099fc584 100644 --- a/pom.xml +++ b/pom.xml @@ -46,6 +46,7 @@ guava guava18 guava19 + guice-intro disruptor handling-spring-static-resources @@ -61,6 +62,7 @@ javaslang javax-servlets javaxval + jasypt jaxb jee7 jjwt @@ -84,6 +86,7 @@ metrics mesos-marathon mockito + mockito2 mocks orika @@ -111,7 +114,6 @@ spring-autowire spring-batch spring-boot - spring-boot-servlet spring-cloud-data-flow spring-cloud spring-core diff --git a/reactor-core/pom.xml b/reactor-core/pom.xml index 2be8892983..3aeb4af3d5 100644 --- a/reactor-core/pom.xml +++ b/reactor-core/pom.xml @@ -50,7 +50,7 @@ - 3.0.4.RELEASE + 3.0.5.RELEASE 4.12 3.6.1 1.1.3 diff --git a/solr-fulltext-search/pom.xml b/solr-fulltext-search/pom.xml index 4afcb5838a..bed6afd48f 100644 --- a/solr-fulltext-search/pom.xml +++ b/solr-fulltext-search/pom.xml @@ -18,7 +18,7 @@ org.apache.solr solr-solrj - 6.1.0 + 6.4.1 log4j diff --git a/solr-fulltext-search/src/main/java/com/baeldung/solr/fulltext/search/model/Item.java b/solr-fulltext-search/src/main/java/com/baeldung/solr/fulltext/search/model/Item.java new file mode 100644 index 0000000000..d7a0524ca9 --- /dev/null +++ b/solr-fulltext-search/src/main/java/com/baeldung/solr/fulltext/search/model/Item.java @@ -0,0 +1,51 @@ +package com.baeldung.solr.fulltext.search.model; + +import org.apache.solr.client.solrj.beans.Field; + +public class Item { + + @Field + private String id; + + @Field + private String description; + + @Field + private String category; + + @Field + private float price; + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public String getCategory() { + return category; + } + + public void setCategory(String category) { + this.category = category; + } + + public float getPrice() { + return price; + } + + public void setPrice(float price) { + this.price = price; + } + +} diff --git a/solr-fulltext-search/src/main/java/com/baeldung/solr/fulltext/search/service/ItemSearchService.java b/solr-fulltext-search/src/main/java/com/baeldung/solr/fulltext/search/service/ItemSearchService.java new file mode 100644 index 0000000000..fa906fc975 --- /dev/null +++ b/solr-fulltext-search/src/main/java/com/baeldung/solr/fulltext/search/service/ItemSearchService.java @@ -0,0 +1,15 @@ +package com.baeldung.solr.fulltext.search.service; + +import java.io.IOException; + +import org.apache.solr.client.solrj.SolrServerException; + +import com.baeldung.solr.fulltext.search.model.Item; + +public interface ItemSearchService { + + public void index(String id, String description, String category, float price) throws SolrServerException, IOException; + + public void indexBean(Item item) throws IOException, SolrServerException; + +} diff --git a/solr-fulltext-search/src/main/java/com/baeldung/solr/fulltext/search/service/ItemSearchServiceImpl.java b/solr-fulltext-search/src/main/java/com/baeldung/solr/fulltext/search/service/ItemSearchServiceImpl.java new file mode 100644 index 0000000000..573cc58bc0 --- /dev/null +++ b/solr-fulltext-search/src/main/java/com/baeldung/solr/fulltext/search/service/ItemSearchServiceImpl.java @@ -0,0 +1,34 @@ +package com.baeldung.solr.fulltext.search.service; + +import java.io.IOException; + +import org.apache.solr.client.solrj.SolrClient; +import org.apache.solr.client.solrj.SolrServerException; +import org.apache.solr.common.SolrInputDocument; + +import com.baeldung.solr.fulltext.search.model.Item; + +public class ItemSearchServiceImpl implements ItemSearchService { + + private final SolrClient solrClient; + + public ItemSearchServiceImpl(SolrClient solrClient) { + this.solrClient = solrClient; + } + + public void index(String id, String description, String category, float price) throws SolrServerException, IOException { + SolrInputDocument doc = new SolrInputDocument(); + doc.addField("id", id); + doc.addField("description", description); + doc.addField("category", category); + doc.addField("price", price); + solrClient.add(doc); + solrClient.commit(); + } + + public void indexBean(Item item) throws IOException, SolrServerException { + solrClient.addBean(item); + solrClient.commit(); + } + +} diff --git a/solr-fulltext-search/src/test/java/com/baeldung/solr/fulltext/search/service/ItemSearchServiceIntegrationTest.java b/solr-fulltext-search/src/test/java/com/baeldung/solr/fulltext/search/service/ItemSearchServiceIntegrationTest.java new file mode 100644 index 0000000000..94661ffc2e --- /dev/null +++ b/solr-fulltext-search/src/test/java/com/baeldung/solr/fulltext/search/service/ItemSearchServiceIntegrationTest.java @@ -0,0 +1,374 @@ +package com.baeldung.solr.fulltext.search.service; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + +import java.util.List; +import java.util.Map; + +import org.apache.solr.client.solrj.SolrClient; +import org.apache.solr.client.solrj.SolrQuery; +import org.apache.solr.client.solrj.impl.HttpSolrClient; +import org.apache.solr.client.solrj.response.FacetField.Count; +import org.apache.solr.client.solrj.response.QueryResponse; +import org.apache.solr.client.solrj.response.RangeFacet; +import org.apache.solr.client.solrj.response.SpellCheckResponse; +import org.apache.solr.client.solrj.response.SpellCheckResponse.Suggestion; +import org.apache.solr.client.solrj.response.SuggesterResponse; +import org.apache.solr.common.SolrDocument; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +import com.baeldung.solr.fulltext.search.model.Item; + +public class ItemSearchServiceIntegrationTest { + + private static SolrClient solrClient; + private static ItemSearchService itemSearchService; + private static final String solrUrl = "http://localhost:8987/solr/item"; + + @BeforeClass + public static void initBeans() throws Exception { + solrClient = new HttpSolrClient.Builder(solrUrl).build(); + itemSearchService = new ItemSearchServiceImpl(solrClient); + + solrClient.commit(); + } + + @Before + public void clearSolrData() throws Exception { + solrClient.deleteByQuery("*:*"); + } + + @Test + public void whenIndexing_thenAvailableOnRetrieval() throws Exception { + itemSearchService.index("hm0001", "Washing Machine", "Home Appliances", 450f); + final SolrDocument indexedDoc = solrClient.getById("hm0001"); + assertEquals("hm0001", indexedDoc.get("id")); + } + + @Test + public void whenIndexingBean_thenAvailableOnRetrieval() throws Exception { + Item item = new Item(); + item.setId("hm0002"); + item.setCategory("Televisions"); + item.setDescription("LED TV 32"); + item.setPrice(500); + itemSearchService.indexBean(item); + } + + @Test + public void whenSearchingByBasicQuery_thenAllMatchingItemsShouldAvialble() throws Exception { + itemSearchService.index("hm0001", "Brand1 Washing Machine", "Home Appliances", 450f); + itemSearchService.index("hm0002", "Brand1 Refrigerator", "Home Appliances", 450f); + itemSearchService.index("hm0003", "LED TV 32", "Brand1 Home Appliances", 450f); + + SolrQuery query = new SolrQuery(); + query.setQuery("brand1"); + query.setStart(0); + query.setRows(10); + + QueryResponse response = solrClient.query(query); + List items = response.getBeans(Item.class); + + assertEquals(3, items.size()); + + } + + @Test + public void whenSearchingWithWildCard_thenAllMatchingItemsShouldAvialble() throws Exception { + itemSearchService.index("hm0001", "Brand1 Washing Machine", "Home Appliances", 450f); + itemSearchService.index("hm0002", "Brand1 Refrigerator", "Home Appliances", 450f); + itemSearchService.index("hm0003", "LED TV 32", "Brand1 Home Appliances", 450f); + + SolrQuery query = new SolrQuery(); + query.setQuery("*rand?"); + + QueryResponse response = solrClient.query(query); + List items = response.getBeans(Item.class); + + assertEquals(3, items.size()); + } + + @Test + public void whenSearchingWithLogicalOperators_thenAllMatchingItemsShouldAvialble() throws Exception { + itemSearchService.index("hm0001", "Brand1 Washing Machine", "Home Appliances", 450f); + itemSearchService.index("hm0002", "Brand1 Refrigerator", "Home Appliances", 450f); + itemSearchService.index("hm0003", "Brand2 LED TV 32", "Washing Appliances", 450f); + + SolrQuery query = new SolrQuery(); + query.setQuery("brand1 AND (Washing OR Refrigerator)"); + + QueryResponse response = solrClient.query(query); + List items = response.getBeans(Item.class); + + assertEquals(2, items.size()); + } + + @Test + public void whenSearchingWithFields_thenAllMatchingItemsShouldAvialble() throws Exception { + itemSearchService.index("0001", "Brand1 Washing Machine", "Home Appliances", 450f); + itemSearchService.index("0002", "Brand1 Refrigerator", "Home Appliances", 450f); + itemSearchService.index("0003", "Brand2 LED TV 32", "Brand1 Washing Home Appliances", 450f); + + SolrQuery query = new SolrQuery(); + query.setQuery("description:Brand* AND category:*Washing*"); + + QueryResponse response = solrClient.query(query); + List items = response.getBeans(Item.class); + + assertEquals(1, items.size()); + } + + @Test + public void whenSearchingWithPhrase_thenAllMatchingItemsShouldAvialble() throws Exception { + itemSearchService.index("hm0001", "Brand1 Washing Machine", "Home Appliances", 450f); + itemSearchService.index("hm0002", "Brand1 Refrigerator", "Home Appliances", 450f); + itemSearchService.index("hm0003", "Brand2 Dishwasher", "Washing tools and equipment ", 450f); + + SolrQuery query = new SolrQuery(); + query.setQuery("washing MachIne"); + + QueryResponse response = solrClient.query(query); + List items = response.getBeans(Item.class); + + assertEquals(2, items.size()); + } + + @Test + public void whenSearchingWithRealPhrase_thenAllMatchingItemsShouldAvialble() throws Exception { + itemSearchService.index("hm0001", "Brand1 Washing Machine", "Home Appliances", 450f); + itemSearchService.index("hm0002", "Brand1 Refrigerator", "Home Appliances", 450f); + itemSearchService.index("hm0003", "Brand2 Dishwasher", "Washing tools and equipment ", 450f); + + SolrQuery query = new SolrQuery(); + query.setQuery("\"washing machine\""); + + QueryResponse response = solrClient.query(query); + List items = response.getBeans(Item.class); + + assertEquals(1, items.size()); + } + + @Test + public void whenSearchingPhraseWithProximity_thenAllMatchingItemsShouldAvialble() throws Exception { + itemSearchService.index("hm0001", "Brand1 Washing Machine", "Home Appliances", 450f); + itemSearchService.index("hm0002", "Brand1 Refrigerator", "Home Appliances", 450f); + itemSearchService.index("hm0003", "Brand2 Dishwasher", "Washing tools and equipment ", 450f); + + SolrQuery query = new SolrQuery(); + query.setQuery("\"Washing equipment\"~2"); + + QueryResponse response = solrClient.query(query); + List items = response.getBeans(Item.class); + + assertEquals(1, items.size()); + } + + @Test + public void whenSearchingWithPriceRange_thenAllMatchingItemsShouldAvialble() throws Exception { + itemSearchService.index("hm0001", "Brand1 Washing Machine", "Home Appliances", 100f); + itemSearchService.index("hm0002", "Brand1 Refrigerator", "Home Appliances", 300f); + itemSearchService.index("hm0003", "Brand2 Dishwasher", "Home Appliances", 200f); + itemSearchService.index("hm0004", "Brand2 Dishwasher", "Washing tools and equipment ", 450f); + + SolrQuery query = new SolrQuery(); + query.setQuery("price:[100 TO 300]"); + + QueryResponse response = solrClient.query(query); + List items = response.getBeans(Item.class); + + assertEquals(3, items.size()); + } + + @Test + public void whenSearchingWithPriceRangeInclusiveExclusive_thenAllMatchingItemsShouldAvialble() throws Exception { + itemSearchService.index("hm0001", "Brand1 Washing Machine", "Home Appliances", 100f); + itemSearchService.index("hm0002", "Brand1 Refrigerator", "Home Appliances", 300f); + itemSearchService.index("hm0003", "Brand2 Dishwasher", "Home Appliances", 200f); + itemSearchService.index("hm0004", "Brand2 Dishwasher", "Washing tools and equipment ", 450f); + + SolrQuery query = new SolrQuery(); + query.setQuery("price:{100 TO 300]"); + + QueryResponse response = solrClient.query(query); + List items = response.getBeans(Item.class); + + assertEquals(2, items.size()); + } + + @Test + public void whenSearchingWithFilterQuery_thenAllMatchingItemsShouldAvialble() throws Exception { + itemSearchService.index("hm0001", "Brand1 Washing Machine", "Home Appliances", 100f); + itemSearchService.index("hm0002", "Brand1 Refrigerator", "Home Appliances", 300f); + itemSearchService.index("hm0003", "Brand2 Ceiling Fan", "Home Appliances", 200f); + itemSearchService.index("hm0004", "Brand2 Dishwasher", "Washing tools and equipment ", 250f); + + SolrQuery query = new SolrQuery(); + query.setQuery("price:[100 TO 300]"); + query.addFilterQuery("description:Brand1", "category:Home Appliances"); + + QueryResponse response = solrClient.query(query); + List items = response.getBeans(Item.class); + + assertEquals(2, items.size()); + } + + @Test + public void whenSearchingWithFacetFields_thenAllMatchingFacetsShouldAvialble() throws Exception { + itemSearchService.index("hm0001", "Brand1 Washing Machine", "CategoryA", 100f); + itemSearchService.index("hm0002", "Brand1 Refrigerator", "CategoryA", 300f); + itemSearchService.index("hm0003", "Brand2 Ceiling Fan", "CategoryB", 200f); + itemSearchService.index("hm0004", "Brand2 Dishwasher", "CategoryB", 250f); + + SolrQuery query = new SolrQuery(); + query.setQuery("*:*"); + query.addFacetField("category"); + + QueryResponse response = solrClient.query(query); + List facetResults = response.getFacetField("category").getValues(); + + assertEquals(2, facetResults.size()); + + for (Count count : facetResults) { + if ("categorya".equalsIgnoreCase(count.getName())) { + assertEquals(2, count.getCount()); + } else if ("categoryb".equalsIgnoreCase(count.getName())) { + assertEquals(2, count.getCount()); + } else { + fail("unexpected category"); + } + } + } + + @Test + public void whenSearchingWithFacetQuery_thenAllMatchingFacetsShouldAvialble() throws Exception { + itemSearchService.index("hm0001", "Brand1 Washing Machine", "CategoryA", 100f); + itemSearchService.index("hm0002", "Brand1 Refrigerator", "CategoryA", 300f); + itemSearchService.index("hm0003", "Brand2 Ceiling Fan", "CategoryB", 200f); + itemSearchService.index("hm0004", "Brand2 Dishwasher", "CategoryB", 250f); + + SolrQuery query = new SolrQuery(); + query.setQuery("*:*"); + + query.addFacetQuery("Washing OR Refrigerator"); + query.addFacetQuery("Brand2"); + + QueryResponse response = solrClient.query(query); + Map facetQueryMap = response.getFacetQuery(); + + assertEquals(2, facetQueryMap.size()); + + for (Map.Entry entry : facetQueryMap.entrySet()) { + String facetQuery = entry.getKey(); + + if ("Washing OR Refrigerator".equals(facetQuery)) { + assertEquals(Integer.valueOf(2), entry.getValue()); + } else if ("Brand2".equals(facetQuery)) { + assertEquals(Integer.valueOf(2), entry.getValue()); + } else { + fail("unexpected query"); + } + + } + } + + @Test + public void whenSearchingWithFacetRange_thenAllMatchingFacetsShouldAvialble() throws Exception { + itemSearchService.index("hm0001", "Brand1 Washing Machine", "CategoryA", 100f); + itemSearchService.index("hm0002", "Brand1 Refrigerator", "CategoryA", 125f); + itemSearchService.index("hm0003", "Brand2 Ceiling Fan", "CategoryB", 150f); + itemSearchService.index("hm0004", "Brand2 Dishwasher", "CategoryB", 250f); + + SolrQuery query = new SolrQuery(); + query.setQuery("*:*"); + + query.addNumericRangeFacet("price", 100, 275, 25); + + QueryResponse response = solrClient.query(query); + List rangeFacets = response.getFacetRanges().get(0).getCounts(); + + assertEquals(7, rangeFacets.size()); + } + + @Test + public void whenSearchingWithHitHighlighting_thenKeywordsShouldBeHighlighted() throws Exception { + itemSearchService.index("hm0001", "Brand1 Washing Machine", "Home Appliances", 100f); + itemSearchService.index("hm0002", "Brand1 Refrigerator", "Home Appliances", 300f); + itemSearchService.index("hm0003", "Brand2 Ceiling Fan", "Home Appliances", 200f); + itemSearchService.index("hm0004", "Brand2 Dishwasher", "Washing equipments", 250f); + + SolrQuery query = new SolrQuery(); + query.setQuery("Appliances"); + query.setHighlight(true); + query.addHighlightField("category"); + query.setHighlightSimplePre(""); + query.setHighlightSimplePost(""); + QueryResponse response = solrClient.query(query); + + Map>> hitHighlightedMap = response.getHighlighting(); + Map> highlightedFieldMap = hitHighlightedMap.get("hm0001"); + List highlightedList = highlightedFieldMap.get("category"); + String highLightedText = highlightedList.get(0); + + assertEquals("Home Appliances", highLightedText); + + } + + @Test + public void whenSearchingWithKeywordWithMistake_thenSpellingSuggestionsShouldBeReturned() throws Exception { + itemSearchService.index("hm0001", "Brand1 Washing Machine", "Home Appliances", 100f); + itemSearchService.index("hm0002", "Brand1 Refrigerator", "Home Appliances", 300f); + itemSearchService.index("hm0003", "Brand2 Ceiling Fan", "Home Appliances", 200f); + itemSearchService.index("hm0004", "Brand2 Dishwasher", "Washing equipments", 250f); + + SolrQuery query = new SolrQuery(); + query.setQuery("hme"); + query.set("spellcheck", "on"); + QueryResponse response = solrClient.query(query); + + SpellCheckResponse spellCheckResponse = response.getSpellCheckResponse(); + + assertEquals(false, spellCheckResponse.isCorrectlySpelled()); + + Suggestion suggestion = spellCheckResponse.getSuggestions().get(0); + + assertEquals("hme", suggestion.getToken()); + + List alternatives = suggestion.getAlternatives(); + String alternative = alternatives.get(0); + + assertEquals("home", alternative); + } + + @Test + public void whenSearchingWithIncompleteKeyword_thenKeywordSuggestionsShouldBeReturned() throws Exception { + itemSearchService.index("hm0001", "Brand1 Washing Machine", "Home Appliances", 100f); + itemSearchService.index("hm0002", "Brand1 Refrigerator", "Home Appliances", 300f); + itemSearchService.index("hm0003", "Brand2 Ceiling Fan", "Home Appliances", 200f); + itemSearchService.index("hm0004", "Brand2 Dishwasher", "Home washing equipments", 250f); + + SolrQuery query = new SolrQuery(); + query.setRequestHandler("/suggest"); + query.set("suggest", "true"); + query.set("suggest.build", "true"); + query.set("suggest.dictionary", "mySuggester"); + query.set("suggest.q", "Hom"); + QueryResponse response = solrClient.query(query); + + SuggesterResponse suggesterResponse = response.getSuggesterResponse(); + Map> suggestedTerms = suggesterResponse.getSuggestedTerms(); + List suggestions = suggestedTerms.get("mySuggester"); + + assertEquals(2, suggestions.size()); + + for (String term : suggestions) { + if (!"Home Appliances".equals(term) && !"Home washing equipments".equals(term)) { + fail("Unexpected suggestions"); + } + } + + } + +} diff --git a/spring-boot-servlet/.gitignore b/spring-boot-auditing/.gitignore similarity index 78% rename from spring-boot-servlet/.gitignore rename to spring-boot-auditing/.gitignore index 60be5b80aa..31ce405488 100644 --- a/spring-boot-servlet/.gitignore +++ b/spring-boot-auditing/.gitignore @@ -2,3 +2,5 @@ .settings/ .classpath .project +*.iml +.idea \ No newline at end of file diff --git a/spring-boot-auditing/pom.xml b/spring-boot-auditing/pom.xml new file mode 100644 index 0000000000..9307db39ee --- /dev/null +++ b/spring-boot-auditing/pom.xml @@ -0,0 +1,198 @@ + + 4.0.0 + com.baeldung + spring-boot-auditing + 0.0.1-SNAPSHOT + war + spring-boot-auditing + This is simple boot application for Spring boot auditing test + + + + org.springframework.boot + spring-boot-starter-parent + 1.5.1.RELEASE + + + + + + org.springframework.boot + spring-boot-starter-thymeleaf + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-data-jpa + + + + org.springframework.boot + spring-boot-starter-actuator + + + + org.springframework.boot + spring-boot-starter-security + + + + io.dropwizard.metrics + metrics-core + + + + com.h2database + h2 + + + + org.springframework.boot + spring-boot-starter-test + test + + + + org.springframework.boot + spring-boot-starter + + + com.jayway.jsonpath + json-path + test + + + org.springframework.boot + spring-boot-starter-mail + + + org.subethamail + subethasmtp + ${subethasmtp.version} + test + + + + org.webjars + bootstrap + ${bootstrap.version} + + + org.webjars + jquery + ${jquery.version} + + + + org.apache.tomcat + tomcat-servlet-api + ${tomee-servlet-api.version} + provided + + + + + + spring-boot + + + src/main/resources + true + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.8 + 1.8 + + + + + org.apache.maven.plugins + maven-war-plugin + + + + pl.project13.maven + git-commit-id-plugin + ${git-commit-id-plugin.version} + + + + org.apache.maven.plugins + maven-surefire-plugin + + + **/*IntegrationTest.java + **/*LiveTest.java + + + + + + + + + + + integration + + + + org.apache.maven.plugins + maven-surefire-plugin + + + integration-test + + test + + + + **/*LiveTest.java + + + **/*IntegrationTest.java + + + + + + + json + + + + + + + + + + + UTF-8 + 1.8 + 4.3.4.RELEASE + 2.2.1 + 3.1.1 + 3.3.7-1 + 3.1.7 + 8.5.11 + + + diff --git a/spring-boot-auditing/src/main/java/org/baeldung/Application.java b/spring-boot-auditing/src/main/java/org/baeldung/Application.java new file mode 100755 index 0000000000..bf7b7bd1a6 --- /dev/null +++ b/spring-boot-auditing/src/main/java/org/baeldung/Application.java @@ -0,0 +1,13 @@ +package org.baeldung; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class Application { + + public static void main(String[] args) throws Throwable { + SpringApplication.run(Application.class, args); + } + +} diff --git a/spring-boot-auditing/src/main/java/org/baeldung/MvcConfig.java b/spring-boot-auditing/src/main/java/org/baeldung/MvcConfig.java new file mode 100755 index 0000000000..fecb8c5c0b --- /dev/null +++ b/spring-boot-auditing/src/main/java/org/baeldung/MvcConfig.java @@ -0,0 +1,18 @@ +package org.baeldung; + +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; + +@Configuration +public class MvcConfig extends WebMvcConfigurerAdapter { + + @Override + public void addViewControllers(ViewControllerRegistry registry) { + registry.addViewController("/home").setViewName("home"); + registry.addViewController("/").setViewName("home"); + registry.addViewController("/hello").setViewName("hello"); + registry.addViewController("/login").setViewName("login"); + } + +} diff --git a/spring-boot-auditing/src/main/java/org/baeldung/WebSecurityConfig.java b/spring-boot-auditing/src/main/java/org/baeldung/WebSecurityConfig.java new file mode 100755 index 0000000000..199edce0bc --- /dev/null +++ b/spring-boot-auditing/src/main/java/org/baeldung/WebSecurityConfig.java @@ -0,0 +1,34 @@ +package org.baeldung; + +import org.springframework.beans.factory.annotation.Autowired; +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.config.annotation.web.configuration.EnableWebSecurity; + +@Configuration +@EnableWebSecurity +public class WebSecurityConfig extends WebSecurityConfigurerAdapter { + @Override + protected void configure(HttpSecurity http) throws Exception { + http + .authorizeRequests() + .antMatchers("/", "/home").permitAll() + .anyRequest().authenticated() + .and() + .formLogin() + .loginPage("/login") + .permitAll() + .and() + .logout() + .permitAll(); + } + + @Autowired + public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { + auth + .inMemoryAuthentication() + .withUser("user").password("password").roles("USER"); + } +} diff --git a/spring-boot-auditing/src/main/java/org/baeldung/auditing/ExposeAttemptedPathAuthorizationAuditListener.java b/spring-boot-auditing/src/main/java/org/baeldung/auditing/ExposeAttemptedPathAuthorizationAuditListener.java new file mode 100644 index 0000000000..bc36ac08b3 --- /dev/null +++ b/spring-boot-auditing/src/main/java/org/baeldung/auditing/ExposeAttemptedPathAuthorizationAuditListener.java @@ -0,0 +1,36 @@ +package org.baeldung.auditing; + +import org.springframework.boot.actuate.audit.AuditEvent; +import org.springframework.boot.actuate.security.AbstractAuthorizationAuditListener; +import org.springframework.security.access.event.AbstractAuthorizationEvent; +import org.springframework.security.access.event.AuthorizationFailureEvent; +import org.springframework.security.web.FilterInvocation; +import org.springframework.stereotype.Component; + +import java.util.HashMap; +import java.util.Map; + +@Component +public class ExposeAttemptedPathAuthorizationAuditListener extends AbstractAuthorizationAuditListener { + + public static final String AUTHORIZATION_FAILURE = "AUTHORIZATION_FAILURE"; + + @Override + public void onApplicationEvent(AbstractAuthorizationEvent event) { + if (event instanceof AuthorizationFailureEvent) { + onAuthorizationFailureEvent((AuthorizationFailureEvent) event); + } + } + + private void onAuthorizationFailureEvent(AuthorizationFailureEvent event) { + Map data = new HashMap<>(); + data.put("type", event.getAccessDeniedException().getClass().getName()); + data.put("message", event.getAccessDeniedException().getMessage()); + data.put("requestUrl", ((FilterInvocation)event.getSource()).getRequestUrl() ); + if (event.getAuthentication().getDetails() != null) { + data.put("details", event.getAuthentication().getDetails()); + } + publish(new AuditEvent(event.getAuthentication().getName(), AUTHORIZATION_FAILURE, + data)); + } +} diff --git a/spring-boot-auditing/src/main/java/org/baeldung/auditing/LoginAttemptsLogger.java b/spring-boot-auditing/src/main/java/org/baeldung/auditing/LoginAttemptsLogger.java new file mode 100644 index 0000000000..5be8cebfd3 --- /dev/null +++ b/spring-boot-auditing/src/main/java/org/baeldung/auditing/LoginAttemptsLogger.java @@ -0,0 +1,25 @@ +package org.baeldung.auditing; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.boot.actuate.audit.AuditEvent; +import org.springframework.boot.actuate.audit.listener.AuditApplicationEvent; +import org.springframework.context.event.EventListener; +import org.springframework.security.web.authentication.WebAuthenticationDetails; +import org.springframework.stereotype.Component; + +@Component +public class LoginAttemptsLogger { + private static final Logger LOGGER = LoggerFactory.getLogger(LoginAttemptsLogger.class); + + @EventListener + public void auditEventHappened(AuditApplicationEvent auditApplicationEvent) { + AuditEvent auditEvent = auditApplicationEvent.getAuditEvent(); + LOGGER.debug("Principal " + auditEvent.getPrincipal() + " - " + auditEvent.getType()); + + WebAuthenticationDetails details = (WebAuthenticationDetails) auditEvent.getData().get("details"); + LOGGER.debug(" Remote IP address: " + details.getRemoteAddress()); + LOGGER.debug(" Session Id: " + details.getSessionId()); + LOGGER.debug(" Request URL: " + auditEvent.getData().get("requestUrl")); + } +} diff --git a/spring-boot-auditing/src/main/resources/application.properties b/spring-boot-auditing/src/main/resources/application.properties new file mode 100644 index 0000000000..cf09473b60 --- /dev/null +++ b/spring-boot-auditing/src/main/resources/application.properties @@ -0,0 +1 @@ +logging.level.org.springframework=INFO \ No newline at end of file diff --git a/spring-boot-auditing/src/main/resources/logback.xml b/spring-boot-auditing/src/main/resources/logback.xml new file mode 100644 index 0000000000..78913ee76f --- /dev/null +++ b/spring-boot-auditing/src/main/resources/logback.xml @@ -0,0 +1,14 @@ + + + + + web - %date [%thread] %-5level %logger{36} - %message%n + + + + + + + + + \ No newline at end of file diff --git a/spring-boot-auditing/src/main/resources/templates/hello.html b/spring-boot-auditing/src/main/resources/templates/hello.html new file mode 100755 index 0000000000..46feef7e2c --- /dev/null +++ b/spring-boot-auditing/src/main/resources/templates/hello.html @@ -0,0 +1,13 @@ + + + + Hello World! + + +

Hello [[${#httpServletRequest.remoteUser}]]!

+
+ +
+ + \ No newline at end of file diff --git a/spring-boot-auditing/src/main/resources/templates/home.html b/spring-boot-auditing/src/main/resources/templates/home.html new file mode 100755 index 0000000000..fe4e8b337e --- /dev/null +++ b/spring-boot-auditing/src/main/resources/templates/home.html @@ -0,0 +1,11 @@ + + + + Spring Security Example + + +

Welcome!

+ +

Click here to see a greeting.

+ + \ No newline at end of file diff --git a/spring-boot-auditing/src/main/resources/templates/login.html b/spring-boot-auditing/src/main/resources/templates/login.html new file mode 100755 index 0000000000..a1785313f5 --- /dev/null +++ b/spring-boot-auditing/src/main/resources/templates/login.html @@ -0,0 +1,20 @@ + + + + Spring Security Example + + +
+ Invalid username and password. +
+
+ You have been logged out. +
+
+
+
+
+
+ + \ No newline at end of file diff --git a/spring-boot-servlet/README.md b/spring-boot-servlet/README.md deleted file mode 100644 index 262a11fc36..0000000000 --- a/spring-boot-servlet/README.md +++ /dev/null @@ -1,2 +0,0 @@ -###Relevant Articles: -- [How to Register a Servlet in a Java Web Application](http://www.baeldung.com/how-to-register-a-servlet-in-a-java-web-application/) \ No newline at end of file diff --git a/spring-boot-servlet/pom.xml b/spring-boot-servlet/pom.xml deleted file mode 100644 index 3818e3468f..0000000000 --- a/spring-boot-servlet/pom.xml +++ /dev/null @@ -1,55 +0,0 @@ - - 4.0.0 - com.baeldung - spring-boot-servlet - 0.0.1-SNAPSHOT - war - spring-boot-servlet - - - org.springframework.boot - spring-boot-dependencies - 1.5.1.RELEASE - - - - - - org.springframework.boot - spring-boot-starter-web - - - org.springframework.boot - spring-boot-starter-tomcat - provided - - - - org.apache.tomcat.embed - tomcat-embed-core - ${tomcat.version} - - - org.apache.tomcat.embed - tomcat-embed-jasper - ${tomcat.version} - - - - - - - org.springframework.boot - spring-boot-maven-plugin - - - - - - UTF-8 - 1.8 - 8.5.11 - - - diff --git a/spring-boot-servlet/src/main/java/META-INF/MANIFEST.MF b/spring-boot-servlet/src/main/java/META-INF/MANIFEST.MF deleted file mode 100644 index 69ebae1751..0000000000 --- a/spring-boot-servlet/src/main/java/META-INF/MANIFEST.MF +++ /dev/null @@ -1,2 +0,0 @@ -Manifest-Version: 1.0 -Main-Class: com.baeldung.ApplicationMain diff --git a/spring-boot-servlet/src/main/resources/application.properties b/spring-boot-servlet/src/main/resources/application.properties deleted file mode 100644 index 4e9e2b4cf1..0000000000 --- a/spring-boot-servlet/src/main/resources/application.properties +++ /dev/null @@ -1,10 +0,0 @@ -#Server Configuration -#server.port=8080 -#server.context-path=/javabootdata -#Resource Handling -#spring.resources.static-locations=classpath:/WEB-INF/resources -#spring.mvc.view.prefix=/WEB-INF/ -#spring.mvc.view.suffix=.jsp -#spring.resources.cache-period=3600 -servlet.name=dispatcherExample -servlet.mapping=/dispatcherExampleURL \ No newline at end of file diff --git a/spring-boot/README.MD b/spring-boot/README.MD index d0a02c69fc..78ef3c843c 100644 --- a/spring-boot/README.MD +++ b/spring-boot/README.MD @@ -10,3 +10,4 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring - [The @ServletComponentScan Annotation in Spring Boot](http://www.baeldung.com/spring-servletcomponentscan) - [A Custom Data Binder in Spring MVC](http://www.baeldung.com/spring-mvc-custom-data-binder) - [Intro to Building an Application with Spring Boot](http://www.baeldung.com/intro-to-spring-boot) +- [How to Register a Servlet in a Java Web Application](http://www.baeldung.com/how-to-register-a-servlet-in-a-java-web-application/) diff --git a/spring-boot/pom.xml b/spring-boot/pom.xml index e77ab10aff..65b0f247f8 100644 --- a/spring-boot/pom.xml +++ b/spring-boot/pom.xml @@ -41,6 +41,24 @@ spring-boot-starter-security
+ + org.springframework.boot + spring-boot-starter-tomcat + provided + + + + org.apache.tomcat.embed + tomcat-embed-core + ${tomcat.version} + + + + org.apache.tomcat.embed + tomcat-embed-jasper + ${tomcat.version} + + io.dropwizard.metrics metrics-core diff --git a/spring-boot-servlet/src/main/java/com/baeldung/ApplicationMain.java b/spring-boot/src/main/java/com/baeldung/servlets/ApplicationMain.java similarity index 95% rename from spring-boot-servlet/src/main/java/com/baeldung/ApplicationMain.java rename to spring-boot/src/main/java/com/baeldung/servlets/ApplicationMain.java index 66f2e85999..a6ea3757fe 100644 --- a/spring-boot-servlet/src/main/java/com/baeldung/ApplicationMain.java +++ b/spring-boot/src/main/java/com/baeldung/servlets/ApplicationMain.java @@ -1,4 +1,4 @@ -package com.baeldung; +package com.baeldung.servlets; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @@ -16,4 +16,4 @@ public class ApplicationMain extends SpringBootServletInitializer { protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(ApplicationMain.class); } -} \ No newline at end of file +} diff --git a/spring-boot-servlet/src/main/java/com/baeldung/configuration/WebAppInitializer.java b/spring-boot/src/main/java/com/baeldung/servlets/configuration/WebAppInitializer.java similarity index 96% rename from spring-boot-servlet/src/main/java/com/baeldung/configuration/WebAppInitializer.java rename to spring-boot/src/main/java/com/baeldung/servlets/configuration/WebAppInitializer.java index b7e22500f4..eadd40355a 100644 --- a/spring-boot-servlet/src/main/java/com/baeldung/configuration/WebAppInitializer.java +++ b/spring-boot/src/main/java/com/baeldung/servlets/configuration/WebAppInitializer.java @@ -1,4 +1,4 @@ -package com.baeldung.configuration; +package com.baeldung.servlets.configuration; import org.springframework.web.WebApplicationInitializer; import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; @@ -29,4 +29,4 @@ public class WebAppInitializer implements WebApplicationInitializer { servletTwo.addMapping("/"); } -} \ No newline at end of file +} diff --git a/spring-boot-servlet/src/main/java/com/baeldung/configuration/WebMvcConfigure.java b/spring-boot/src/main/java/com/baeldung/servlets/configuration/WebMvcConfigure.java similarity index 97% rename from spring-boot-servlet/src/main/java/com/baeldung/configuration/WebMvcConfigure.java rename to spring-boot/src/main/java/com/baeldung/servlets/configuration/WebMvcConfigure.java index de9067de6e..3d6a10c2ac 100644 --- a/spring-boot-servlet/src/main/java/com/baeldung/configuration/WebMvcConfigure.java +++ b/spring-boot/src/main/java/com/baeldung/servlets/configuration/WebMvcConfigure.java @@ -1,4 +1,4 @@ -package com.baeldung.configuration; +package com.baeldung.servlets.configuration; import org.springframework.boot.web.support.ErrorPageFilter; import org.springframework.context.annotation.Bean; @@ -37,4 +37,3 @@ public class WebMvcConfigure extends WebMvcConfigurerAdapter { return new ErrorPageFilter(); } } - diff --git a/spring-boot-servlet/src/main/java/com/baeldung/props/Constants.java b/spring-boot/src/main/java/com/baeldung/servlets/props/Constants.java similarity index 95% rename from spring-boot-servlet/src/main/java/com/baeldung/props/Constants.java rename to spring-boot/src/main/java/com/baeldung/servlets/props/Constants.java index 421401eec7..6345d1f969 100644 --- a/spring-boot-servlet/src/main/java/com/baeldung/props/Constants.java +++ b/spring-boot/src/main/java/com/baeldung/servlets/props/Constants.java @@ -1,4 +1,4 @@ -package com.baeldung.props; +package com.baeldung.servlets.props; import org.springframework.beans.factory.annotation.Autowired; diff --git a/spring-boot-servlet/src/main/java/com/baeldung/props/PropertyLoader.java b/spring-boot/src/main/java/com/baeldung/servlets/props/PropertyLoader.java similarity index 94% rename from spring-boot-servlet/src/main/java/com/baeldung/props/PropertyLoader.java rename to spring-boot/src/main/java/com/baeldung/servlets/props/PropertyLoader.java index 5d890d96fa..c29da45929 100644 --- a/spring-boot-servlet/src/main/java/com/baeldung/props/PropertyLoader.java +++ b/spring-boot/src/main/java/com/baeldung/servlets/props/PropertyLoader.java @@ -1,4 +1,4 @@ -package com.baeldung.props; +package com.baeldung.servlets.props; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/spring-boot-servlet/src/main/java/com/baeldung/props/PropertySourcesLoader.java b/spring-boot/src/main/java/com/baeldung/servlets/props/PropertySourcesLoader.java similarity index 95% rename from spring-boot-servlet/src/main/java/com/baeldung/props/PropertySourcesLoader.java rename to spring-boot/src/main/java/com/baeldung/servlets/props/PropertySourcesLoader.java index 8c7b3a4af5..56a6751326 100644 --- a/spring-boot-servlet/src/main/java/com/baeldung/props/PropertySourcesLoader.java +++ b/spring-boot/src/main/java/com/baeldung/servlets/props/PropertySourcesLoader.java @@ -1,4 +1,4 @@ -package com.baeldung.props; +package com.baeldung.servlets.props; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/spring-boot-servlet/src/main/java/com/baeldung/servlets/GenericCustomServlet.java b/spring-boot/src/main/java/com/baeldung/servlets/servlets/GenericCustomServlet.java similarity index 93% rename from spring-boot-servlet/src/main/java/com/baeldung/servlets/GenericCustomServlet.java rename to spring-boot/src/main/java/com/baeldung/servlets/servlets/GenericCustomServlet.java index c6543c9eef..49dd9404b7 100644 --- a/spring-boot-servlet/src/main/java/com/baeldung/servlets/GenericCustomServlet.java +++ b/spring-boot/src/main/java/com/baeldung/servlets/servlets/GenericCustomServlet.java @@ -1,4 +1,4 @@ -package com.baeldung.servlets; +package com.baeldung.servlets.servlets; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; diff --git a/spring-boot-servlet/src/main/java/com/baeldung/servlets/javaee/AnnotationServlet.java b/spring-boot/src/main/java/com/baeldung/servlets/servlets/javaee/AnnotationServlet.java similarity index 93% rename from spring-boot-servlet/src/main/java/com/baeldung/servlets/javaee/AnnotationServlet.java rename to spring-boot/src/main/java/com/baeldung/servlets/servlets/javaee/AnnotationServlet.java index d971e68cfa..b50a7d5454 100644 --- a/spring-boot-servlet/src/main/java/com/baeldung/servlets/javaee/AnnotationServlet.java +++ b/spring-boot/src/main/java/com/baeldung/servlets/servlets/javaee/AnnotationServlet.java @@ -1,4 +1,4 @@ -package com.baeldung.servlets.javaee; +package com.baeldung.servlets.servlets.javaee; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; diff --git a/spring-boot-servlet/src/main/java/com/baeldung/servlets/javaee/EEWebXmlServlet.java b/spring-boot/src/main/java/com/baeldung/servlets/servlets/javaee/EEWebXmlServlet.java similarity index 92% rename from spring-boot-servlet/src/main/java/com/baeldung/servlets/javaee/EEWebXmlServlet.java rename to spring-boot/src/main/java/com/baeldung/servlets/servlets/javaee/EEWebXmlServlet.java index 4209e815cd..c7b373064f 100644 --- a/spring-boot-servlet/src/main/java/com/baeldung/servlets/javaee/EEWebXmlServlet.java +++ b/spring-boot/src/main/java/com/baeldung/servlets/servlets/javaee/EEWebXmlServlet.java @@ -1,4 +1,4 @@ -package com.baeldung.servlets.javaee; +package com.baeldung.servlets.servlets.javaee; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; @@ -17,4 +17,4 @@ public class EEWebXmlServlet extends HttpServlet { PrintWriter out = response.getWriter(); out.println("

Hello World

"); } -} \ No newline at end of file +} diff --git a/spring-boot-servlet/src/main/java/com/baeldung/servlets/springboot/SpringRegistrationBeanServlet.java b/spring-boot/src/main/java/com/baeldung/servlets/servlets/springboot/SpringRegistrationBeanServlet.java similarity index 82% rename from spring-boot-servlet/src/main/java/com/baeldung/servlets/springboot/SpringRegistrationBeanServlet.java rename to spring-boot/src/main/java/com/baeldung/servlets/servlets/springboot/SpringRegistrationBeanServlet.java index 4a34465894..e3c225d429 100644 --- a/spring-boot-servlet/src/main/java/com/baeldung/servlets/springboot/SpringRegistrationBeanServlet.java +++ b/spring-boot/src/main/java/com/baeldung/servlets/servlets/springboot/SpringRegistrationBeanServlet.java @@ -1,6 +1,6 @@ -package com.baeldung.servlets.springboot; +package com.baeldung.servlets.servlets.springboot; -import com.baeldung.servlets.GenericCustomServlet; +import com.baeldung.servlets.servlets.GenericCustomServlet; import org.springframework.boot.web.servlet.ServletRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -15,5 +15,3 @@ public class SpringRegistrationBeanServlet { return bean; } } - - diff --git a/spring-boot-servlet/src/main/java/com/baeldung/servlets/springboot/embedded/EmbeddedTomcatExample.java b/spring-boot/src/main/java/com/baeldung/servlets/servlets/springboot/embedded/EmbeddedTomcatExample.java similarity index 90% rename from spring-boot-servlet/src/main/java/com/baeldung/servlets/springboot/embedded/EmbeddedTomcatExample.java rename to spring-boot/src/main/java/com/baeldung/servlets/servlets/springboot/embedded/EmbeddedTomcatExample.java index b2458f33c7..9e460d03a8 100644 --- a/spring-boot-servlet/src/main/java/com/baeldung/servlets/springboot/embedded/EmbeddedTomcatExample.java +++ b/spring-boot/src/main/java/com/baeldung/servlets/servlets/springboot/embedded/EmbeddedTomcatExample.java @@ -1,4 +1,4 @@ -package com.baeldung.servlets.springboot.embedded; +package com.baeldung.servlets.servlets.springboot.embedded; import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory; import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory; diff --git a/spring-boot/src/main/resources/application.properties b/spring-boot/src/main/resources/application.properties index d30045d1dc..8c6549f53d 100644 --- a/spring-boot/src/main/resources/application.properties +++ b/spring-boot/src/main/resources/application.properties @@ -28,4 +28,8 @@ security.user.name=admin1 security.user.password=secret1 management.security.role=SUPERUSER -logging.level.org.springframework=INFO \ No newline at end of file +logging.level.org.springframework=INFO + +#Servlet Configuration +servlet.name=dispatcherExample +servlet.mapping=/dispatcherExampleURL diff --git a/spring-boot-servlet/src/main/resources/custom.properties b/spring-boot/src/main/resources/custom.properties similarity index 100% rename from spring-boot-servlet/src/main/resources/custom.properties rename to spring-boot/src/main/resources/custom.properties diff --git a/spring-boot-servlet/src/main/webapp/WEB-INF/context.xml b/spring-boot/src/main/webapp/WEB-INF/context.xml similarity index 100% rename from spring-boot-servlet/src/main/webapp/WEB-INF/context.xml rename to spring-boot/src/main/webapp/WEB-INF/context.xml diff --git a/spring-boot-servlet/src/main/webapp/WEB-INF/dispatcher.xml b/spring-boot/src/main/webapp/WEB-INF/dispatcher.xml similarity index 100% rename from spring-boot-servlet/src/main/webapp/WEB-INF/dispatcher.xml rename to spring-boot/src/main/webapp/WEB-INF/dispatcher.xml diff --git a/spring-boot-servlet/src/main/webapp/WEB-INF/web.xml b/spring-boot/src/main/webapp/WEB-INF/web.xml similarity index 100% rename from spring-boot-servlet/src/main/webapp/WEB-INF/web.xml rename to spring-boot/src/main/webapp/WEB-INF/web.xml diff --git a/spring-boot-servlet/src/main/webapp/annotationservlet.jsp b/spring-boot/src/main/webapp/annotationservlet.jsp similarity index 100% rename from spring-boot-servlet/src/main/webapp/annotationservlet.jsp rename to spring-boot/src/main/webapp/annotationservlet.jsp diff --git a/spring-boot-servlet/src/main/webapp/index.jsp b/spring-boot/src/main/webapp/index.jsp similarity index 100% rename from spring-boot-servlet/src/main/webapp/index.jsp rename to spring-boot/src/main/webapp/index.jsp diff --git a/spring-cloud/spring-cloud-bootstrap/README.MD b/spring-cloud/spring-cloud-bootstrap/README.MD index d6f8faf31e..251c861830 100644 --- a/spring-cloud/spring-cloud-bootstrap/README.MD +++ b/spring-cloud/spring-cloud-bootstrap/README.MD @@ -1,3 +1,12 @@ ### Relevant Articles: - [Spring Cloud – Bootstrapping](http://www.baeldung.com/spring-cloud-bootstrapping) - [Spring Cloud – Securing Services](http://www.baeldung.com/spring-cloud-securing-services) + +- To run the project: + - copy the appliction-config folder to c:\Users\{username}\ on Windows or /Users/{username}/ on *nix. Then open a git bash terminal in application-config and run: + - git init + - git add . + - git commit -m "First commit" + - start the config server + - start the discover server + - start all the other servers in any order (gateway, svc-book, svc-rating, zipkin) diff --git a/spring-cloud/spring-cloud-bootstrap/application-config/book-service.properties b/spring-cloud/spring-cloud-bootstrap/application-config/book-service.properties index e1244a0cf0..49f7d1ed91 100644 --- a/spring-cloud/spring-cloud-bootstrap/application-config/book-service.properties +++ b/spring-cloud/spring-cloud-bootstrap/application-config/book-service.properties @@ -15,3 +15,6 @@ logging.level.org.springframework.security=debug spring.redis.host=localhost spring.redis.port=6379 + +spring.sleuth.sampler.percentage=1.0 +spring.sleuth.web.skipPattern=(^cleanup.*) diff --git a/spring-cloud/spring-cloud-bootstrap/application-config/gateway.properties b/spring-cloud/spring-cloud-bootstrap/application-config/gateway.properties index 09f7f3bf4a..e9e593284c 100644 --- a/spring-cloud/spring-cloud-bootstrap/application-config/gateway.properties +++ b/spring-cloud/spring-cloud-bootstrap/application-config/gateway.properties @@ -24,4 +24,7 @@ logging.level.org.springframework.security=debug logging.level.org.springframework.cloud.netflix.zuul=debug spring.redis.host=localhost -spring.redis.port=6379 \ No newline at end of file +spring.redis.port=6379 + +spring.sleuth.sampler.percentage=1.0 +spring.sleuth.web.skipPattern=(^cleanup.*|.+favicon.*) \ No newline at end of file diff --git a/spring-cloud/spring-cloud-bootstrap/application-config/rating-service.properties b/spring-cloud/spring-cloud-bootstrap/application-config/rating-service.properties index 4817d12c83..b7cbb6fbd6 100644 --- a/spring-cloud/spring-cloud-bootstrap/application-config/rating-service.properties +++ b/spring-cloud/spring-cloud-bootstrap/application-config/rating-service.properties @@ -15,3 +15,6 @@ logging.level.org.springframework.security=debug spring.redis.host=localhost spring.redis.port=6379 + +spring.sleuth.sampler.percentage=1.0 +spring.sleuth.web.skipPattern=(^cleanup.*) diff --git a/spring-cloud/spring-cloud-bootstrap/application-config/zipkin.properties b/spring-cloud/spring-cloud-bootstrap/application-config/zipkin.properties new file mode 100644 index 0000000000..ca3aed2263 --- /dev/null +++ b/spring-cloud/spring-cloud-bootstrap/application-config/zipkin.properties @@ -0,0 +1,7 @@ +spring.application.name=zipkin +server.port=9411 + +eureka.client.region = default +eureka.client.registryFetchIntervalSeconds = 5 + +logging.level.org.springframework.web=debug diff --git a/spring-cloud/spring-cloud-bootstrap/gateway/pom.xml b/spring-cloud/spring-cloud-bootstrap/gateway/pom.xml index 044730ba22..97c440c249 100644 --- a/spring-cloud/spring-cloud-bootstrap/gateway/pom.xml +++ b/spring-cloud/spring-cloud-bootstrap/gateway/pom.xml @@ -41,6 +41,11 @@ spring-boot-starter-data-redis
+ + org.springframework.cloud + spring-cloud-starter-zipkin + + org.springframework.boot spring-boot-starter-test diff --git a/spring-cloud/spring-cloud-bootstrap/gateway/src/main/java/com/baeldung/spring/cloud/bootstrap/gateway/GatewayApplication.java b/spring-cloud/spring-cloud-bootstrap/gateway/src/main/java/com/baeldung/spring/cloud/bootstrap/gateway/GatewayApplication.java index b5ae1e4e7b..16aae66cab 100644 --- a/spring-cloud/spring-cloud-bootstrap/gateway/src/main/java/com/baeldung/spring/cloud/bootstrap/gateway/GatewayApplication.java +++ b/spring-cloud/spring-cloud-bootstrap/gateway/src/main/java/com/baeldung/spring/cloud/bootstrap/gateway/GatewayApplication.java @@ -1,6 +1,9 @@ package com.baeldung.spring.cloud.bootstrap.gateway; +import com.netflix.appinfo.InstanceInfo; +import com.netflix.discovery.EurekaClient; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.loadbalancer.LoadBalanced; @@ -8,8 +11,13 @@ import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.netflix.ribbon.RibbonClientSpecification; import org.springframework.cloud.netflix.ribbon.SpringClientFactory; import org.springframework.cloud.netflix.zuul.EnableZuulProxy; +import org.springframework.cloud.sleuth.metric.SpanMetricReporter; +import org.springframework.cloud.sleuth.zipkin.HttpZipkinSpanReporter; +import org.springframework.cloud.sleuth.zipkin.ZipkinProperties; +import org.springframework.cloud.sleuth.zipkin.ZipkinSpanReporter; import org.springframework.context.annotation.Bean; import org.springframework.web.client.RestTemplate; +import zipkin.Span; import java.util.ArrayList; import java.util.List; @@ -18,23 +26,50 @@ import java.util.List; @EnableZuulProxy @EnableEurekaClient public class GatewayApplication { - public static void main(String[] args) { - SpringApplication.run(GatewayApplication.class, args); - } + public static void main(String[] args) { + SpringApplication.run(GatewayApplication.class, args); + } - @Autowired(required = false) - private List configurations = new ArrayList<>(); + @Autowired(required = false) + private List configurations = new ArrayList<>(); + @Autowired + private EurekaClient eurekaClient; + @Autowired + private SpanMetricReporter spanMetricReporter; + @Autowired + private ZipkinProperties zipkinProperties; + @Value("${spring.sleuth.web.skipPattern}") + private String skipPattern; - @Bean - @LoadBalanced - RestTemplate restTemplate() { - return new RestTemplate(); - } + @Bean + @LoadBalanced + RestTemplate restTemplate() { + return new RestTemplate(); + } - @Bean - public SpringClientFactory springClientFactory() { - SpringClientFactory factory = new SpringClientFactory(); - factory.setConfigurations(this.configurations); - return factory; - } + @Bean + public SpringClientFactory springClientFactory() { + SpringClientFactory factory = new SpringClientFactory(); + factory.setConfigurations(this.configurations); + return factory; + } + + @Bean + public ZipkinSpanReporter makeZipkinSpanReporter() { + return new ZipkinSpanReporter() { + private HttpZipkinSpanReporter delegate; + private String baseUrl; + + @Override + public void report(Span span) { + InstanceInfo instance = eurekaClient.getNextServerFromEureka("zipkin", false); + if (!(baseUrl != null && instance.getHomePageUrl().equals(baseUrl))) { + baseUrl = instance.getHomePageUrl(); + delegate = new HttpZipkinSpanReporter(baseUrl, zipkinProperties.getFlushInterval(), zipkinProperties.getCompression().isEnabled(), spanMetricReporter); + if (!span.name.matches(skipPattern)) delegate.report(span); + } + if (!span.name.matches(skipPattern)) delegate.report(span); + } + }; + } } diff --git a/spring-cloud/spring-cloud-bootstrap/gateway/src/main/java/com/baeldung/spring/cloud/bootstrap/gateway/SecurityConfig.java b/spring-cloud/spring-cloud-bootstrap/gateway/src/main/java/com/baeldung/spring/cloud/bootstrap/gateway/SecurityConfig.java index 9e5c424403..935e50ec72 100644 --- a/spring-cloud/spring-cloud-bootstrap/gateway/src/main/java/com/baeldung/spring/cloud/bootstrap/gateway/SecurityConfig.java +++ b/spring-cloud/spring-cloud-bootstrap/gateway/src/main/java/com/baeldung/spring/cloud/bootstrap/gateway/SecurityConfig.java @@ -23,6 +23,7 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter { protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .antMatchers("/book-service/books").permitAll() + .antMatchers("/zipkin/**").permitAll() .antMatchers("/eureka/**").hasRole("ADMIN") .anyRequest().authenticated() .and() diff --git a/spring-cloud/spring-cloud-bootstrap/pom.xml b/spring-cloud/spring-cloud-bootstrap/pom.xml index ccfbdb9735..83879cf7d4 100644 --- a/spring-cloud/spring-cloud-bootstrap/pom.xml +++ b/spring-cloud/spring-cloud-bootstrap/pom.xml @@ -16,6 +16,7 @@ gateway svc-book svc-rating + zipkin diff --git a/spring-cloud/spring-cloud-bootstrap/svc-book/pom.xml b/spring-cloud/spring-cloud-bootstrap/svc-book/pom.xml index c351c444f6..cbf7083b83 100644 --- a/spring-cloud/spring-cloud-bootstrap/svc-book/pom.xml +++ b/spring-cloud/spring-cloud-bootstrap/svc-book/pom.xml @@ -53,6 +53,11 @@ runtime + + org.springframework.cloud + spring-cloud-starter-zipkin + + org.springframework.boot spring-boot-starter-test diff --git a/spring-cloud/spring-cloud-bootstrap/svc-book/src/main/java/com/baeldung/spring/cloud/bootstrap/svcbook/BookServiceApplication.java b/spring-cloud/spring-cloud-bootstrap/svc-book/src/main/java/com/baeldung/spring/cloud/bootstrap/svcbook/BookServiceApplication.java index c5499cd924..63ff0fb134 100644 --- a/spring-cloud/spring-cloud-bootstrap/svc-book/src/main/java/com/baeldung/spring/cloud/bootstrap/svcbook/BookServiceApplication.java +++ b/spring-cloud/spring-cloud-bootstrap/svc-book/src/main/java/com/baeldung/spring/cloud/bootstrap/svcbook/BookServiceApplication.java @@ -1,13 +1,52 @@ package com.baeldung.spring.cloud.bootstrap.svcbook; +import com.netflix.appinfo.InstanceInfo; +import com.netflix.discovery.EurekaClient; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; +import org.springframework.cloud.sleuth.metric.SpanMetricReporter; +import org.springframework.cloud.sleuth.zipkin.HttpZipkinSpanReporter; +import org.springframework.cloud.sleuth.zipkin.ZipkinProperties; +import org.springframework.cloud.sleuth.zipkin.ZipkinSpanReporter; +import org.springframework.context.annotation.Bean; +import zipkin.Span; @SpringBootApplication @EnableEurekaClient public class BookServiceApplication { + + @Autowired + private EurekaClient eurekaClient; + @Autowired + private SpanMetricReporter spanMetricReporter; + @Autowired + private ZipkinProperties zipkinProperties; + @Value("${spring.sleuth.web.skipPattern}") + private String skipPattern; + public static void main(String[] args) { SpringApplication.run(BookServiceApplication.class, args); } + + @Bean + public ZipkinSpanReporter makeZipkinSpanReporter() { + return new ZipkinSpanReporter() { + private HttpZipkinSpanReporter delegate; + private String baseUrl; + + @Override + public void report(Span span) { + InstanceInfo instance = eurekaClient.getNextServerFromEureka("zipkin", false); + if (!(baseUrl != null && instance.getHomePageUrl().equals(baseUrl))) { + baseUrl = instance.getHomePageUrl(); + delegate = new HttpZipkinSpanReporter(baseUrl, zipkinProperties.getFlushInterval(), zipkinProperties.getCompression().isEnabled(), spanMetricReporter); + if (!span.name.matches(skipPattern)) delegate.report(span); + } + if (!span.name.matches(skipPattern)) delegate.report(span); + } + }; + } } diff --git a/spring-cloud/spring-cloud-bootstrap/svc-book/src/main/resources/bootstrap.properties b/spring-cloud/spring-cloud-bootstrap/svc-book/src/main/resources/bootstrap.properties index 8f3a3261ac..481cdc182c 100644 --- a/spring-cloud/spring-cloud-bootstrap/svc-book/src/main/resources/bootstrap.properties +++ b/spring-cloud/spring-cloud-bootstrap/svc-book/src/main/resources/bootstrap.properties @@ -4,4 +4,4 @@ spring.cloud.config.discovery.enabled=true spring.cloud.config.username=configUser spring.cloud.config.password=configPassword -eureka.client.serviceUrl.defaultZone=http://discUser:discPassword@localhost:8082/eureka/ +eureka.client.serviceUrl.defaultZone=http://discUser:discPassword@localhost:8082/eureka/ \ No newline at end of file diff --git a/spring-cloud/spring-cloud-bootstrap/svc-rating/pom.xml b/spring-cloud/spring-cloud-bootstrap/svc-rating/pom.xml index 2285286812..f02ba88f04 100644 --- a/spring-cloud/spring-cloud-bootstrap/svc-rating/pom.xml +++ b/spring-cloud/spring-cloud-bootstrap/svc-rating/pom.xml @@ -53,6 +53,11 @@ runtime + + org.springframework.cloud + spring-cloud-starter-zipkin + + org.springframework.boot spring-boot-starter-test diff --git a/spring-cloud/spring-cloud-bootstrap/svc-rating/src/main/java/com/baeldung/spring/cloud/bootstrap/svcrating/RatingServiceApplication.java b/spring-cloud/spring-cloud-bootstrap/svc-rating/src/main/java/com/baeldung/spring/cloud/bootstrap/svcrating/RatingServiceApplication.java index 61074e0bcc..0d85c5c825 100644 --- a/spring-cloud/spring-cloud-bootstrap/svc-rating/src/main/java/com/baeldung/spring/cloud/bootstrap/svcrating/RatingServiceApplication.java +++ b/spring-cloud/spring-cloud-bootstrap/svc-rating/src/main/java/com/baeldung/spring/cloud/bootstrap/svcrating/RatingServiceApplication.java @@ -1,13 +1,51 @@ package com.baeldung.spring.cloud.bootstrap.svcrating; +import com.netflix.appinfo.InstanceInfo; +import com.netflix.discovery.EurekaClient; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; +import org.springframework.cloud.sleuth.metric.SpanMetricReporter; +import org.springframework.cloud.sleuth.zipkin.HttpZipkinSpanReporter; +import org.springframework.cloud.sleuth.zipkin.ZipkinProperties; +import org.springframework.cloud.sleuth.zipkin.ZipkinSpanReporter; +import org.springframework.context.annotation.Bean; +import zipkin.Span; @SpringBootApplication @EnableEurekaClient public class RatingServiceApplication { + @Autowired + private EurekaClient eurekaClient; + @Autowired + private SpanMetricReporter spanMetricReporter; + @Autowired + private ZipkinProperties zipkinProperties; + @Value("${spring.sleuth.web.skipPattern}") + private String skipPattern; + public static void main(String[] args) { SpringApplication.run(RatingServiceApplication.class, args); } + + @Bean + public ZipkinSpanReporter makeZipkinSpanReporter() { + return new ZipkinSpanReporter() { + private HttpZipkinSpanReporter delegate; + private String baseUrl; + + @Override + public void report(Span span) { + InstanceInfo instance = eurekaClient.getNextServerFromEureka("zipkin", false); + if (!(baseUrl != null && instance.getHomePageUrl().equals(baseUrl))) { + baseUrl = instance.getHomePageUrl(); + delegate = new HttpZipkinSpanReporter(baseUrl, zipkinProperties.getFlushInterval(), zipkinProperties.getCompression().isEnabled(), spanMetricReporter); + if (!span.name.matches(skipPattern)) delegate.report(span); + } + if (!span.name.matches(skipPattern)) delegate.report(span); + } + }; + } } \ No newline at end of file diff --git a/spring-cloud/spring-cloud-bootstrap/zipkin/pom.xml b/spring-cloud/spring-cloud-bootstrap/zipkin/pom.xml new file mode 100644 index 0000000000..c2fd7cd2dc --- /dev/null +++ b/spring-cloud/spring-cloud-bootstrap/zipkin/pom.xml @@ -0,0 +1,88 @@ + + + 4.0.0 + + zipkin + 1.0.0-SNAPSHOT + + + org.springframework.boot + spring-boot-starter-parent + 1.4.4.RELEASE + + + + + + org.springframework.cloud + spring-cloud-starter-config + + + org.springframework.cloud + spring-cloud-starter-eureka + + + + io.zipkin.java + zipkin-server + + + + io.zipkin.java + zipkin-autoconfigure-ui + runtime + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + org.springframework.cloud + spring-cloud-dependencies + ${spring-cloud-dependencies.version} + pom + import + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + org.apache.maven.plugins + maven-compiler-plugin + ${maven-compiler-plugin.version} + + 1.8 + 1.8 + + + + org.apache.maven.plugins + maven-surefire-plugin + + + **/*LiveTest.java + + + + + + + + Brixton.SR7 + 3.6.0 + + \ No newline at end of file diff --git a/spring-cloud/spring-cloud-bootstrap/zipkin/src/main/java/com/baeldung/spring/cloud/bootstrap/zipkin/ZipkinApplication.java b/spring-cloud/spring-cloud-bootstrap/zipkin/src/main/java/com/baeldung/spring/cloud/bootstrap/zipkin/ZipkinApplication.java new file mode 100644 index 0000000000..bb1355e258 --- /dev/null +++ b/spring-cloud/spring-cloud-bootstrap/zipkin/src/main/java/com/baeldung/spring/cloud/bootstrap/zipkin/ZipkinApplication.java @@ -0,0 +1,15 @@ +package com.baeldung.spring.cloud.bootstrap.zipkin; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.netflix.eureka.EnableEurekaClient; +import zipkin.server.EnableZipkinServer; + +@SpringBootApplication +@EnableEurekaClient +@EnableZipkinServer +public class ZipkinApplication { + public static void main(String[] args) { + SpringApplication.run(ZipkinApplication.class, args); + } +} diff --git a/spring-cloud/spring-cloud-bootstrap/zipkin/src/main/resources/bootstrap.properties b/spring-cloud/spring-cloud-bootstrap/zipkin/src/main/resources/bootstrap.properties new file mode 100644 index 0000000000..9569179a4f --- /dev/null +++ b/spring-cloud/spring-cloud-bootstrap/zipkin/src/main/resources/bootstrap.properties @@ -0,0 +1,7 @@ +spring.cloud.config.name=zipkin +spring.cloud.config.discovery.service-id=config +spring.cloud.config.discovery.enabled=true +spring.cloud.config.username=configUser +spring.cloud.config.password=configPassword + +eureka.client.serviceUrl.defaultZone=http://discUser:discPassword@localhost:8082/eureka/ \ No newline at end of file diff --git a/spring-data-rest/README.md b/spring-data-rest/README.md index 4e8828a688..f77c4dc202 100644 --- a/spring-data-rest/README.md +++ b/spring-data-rest/README.md @@ -15,3 +15,4 @@ To view the running application, visit [http://localhost:8080](http://localhost: ###Relevant Articles: - [Guide to Spring Data REST Validators](http://www.baeldung.com/spring-data-rest-validators) +- [Working with Relationships in Spring Data REST](http://www.baeldung.com/spring-data-rest-relationships) diff --git a/testng/pom.xml b/testng/pom.xml index 272b1d316d..83b32bb84d 100644 --- a/testng/pom.xml +++ b/testng/pom.xml @@ -1,115 +1,121 @@ - - - 4.0.0 - com.baeldung - testng - 0.1.0-SNAPSHOT - jar - testng - - - - - - - org.slf4j - slf4j-api - ${org.slf4j.version} - - - ch.qos.logback - logback-classic - ${logback.version} - - - - - - org.testng - testng - ${testng.version} - test - - - - - - testng - - - src/main/resources - true - - - - - src/main/resources - true - - - - - - - org.apache.maven.plugins - maven-compiler-plugin - ${maven-compiler-plugin.version} - - 1.8 - 1.8 - - - - - org.apache.maven.plugins - maven-surefire-plugin - ${maven-surefire-plugin.version} - - - **/*IntegrationTest.java - **/*LongRunningUnitTest.java - **/*ManualTest.java - - true - - - - - org.apache.maven.plugins - maven-dependency-plugin - - - copy-dependencies - prepare-package - - copy-dependencies - - - ${project.build.directory}/libs - - - - - - - - - - - - 1.7.21 - 1.1.7 - - - 6.10 - - - 3.6.0 - 2.19.1 - - - - + + + 4.0.0 + com.baeldung + testng + 0.1.0-SNAPSHOT + jar + testng + + + + + + + org.slf4j + slf4j-api + ${org.slf4j.version} + + + ch.qos.logback + logback-classic + ${logback.version} + + + + + + org.testng + testng + ${testng.version} + test + + + + + + testng + + + src/main/resources + true + + + + + src/main/resources + true + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + ${maven-compiler-plugin.version} + + 1.8 + 1.8 + + + + + org.apache.maven.plugins + maven-surefire-plugin + ${maven-surefire-plugin.version} + + + **/*IntegrationTest.java + **/*LongRunningUnitTest.java + **/*ManualTest.java + + + src\test\resources\parametrized_testng.xml + src\test\resources\test_group.xml + src\test\resources\test_setup.xml + src\test\resources\test_suite.xml + + + true + + + + + org.apache.maven.plugins + maven-dependency-plugin + + + copy-dependencies + prepare-package + + copy-dependencies + + + ${project.build.directory}/libs + + + + + + + + + + + + 1.7.21 + 1.1.7 + + + 6.10 + + + 3.6.0 + 2.19.1 + + + + \ No newline at end of file diff --git a/testng/src/test/java/baeldung/com/DependentTests.java b/testng/src/test/java/baeldung/com/DependentTests.java index b60f8a6c0d..8d3bbfc11b 100644 --- a/testng/src/test/java/baeldung/com/DependentTests.java +++ b/testng/src/test/java/baeldung/com/DependentTests.java @@ -1,25 +1,25 @@ -package baeldung.com; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.testng.Assert; -import org.testng.annotations.Test; - -public class DependentTests { - - private static final Logger LOGGER = LoggerFactory.getLogger(DependentTests.class); - - private String email = "abc@qwe.com"; - - @Test - public void givenEmail_ifValid_thenTrue() { - boolean valid = email.contains("@"); - Assert.assertEquals(valid, true); - } - - @Test(dependsOnMethods = {"givenEmail_ifValid_thenTrue"}) - public void givenValidEmail_whenLoggedIn_thenTrue() { - LOGGER.info("Email {} valid >> logging in", email); - } -} - +package baeldung.com; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.testng.Assert; +import org.testng.annotations.Test; + +public class DependentTests { + + private static final Logger LOGGER = LoggerFactory.getLogger(DependentTests.class); + + private String email = "abc@qwe.com"; + + @Test + public void givenEmail_ifValid_thenTrue() { + boolean valid = email.contains("@"); + Assert.assertEquals(valid, true); + } + + @Test(dependsOnMethods = {"givenEmail_ifValid_thenTrue"}) + public void givenValidEmail_whenLoggedIn_thenTrue() { + LOGGER.info("Email {} valid >> logging in", email); + } +} + diff --git a/testng/src/test/java/baeldung/com/MultiThreadedTests.java b/testng/src/test/java/baeldung/com/MultiThreadedTests.java new file mode 100644 index 0000000000..4476eaa7a0 --- /dev/null +++ b/testng/src/test/java/baeldung/com/MultiThreadedTests.java @@ -0,0 +1,14 @@ +package baeldung.com; + +import org.testng.Assert; +import org.testng.annotations.Test; + +public class MultiThreadedTests { + + @Test(threadPoolSize = 5, invocationCount = 10, timeOut = 1000) + public void givenMethod_whenRunInThreads_thenCorrect() { + int count = Thread.activeCount(); + Assert.assertTrue(count > 1); + } + +} diff --git a/testng/src/test/java/baeldung/com/ParametrizedTests.java b/testng/src/test/java/baeldung/com/ParametrizedTests.java index d3813f5382..693a1b5cde 100644 --- a/testng/src/test/java/baeldung/com/ParametrizedTests.java +++ b/testng/src/test/java/baeldung/com/ParametrizedTests.java @@ -1,77 +1,77 @@ -package baeldung.com; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.testng.Assert; -import org.testng.annotations.DataProvider; -import org.testng.annotations.Parameters; -import org.testng.annotations.Test; - -public class ParametrizedTests { - - private static final Logger LOGGER = LoggerFactory.getLogger(ParametrizedTests.class); - - @Test - @Parameters({"value", "isEven"}) - public void givenNumberFromXML_ifEvenCheckOK_thenCorrect(int value, boolean isEven) { - Assert.assertEquals(isEven, value % 2 == 0); - } - - @DataProvider(name = "numbers") - public static Object[][] evenNumbers() { - return new Object[][]{{1, false}, {2, true}, {4, true}}; - } - - @Test(dataProvider = "numbers") - public void givenNumberFromDataProvider_ifEvenCheckOK_thenCorrect(Integer number, boolean expected) { - Assert.assertEquals(expected, number % 2 == 0); - } - - @Test(dataProvider = "numbersObject") - public void givenNumberObjectFromDataProvider_ifEvenCheckOK_thenCorrect(EvenNumber number) { - Assert.assertEquals(number.isEven(), number.getValue() % 2 == 0); - } - - @DataProvider(name = "numbersObject") - public Object[][] parameterProvider() { - return new Object[][]{{new EvenNumber(1, false)}, {new EvenNumber(2, true)}, {new EvenNumber(4, true),}}; - } - -} - - -class EvenNumber { - private int value; - private boolean isEven; - - public EvenNumber(int number, boolean isEven) { - this.value = number; - this.isEven = isEven; - } - - public int getValue() { - return value; - } - - public void setValue(int value) { - this.value = value; - } - - public boolean isEven() { - return isEven; - } - - public void setEven(boolean even) { - isEven = even; - } - - @Override - public String toString() { - return "EvenNumber{" + - "value=" + value + - ", isEven=" + isEven + - '}'; - } -} - - +package baeldung.com; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.testng.Assert; +import org.testng.annotations.DataProvider; +import org.testng.annotations.Parameters; +import org.testng.annotations.Test; + +public class ParametrizedTests { + + private static final Logger LOGGER = LoggerFactory.getLogger(ParametrizedTests.class); + + @Test + @Parameters({"value", "isEven"}) + public void givenNumberFromXML_ifEvenCheckOK_thenCorrect(int value, boolean isEven) { + Assert.assertEquals(isEven, value % 2 == 0); + } + + @DataProvider(name = "numbers") + public static Object[][] evenNumbers() { + return new Object[][]{{1, false}, {2, true}, {4, true}}; + } + + @Test(dataProvider = "numbers") + public void givenNumberFromDataProvider_ifEvenCheckOK_thenCorrect(Integer number, boolean expected) { + Assert.assertEquals(expected, number % 2 == 0); + } + + @Test(dataProvider = "numbersObject") + public void givenNumberObjectFromDataProvider_ifEvenCheckOK_thenCorrect(EvenNumber number) { + Assert.assertEquals(number.isEven(), number.getValue() % 2 == 0); + } + + @DataProvider(name = "numbersObject") + public Object[][] parameterProvider() { + return new Object[][]{{new EvenNumber(1, false)}, {new EvenNumber(2, true)}, {new EvenNumber(4, true),}}; + } + +} + + +class EvenNumber { + private int value; + private boolean isEven; + + public EvenNumber(int number, boolean isEven) { + this.value = number; + this.isEven = isEven; + } + + public int getValue() { + return value; + } + + public void setValue(int value) { + this.value = value; + } + + public boolean isEven() { + return isEven; + } + + public void setEven(boolean even) { + isEven = even; + } + + @Override + public String toString() { + return "EvenNumber{" + + "value=" + value + + ", isEven=" + isEven + + '}'; + } +} + + diff --git a/testng/src/test/java/baeldung/com/RegistrationTest.java b/testng/src/test/java/baeldung/com/RegistrationTest.java index ec551d9c27..e9d744902b 100644 --- a/testng/src/test/java/baeldung/com/RegistrationTest.java +++ b/testng/src/test/java/baeldung/com/RegistrationTest.java @@ -1,14 +1,14 @@ -package baeldung.com; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.testng.annotations.Test; - -public class RegistrationTest { - private static final Logger LOGGER = LoggerFactory.getLogger(RegistrationTest.class); - - @Test - public void whenCalledFromSuite_thanOK() { - LOGGER.info("Registration successful"); - } -} +package baeldung.com; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.testng.annotations.Test; + +public class RegistrationTest { + private static final Logger LOGGER = LoggerFactory.getLogger(RegistrationTest.class); + + @Test + public void whenCalledFromSuite_thanOK() { + LOGGER.info("Registration successful"); + } +} diff --git a/testng/src/test/java/baeldung/com/SignInTest.java b/testng/src/test/java/baeldung/com/SignInTest.java index f0547374d1..d5cad78de0 100644 --- a/testng/src/test/java/baeldung/com/SignInTest.java +++ b/testng/src/test/java/baeldung/com/SignInTest.java @@ -1,14 +1,14 @@ -package baeldung.com; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.testng.annotations.Test; - -public class SignInTest { - private static final Logger LOGGER = LoggerFactory.getLogger(SignInTest.class); - - @Test - public void whenCalledFromSuite_thanOK() { - LOGGER.info("SignIn successful"); - } -} +package baeldung.com; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.testng.annotations.Test; + +public class SignInTest { + private static final Logger LOGGER = LoggerFactory.getLogger(SignInTest.class); + + @Test + public void whenCalledFromSuite_thanOK() { + LOGGER.info("SignIn successful"); + } +} diff --git a/testng/src/test/java/baeldung/com/SummationServiceTest.java b/testng/src/test/java/baeldung/com/SummationServiceTest.java index f377a009df..ceed53ca42 100644 --- a/testng/src/test/java/baeldung/com/SummationServiceTest.java +++ b/testng/src/test/java/baeldung/com/SummationServiceTest.java @@ -1,98 +1,98 @@ -package baeldung.com; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.testng.Assert; -import org.testng.TestNG; -import org.testng.annotations.*; - -import java.util.ArrayList; -import java.util.List; - -public class SummationServiceTest extends TestNG { - private static final Logger LOGGER = LoggerFactory.getLogger(DependentTests.class); - - private List numbers; - - private int testCount = 0; - - @BeforeClass - public void initialize() { - numbers = new ArrayList<>(); - } - - @AfterClass - public void tearDown() { - numbers = null; - } - - @BeforeSuite(groups = "regression") - public void runBeforeRegressionSuite() { - numbers = new ArrayList<>(); - numbers.add(-11); - numbers.add(2); - } - - @AfterSuite(groups = "regression") - public void runAfterRegressionSuite() { - numbers = null; - } - - @BeforeGroups("negative_tests") - public void runBeforeEachNegativeGroup() { - numbers.clear(); - } - - @BeforeGroups("regression") - public void runBeforeEachRegressionGroup() { - numbers.add(-11); - numbers.add(2); - } - - @BeforeGroups("positive_tests") - public void runBeforeEachPositiveGroup() { - numbers.add(1); - numbers.add(2); - numbers.add(3); - } - - @AfterGroups("positive_tests,regression,negative_tests") - public void runAfterEachGroup() { - numbers.clear(); - } - - @BeforeMethod - public void runBeforeEachTest() { - testCount++; - } - - @AfterMethod - public void runAfterEachTest() { - - } - - - @Test(groups = "positive_tests", enabled = false) - public void givenNumbers_sumEquals_thenCorrect() { - int sum = numbers.stream().reduce(0, Integer::sum); - Assert.assertEquals(sum, 6); - } - - @Test(groups = "negative_tests") - public void givenEmptyList_sumEqualsZero_thenCorrect() { - int sum = numbers.stream().reduce(0, Integer::sum); - Assert.assertEquals(0, sum); - } - - @Test(groups = "regression") - public void givenNegativeNumber_sumLessthanZero_thenCorrect() { - int sum = numbers.stream().reduce(0, Integer::sum); - Assert.assertTrue(sum < 0); - } - - @Test(expectedExceptions = ArithmeticException.class) - public void givenNumber_whenThrowsException_thenCorrect() { - int i = 1 / 0; - } - -} +package baeldung.com; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.testng.Assert; +import org.testng.TestNG; +import org.testng.annotations.*; + +import java.util.ArrayList; +import java.util.List; + +public class SummationServiceTest extends TestNG { + private static final Logger LOGGER = LoggerFactory.getLogger(DependentTests.class); + + private List numbers; + + private int testCount = 0; + + @BeforeClass + public void initialize() { + numbers = new ArrayList<>(); + } + + @AfterClass + public void tearDown() { + numbers = null; + } + + @BeforeSuite(groups = "regression") + public void runBeforeRegressionSuite() { + numbers = new ArrayList<>(); + numbers.add(-11); + numbers.add(2); + } + + @AfterSuite(groups = "regression") + public void runAfterRegressionSuite() { + numbers = null; + } + + @BeforeGroups("negative_tests") + public void runBeforeEachNegativeGroup() { + numbers.clear(); + } + + @BeforeGroups("regression") + public void runBeforeEachRegressionGroup() { + numbers.add(-11); + numbers.add(2); + } + + @BeforeGroups("positive_tests") + public void runBeforeEachPositiveGroup() { + numbers.add(1); + numbers.add(2); + numbers.add(3); + } + + @AfterGroups("positive_tests,regression,negative_tests") + public void runAfterEachGroup() { + numbers.clear(); + } + + @BeforeMethod + public void runBeforeEachTest() { + testCount++; + } + + @AfterMethod + public void runAfterEachTest() { + + } + + + @Test(groups = "positive_tests", enabled = false) + public void givenNumbers_sumEquals_thenCorrect() { + int sum = numbers.stream().reduce(0, Integer::sum); + Assert.assertEquals(sum, 6); + } + + @Test(groups = "negative_tests") + public void givenEmptyList_sumEqualsZero_thenCorrect() { + int sum = numbers.stream().reduce(0, Integer::sum); + Assert.assertEquals(0, sum); + } + + @Test(groups = "regression") + public void givenNegativeNumber_sumLessthanZero_thenCorrect() { + int sum = numbers.stream().reduce(0, Integer::sum); + Assert.assertTrue(sum < 0); + } + + @Test(expectedExceptions = ArithmeticException.class) + public void givenNumber_whenThrowsException_thenCorrect() { + int i = 1 / 0; + } + +} diff --git a/testng/src/test/java/baeldung/com/TestGroup.java b/testng/src/test/java/baeldung/com/TestGroup.java index 08bb5c996e..a592000bed 100644 --- a/testng/src/test/java/baeldung/com/TestGroup.java +++ b/testng/src/test/java/baeldung/com/TestGroup.java @@ -1,44 +1,44 @@ -package baeldung.com; - -import org.testng.annotations.AfterGroups; -import org.testng.annotations.BeforeGroups; -import org.testng.annotations.Test; - -public class TestGroup { - - @BeforeGroups("database") - public void setupDB() { - System.out.println("setupDB()"); - } - - @AfterGroups("database") - public void cleanDB() { - System.out.println("cleanDB()"); - } - - @Test(groups= "selenium-test") - public void runSelenium() { - System.out.println("runSelenium()"); - } - - @Test(groups= "selenium-test") - public void runSelenium1() { - System.out.println("runSelenium()1"); - } - - @Test(groups = "database") - public void testConnectOracle() { - System.out.println("testConnectOracle()"); - } - - @Test(groups = "database") - public void testConnectMsSQL() { - System.out.println("testConnectMsSQL"); - } - - @Test(dependsOnGroups = {"database","selenium-test"}) - public void runFinal() { - System.out.println("runFinal"); - } - +package baeldung.com; + +import org.testng.annotations.AfterGroups; +import org.testng.annotations.BeforeGroups; +import org.testng.annotations.Test; + +public class TestGroup { + + @BeforeGroups("database") + public void setupDB() { + System.out.println("setupDB()"); + } + + @AfterGroups("database") + public void cleanDB() { + System.out.println("cleanDB()"); + } + + @Test(groups = "selenium-test") + public void runSelenium() { + System.out.println("runSelenium()"); + } + + @Test(groups = "selenium-test") + public void runSelenium1() { + System.out.println("runSelenium()1"); + } + + @Test(groups = "database") + public void testConnectOracle() { + System.out.println("testConnectOracle()"); + } + + @Test(groups = "database") + public void testConnectMsSQL() { + System.out.println("testConnectMsSQL"); + } + + @Test(dependsOnGroups = {"database", "selenium-test"}) + public void runFinal() { + System.out.println("runFinal"); + } + } \ No newline at end of file diff --git a/testng/src/test/java/baeldung/com/TimeOutTest.java b/testng/src/test/java/baeldung/com/TimeOutTest.java index d54a914e08..f84367a972 100644 --- a/testng/src/test/java/baeldung/com/TimeOutTest.java +++ b/testng/src/test/java/baeldung/com/TimeOutTest.java @@ -1,11 +1,11 @@ -package baeldung.com; - -import org.testng.annotations.Test; - -public class TimeOutTest { - - @Test(timeOut = 1000, enabled = false) - public void givenExecution_takeMoreTime_thenFail() { - while (true) ; - } -} +package baeldung.com; + +import org.testng.annotations.Test; + +public class TimeOutTest { + + @Test(timeOut = 1000, enabled = false) + public void givenExecution_takeMoreTime_thenFail() { + while (true) ; + } +} diff --git a/testng/src/test/java/com/baeldung/reports/CustomisedListener.java b/testng/src/test/java/com/baeldung/reports/CustomisedListener.java new file mode 100644 index 0000000000..e8338c941d --- /dev/null +++ b/testng/src/test/java/com/baeldung/reports/CustomisedListener.java @@ -0,0 +1,67 @@ +package com.baeldung.reports; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.testng.ITestContext; +import org.testng.ITestListener; +import org.testng.ITestResult; + +public class CustomisedListener implements ITestListener { + private static final Logger LOGGER = LoggerFactory.getLogger("TEST_REPORT"); + + @Override + public void onFinish(ITestContext arg0) { + LOGGER.info("PASSED TEST CASES"); + arg0.getPassedTests() + .getAllResults() + .stream() + .forEach(result -> { + LOGGER.info(result.getName()); + }); + LOGGER.info("FAILED TEST CASES"); + arg0.getFailedTests() + .getAllResults() + .stream() + .forEach(result -> { + LOGGER.info(result.getName()); + }); + LOGGER.info("Test completed on: " + arg0.getEndDate().toString()); + } + + @Override + public void onStart(ITestContext arg0) { + LOGGER.info("Started testing on: " + arg0.getStartDate() + .toString()); + } + + @Override + public void onTestFailedButWithinSuccessPercentage(ITestResult arg0) { + // TODO Auto-generated method stub + + } + + @Override + public void onTestFailure(ITestResult arg0) { + LOGGER.info("Failed : " + arg0.getName()); + + } + + @Override + public void onTestSkipped(ITestResult arg0) { + LOGGER.info("Skipped Test: " + arg0.getName()); + + } + + @Override + public void onTestStart(ITestResult arg0) { + LOGGER.info("Testing: " + arg0.getName() + " " + arg0.getStartMillis()); + + } + + @Override + public void onTestSuccess(ITestResult arg0) { + LOGGER.info("Tested: " + arg0.getName() + " " + arg0.getEndMillis()); + + } + +} diff --git a/testng/src/test/java/com/baeldung/reports/CustomisedReports.java b/testng/src/test/java/com/baeldung/reports/CustomisedReports.java new file mode 100644 index 0000000000..07ba2162cc --- /dev/null +++ b/testng/src/test/java/com/baeldung/reports/CustomisedReports.java @@ -0,0 +1,50 @@ +package com.baeldung.reports; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.testng.IReporter; +import org.testng.ISuite; +import org.testng.ISuiteResult; +import org.testng.ITestContext; +import org.testng.xml.XmlSuite; + +import java.util.List; +import java.util.Map; + +public class CustomisedReports implements IReporter { + private static final Logger LOGGER = LoggerFactory.getLogger("TEST_REPORT"); + + @Override + public void generateReport(List xmlSuites, List suites, String outputDirectory) { + + suites.stream() + .forEach(suite -> { + String suiteName = suite.getName(); + Map suiteResults = suite.getResults(); + suiteResults.values() + .stream() + .forEach(result -> { + ITestContext context + = ((ISuiteResult) result).getTestContext(); + + LOGGER.info("Passed tests for suite '" + + suiteName + "' is:" + + context.getPassedTests() + .getAllResults() + .size()); + LOGGER.info("Failed tests for suite '" + + suiteName + "' is:" + + context.getFailedTests() + .getAllResults() + .size()); + LOGGER.info("Skipped tests for suite '" + + suiteName + "' is:" + + context.getSkippedTests() + .getAllResults() + .size()); + }); + }); + + } + +} diff --git a/testng/src/test/resources/logback.xml b/testng/src/test/resources/logback.xml index e9ae1894a6..346682f033 100644 --- a/testng/src/test/resources/logback.xml +++ b/testng/src/test/resources/logback.xml @@ -1,14 +1,14 @@ - - - - - web - %date [%thread] %-5level %logger{36} - %message%n - - - - - - - - + + + + + web - %date [%thread] %-5level %logger{36} - %message%n + + + + + + + + \ No newline at end of file diff --git a/testng/src/test/resources/parametrized_test.xml b/testng/src/test/resources/parametrized_testng.xml similarity index 74% rename from testng/src/test/resources/parametrized_test.xml rename to testng/src/test/resources/parametrized_testng.xml index 932af30e4e..c8ec7209f8 100644 --- a/testng/src/test/resources/parametrized_test.xml +++ b/testng/src/test/resources/parametrized_testng.xml @@ -1,10 +1,13 @@ - - - - - - - - - + + + + + + + + + + + + \ No newline at end of file diff --git a/testng/src/test/resources/test_group.xml b/testng/src/test/resources/test_group.xml index 26868375f2..77edb85cdc 100644 --- a/testng/src/test/resources/test_group.xml +++ b/testng/src/test/resources/test_group.xml @@ -1,13 +1,13 @@ - - - - - - - - - - - - + + + + + + + + + + + + \ No newline at end of file diff --git a/testng/src/test/resources/test_setup.xml b/testng/src/test/resources/test_setup.xml index 7d9708193e..9197f9fd0e 100644 --- a/testng/src/test/resources/test_setup.xml +++ b/testng/src/test/resources/test_setup.xml @@ -1,17 +1,17 @@ - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/testng/src/test/resources/test_suite.xml b/testng/src/test/resources/test_suite.xml index 0fe5d1cc40..0ccbbd2714 100644 --- a/testng/src/test/resources/test_suite.xml +++ b/testng/src/test/resources/test_suite.xml @@ -1,9 +1,12 @@ - - - - - - - - + + + + + + + + + + + \ No newline at end of file