diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000000..c2a369a1b3 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,17 @@ +language: java + +install: travis_wait 40 mvn -q clean install -Dgib.enabled=true + +jdk: + - oraclejdk8 + +sudo: false +addons: + apt: + packages: + - oracle-java8-installer + +cache: + directories: + - .autoconf + - $HOME/.m2 \ No newline at end of file diff --git a/apache-poi/README.md b/apache-poi/README.md index cef6810c97..10fe8ba2e7 100644 --- a/apache-poi/README.md +++ b/apache-poi/README.md @@ -1,2 +1,3 @@ ### Relevant Articles: - [Microsoft Word Processing in Java with Apache POI](http://www.baeldung.com/java-microsoft-word-with-apache-poi) +- [Working with Microsoft Excel in Java](http://www.baeldung.com/java-microsoft-excel) diff --git a/apache-poi/pom.xml b/apache-poi/pom.xml index d94b2e2ad1..d8a2cc72e0 100644 --- a/apache-poi/pom.xml +++ b/apache-poi/pom.xml @@ -9,6 +9,7 @@ 3.6.0 4.12 3.15 + 1.0.6 @@ -37,5 +38,10 @@ poi-ooxml ${poi.version} + + org.jxls + jxls-jexcel + ${jexcel.version} + diff --git a/apache-poi/src/main/java/com/baeldung/jexcel/JExcelHelper.java b/apache-poi/src/main/java/com/baeldung/jexcel/JExcelHelper.java new file mode 100644 index 0000000000..4ad3fc766c --- /dev/null +++ b/apache-poi/src/main/java/com/baeldung/jexcel/JExcelHelper.java @@ -0,0 +1,74 @@ +package com.baeldung.jexcel; + +import jxl.*; +import java.util.Map; +import java.util.HashMap; +import java.util.ArrayList; +import java.util.List; +import jxl.read.biff.BiffException; +import java.io.File; +import java.io.IOException; +import jxl.write.*; +import jxl.write.Number; +import jxl.format.Colour; + +public class JExcelHelper { + + public Map> readJExcel(String fileLocation) throws IOException, BiffException { + Map> data = new HashMap<>(); + + Workbook workbook = Workbook.getWorkbook(new File(fileLocation)); + Sheet sheet = workbook.getSheet(0); + int rows = sheet.getRows(); + int columns = sheet.getColumns(); + + for (int i = 0; i < rows; i++) { + data.put(i, new ArrayList()); + for (int j = 0; j < columns; j++) { + data.get(i).add(sheet.getCell(j, i).getContents()); + } + } + return data; + } + + public void writeJExcel() throws IOException, WriteException { + WritableWorkbook workbook = null; + try { + File currDir = new File("."); + String path = currDir.getAbsolutePath(); + String fileLocation = path.substring(0, path.length() - 1) + "temp.xls"; + + workbook = Workbook.createWorkbook(new File(fileLocation)); + + WritableSheet sheet = workbook.createSheet("Sheet 1", 0); + + WritableCellFormat headerFormat = new WritableCellFormat(); + WritableFont font = new WritableFont(WritableFont.ARIAL, 16, WritableFont.BOLD); + headerFormat.setFont(font); + headerFormat.setBackground(Colour.LIGHT_BLUE); + headerFormat.setWrap(true); + Label headerLabel = new Label(0, 0, "Name", headerFormat); + sheet.setColumnView(0, 60); + sheet.addCell(headerLabel); + + headerLabel = new Label(1, 0, "Age", headerFormat); + sheet.setColumnView(0, 40); + sheet.addCell(headerLabel); + + WritableCellFormat cellFormat = new WritableCellFormat(); + cellFormat.setWrap(true); + + Label cellLabel = new Label(0, 2, "John Smith", cellFormat); + sheet.addCell(cellLabel); + Number cellNumber = new Number(1, 2, 20, cellFormat); + sheet.addCell(cellNumber); + + workbook.write(); + } finally { + if (workbook != null) { + workbook.close(); + } + } + + } +} \ No newline at end of file diff --git a/apache-poi/src/main/java/com/baeldung/poi/excel/ExcelPOIHelper.java b/apache-poi/src/main/java/com/baeldung/poi/excel/ExcelPOIHelper.java new file mode 100644 index 0000000000..b6b0cbef20 --- /dev/null +++ b/apache-poi/src/main/java/com/baeldung/poi/excel/ExcelPOIHelper.java @@ -0,0 +1,128 @@ +package com.baeldung.poi.excel; + +import org.apache.poi.ss.usermodel.Cell; +import org.apache.poi.ss.usermodel.CellType; +import org.apache.poi.ss.usermodel.CellStyle; +import org.apache.poi.ss.usermodel.IndexedColors; +import org.apache.poi.ss.usermodel.Row; +import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.ss.usermodel.Workbook; +import org.apache.poi.xssf.usermodel.XSSFWorkbook; +import org.apache.poi.xssf.usermodel.XSSFFont; +import org.apache.poi.ss.usermodel.DateUtil; +import org.apache.poi.ss.usermodel.FillPatternType; +import java.io.File; +import java.io.FileOutputStream; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.Map; +import java.util.HashMap; +import java.util.ArrayList; +import java.util.List; + +public class ExcelPOIHelper { + + public Map> readExcel(String fileLocation) throws IOException { + + Map> data = new HashMap<>(); + FileInputStream file = new FileInputStream(new File(fileLocation)); + Workbook workbook = new XSSFWorkbook(file); + Sheet sheet = workbook.getSheetAt(0); + int i = 0; + for (Row row : sheet) { + data.put(i, new ArrayList()); + for (Cell cell : row) { + switch (cell.getCellTypeEnum()) { + case STRING: + data.get(i) + .add(cell.getRichStringCellValue() + .getString()); + break; + case NUMERIC: + if (DateUtil.isCellDateFormatted(cell)) { + data.get(i) + .add(cell.getDateCellValue() + ""); + } else { + data.get(i) + .add((int)cell.getNumericCellValue() + ""); + } + break; + case BOOLEAN: + data.get(i) + .add(cell.getBooleanCellValue() + ""); + break; + case FORMULA: + data.get(i) + .add(cell.getCellFormula() + ""); + break; + default: + data.get(i) + .add(" "); + } + } + i++; + } + if (workbook != null){ + workbook.close(); + } + return data; + } + + public void writeExcel() throws IOException { + Workbook workbook = new XSSFWorkbook(); + + try { + Sheet sheet = workbook.createSheet("Persons"); + sheet.setColumnWidth(0, 6000); + sheet.setColumnWidth(1, 4000); + + Row header = sheet.createRow(0); + + CellStyle headerStyle = workbook.createCellStyle(); + + headerStyle.setFillForegroundColor(IndexedColors.LIGHT_BLUE.getIndex()); + headerStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND); + + XSSFFont font = ((XSSFWorkbook) workbook).createFont(); + font.setFontName("Arial"); + font.setFontHeightInPoints((short) 16); + font.setBold(true); + headerStyle.setFont(font); + + Cell headerCell = header.createCell(0); + headerCell.setCellValue("Name"); + headerCell.setCellStyle(headerStyle); + + headerCell = header.createCell(1); + headerCell.setCellValue("Age"); + headerCell.setCellStyle(headerStyle); + + CellStyle style = workbook.createCellStyle(); + style.setWrapText(true); + + Row row = sheet.createRow(2); + Cell cell = row.createCell(0); + cell.setCellValue("John Smith"); + cell.setCellStyle(style); + + cell = row.createCell(1); + cell.setCellValue(20); + cell.setCellStyle(style); + + File currDir = new File("."); + String path = currDir.getAbsolutePath(); + String fileLocation = path.substring(0, path.length() - 1) + "temp.xlsx"; + + FileOutputStream outputStream = new FileOutputStream(fileLocation); + workbook.write(outputStream); + } finally { + if (workbook != null) { + + workbook.close(); + + } + } + } + +} \ No newline at end of file diff --git a/apache-poi/src/test/java/com/baeldung/jexcel/JExcelTest.java b/apache-poi/src/test/java/com/baeldung/jexcel/JExcelTest.java new file mode 100644 index 0000000000..8ee465be34 --- /dev/null +++ b/apache-poi/src/test/java/com/baeldung/jexcel/JExcelTest.java @@ -0,0 +1,56 @@ +package com.baeldung.jexcel; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import jxl.read.biff.BiffException; +import java.util.Map; +import java.util.ArrayList; +import java.util.List; + +import com.baeldung.jexcel.JExcelHelper; + +import jxl.write.WriteException; +import jxl.read.biff.BiffException; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import org.junit.Test; +import org.junit.Before; + +public class JExcelTest { + + private JExcelHelper jExcelHelper; + private static String FILE_NAME = "temp.xls"; + private String fileLocation; + + @Before + public void generateExcelFile() throws IOException, WriteException { + + File currDir = new File("."); + String path = currDir.getAbsolutePath(); + fileLocation = path.substring(0, path.length() - 1) + FILE_NAME; + + jExcelHelper = new JExcelHelper(); + jExcelHelper.writeJExcel(); + + } + + @Test + public void whenParsingJExcelFile_thenCorrect() throws IOException, BiffException { + Map> data = jExcelHelper.readJExcel(fileLocation); + + assertEquals("Name", data.get(0) + .get(0)); + assertEquals("Age", data.get(0) + .get(1)); + + assertEquals("John Smith", data.get(2) + .get(0)); + assertEquals("20", data.get(2) + .get(1)); + } + +} \ No newline at end of file diff --git a/apache-poi/src/test/java/com/baeldung/poi/excel/ExcelTest.java b/apache-poi/src/test/java/com/baeldung/poi/excel/ExcelTest.java new file mode 100644 index 0000000000..34fa64dd94 --- /dev/null +++ b/apache-poi/src/test/java/com/baeldung/poi/excel/ExcelTest.java @@ -0,0 +1,53 @@ +package com.baeldung.poi.excel; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import jxl.read.biff.BiffException; +import java.util.Map; +import java.util.ArrayList; +import java.util.List; + +import com.baeldung.poi.excel.ExcelPOIHelper; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import org.junit.Test; +import org.junit.Before; + +public class ExcelTest { + + private ExcelPOIHelper excelPOIHelper; + private static String FILE_NAME = "temp.xlsx"; + private String fileLocation; + + @Before + public void generateExcelFile() throws IOException { + + File currDir = new File("."); + String path = currDir.getAbsolutePath(); + fileLocation = path.substring(0, path.length() - 1) + FILE_NAME; + + excelPOIHelper = new ExcelPOIHelper(); + excelPOIHelper.writeExcel(); + + } + + @Test + public void whenParsingPOIExcelFile_thenCorrect() throws IOException { + Map> data = excelPOIHelper.readExcel(fileLocation); + + assertEquals("Name", data.get(0) + .get(0)); + assertEquals("Age", data.get(0) + .get(1)); + + assertEquals("John Smith", data.get(1) + .get(0)); + assertEquals("20", data.get(1) + .get(1)); + } + +} \ No newline at end of file diff --git a/apache-poi/temp.xls b/apache-poi/temp.xls new file mode 100644 index 0000000000..1fad76d88d Binary files /dev/null and b/apache-poi/temp.xls differ diff --git a/apache-poi/temp.xlsx b/apache-poi/temp.xlsx new file mode 100644 index 0000000000..5281b2c4de Binary files /dev/null and b/apache-poi/temp.xlsx differ diff --git a/core-java/0.004102810554955205 b/core-java/0.004102810554955205 new file mode 100644 index 0000000000..e69de29bb2 diff --git a/core-java/0.04832801936270381 b/core-java/0.04832801936270381 new file mode 100644 index 0000000000..e69de29bb2 diff --git a/core-java/0.5633433244738808 b/core-java/0.5633433244738808 new file mode 100644 index 0000000000..e69de29bb2 diff --git a/core-java/0.5967303215007616 b/core-java/0.5967303215007616 new file mode 100644 index 0000000000..e69de29bb2 diff --git a/core-java/0.6256429734439612 b/core-java/0.6256429734439612 new file mode 100644 index 0000000000..e69de29bb2 diff --git a/core-java/0.9252611327674576 b/core-java/0.9252611327674576 new file mode 100644 index 0000000000..e69de29bb2 diff --git a/core-java/0.9799201796740292 b/core-java/0.9799201796740292 new file mode 100644 index 0000000000..e69de29bb2 diff --git a/core-java/README.md b/core-java/README.md index cd16935864..341dbdf910 100644 --- a/core-java/README.md +++ b/core-java/README.md @@ -57,3 +57,4 @@ - [Guide to java.util.concurrent.Future](http://www.baeldung.com/java-future) - [Guide to java.util.concurrent.BlockingQueue](http://www.baeldung.com/java-blocking-queue) - [Guide to CountDownLatch in Java](http://www.baeldung.com/java-countdown-latch) +- [How to Design a Genetic Algorithm in Java](http://www.baeldung.com/java-genetic-algorithm) diff --git a/core-java/pom.xml b/core-java/pom.xml index 85afee2968..2b6f065c85 100644 --- a/core-java/pom.xml +++ b/core-java/pom.xml @@ -1,5 +1,5 @@ + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 com.baeldung core-java @@ -10,6 +10,44 @@ + + org.neo4j + neo4j + 3.1.0 + + + + org.neo4j.driver + neo4j-java-driver + 1.1.1 + + + + org.neo4j + neo4j-jdbc-driver + 3.0.1 + + + + org.neo4j + neo4j-ogm-core + 2.1.1 + + + + org.neo4j + neo4j-ogm-embedded-driver + 2.1.1 + + + + com.google.inject + guice + 4.1.0 + no_aop + test + + net.sourceforge.collections @@ -63,7 +101,6 @@ grep4j ${grep4j.version} - @@ -154,6 +191,12 @@ ${mockito.version} test + + com.jayway.awaitility + awaitility + ${avaitility.version} + test + commons-codec @@ -263,7 +306,8 @@ true - + org.baeldung.executable.ExecutableMavenJar @@ -371,6 +415,7 @@ 1.10.19 6.10 3.6.1 + 1.7.0 3.6.0 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 6d696dd272..3f7c8bf4b3 100644 --- a/core-java/src/main/java/com/baeldung/algorithms/RunAlgorithm.java +++ b/core-java/src/main/java/com/baeldung/algorithms/RunAlgorithm.java @@ -24,7 +24,8 @@ public class RunAlgorithm { SlopeOne.slopeOne(3); break; case 3: - SimpleGeneticAlgorithm.runAlgorithm(50, "1011000100000100010000100000100111001000000100000100000000001111"); + SimpleGeneticAlgorithm ga = new SimpleGeneticAlgorithm(); + ga.runAlgorithm(50, "1011000100000100010000100000100111001000000100000100000000001111"); break; default: System.out.println("Unknown option"); diff --git a/core-java/src/main/java/com/baeldung/algorithms/ga/binary/Individual.java b/core-java/src/main/java/com/baeldung/algorithms/ga/binary/Individual.java index e9b6c8f66a..2a740777f3 100644 --- a/core-java/src/main/java/com/baeldung/algorithms/ga/binary/Individual.java +++ b/core-java/src/main/java/com/baeldung/algorithms/ga/binary/Individual.java @@ -5,40 +5,40 @@ import lombok.Data; @Data public class Individual { - protected int defaultGeneLength = 64; - private byte[] genes = new byte[defaultGeneLength]; - private int fitness = 0; + protected int defaultGeneLength = 64; + private byte[] genes = new byte[defaultGeneLength]; + private int fitness = 0; - public Individual() { - for (int i = 0; i < genes.length; i++) { - byte gene = (byte) Math.round(Math.random()); - genes[i] = gene; - } - } + public Individual() { + for (int i = 0; i < genes.length; i++) { + byte gene = (byte) Math.round(Math.random()); + genes[i] = gene; + } + } - protected byte getSingleGene(int index) { - return genes[index]; - } + protected byte getSingleGene(int index) { + return genes[index]; + } - protected void setSingleGene(int index, byte value) { - genes[index] = value; - fitness = 0; - } + protected void setSingleGene(int index, byte value) { + genes[index] = value; + fitness = 0; + } - public int getFitness() { - if (fitness == 0) { - fitness = SimpleGeneticAlgorithm.getFitness(this); - } - return fitness; - } + public int getFitness() { + if (fitness == 0) { + fitness = SimpleGeneticAlgorithm.getFitness(this); + } + return fitness; + } - @Override - public String toString() { - String geneString = ""; - for (int i = 0; i < genes.length; i++) { - geneString += getSingleGene(i); - } - return geneString; - } + @Override + public String toString() { + String geneString = ""; + for (int i = 0; i < genes.length; i++) { + geneString += getSingleGene(i); + } + return geneString; + } } diff --git a/core-java/src/main/java/com/baeldung/algorithms/ga/binary/Population.java b/core-java/src/main/java/com/baeldung/algorithms/ga/binary/Population.java index dbd1e04e0f..47677d7d88 100644 --- a/core-java/src/main/java/com/baeldung/algorithms/ga/binary/Population.java +++ b/core-java/src/main/java/com/baeldung/algorithms/ga/binary/Population.java @@ -8,33 +8,33 @@ import lombok.Data; @Data public class Population { - private List individuals; + private List individuals; - public Population(int size, boolean createNew) { - individuals = new ArrayList<>(); - if (createNew) { - createNewPopulation(size); - } - } + public Population(int size, boolean createNew) { + individuals = new ArrayList<>(); + if (createNew) { + createNewPopulation(size); + } + } - protected Individual getIndividual(int index) { - return individuals.get(index); - } + protected Individual getIndividual(int index) { + return individuals.get(index); + } - protected Individual getFittest() { - Individual fittest = individuals.get(0); - for (int i = 0; i < individuals.size(); i++) { - if (fittest.getFitness() <= getIndividual(i).getFitness()) { - fittest = getIndividual(i); - } - } - return fittest; - } + protected Individual getFittest() { + Individual fittest = individuals.get(0); + for (int i = 0; i < individuals.size(); i++) { + if (fittest.getFitness() <= getIndividual(i).getFitness()) { + fittest = getIndividual(i); + } + } + return fittest; + } - private void createNewPopulation(int size) { - for (int i = 0; i < size; i++) { - Individual newIndividual = new Individual(); - individuals.add(i, newIndividual); - } - } + private void createNewPopulation(int size) { + for (int i = 0; i < size; i++) { + Individual newIndividual = new Individual(); + individuals.add(i, newIndividual); + } + } } diff --git a/core-java/src/main/java/com/baeldung/algorithms/ga/binary/SimpleGeneticAlgorithm.java b/core-java/src/main/java/com/baeldung/algorithms/ga/binary/SimpleGeneticAlgorithm.java index 0f640e676a..e62eab0d57 100644 --- a/core-java/src/main/java/com/baeldung/algorithms/ga/binary/SimpleGeneticAlgorithm.java +++ b/core-java/src/main/java/com/baeldung/algorithms/ga/binary/SimpleGeneticAlgorithm.java @@ -5,115 +5,113 @@ import lombok.Data; @Data public class SimpleGeneticAlgorithm { - private static final double uniformRate = 0.5; - private static final double mutationRate = 0.025; - private static final int tournamentSize = 5; - private static final boolean elitism = true; - private static byte[] solution = new byte[64]; + private static final double uniformRate = 0.5; + private static final double mutationRate = 0.025; + private static final int tournamentSize = 5; + private static final boolean elitism = true; + private static byte[] solution = new byte[64]; - public static boolean runAlgorithm(int populationSize, String solution) { - if (solution.length() != SimpleGeneticAlgorithm.solution.length) { - throw new RuntimeException( - "The solution needs to have " + SimpleGeneticAlgorithm.solution.length + " bytes"); - } - SimpleGeneticAlgorithm.setSolution(solution); - Population myPop = new Population(populationSize, true); + public boolean runAlgorithm(int populationSize, String solution) { + if (solution.length() != SimpleGeneticAlgorithm.solution.length) { + throw new RuntimeException("The solution needs to have " + SimpleGeneticAlgorithm.solution.length + " bytes"); + } + setSolution(solution); + Population myPop = new Population(populationSize, true); - int generationCount = 1; - while (myPop.getFittest().getFitness() < SimpleGeneticAlgorithm.getMaxFitness()) { - System.out.println( - "Generation: " + generationCount + " Correct genes found: " + myPop.getFittest().getFitness()); - myPop = SimpleGeneticAlgorithm.evolvePopulation(myPop); - generationCount++; - } - System.out.println("Solution found!"); - System.out.println("Generation: " + generationCount); - System.out.println("Genes: "); - System.out.println(myPop.getFittest()); - return true; - } + int generationCount = 1; + while (myPop.getFittest().getFitness() < getMaxFitness()) { + System.out.println("Generation: " + generationCount + " Correct genes found: " + myPop.getFittest().getFitness()); + myPop = evolvePopulation(myPop); + generationCount++; + } + System.out.println("Solution found!"); + System.out.println("Generation: " + generationCount); + System.out.println("Genes: "); + System.out.println(myPop.getFittest()); + return true; + } - public static Population evolvePopulation(Population pop) { - int elitismOffset; - Population newPopulation = new Population(pop.getIndividuals().size(), false); + public Population evolvePopulation(Population pop) { + int elitismOffset; + Population newPopulation = new Population(pop.getIndividuals().size(), false); - if (elitism) { - newPopulation.getIndividuals().add(0, pop.getFittest()); - elitismOffset = 1; - } else { - elitismOffset = 0; - } + if (elitism) { + newPopulation.getIndividuals().add(0, pop.getFittest()); + elitismOffset = 1; + } else { + elitismOffset = 0; + } - for (int i = elitismOffset; i < pop.getIndividuals().size(); i++) { - Individual indiv1 = tournamentSelection(pop); - Individual indiv2 = tournamentSelection(pop); - Individual newIndiv = crossover(indiv1, indiv2); - newPopulation.getIndividuals().add(i, newIndiv); - } + for (int i = elitismOffset; i < pop.getIndividuals().size(); i++) { + Individual indiv1 = tournamentSelection(pop); + Individual indiv2 = tournamentSelection(pop); + Individual newIndiv = crossover(indiv1, indiv2); + newPopulation.getIndividuals().add(i, newIndiv); + } - for (int i = elitismOffset; i < newPopulation.getIndividuals().size(); i++) { - mutate(newPopulation.getIndividual(i)); - } + for (int i = elitismOffset; i < newPopulation.getIndividuals().size(); i++) { + mutate(newPopulation.getIndividual(i)); + } - return newPopulation; - } + return newPopulation; + } - private static Individual crossover(Individual indiv1, Individual indiv2) { - Individual newSol = new Individual(); - for (int i = 0; i < newSol.getDefaultGeneLength(); i++) { - if (Math.random() <= uniformRate) { - newSol.setSingleGene(i, indiv1.getSingleGene(i)); - } else { - newSol.setSingleGene(i, indiv2.getSingleGene(i)); - } - } - return newSol; - } + private Individual crossover(Individual indiv1, Individual indiv2) { + Individual newSol = new Individual(); + for (int i = 0; i < newSol.getDefaultGeneLength(); i++) { + if (Math.random() <= uniformRate) { + newSol.setSingleGene(i, indiv1.getSingleGene(i)); + } else { + newSol.setSingleGene(i, indiv2.getSingleGene(i)); + } + } + return newSol; + } - private static void mutate(Individual indiv) { - for (int i = 0; i < indiv.getDefaultGeneLength(); i++) { - if (Math.random() <= mutationRate) { - byte gene = (byte) Math.round(Math.random()); - indiv.setSingleGene(i, gene); - } - } - } + private void mutate(Individual indiv) { + for (int i = 0; i < indiv.getDefaultGeneLength(); i++) { + if (Math.random() <= mutationRate) { + byte gene = (byte) Math.round(Math.random()); + indiv.setSingleGene(i, gene); + } + } + } - private static Individual tournamentSelection(Population pop) { - Population tournament = new Population(tournamentSize, false); - for (int i = 0; i < tournamentSize; i++) { - int randomId = (int) (Math.random() * pop.getIndividuals().size()); - tournament.getIndividuals().add(i, pop.getIndividual(randomId)); - } - Individual fittest = tournament.getFittest(); - return fittest; - } + private Individual tournamentSelection(Population pop) { + Population tournament = new Population(tournamentSize, false); + for (int i = 0; i < tournamentSize; i++) { + int randomId = (int) (Math.random() * pop.getIndividuals().size()); + tournament.getIndividuals().add(i, pop.getIndividual(randomId)); + } + Individual fittest = tournament.getFittest(); + return fittest; + } - protected static int getFitness(Individual individual) { - int fitness = 0; - for (int i = 0; i < individual.getDefaultGeneLength() && i < solution.length; i++) { - if (individual.getSingleGene(i) == solution[i]) { - fitness++; - } - } - return fitness; - } + protected static int getFitness(Individual individual) { + int fitness = 0; + for (int i = 0; i < individual.getDefaultGeneLength() && i < solution.length; i++) { + if (individual.getSingleGene(i) == solution[i]) { + fitness++; + } + } + return fitness; + } - protected static int getMaxFitness() { - int maxFitness = solution.length; - return maxFitness; - } + protected int getMaxFitness() { + int maxFitness = solution.length; + return maxFitness; + } - protected static void setSolution(String newSolution) { - solution = new byte[newSolution.length()]; - for (int i = 0; i < newSolution.length(); i++) { - String character = newSolution.substring(i, i + 1); - if (character.contains("0") || character.contains("1")) { - solution[i] = Byte.parseByte(character); - } else { - solution[i] = 0; - } - } - } + protected void setSolution(String newSolution) { + solution = new byte[newSolution.length()]; + for (int i = 0; i < newSolution.length(); i++) { + String character = newSolution.substring(i, i + 1); + if (character.contains("0") || character.contains("1")) { + solution[i] = Byte.parseByte(character); + } else { + solution[i] = 0; + } + } + } } diff --git a/core-java/src/main/java/com/baeldung/concurrent/future/SquareCalculator.java b/core-java/src/main/java/com/baeldung/concurrent/future/SquareCalculator.java index e53a2413d1..bcd559dd3b 100644 --- a/core-java/src/main/java/com/baeldung/concurrent/future/SquareCalculator.java +++ b/core-java/src/main/java/com/baeldung/concurrent/future/SquareCalculator.java @@ -15,6 +15,6 @@ public class SquareCalculator { return executor.submit(() -> { Thread.sleep(1000); return input * input; - }); + }); } } diff --git a/core-java/src/main/java/com/baeldung/graph/Car.java b/core-java/src/main/java/com/baeldung/graph/Car.java new file mode 100644 index 0000000000..1dc65a0d4b --- /dev/null +++ b/core-java/src/main/java/com/baeldung/graph/Car.java @@ -0,0 +1,50 @@ +package com.baeldung.graph; + +import org.neo4j.ogm.annotation.GraphId; +import org.neo4j.ogm.annotation.NodeEntity; +import org.neo4j.ogm.annotation.Relationship; + +/** + * @author Danil Kornishev (danil.kornishev@mastercard.com) + */ +@NodeEntity +public class Car { + @GraphId + private Long id; + + private String make; + + @Relationship(direction = "INCOMING") + private Company company; + + public Car(String make, String model) { + this.make = make; + this.model = model; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getMake() { + return make; + } + + public void setMake(String make) { + this.make = make; + } + + public String getModel() { + return model; + } + + public void setModel(String model) { + this.model = model; + } + + private String model; +} diff --git a/core-java/src/main/java/com/baeldung/graph/Company.java b/core-java/src/main/java/com/baeldung/graph/Company.java new file mode 100644 index 0000000000..1fe892b331 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/graph/Company.java @@ -0,0 +1,45 @@ +package com.baeldung.graph; + +import org.neo4j.ogm.annotation.NodeEntity; +import org.neo4j.ogm.annotation.Relationship; + +/** + * @author Danil Kornishev (danil.kornishev@mastercard.com) + */ +@NodeEntity +public class Company { + private Long id; + + private String name; + + @Relationship(type="owns") + private Car car; + + public Company(String name) { + this.name = name; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Car getCar() { + return car; + } + + public void setCar(Car car) { + this.car = car; + } +} diff --git a/core-java/src/main/java/com/baeldung/java_8_features/groupingby/BlogPost.java b/core-java/src/main/java/com/baeldung/java_8_features/groupingby/BlogPost.java new file mode 100644 index 0000000000..afc05e356a --- /dev/null +++ b/core-java/src/main/java/com/baeldung/java_8_features/groupingby/BlogPost.java @@ -0,0 +1,36 @@ +package com.baeldung.java_8_features.groupingby; + +public class BlogPost { + private String title; + private String author; + private BlogPostType type; + private int likes; + + public BlogPost(String title, String author, BlogPostType type, int likes) { + this.title = title; + this.author = author; + this.type = type; + this.likes = likes; + } + + public String getTitle() { + return title; + } + + public String getAuthor() { + return author; + } + + public BlogPostType getType() { + return type; + } + + public int getLikes() { + return likes; + } + + @Override + public String toString() { + return "BlogPost{" + "title='" + title + '\'' + ", type=" + type + ", likes=" + likes + '}'; + } +} diff --git a/core-java/src/main/java/com/baeldung/java_8_features/groupingby/BlogPostType.java b/core-java/src/main/java/com/baeldung/java_8_features/groupingby/BlogPostType.java new file mode 100644 index 0000000000..2029784e91 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/java_8_features/groupingby/BlogPostType.java @@ -0,0 +1,5 @@ +package com.baeldung.java_8_features.groupingby; + +public enum BlogPostType { + NEWS, REVIEW, GUIDE +} diff --git a/core-java/src/main/java/com/baeldung/strategy/ChristmasDiscounter.java b/core-java/src/main/java/com/baeldung/strategy/ChristmasDiscounter.java new file mode 100644 index 0000000000..a0c36bb63e --- /dev/null +++ b/core-java/src/main/java/com/baeldung/strategy/ChristmasDiscounter.java @@ -0,0 +1,11 @@ +package com.baeldung.strategy; + +import java.math.BigDecimal; + +public class ChristmasDiscounter implements Discounter { + + @Override + public BigDecimal apply(BigDecimal amount) { + return amount.multiply(BigDecimal.valueOf(0.9)); + } +} diff --git a/core-java/src/main/java/com/baeldung/strategy/Discounter.java b/core-java/src/main/java/com/baeldung/strategy/Discounter.java new file mode 100644 index 0000000000..00bf4855d1 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/strategy/Discounter.java @@ -0,0 +1,23 @@ +package com.baeldung.strategy; + +import java.math.BigDecimal; +import java.util.function.UnaryOperator; + +public interface Discounter extends UnaryOperator { + + default Discounter combine(Discounter after) { + return value -> after.apply(this.apply(value)); + } + + static Discounter christmas() { + return (amount) -> amount.multiply(BigDecimal.valueOf(0.9)); + } + + static Discounter newYear() { + return (amount) -> amount.multiply(BigDecimal.valueOf(0.8)); + } + + static Discounter easter() { + return (amount) -> amount.multiply(BigDecimal.valueOf(0.5)); + } +} diff --git a/core-java/src/main/java/com/baeldung/strategy/EasterDiscounter.java b/core-java/src/main/java/com/baeldung/strategy/EasterDiscounter.java new file mode 100644 index 0000000000..990d10073b --- /dev/null +++ b/core-java/src/main/java/com/baeldung/strategy/EasterDiscounter.java @@ -0,0 +1,11 @@ +package com.baeldung.strategy; + +import java.math.BigDecimal; + +public class EasterDiscounter implements Discounter { + + @Override + public BigDecimal apply(BigDecimal amount) { + return amount.multiply(BigDecimal.valueOf(0.5)); + } +} diff --git a/core-java/src/main/java/com/baeldung/stream/InfiniteStreams.java b/core-java/src/main/java/com/baeldung/stream/InfiniteStreams.java new file mode 100644 index 0000000000..097b516f8c --- /dev/null +++ b/core-java/src/main/java/com/baeldung/stream/InfiniteStreams.java @@ -0,0 +1,27 @@ +package com.baeldung.stream; + + +import java.util.stream.Stream; + +public class InfiniteStreams { + public static void main(String[] args) { + doWhileOldWay(); + + doWhileStreamWay(); + + } + + private static void doWhileOldWay() { + + int i = 0; + while (i < 10) { + System.out.println(i); + i++; + } + } + + private static void doWhileStreamWay() { + Stream integers = Stream.iterate(0, i -> i + 1); + integers.limit(10).forEach(System.out::println); + } +} diff --git a/core-java/src/main/java/com/baeldung/string/JoinerSplitter.java b/core-java/src/main/java/com/baeldung/string/JoinerSplitter.java new file mode 100644 index 0000000000..085be66801 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/string/JoinerSplitter.java @@ -0,0 +1,36 @@ +package com.baeldung.string; + +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +public class JoinerSplitter { + + public static String join ( String[] arrayOfString ) { + return Arrays.asList(arrayOfString) + .stream() + .map(x -> x) + .collect(Collectors.joining(",")); + } + + public static String joinWithPrefixPostFix ( String[] arrayOfString ) { + return Arrays.asList(arrayOfString) + .stream() + .map(x -> x) + .collect(Collectors.joining(",","[","]")); + } + + public static List split ( String str ) { + return Stream.of(str.split(",")) + .map (elem -> new String(elem)) + .collect(Collectors.toList()); + } + + public static List splitToListOfChar ( String str ) { + return str.chars() + .mapToObj(item -> (char) item) + .collect(Collectors.toList()); + } + +} diff --git a/core-java/src/test/java/com/baeldung/CharArrayToStringUnitTest.java b/core-java/src/test/java/com/baeldung/CharArrayToStringUnitTest.java index 2f7830bbf4..3488f8b390 100644 --- a/core-java/src/test/java/com/baeldung/CharArrayToStringUnitTest.java +++ b/core-java/src/test/java/com/baeldung/CharArrayToStringUnitTest.java @@ -8,55 +8,55 @@ public class CharArrayToStringUnitTest { @Test public void givenCharArray_whenCallingStringConstructor_shouldConvertToString() { - char[] charArray = {'c', 'h', 'a', 'r', 'a', 'c', 't', 'e', 'r'}; + char[] charArray = { 'c', 'h', 'a', 'r', 'a', 'c', 't', 'e', 'r' }; String result = new String(charArray); String expectedValue = "character"; - + assertEquals(expectedValue, result); } - + @Test - public void givenCharArray_whenCallingStringConstructorWithOffsetAndLength_shouldConvertToString(){ - char[] charArray = {'c', 'h', 'a', 'r', 'a', 'c', 't', 'e', 'r'}; + public void givenCharArray_whenCallingStringConstructorWithOffsetAndLength_shouldConvertToString() { + char[] charArray = { 'c', 'h', 'a', 'r', 'a', 'c', 't', 'e', 'r' }; String result = new String(charArray, 4, 3); String expectedValue = "act"; - + assertEquals(expectedValue, result); } - + @Test - public void givenCharArray_whenCallingStringCopyValueOf_shouldConvertToString(){ - char[] charArray = {'c', 'h', 'a', 'r', 'a', 'c', 't', 'e', 'r'}; + public void givenCharArray_whenCallingStringCopyValueOf_shouldConvertToString() { + char[] charArray = { 'c', 'h', 'a', 'r', 'a', 'c', 't', 'e', 'r' }; String result = String.copyValueOf(charArray); String expectedValue = "character"; - + assertEquals(expectedValue, result); } - + @Test - public void givenCharArray_whenCallingStringCopyValueOfWithOffsetAndLength_shouldConvertToString(){ - char[] charArray = {'c', 'h', 'a', 'r', 'a', 'c', 't', 'e', 'r'}; + public void givenCharArray_whenCallingStringCopyValueOfWithOffsetAndLength_shouldConvertToString() { + char[] charArray = { 'c', 'h', 'a', 'r', 'a', 'c', 't', 'e', 'r' }; String result = String.copyValueOf(charArray, 0, 4); String expectedValue = "char"; - + assertEquals(expectedValue, result); } - + @Test - public void givenCharArray_whenCallingStringValueOf_shouldConvertToString(){ - char[] charArray = {'c', 'h', 'a', 'r', 'a', 'c', 't', 'e', 'r'}; + public void givenCharArray_whenCallingStringValueOf_shouldConvertToString() { + char[] charArray = { 'c', 'h', 'a', 'r', 'a', 'c', 't', 'e', 'r' }; String result = String.valueOf(charArray); String expectedValue = "character"; - + assertEquals(expectedValue, result); } - + @Test - public void givenCharArray_whenCallingStringValueOfWithOffsetAndLength_shouldConvertToString(){ - char[] charArray = {'c', 'h', 'a', 'r', 'a', 'c', 't', 'e', 'r'}; + public void givenCharArray_whenCallingStringValueOfWithOffsetAndLength_shouldConvertToString() { + char[] charArray = { 'c', 'h', 'a', 'r', 'a', 'c', 't', 'e', 'r' }; String result = String.valueOf(charArray, 3, 4); String expectedValue = "ract"; - + assertEquals(expectedValue, result); } } diff --git a/core-java/src/test/java/com/baeldung/StringToCharArrayUnitTest.java b/core-java/src/test/java/com/baeldung/StringToCharArrayUnitTest.java index 2e7dc24a17..cd996e58e2 100644 --- a/core-java/src/test/java/com/baeldung/StringToCharArrayUnitTest.java +++ b/core-java/src/test/java/com/baeldung/StringToCharArrayUnitTest.java @@ -6,15 +6,15 @@ import org.junit.Test; public class StringToCharArrayUnitTest { -@Test -public void givenString_whenCallingStringToCharArray_shouldConvertToCharArray() { - String givenString = "characters"; + @Test + public void givenString_whenCallingStringToCharArray_shouldConvertToCharArray() { + String givenString = "characters"; - char[] result = givenString.toCharArray(); + char[] result = givenString.toCharArray(); - char[] expectedCharArray = { 'c', 'h', 'a', 'r', 'a', 'c', 't', 'e', 'r', 's' }; + char[] expectedCharArray = { 'c', 'h', 'a', 'r', 'a', 'c', 't', 'e', 'r', 's' }; - assertArrayEquals(expectedCharArray, result); -} + assertArrayEquals(expectedCharArray, result); + } } diff --git a/core-java/src/test/java/com/baeldung/algorithms/BinaryGeneticAlgorithmTest.java b/core-java/src/test/java/com/baeldung/algorithms/BinaryGeneticAlgorithmTest.java deleted file mode 100644 index 8a16311dfb..0000000000 --- a/core-java/src/test/java/com/baeldung/algorithms/BinaryGeneticAlgorithmTest.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.baeldung.algorithms; - -import org.junit.Assert; -import org.junit.Test; - -import com.baeldung.algorithms.ga.binary.SimpleGeneticAlgorithm; - -public class BinaryGeneticAlgorithmTest { - - @Test - public void testGA() { - Assert.assertTrue(SimpleGeneticAlgorithm.runAlgorithm(50, - "1011000100000100010000100000100111001000000100000100000000001111")); - } - -} diff --git a/core-java/src/test/java/com/baeldung/algorithms/BinaryGeneticAlgorithmUnitTest.java b/core-java/src/test/java/com/baeldung/algorithms/BinaryGeneticAlgorithmUnitTest.java new file mode 100644 index 0000000000..2e92177c8c --- /dev/null +++ b/core-java/src/test/java/com/baeldung/algorithms/BinaryGeneticAlgorithmUnitTest.java @@ -0,0 +1,16 @@ +package com.baeldung.algorithms; + +import org.junit.Assert; +import org.junit.Test; + +import com.baeldung.algorithms.ga.binary.SimpleGeneticAlgorithm; + +public class BinaryGeneticAlgorithmUnitTest { + + @Test + public void testGA() { + SimpleGeneticAlgorithm ga = new SimpleGeneticAlgorithm(); + Assert.assertTrue(ga.runAlgorithm(50, "1011000100000100010000100000100111001000000100000100000000001111")); + } + +} diff --git a/core-java/src/test/java/com/baeldung/concurrent/priorityblockingqueue/PriorityBlockingQueueUnitTest.java b/core-java/src/test/java/com/baeldung/concurrent/priorityblockingqueue/PriorityBlockingQueueUnitTest.java index 5e6855e18a..0272726465 100644 --- a/core-java/src/test/java/com/baeldung/concurrent/priorityblockingqueue/PriorityBlockingQueueUnitTest.java +++ b/core-java/src/test/java/com/baeldung/concurrent/priorityblockingqueue/PriorityBlockingQueueUnitTest.java @@ -32,13 +32,14 @@ public class PriorityBlockingQueueUnitTest { PriorityBlockingQueue queue = new PriorityBlockingQueue<>(); final Thread thread = new Thread(() -> { - System.out.println("Polling..."); - while (true) { - try { - Integer poll = queue.take(); - System.out.println("Polled: " + poll); - } catch (InterruptedException e) {} - } + System.out.println("Polling..."); + while (true) { + try { + Integer poll = queue.take(); + System.out.println("Polled: " + poll); + } catch (InterruptedException e) { + } + } }); thread.start(); diff --git a/core-java/src/test/java/com/baeldung/graph/Neo4JServerTest.java b/core-java/src/test/java/com/baeldung/graph/Neo4JServerTest.java new file mode 100644 index 0000000000..b41588b71e --- /dev/null +++ b/core-java/src/test/java/com/baeldung/graph/Neo4JServerTest.java @@ -0,0 +1,60 @@ +package com.baeldung.graph; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.ResultSet; +import java.sql.Statement; + +import org.junit.Ignore; +import org.junit.Test; +import org.neo4j.driver.v1.AuthTokens; +import org.neo4j.driver.v1.Driver; +import org.neo4j.driver.v1.GraphDatabase; +import org.neo4j.driver.v1.Session; +import org.neo4j.driver.v1.StatementResult; +import org.testng.Assert; + +@Ignore +public class Neo4JServerTest { + + @Test + public void standAloneDriver() { + Driver driver = GraphDatabase.driver("bolt://localhost:7687", AuthTokens.basic("neo4j", "12345")); + Session session = driver.session(); + + session.run("CREATE (baeldung:Company {name:\"Baeldung\"}) " + + "-[:owns]-> (tesla:Car {make: 'tesla', model: 'modelX'})" + + "RETURN baeldung, tesla"); + + StatementResult result = session.run("MATCH (company:Company)-[:owns]-> (car:Car)" + + "WHERE car.make='tesla' and car.model='modelX'" + + "RETURN company.name"); + + Assert.assertTrue(result.hasNext()); + Assert.assertEquals(result.next().get("company.name").asString(), "Baeldung"); + + session.close(); + driver.close(); + } + + @Test + public void standAloneJdbc() throws Exception { + Connection con = DriverManager.getConnection("jdbc:neo4j:bolt://localhost/?user=neo4j,password=12345,scheme=basic"); + + // Querying + try (Statement stmt = con.createStatement()) { + stmt.execute("CREATE (baeldung:Company {name:\"Baeldung\"}) " + + "-[:owns]-> (tesla:Car {make: 'tesla', model: 'modelX'})" + + "RETURN baeldung, tesla"); + + ResultSet rs = stmt.executeQuery("MATCH (company:Company)-[:owns]-> (car:Car)" + + "WHERE car.make='tesla' and car.model='modelX'" + + "RETURN company.name"); + + while (rs.next()) { + Assert.assertEquals(rs.getString("company.name"), "Baeldung"); + } + } + con.close(); + } +} diff --git a/core-java/src/test/java/com/baeldung/graph/Neo4jOgmTest.java b/core-java/src/test/java/com/baeldung/graph/Neo4jOgmTest.java new file mode 100644 index 0000000000..00bd47d029 --- /dev/null +++ b/core-java/src/test/java/com/baeldung/graph/Neo4jOgmTest.java @@ -0,0 +1,46 @@ +package com.baeldung.graph; + +import org.junit.Test; +import org.neo4j.ogm.config.Configuration; +import org.neo4j.ogm.model.Result; +import org.neo4j.ogm.session.Session; +import org.neo4j.ogm.session.SessionFactory; +import org.testng.Assert; + +import java.util.HashMap; +import java.util.Map; + +/** + * @author Danil Kornishev (danil.kornishev@mastercard.com) + */ +public class Neo4jOgmTest { + + @Test + public void testOgm() { + Configuration conf = new Configuration(); + conf.driverConfiguration().setDriverClassName("org.neo4j.ogm.drivers.embedded.driver.EmbeddedDriver"); + + SessionFactory factory = new SessionFactory(conf, "com.baeldung.graph"); + Session session = factory.openSession(); + + Car tesla = new Car("tesla", "modelS"); + Company baeldung = new Company("baeldung"); + + baeldung.setCar(tesla); + + session.save(baeldung); + + Map params = new HashMap<>(); + params.put("make", "tesla"); + Result result = session.query("MATCH (car:Car) <-[:owns]- (company:Company)" + + " WHERE car.make=$make" + + " RETURN company", params); + + Map firstResult = result.iterator().next(); + + Assert.assertEquals(firstResult.size(), 1); + + Company actual = (Company) firstResult.get("company"); + Assert.assertEquals(actual.getName(), baeldung.getName()); + } +} diff --git a/core-java/src/test/java/com/baeldung/graph/Neo4jTest.java b/core-java/src/test/java/com/baeldung/graph/Neo4jTest.java new file mode 100644 index 0000000000..6956c2c39f --- /dev/null +++ b/core-java/src/test/java/com/baeldung/graph/Neo4jTest.java @@ -0,0 +1,167 @@ +package com.baeldung.graph; + + +import java.io.File; +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.neo4j.graphdb.GraphDatabaseService; +import org.neo4j.graphdb.Label; +import org.neo4j.graphdb.Node; +import org.neo4j.graphdb.NotFoundException; +import org.neo4j.graphdb.RelationshipType; +import org.neo4j.graphdb.Result; +import org.neo4j.graphdb.factory.GraphDatabaseFactory; +import org.testng.Assert; + +public class Neo4jTest { + + private static GraphDatabaseService graphDb; + + @Before + public void setUp() { + GraphDatabaseFactory graphDbFactory = new GraphDatabaseFactory(); + graphDb = graphDbFactory.newEmbeddedDatabase(new File("data/cars")); + } + + @After + public void tearDown() { + graphDb.shutdown(); + } + + @Test + public void testPersonCar() { + graphDb.beginTx(); + Node car = graphDb.createNode(Label.label("Car")); + car.setProperty("make", "tesla"); + car.setProperty("model", "model3"); + + Node owner = graphDb.createNode(Label.label("Person")); + owner.setProperty("firstName", "baeldung"); + owner.setProperty("lastName", "baeldung"); + + owner.createRelationshipTo(car, RelationshipType.withName("owner")); + + Result result = graphDb.execute("MATCH (c:Car) <-[owner]- (p:Person) " + + "WHERE c.make = 'tesla'" + + "RETURN p.firstName, p.lastName"); + + Map firstResult = result.next(); + Assert.assertEquals("baeldung", firstResult.get("p.firstName")); + } + + @Test + public void testCreateNode() { + + graphDb.beginTx(); + + Result result = graphDb.execute("CREATE (baeldung:Company {name:\"Baeldung\"})" + + "RETURN baeldung"); + + Map firstResult = result.next(); + Node firstNode = (Node) firstResult.get("baeldung"); + Assert.assertEquals(firstNode.getProperty("name"), "Baeldung"); + } + + @Test + public void testCreateNodeAndLink() { + graphDb.beginTx(); + + Result result = graphDb.execute("CREATE (baeldung:Company {name:\"Baeldung\"}) " + + "-[:owns]-> (tesla:Car {make: 'tesla', model: 'modelX'})" + + "RETURN baeldung, tesla"); + + Map firstResult = result.next(); + Assert.assertTrue(firstResult.containsKey("baeldung")); + Assert.assertTrue(firstResult.containsKey("tesla")); + } + + @Test + public void testFindAndReturn() { + graphDb.beginTx(); + + graphDb.execute("CREATE (baeldung:Company {name:\"Baeldung\"}) " + + "-[:owns]-> (tesla:Car {make: 'tesla', model: 'modelX'})" + + "RETURN baeldung, tesla"); + + Result result = graphDb.execute("MATCH (company:Company)-[:owns]-> (car:Car)" + + "WHERE car.make='tesla' and car.model='modelX'" + + "RETURN company.name"); + + Map firstResult = result.next(); + Assert.assertEquals(firstResult.get("company.name"), "Baeldung"); + } + + @Test + public void testUpdate() { + graphDb.beginTx(); + + graphDb.execute("CREATE (baeldung:Company {name:\"Baeldung\"}) " + + "-[:owns]-> (tesla:Car {make: 'tesla', model: 'modelX'})" + + "RETURN baeldung, tesla"); + + Result result = graphDb.execute("MATCH (car:Car)" + + "WHERE car.make='tesla'" + + " SET car.milage=120" + + " SET car :Car:Electro" + + " SET car.model=NULL" + + " RETURN car"); + + Map firstResult = result.next(); + Node car = (Node) firstResult.get("car"); + + Assert.assertEquals(car.getProperty("milage"), 120L); + Assert.assertEquals(car.getLabels(), Arrays.asList(Label.label("Car"), Label.label("Electro"))); + + try { + car.getProperty("model"); + Assert.fail(); + } catch (NotFoundException e) { + // expected + } + } + + @Test + public void testDelete() { + graphDb.beginTx(); + + graphDb.execute("CREATE (baeldung:Company {name:\"Baeldung\"}) " + + "-[:owns]-> (tesla:Car {make: 'tesla', model: 'modelX'})" + + "RETURN baeldung, tesla"); + + graphDb.execute("MATCH (company:Company)" + + " WHERE company.name='Baeldung'" + + " DELETE company"); + + Result result = graphDb.execute("MATCH (company:Company)" + + " WHERE company.name='Baeldung'" + + " RETURN company"); + + Assert.assertFalse(result.hasNext()); + } + + @Test + public void testBindings() { + graphDb.beginTx(); + + Map params = new HashMap<>(); + params.put("name", "baeldung"); + params.put("make", "tesla"); + params.put("model", "modelS"); + + Result result = graphDb.execute("CREATE (baeldung:Company {name:$name}) " + + "-[:owns]-> (tesla:Car {make: $make, model: $model})" + + "RETURN baeldung, tesla", params); + + Map firstResult = result.next(); + Assert.assertTrue(firstResult.containsKey("baeldung")); + Assert.assertTrue(firstResult.containsKey("tesla")); + + Node car = (Node) firstResult.get("tesla"); + Assert.assertEquals(car.getProperty("model"), "modelS"); + } +} diff --git a/core-java/src/test/java/com/baeldung/java/concurrentmap/ConcurrentMapAggregateStatusTest.java b/core-java/src/test/java/com/baeldung/java/concurrentmap/ConcurrentMapAggregateStatusManualTest.java similarity index 97% rename from core-java/src/test/java/com/baeldung/java/concurrentmap/ConcurrentMapAggregateStatusTest.java rename to core-java/src/test/java/com/baeldung/java/concurrentmap/ConcurrentMapAggregateStatusManualTest.java index 73a4cdc0cd..ec865f71c4 100644 --- a/core-java/src/test/java/com/baeldung/java/concurrentmap/ConcurrentMapAggregateStatusTest.java +++ b/core-java/src/test/java/com/baeldung/java/concurrentmap/ConcurrentMapAggregateStatusManualTest.java @@ -14,7 +14,7 @@ import java.util.concurrent.TimeUnit; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotEquals; -public class ConcurrentMapAggregateStatusTest { +public class ConcurrentMapAggregateStatusManualTest { private ExecutorService executorService; private Map concurrentMap; diff --git a/core-java/src/test/java/com/baeldung/java/concurrentmap/ConcurrentMapNullKeyValueTest.java b/core-java/src/test/java/com/baeldung/java/concurrentmap/ConcurrentMapNullKeyValueManualTest.java similarity index 99% rename from core-java/src/test/java/com/baeldung/java/concurrentmap/ConcurrentMapNullKeyValueTest.java rename to core-java/src/test/java/com/baeldung/java/concurrentmap/ConcurrentMapNullKeyValueManualTest.java index 62a3d10add..33e3326427 100644 --- a/core-java/src/test/java/com/baeldung/java/concurrentmap/ConcurrentMapNullKeyValueTest.java +++ b/core-java/src/test/java/com/baeldung/java/concurrentmap/ConcurrentMapNullKeyValueManualTest.java @@ -10,7 +10,7 @@ import java.util.concurrent.ConcurrentMap; import static org.junit.Assert.assertNull; -public class ConcurrentMapNullKeyValueTest { +public class ConcurrentMapNullKeyValueManualTest { ConcurrentMap concurrentMap; diff --git a/core-java/src/test/java/com/baeldung/java/concurrentmap/ConcurrentMapPerformanceTest.java b/core-java/src/test/java/com/baeldung/java/concurrentmap/ConcurrentMapPerformanceManualTest.java similarity index 98% rename from core-java/src/test/java/com/baeldung/java/concurrentmap/ConcurrentMapPerformanceTest.java rename to core-java/src/test/java/com/baeldung/java/concurrentmap/ConcurrentMapPerformanceManualTest.java index a0efa89351..5c1612ca60 100644 --- a/core-java/src/test/java/com/baeldung/java/concurrentmap/ConcurrentMapPerformanceTest.java +++ b/core-java/src/test/java/com/baeldung/java/concurrentmap/ConcurrentMapPerformanceManualTest.java @@ -11,7 +11,7 @@ import java.util.concurrent.*; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; -public class ConcurrentMapPerformanceTest { +public class ConcurrentMapPerformanceManualTest { @Test public void givenMaps_whenGetPut500KTimes_thenConcurrentMapFaster() throws Exception { diff --git a/core-java/src/test/java/com/baeldung/java/concurrentmap/ConcurrentNavigableMapTests.java b/core-java/src/test/java/com/baeldung/java/concurrentmap/ConcurrentNavigableMapManualTests.java similarity index 95% rename from core-java/src/test/java/com/baeldung/java/concurrentmap/ConcurrentNavigableMapTests.java rename to core-java/src/test/java/com/baeldung/java/concurrentmap/ConcurrentNavigableMapManualTests.java index 93087626a4..d102680aa4 100644 --- a/core-java/src/test/java/com/baeldung/java/concurrentmap/ConcurrentNavigableMapTests.java +++ b/core-java/src/test/java/com/baeldung/java/concurrentmap/ConcurrentNavigableMapManualTests.java @@ -10,7 +10,7 @@ import java.util.concurrent.atomic.AtomicInteger; import static org.testng.Assert.*; -public class ConcurrentNavigableMapTests { +public class ConcurrentNavigableMapManualTests { @Test public void givenSkipListMap_whenAccessInMultiThreads_thenOrderingStable() throws InterruptedException { @@ -18,9 +18,7 @@ public class ConcurrentNavigableMapTests { updateMapConcurrently(skipListMap, 4); - Iterator skipListIter = skipListMap - .keySet() - .iterator(); + Iterator skipListIter = skipListMap.keySet().iterator(); int previous = skipListIter.next(); while (skipListIter.hasNext()) { int current = skipListIter.next(); diff --git a/core-java/src/test/java/com/baeldung/java/concurrentmap/ConcurretMapMemoryConsistencyTest.java b/core-java/src/test/java/com/baeldung/java/concurrentmap/ConcurretMapMemoryConsistencyManualTest.java similarity index 97% rename from core-java/src/test/java/com/baeldung/java/concurrentmap/ConcurretMapMemoryConsistencyTest.java rename to core-java/src/test/java/com/baeldung/java/concurrentmap/ConcurretMapMemoryConsistencyManualTest.java index 63a96dd5ee..43cbb2d293 100644 --- a/core-java/src/test/java/com/baeldung/java/concurrentmap/ConcurretMapMemoryConsistencyTest.java +++ b/core-java/src/test/java/com/baeldung/java/concurrentmap/ConcurretMapMemoryConsistencyManualTest.java @@ -10,7 +10,7 @@ import java.util.concurrent.TimeUnit; import static org.junit.Assert.*; -public class ConcurretMapMemoryConsistencyTest { +public class ConcurretMapMemoryConsistencyManualTest { @Test public void givenConcurrentMap_whenSumParallel_thenCorrect() throws Exception { diff --git a/core-java/src/test/java/com/baeldung/java/concurrentmodification/ConcurrentModificationUnitTest.java b/core-java/src/test/java/com/baeldung/java/concurrentmodification/ConcurrentModificationUnitTest.java new file mode 100644 index 0000000000..f7a7bd5fe0 --- /dev/null +++ b/core-java/src/test/java/com/baeldung/java/concurrentmodification/ConcurrentModificationUnitTest.java @@ -0,0 +1,80 @@ +package com.baeldung.java.concurrentmodification; + +import org.junit.Test; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.ConcurrentModificationException; +import java.util.Iterator; +import java.util.List; + +import static java.util.stream.Collectors.toList; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.util.Lists.newArrayList; + +public class ConcurrentModificationUnitTest { + @Test(expected = ConcurrentModificationException.class) + public void givenIterating_whenRemoving_thenThrowException() throws InterruptedException { + + List integers = newArrayList(1, 2, 3); + + for (Integer integer : integers) { + integers.remove(1); + } + } + + @Test + public void givenIterating_whenUsingIteratorRemove_thenNoError() throws InterruptedException { + + List integers = newArrayList(1, 2, 3); + + for (Iterator iterator = integers.iterator(); iterator.hasNext();) { + Integer integer = iterator.next(); + if(integer == 2) { + iterator.remove(); + } + } + + assertThat(integers).containsExactly(1, 3); + } + + @Test + public void givenIterating_whenUsingRemovalList_thenNoError() throws InterruptedException { + + List integers = newArrayList(1, 2, 3); + List toRemove = newArrayList(); + + for (Integer integer : integers) { + if(integer == 2) { + toRemove.add(integer); + } + } + integers.removeAll(toRemove); + + assertThat(integers).containsExactly(1, 3); + } + + @Test + public void whenUsingRemoveIf_thenRemoveElements() throws InterruptedException { + + Collection integers = newArrayList(1, 2, 3); + + integers.removeIf(i -> i == 2); + + assertThat(integers).containsExactly(1, 3); + } + + @Test + public void whenUsingStream_thenRemoveElements() { + Collection integers = newArrayList(1, 2, 3); + + List collected = integers + .stream() + .filter(i -> i != 2) + .map(Object::toString) + .collect(toList()); + + assertThat(collected).containsExactly("1", "3"); + } + +} diff --git a/core-java/src/test/java/com/baeldung/java8/Java8GroupingByCollectorUnitTest.java b/core-java/src/test/java/com/baeldung/java8/Java8GroupingByCollectorUnitTest.java new file mode 100644 index 0000000000..4452b4db9a --- /dev/null +++ b/core-java/src/test/java/com/baeldung/java8/Java8GroupingByCollectorUnitTest.java @@ -0,0 +1,231 @@ +package com.baeldung.java8; + +import com.baeldung.java_8_features.groupingby.BlogPost; +import com.baeldung.java_8_features.groupingby.BlogPostType; +import org.junit.Test; + +import java.util.*; +import java.util.concurrent.ConcurrentMap; + +import static java.util.Comparator.comparingInt; +import static java.util.stream.Collectors.*; +import static org.junit.Assert.*; + +public class Java8GroupingByCollectorUnitTest { + + private static final List posts = Arrays.asList( + new BlogPost("News item 1", "Author 1", BlogPostType.NEWS, 15), + new BlogPost("Tech review 1", "Author 2", BlogPostType.REVIEW, 5), + new BlogPost("Programming guide", "Author 1", BlogPostType.GUIDE, 20), + new BlogPost("News item 2", "Author 2", BlogPostType.NEWS, 35), + new BlogPost("Tech review 2", "Author 1", BlogPostType.REVIEW, 15)); + + @Test + public void givenAListOfPosts_whenGroupedByType_thenGetAMapBetweenTypeAndPosts() { + Map> postsPerType = posts + .stream() + .collect(groupingBy(BlogPost::getType)); + + assertEquals(2, postsPerType + .get(BlogPostType.NEWS) + .size()); + assertEquals(1, postsPerType + .get(BlogPostType.GUIDE) + .size()); + assertEquals(2, postsPerType + .get(BlogPostType.REVIEW) + .size()); + } + + @Test + public void givenAListOfPosts_whenGroupedByTypeAndTheirTitlesAreJoinedInAString_thenGetAMapBetweenTypeAndCsvTitles() { + Map postsPerType = posts + .stream() + .collect(groupingBy(BlogPost::getType, mapping(BlogPost::getTitle, joining(", ", "Post titles: [", "]")))); + + assertEquals("Post titles: [News item 1, News item 2]", postsPerType.get(BlogPostType.NEWS)); + assertEquals("Post titles: [Programming guide]", postsPerType.get(BlogPostType.GUIDE)); + assertEquals("Post titles: [Tech review 1, Tech review 2]", postsPerType.get(BlogPostType.REVIEW)); + } + + @Test + public void givenAListOfPosts_whenGroupedByTypeAndSumTheLikes_thenGetAMapBetweenTypeAndPostLikes() { + Map likesPerType = posts + .stream() + .collect(groupingBy(BlogPost::getType, summingInt(BlogPost::getLikes))); + + assertEquals(50, likesPerType + .get(BlogPostType.NEWS) + .intValue()); + assertEquals(20, likesPerType + .get(BlogPostType.REVIEW) + .intValue()); + assertEquals(20, likesPerType + .get(BlogPostType.GUIDE) + .intValue()); + } + + @Test + public void givenAListOfPosts_whenGroupedByTypeInAnEnumMap_thenGetAnEnumMapBetweenTypeAndPosts() { + EnumMap> postsPerType = posts + .stream() + .collect(groupingBy(BlogPost::getType, () -> new EnumMap<>(BlogPostType.class), toList())); + + assertEquals(2, postsPerType + .get(BlogPostType.NEWS) + .size()); + assertEquals(1, postsPerType + .get(BlogPostType.GUIDE) + .size()); + assertEquals(2, postsPerType + .get(BlogPostType.REVIEW) + .size()); + } + + @Test + public void givenAListOfPosts_whenGroupedByTypeInSets_thenGetAMapBetweenTypesAndSetsOfPosts() { + Map> postsPerType = posts + .stream() + .collect(groupingBy(BlogPost::getType, toSet())); + + assertEquals(2, postsPerType + .get(BlogPostType.NEWS) + .size()); + assertEquals(1, postsPerType + .get(BlogPostType.GUIDE) + .size()); + assertEquals(2, postsPerType + .get(BlogPostType.REVIEW) + .size()); + } + + @Test + public void givenAListOfPosts_whenGroupedByTypeConcurrently_thenGetAMapBetweenTypeAndPosts() { + ConcurrentMap> postsPerType = posts + .parallelStream() + .collect(groupingByConcurrent(BlogPost::getType)); + + assertEquals(2, postsPerType + .get(BlogPostType.NEWS) + .size()); + assertEquals(1, postsPerType + .get(BlogPostType.GUIDE) + .size()); + assertEquals(2, postsPerType + .get(BlogPostType.REVIEW) + .size()); + } + + @Test + public void givenAListOfPosts_whenGroupedByTypeAndAveragingLikes_thenGetAMapBetweenTypeAndAverageNumberOfLikes() { + Map averageLikesPerType = posts + .stream() + .collect(groupingBy(BlogPost::getType, averagingInt(BlogPost::getLikes))); + + assertEquals(25, averageLikesPerType + .get(BlogPostType.NEWS) + .intValue()); + assertEquals(20, averageLikesPerType + .get(BlogPostType.GUIDE) + .intValue()); + assertEquals(10, averageLikesPerType + .get(BlogPostType.REVIEW) + .intValue()); + } + + @Test + public void givenAListOfPosts_whenGroupedByTypeAndCounted_thenGetAMapBetweenTypeAndNumberOfPosts() { + Map numberOfPostsPerType = posts + .stream() + .collect(groupingBy(BlogPost::getType, counting())); + + assertEquals(2, numberOfPostsPerType + .get(BlogPostType.NEWS) + .intValue()); + assertEquals(1, numberOfPostsPerType + .get(BlogPostType.GUIDE) + .intValue()); + assertEquals(2, numberOfPostsPerType + .get(BlogPostType.REVIEW) + .intValue()); + } + + @Test + public void givenAListOfPosts_whenGroupedByTypeAndMaxingLikes_thenGetAMapBetweenTypeAndMaximumNumberOfLikes() { + Map> maxLikesPerPostType = posts + .stream() + .collect(groupingBy(BlogPost::getType, maxBy(comparingInt(BlogPost::getLikes)))); + + assertTrue(maxLikesPerPostType + .get(BlogPostType.NEWS) + .isPresent()); + assertEquals(35, maxLikesPerPostType + .get(BlogPostType.NEWS) + .get() + .getLikes()); + + assertTrue(maxLikesPerPostType + .get(BlogPostType.GUIDE) + .isPresent()); + assertEquals(20, maxLikesPerPostType + .get(BlogPostType.GUIDE) + .get() + .getLikes()); + + assertTrue(maxLikesPerPostType + .get(BlogPostType.REVIEW) + .isPresent()); + assertEquals(15, maxLikesPerPostType + .get(BlogPostType.REVIEW) + .get() + .getLikes()); + } + + @Test + public void givenAListOfPosts_whenGroupedByAuthorAndThenByType_thenGetAMapBetweenAuthorAndMapsBetweenTypeAndBlogPosts() { + Map>> map = posts + .stream() + .collect(groupingBy(BlogPost::getAuthor, groupingBy(BlogPost::getType))); + + assertEquals(1, map + .get("Author 1") + .get(BlogPostType.NEWS) + .size()); + assertEquals(1, map + .get("Author 1") + .get(BlogPostType.GUIDE) + .size()); + assertEquals(1, map + .get("Author 1") + .get(BlogPostType.REVIEW) + .size()); + + assertEquals(1, map + .get("Author 2") + .get(BlogPostType.NEWS) + .size()); + assertEquals(1, map + .get("Author 2") + .get(BlogPostType.REVIEW) + .size()); + assertNull(map + .get("Author 2") + .get(BlogPostType.GUIDE)); + } + + @Test + public void givenAListOfPosts_whenGroupedByTypeAndSummarizingLikes_thenGetAMapBetweenTypeAndSummary() { + Map likeStatisticsPerType = posts + .stream() + .collect(groupingBy(BlogPost::getType, summarizingInt(BlogPost::getLikes))); + + IntSummaryStatistics newsLikeStatistics = likeStatisticsPerType.get(BlogPostType.NEWS); + + assertEquals(2, newsLikeStatistics.getCount()); + assertEquals(50, newsLikeStatistics.getSum()); + assertEquals(25.0, newsLikeStatistics.getAverage(), 0.001); + assertEquals(35, newsLikeStatistics.getMax()); + assertEquals(15, newsLikeStatistics.getMin()); + } + +} diff --git a/core-java/src/test/java/com/baeldung/strategy/StrategyDesignPatternUnitTest.java b/core-java/src/test/java/com/baeldung/strategy/StrategyDesignPatternUnitTest.java new file mode 100644 index 0000000000..7ca1d000be --- /dev/null +++ b/core-java/src/test/java/com/baeldung/strategy/StrategyDesignPatternUnitTest.java @@ -0,0 +1,73 @@ +package com.baeldung.strategy; + +import org.junit.Test; + +import java.math.BigDecimal; +import java.util.Arrays; +import java.util.List; +import java.util.function.Function; + +import static com.baeldung.strategy.Discounter.*; +import static org.assertj.core.api.Assertions.assertThat; + +public class StrategyDesignPatternUnitTest { + @Test + public void shouldDivideByTwo_WhenApplyingStaffDiscounter() { + Discounter staffDiscounter = new EasterDiscounter(); + + final BigDecimal discountedValue = staffDiscounter + .apply(BigDecimal.valueOf(100)); + + assertThat(discountedValue) + .isEqualByComparingTo(BigDecimal.valueOf(50)); + } + + @Test + public void shouldDivideByTwo_WhenApplyingStaffDiscounterWithAnonyousTypes() { + Discounter staffDiscounter = new Discounter() { + @Override + public BigDecimal apply( BigDecimal amount) { + return amount.multiply(BigDecimal.valueOf(0.5)); + } + }; + + final BigDecimal discountedValue = staffDiscounter + .apply(BigDecimal.valueOf(100)); + + assertThat(discountedValue) + .isEqualByComparingTo(BigDecimal.valueOf(50)); + } + + @Test + public void shouldDivideByTwo_WhenApplyingStaffDiscounterWithLamda() { + Discounter staffDiscounter = amount -> amount.multiply(BigDecimal.valueOf(0.5)); + + final BigDecimal discountedValue = staffDiscounter + .apply(BigDecimal.valueOf(100)); + + assertThat(discountedValue) + .isEqualByComparingTo(BigDecimal.valueOf(50)); + } + + @Test + public void shouldApplyAllDiscounts() { + List discounters = Arrays.asList(christmas(), newYear(), easter()); + + BigDecimal amount = BigDecimal.valueOf(100); + + final Discounter combinedDiscounter = discounters + .stream() + .reduce(v -> v, Discounter::combine); + + combinedDiscounter.apply(amount); + } + + @Test + public void shouldChainDiscounters() { + final Function combinedDiscounters = Discounter + .christmas() + .andThen(newYear()); + + combinedDiscounters.apply(BigDecimal.valueOf(100)); + } +} diff --git a/core-java/src/test/java/com/baeldung/stream/InfiniteStreamTest.java b/core-java/src/test/java/com/baeldung/stream/InfiniteStreamTest.java new file mode 100644 index 0000000000..a1537a1735 --- /dev/null +++ b/core-java/src/test/java/com/baeldung/stream/InfiniteStreamTest.java @@ -0,0 +1,48 @@ +package com.baeldung.stream; + + +import org.junit.Test; + +import java.util.Arrays; +import java.util.List; +import java.util.Random; +import java.util.UUID; +import java.util.function.Supplier; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import static org.junit.Assert.assertEquals; + +public class InfiniteStreamTest { + + @Test + public void givenInfiniteStream_whenUseIntermediateLimitMethod_thenShouldTerminateInFiniteTime() { + //given + Stream infiniteStream = Stream.iterate(0, i -> i + 2); + + //when + List collect = infiniteStream + .limit(10) + .collect(Collectors.toList()); + + //then + assertEquals(collect, Arrays.asList(0, 2, 4, 6, 8, 10, 12, 14, 16, 18)); + } + + @Test + public void givenInfiniteStreamOfRandomInts_whenUseLimit_shouldTerminateInFiniteTime() { + //given + Supplier randomUUIDSupplier = UUID::randomUUID; + Stream infiniteStreamOfRandomUUID = Stream.generate(randomUUIDSupplier); + + //when + List randomInts = infiniteStreamOfRandomUUID + .skip(10) + .limit(10) + .collect(Collectors.toList()); + + //then + assertEquals(randomInts.size(), 10); + } + +} diff --git a/core-java/src/test/java/com/baeldung/string/JoinerSplitterTest.java b/core-java/src/test/java/com/baeldung/string/JoinerSplitterTest.java new file mode 100644 index 0000000000..303296cbc6 --- /dev/null +++ b/core-java/src/test/java/com/baeldung/string/JoinerSplitterTest.java @@ -0,0 +1,65 @@ +package com.baeldung.string; + +import static org.junit.Assert.*; + +import java.util.ArrayList; +import java.util.List; + +import org.junit.Test; + +import com.baeldung.string.JoinerSplitter; + +public class JoinerSplitterTest { + + @Test + public void provided_array_convert_to_stream_and_convert_to_string() { + + String[] programming_languages = {"java", "python", "nodejs", "ruby"}; + + String expectation = "java,python,nodejs,ruby"; + + String result = JoinerSplitter.join(programming_languages); + assertEquals(result, expectation); + } + + @Test + public void provided_array_convert_to_stream_and_convert_to_prefixPostfix() { + String[] programming_languages = {"java", "python", + "nodejs", "ruby"}; + String expectation = "[java,python,nodejs,ruby]"; + + String result = JoinerSplitter.joinWithPrefixPostFix(programming_languages); + assertEquals(result, expectation); + } + + @Test + public void provided_string_convert_to_stream_and_convert_to_listOfString() { + String programming_languages = "java,python,nodejs,ruby"; + + List expectation = new ArrayList(); + expectation.add("java"); + expectation.add("python"); + expectation.add("nodejs"); + expectation.add("ruby"); + + List result = JoinerSplitter.split(programming_languages); + + assertEquals(result, expectation); + } + + @Test + public void provided_string_convert_to_stream_and_convert_to_listOfChar() { + String programming_languages = "java,python,nodejs,ruby"; + + List expectation = new ArrayList(); + char[] charArray = programming_languages.toCharArray(); + for (char c : charArray) { + expectation.add(c); + } + + List result = JoinerSplitter.splitToListOfChar(programming_languages); + assertEquals(result, expectation); + + } + +} diff --git a/core-java/src/test/java/com/baeldung/weakhashmap/WeakHashMapTest.java b/core-java/src/test/java/com/baeldung/weakhashmap/WeakHashMapTest.java new file mode 100644 index 0000000000..1c5a261eea --- /dev/null +++ b/core-java/src/test/java/com/baeldung/weakhashmap/WeakHashMapTest.java @@ -0,0 +1,72 @@ +package com.baeldung.weakhashmap; + + +import org.junit.Test; + +import java.util.WeakHashMap; +import java.util.concurrent.TimeUnit; + +import static com.jayway.awaitility.Awaitility.await; +import static org.junit.Assert.assertTrue; + +public class WeakHashMapTest { + + @Test + public void givenWeakHashMap_whenCacheValueThatHasNoReferenceToIt_GCShouldReclaimThatObject() { + //given + WeakHashMap map = new WeakHashMap<>(); + BigImage bigImage = new BigImage("image_id"); + UniqueImageName imageName = new UniqueImageName("name_of_big_image"); + + map.put(imageName, bigImage); + assertTrue(map.containsKey(imageName)); + + //when big image key is not reference anywhere + imageName = null; + System.gc(); + + //then GC will finally reclaim that object + await().atMost(10, TimeUnit.SECONDS).until(map::isEmpty); + } + + @Test + public void givenWeakHashMap_whenCacheValueThatHasNoReferenceToIt_GCShouldReclaimThatObjectButLeaveReferencedObject() { + //given + WeakHashMap map = new WeakHashMap<>(); + BigImage bigImageFirst = new BigImage("foo"); + UniqueImageName imageNameFirst = new UniqueImageName("name_of_big_image"); + + BigImage bigImageSecond = new BigImage("foo_2"); + UniqueImageName imageNameSecond = new UniqueImageName("name_of_big_image_2"); + + map.put(imageNameFirst, bigImageFirst); + map.put(imageNameSecond, bigImageSecond); + assertTrue(map.containsKey(imageNameFirst)); + assertTrue(map.containsKey(imageNameSecond)); + + //when + imageNameFirst = null; + System.gc(); + + //then + await().atMost(10, TimeUnit.SECONDS).until(() -> map.size() == 1); + await().atMost(10, TimeUnit.SECONDS).until(() -> map.containsKey(imageNameSecond)); + } + + + class BigImage { + public final String imageId; + + BigImage(String imageId) { + this.imageId = imageId; + } + } + + class UniqueImageName { + public final String imageName; + + UniqueImageName(String imageName) { + this.imageName = imageName; + } + } +} diff --git a/core-java/src/test/java/org/baeldung/java/JavaRandomUnitTest.java b/core-java/src/test/java/org/baeldung/java/JavaRandomUnitTest.java index 4febe7c9fc..08f98025c3 100644 --- a/core-java/src/test/java/org/baeldung/java/JavaRandomUnitTest.java +++ b/core-java/src/test/java/org/baeldung/java/JavaRandomUnitTest.java @@ -1,12 +1,12 @@ package org.baeldung.java; -import java.nio.charset.Charset; -import java.util.Random; - import org.apache.commons.lang3.RandomStringUtils; import org.apache.commons.math3.random.RandomDataGenerator; import org.junit.Test; +import java.nio.charset.Charset; +import java.util.Random; + public class JavaRandomUnitTest { // tests - random long @@ -164,7 +164,7 @@ public class JavaRandomUnitTest { final int targetStringLength = 10; final StringBuilder buffer = new StringBuilder(targetStringLength); for (int i = 0; i < targetStringLength; i++) { - final int randomLimitedInt = leftLimit + (int) (new Random().nextFloat() * (rightLimit - leftLimit)); + final int randomLimitedInt = leftLimit + (int) (new Random().nextFloat() * (rightLimit - leftLimit + 1)); buffer.append((char) randomLimitedInt); } final String generatedString = buffer.toString(); diff --git a/core-java/src/test/java/org/baeldung/java/streams/ThreadPoolInParallelStreamTest.java b/core-java/src/test/java/org/baeldung/java/streams/ThreadPoolInParallelStreamTest.java index 5fd3fa4cb0..c2eb1cff5d 100644 --- a/core-java/src/test/java/org/baeldung/java/streams/ThreadPoolInParallelStreamTest.java +++ b/core-java/src/test/java/org/baeldung/java/streams/ThreadPoolInParallelStreamTest.java @@ -1,5 +1,5 @@ package org.baeldung.java.streams; - + import org.junit.Test; import java.util.ArrayList; @@ -14,28 +14,25 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; public class ThreadPoolInParallelStreamTest { - + @Test - public void giveRangeOfLongs_whenSummedInParallel_shouldBeEqualToExpectedTotal() - throws InterruptedException, ExecutionException { + public void giveRangeOfLongs_whenSummedInParallel_shouldBeEqualToExpectedTotal() throws InterruptedException, ExecutionException { long firstNum = 1; long lastNum = 1_000_000; - List aList = LongStream.rangeClosed(firstNum, lastNum).boxed() - .collect(Collectors.toList()); + List aList = LongStream.rangeClosed(firstNum, lastNum).boxed().collect(Collectors.toList()); ForkJoinPool customThreadPool = new ForkJoinPool(4); - long actualTotal = customThreadPool.submit(() -> aList.parallelStream() - .reduce(0L, Long::sum)).get(); - - assertEquals((lastNum + firstNum) * lastNum / 2, actualTotal); + long actualTotal = customThreadPool.submit(() -> aList.parallelStream().reduce(0L, Long::sum)).get(); + + assertEquals((lastNum + firstNum) * lastNum / 2, actualTotal); } - + @Test - public void givenList_whenCallingParallelStream_shouldBeParallelStream(){ + public void givenList_whenCallingParallelStream_shouldBeParallelStream() { List aList = new ArrayList<>(); Stream parallelStream = aList.parallelStream(); - + assertTrue(parallelStream.isParallel()); } } diff --git a/couchbase-sdk/pom.xml b/couchbase-sdk/pom.xml index 301fd81c51..1200fab454 100644 --- a/couchbase-sdk/pom.xml +++ b/couchbase-sdk/pom.xml @@ -102,10 +102,10 @@ 1.8 UTF-8 - 2.3.6 - 4.3.4.RELEASE - 1.1.7 - 1.7.21 + 2.4.0 + 4.3.5.RELEASE + 1.1.8 + 1.7.22 4.12 3.5 3.6.0 diff --git a/couchbase-sdk/src/main/java/com/baeldung/couchbase/mapreduce/CouchbaseKeyGenerator.java b/couchbase-sdk/src/main/java/com/baeldung/couchbase/mapreduce/CouchbaseKeyGenerator.java new file mode 100644 index 0000000000..9ac1bbb3f7 --- /dev/null +++ b/couchbase-sdk/src/main/java/com/baeldung/couchbase/mapreduce/CouchbaseKeyGenerator.java @@ -0,0 +1,6 @@ +package com.baeldung.couchbase.mapreduce; + +public interface CouchbaseKeyGenerator { + + String generateKey(T t); +} diff --git a/couchbase-sdk/src/main/java/com/baeldung/couchbase/mapreduce/DuplicateKeyException.java b/couchbase-sdk/src/main/java/com/baeldung/couchbase/mapreduce/DuplicateKeyException.java new file mode 100644 index 0000000000..78baaa155c --- /dev/null +++ b/couchbase-sdk/src/main/java/com/baeldung/couchbase/mapreduce/DuplicateKeyException.java @@ -0,0 +1,10 @@ +package com.baeldung.couchbase.mapreduce; + +@SuppressWarnings("serial") +public class DuplicateKeyException extends Exception { + + public DuplicateKeyException(String s) { + super(s); + } + +} diff --git a/couchbase-sdk/src/main/java/com/baeldung/couchbase/mapreduce/RandomUUIDGenerator.java b/couchbase-sdk/src/main/java/com/baeldung/couchbase/mapreduce/RandomUUIDGenerator.java new file mode 100644 index 0000000000..9baf4a4f43 --- /dev/null +++ b/couchbase-sdk/src/main/java/com/baeldung/couchbase/mapreduce/RandomUUIDGenerator.java @@ -0,0 +1,11 @@ +package com.baeldung.couchbase.mapreduce; + +import java.util.UUID; + +public class RandomUUIDGenerator implements CouchbaseKeyGenerator { + + @Override + public String generateKey(T t) { + return UUID.randomUUID().toString(); + } +} diff --git a/couchbase-sdk/src/main/java/com/baeldung/couchbase/mapreduce/StudentGrade.java b/couchbase-sdk/src/main/java/com/baeldung/couchbase/mapreduce/StudentGrade.java new file mode 100644 index 0000000000..846aba716a --- /dev/null +++ b/couchbase-sdk/src/main/java/com/baeldung/couchbase/mapreduce/StudentGrade.java @@ -0,0 +1,50 @@ +package com.baeldung.couchbase.mapreduce; + +public class StudentGrade { + + private String name; + private String course; + private Integer grade; + private Integer hours; + + public StudentGrade() { } + + public StudentGrade(String name, String course, Integer grade, Integer hours) { + this.name = name; + this.course = course; + this.grade = grade; + this.hours = hours; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getCourse() { + return course; + } + + public void setCourse(String course) { + this.course = course; + } + + public Integer getGrade() { + return grade; + } + + public void setGrade(Integer grade) { + this.grade = grade; + } + + public Integer getHours() { + return hours; + } + + public void setHours(Integer hours) { + this.hours = hours; + } +} diff --git a/couchbase-sdk/src/main/java/com/baeldung/couchbase/mapreduce/StudentGradeKeyGenerator.java b/couchbase-sdk/src/main/java/com/baeldung/couchbase/mapreduce/StudentGradeKeyGenerator.java new file mode 100644 index 0000000000..680e37ba57 --- /dev/null +++ b/couchbase-sdk/src/main/java/com/baeldung/couchbase/mapreduce/StudentGradeKeyGenerator.java @@ -0,0 +1,9 @@ +package com.baeldung.couchbase.mapreduce; + +public class StudentGradeKeyGenerator implements CouchbaseKeyGenerator { + + @Override + public String generateKey(StudentGrade g) { + return g.getName() + ":" + g.getCourse(); + } +} diff --git a/couchbase-sdk/src/main/java/com/baeldung/couchbase/mapreduce/StudentGradeQueryBuilder.java b/couchbase-sdk/src/main/java/com/baeldung/couchbase/mapreduce/StudentGradeQueryBuilder.java new file mode 100644 index 0000000000..37bb03645a --- /dev/null +++ b/couchbase-sdk/src/main/java/com/baeldung/couchbase/mapreduce/StudentGradeQueryBuilder.java @@ -0,0 +1,70 @@ +package com.baeldung.couchbase.mapreduce; + +import com.couchbase.client.deps.com.fasterxml.jackson.databind.ObjectMapper; +import com.couchbase.client.java.document.json.JsonArray; +import com.couchbase.client.java.view.ViewQuery; + +public class StudentGradeQueryBuilder { + + final ObjectMapper om = new ObjectMapper(); + + public ViewQuery findAll() { + return ViewQuery.from("studentGrades", "findByCourse"); + } + + public ViewQuery findByCourse(String course) { + return ViewQuery.from("studentGrades", "findByCourse") + .key(course); + } + + public ViewQuery findByCourses(String... courses) { + return ViewQuery.from("studentGrades", "findByCourse") + .keys(JsonArray.from(courses)); + } + + public ViewQuery findByGradeInRange(int lower, int upper, boolean inclusiveEnd) { + return ViewQuery.from("studentGrades", "findByGrade") + .startKey(lower) + .endKey(upper) + .inclusiveEnd(inclusiveEnd); + } + + public ViewQuery findByGradeLessThan(int upper) { + return ViewQuery.from("studentGrades", "findByGrade") + .endKey(upper) + .inclusiveEnd(false); + } + + public ViewQuery findByGradeGreaterThan(int lower) { + return ViewQuery.from("studentGrades", "findByGrade") + .startKey(lower); + } + + public ViewQuery findByCourseAndGradeInRange(String course, int minGrade, int maxGrade, boolean inclusiveEnd) { + return ViewQuery.from("studentGrades", "findByCourseAndGrade") + .startKey(JsonArray.from(course, minGrade)) + .endKey(JsonArray.from(course, maxGrade)) + .inclusiveEnd(inclusiveEnd); + } + + public ViewQuery findTopGradesByCourse(String course, int limit) { + return ViewQuery.from("studentGrades", "findByCourseAndGrade") + .startKey(JsonArray.from(course, 100)) + .endKey(JsonArray.from(course, 0)) + .inclusiveEnd(true) + .descending() + .limit(limit); + } + + public ViewQuery countStudentsByCourse() { + return ViewQuery.from("studentGrades", "countStudentsByCourse") + .reduce() + .groupLevel(1); + } + + public ViewQuery sumCreditsByStudent() { + return ViewQuery.from("studentGrades", "sumCreditsByStudent") + .reduce() + .groupLevel(1); + } +} diff --git a/couchbase-sdk/src/main/java/com/baeldung/couchbase/mapreduce/StudentGradeService.java b/couchbase-sdk/src/main/java/com/baeldung/couchbase/mapreduce/StudentGradeService.java new file mode 100644 index 0000000000..2d2c63f699 --- /dev/null +++ b/couchbase-sdk/src/main/java/com/baeldung/couchbase/mapreduce/StudentGradeService.java @@ -0,0 +1,169 @@ +package com.baeldung.couchbase.mapreduce; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; + +import com.couchbase.client.deps.com.fasterxml.jackson.databind.ObjectMapper; +import com.couchbase.client.java.Bucket; +import com.couchbase.client.java.CouchbaseCluster; +import com.couchbase.client.java.document.JsonDocument; +import com.couchbase.client.java.document.json.JsonArray; +import com.couchbase.client.java.document.json.JsonObject; +import com.couchbase.client.java.view.ViewQuery; +import com.couchbase.client.java.view.ViewResult; +import com.couchbase.client.java.view.ViewRow; + +public class StudentGradeService { + + final CouchbaseKeyGenerator keyGenerator; + final CouchbaseCluster cluster; + final Bucket bucket; + final ObjectMapper om = new ObjectMapper(); + final StudentGradeQueryBuilder queryBuilder; + + public StudentGradeService(CouchbaseKeyGenerator keyGenerator) { + this.keyGenerator = keyGenerator; + this.queryBuilder = new StudentGradeQueryBuilder(); + cluster = CouchbaseCluster.create("127.0.0.1"); + bucket = cluster.openBucket("baeldung-tutorial"); + } + + public String insert(StudentGrade studentGrade) throws DuplicateKeyException { + String id = keyGenerator.generateKey(studentGrade); + if(bucket.exists(id)) { + throw new DuplicateKeyException("document already exists with key " + id); + } + JsonObject content = JsonObject.empty() + .put("type", "StudentGrade") + .put("name", studentGrade.getName()) + .put("course", studentGrade.getCourse()) + .put("grade", studentGrade.getGrade()) + .put("hours", studentGrade.getHours()); + JsonDocument doc = JsonDocument.create(id, content); + bucket.insert(doc); + return id; + } + + public List findAll() { + ViewQuery query = queryBuilder.findAll(); + ViewResult result = bucket.query(query); + return extractDocuments(result); + } + + private List extractDocuments(ViewResult result) { + List docs = new ArrayList<>(); + for(ViewRow row : result.allRows()) { + JsonDocument doc = row.document(); + docs.add(doc); + } + return docs; + } + + public List findByCourse(String course) { + ViewQuery query = queryBuilder.findByCourse(course); + ViewResult result = bucket.query(query); + return extractDocuments(result); + } + + public List findByCourses(String... courses) { + ViewQuery query = queryBuilder.findByCourses(courses); + ViewResult result = bucket.query(query); + return extractDocuments(result); + } + + public List findByGradeInRange(int lower, int upper, boolean inclusiveEnd) { + ViewQuery query = queryBuilder.findByGradeInRange(lower, upper, inclusiveEnd); + ViewResult result = bucket.query(query); + return extractDocuments(result); + } + + public List findByGradeLessThan(int upper) { + ViewQuery query = queryBuilder.findByGradeLessThan(upper); + ViewResult result = bucket.query(query); + return extractDocuments(result); + } + + public List findByGradeGreaterThan(int lower) { + ViewQuery query = queryBuilder.findByGradeGreaterThan(lower); + ViewResult result = bucket.query(query); + return extractDocuments(result); + } + + public List findByCourseAndGradeInRange(String course, int minGrade, int maxGrade, boolean inclusiveEnd) { + ViewQuery query = queryBuilder.findByCourseAndGradeInRange(course, minGrade, maxGrade, inclusiveEnd); + ViewResult result = bucket.query(query); + return extractDocuments(result); + } + + public List findTopGradesByCourse(String course, int limit) { + ViewQuery query = queryBuilder.findTopGradesByCourse(course, limit); + ViewResult result = bucket.query(query); + return extractDocuments(result); + } + + public Map countStudentsByCourse() { + ViewQuery query = ViewQuery.from("studentGrades", "countStudentsByCourse") + .reduce() + .groupLevel(1); + ViewResult result = bucket.query(query); + + Map numStudentsByCourse = new HashMap<>(); + for(ViewRow row : result.allRows()) { + JsonArray keyArray = (JsonArray) row.key(); + String course = keyArray.getString(0); + long count = Long.valueOf(row.value().toString()); + numStudentsByCourse.put(course, count); + } + + return numStudentsByCourse; + } + + public Map sumCreditHoursByStudent() { + ViewQuery query = ViewQuery.from("studentGrades", "sumHoursByStudent") + .reduce() + .groupLevel(1); + ViewResult result = bucket.query(query); + + Map creditHoursByStudent = new HashMap<>(); + for(ViewRow row : result.allRows()) { + String course = (String) row.key(); + long sum = Long.valueOf(row.value().toString()); + creditHoursByStudent.put(course, sum); + } + + return creditHoursByStudent; + } + + public Map sumGradePointsByStudent() { + ViewQuery query = ViewQuery.from("studentGrades", "sumGradePointsByStudent") + .reduce() + .groupLevel(1); + ViewResult result = bucket.query(query); + + Map gradePointsByStudent = new HashMap<>(); + for(ViewRow row : result.allRows()) { + String course = (String) row.key(); + long sum = Long.valueOf(row.value().toString()); + gradePointsByStudent.put(course, sum); + } + + return gradePointsByStudent; + } + + public Map calculateGpaByStudent() { + Map creditHoursByStudent = sumCreditHoursByStudent(); + Map gradePointsByStudent = sumGradePointsByStudent(); + + Map result = new HashMap<>(); + for(Entry creditHoursEntry : creditHoursByStudent.entrySet()) { + String name = creditHoursEntry.getKey(); + long totalHours = creditHoursEntry.getValue(); + long totalGradePoints = gradePointsByStudent.get(name); + result.put(name, ((float) totalGradePoints / totalHours)); + } + return result; + } +} diff --git a/couchbase-sdk/src/test/java/com/baeldung/couchbase/mapreduce/StudentGradeServiceIntegrationTest.java b/couchbase-sdk/src/test/java/com/baeldung/couchbase/mapreduce/StudentGradeServiceIntegrationTest.java new file mode 100644 index 0000000000..00d462e32a --- /dev/null +++ b/couchbase-sdk/src/test/java/com/baeldung/couchbase/mapreduce/StudentGradeServiceIntegrationTest.java @@ -0,0 +1,150 @@ +package com.baeldung.couchbase.mapreduce; + +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import org.junit.BeforeClass; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.couchbase.client.java.document.JsonDocument; +import com.couchbase.client.java.view.ViewResult; +import com.couchbase.client.java.view.ViewRow; + +public class StudentGradeServiceIntegrationTest { + private static final Logger logger = LoggerFactory.getLogger(StudentGradeServiceIntegrationTest.class); + + static StudentGradeService studentGradeService; + static Set gradeIds = new HashSet<>(); + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + studentGradeService = new StudentGradeService(new StudentGradeKeyGenerator()); + insertStudentGrade(new StudentGrade("John Doe", "History", 80, 3)); + insertStudentGrade(new StudentGrade("Jane Doe", "History", 89, 3)); + insertStudentGrade(new StudentGrade("Bob Smith", "History", 90, 3)); + insertStudentGrade(new StudentGrade("Mary Jones", "History", 92, 3)); + insertStudentGrade(new StudentGrade("Jane Doe", "Math", 59, 3)); + insertStudentGrade(new StudentGrade("Bob Smith", "Math", 91, 3)); + insertStudentGrade(new StudentGrade("Mary Jones", "Math", 86, 3)); + insertStudentGrade(new StudentGrade("John Doe", "Science", 85, 4)); + insertStudentGrade(new StudentGrade("Bob Smith", "Science", 97, 4)); + insertStudentGrade(new StudentGrade("Mary Jones", "Science", 84, 4)); + } + + private static void insertStudentGrade(StudentGrade studentGrade) { + try { + String id = studentGradeService.insert(studentGrade); + gradeIds.add(id); + } catch (DuplicateKeyException e) { + } + } + + @Test + public final void whenFindAll_thenSuccess() { + List docs = studentGradeService.findAll(); + printDocuments(docs); + } + + @Test + public final void whenFindByCourse_thenSuccess() { + List docs = studentGradeService.findByCourse("History"); + printDocuments(docs); + } + + @Test + public final void whenFindByCourses_thenSuccess() { + List docs = studentGradeService.findByCourses("History", "Science"); + printDocuments(docs); + } + + @Test + public final void whenFindByGradeInRange_thenSuccess() { + List docs = studentGradeService.findByGradeInRange(80, 89, true); + printDocuments(docs); + } + + @Test + public final void whenFindByGradeLessThan_thenSuccess() { + List docs = studentGradeService.findByGradeLessThan(60); + printDocuments(docs); + } + + @Test + public final void whenFindByGradeGreaterThan_thenSuccess() { + List docs = studentGradeService.findByGradeGreaterThan(90); + printDocuments(docs); + } + + @Test + public final void whenFindByCourseAndGradeInRange_thenSuccess() { + List docs = studentGradeService.findByCourseAndGradeInRange("Math", 80, 89, true); + printDocuments(docs); + } + + @Test + public final void whenFindTopGradesByCourse_thenSuccess() { + List docs = studentGradeService.findTopGradesByCourse("Science", 2); + printDocuments(docs); + } + + @Test + public final void whenCountStudentsByCourse_thenSuccess() { + Map map = studentGradeService.countStudentsByCourse(); + printMap(map); + } + + @Test + public final void whenSumCreditHoursByStudent_thenSuccess() { + Map map = studentGradeService.sumCreditHoursByStudent(); + printMap(map); + } + + @Test + public final void whenSumGradePointsByStudent_thenSuccess() { + Map map = studentGradeService.sumGradePointsByStudent(); + printMap(map); + } + + @Test + public final void whenCalculateGpaByStudent_thenSuccess() { + Map map = studentGradeService.calculateGpaByStudent(); + printGpaMap(map); + } + + private void printMap(Map map) { + for(Map.Entry entry : map.entrySet()) { + logger.info(entry.getKey() + "=" + entry.getValue()); + } + } + + private void printGpaMap(Map map) { + for(Map.Entry entry : map.entrySet()) { + logger.info(entry.getKey() + "=" + entry.getValue()); + } + } + + private void printDocuments(List docs) { + for(JsonDocument doc : docs) { + String key = doc.id(); + logger.info(key + " = " + doc.content().toString()); + } + } + + private void printViewResultDocuments(ViewResult result) { + for(ViewRow row : result.allRows()) { + JsonDocument doc = row.document(); + String key = doc.id(); + logger.info(key + "=" + doc.content().toString()); + } + } + + private void printViewResultRows(ViewResult result) { + for(ViewRow row : result.allRows()) { + logger.info(row.toString()); + } + } +} diff --git a/couchbase-sdk/src/test/resources/logback.xml b/couchbase-sdk/src/test/resources/logback.xml new file mode 100644 index 0000000000..efcc6fb4c7 --- /dev/null +++ b/couchbase-sdk/src/test/resources/logback.xml @@ -0,0 +1,17 @@ + + + + + web - %date [%thread] %-5level %logger{36} - %message%n + + + + + + + + + + + + \ No newline at end of file diff --git a/guava/pom.xml b/guava/pom.xml index f65a9a2081..0edbb90efd 100644 --- a/guava/pom.xml +++ b/guava/pom.xml @@ -57,6 +57,32 @@ test + + + + org.slf4j + slf4j-api + ${org.slf4j.version} + + + ch.qos.logback + logback-classic + ${logback.version} + + + + org.slf4j + jcl-over-slf4j + ${org.slf4j.version} + + + + org.slf4j + log4j-over-slf4j + ${org.slf4j.version} + + + org.assertj assertj-core @@ -112,6 +138,9 @@ 3.6.0 2.19.1 + + 1.7.21 + 1.1.7 \ No newline at end of file diff --git a/guava/src/test/java/org/baeldung/guava/CustomEvent.java b/guava/src/main/java/org/baeldung/guava/CustomEvent.java similarity index 99% rename from guava/src/test/java/org/baeldung/guava/CustomEvent.java rename to guava/src/main/java/org/baeldung/guava/CustomEvent.java index 78348065b2..2d5c3382d4 100644 --- a/guava/src/test/java/org/baeldung/guava/CustomEvent.java +++ b/guava/src/main/java/org/baeldung/guava/CustomEvent.java @@ -1,6 +1,5 @@ package org.baeldung.guava; - public class CustomEvent { private String action; diff --git a/guava/src/test/java/org/baeldung/guava/EventBusWrapper.java b/guava/src/main/java/org/baeldung/guava/EventBusWrapper.java similarity index 69% rename from guava/src/test/java/org/baeldung/guava/EventBusWrapper.java rename to guava/src/main/java/org/baeldung/guava/EventBusWrapper.java index eddaca0baf..243bc9e6ea 100644 --- a/guava/src/test/java/org/baeldung/guava/EventBusWrapper.java +++ b/guava/src/main/java/org/baeldung/guava/EventBusWrapper.java @@ -6,17 +6,16 @@ class EventBusWrapper { private static EventBus eventBus = new EventBus(); - static void register(Object object){ + static void register(Object object) { eventBus.register(object); } - static void unregister(Object object){ + static void unregister(Object object) { eventBus.unregister(object); } - static void post(Object object){ + static void post(Object object) { eventBus.post(object); } - } diff --git a/guava/src/main/java/org/baeldung/guava/EventListener.java b/guava/src/main/java/org/baeldung/guava/EventListener.java new file mode 100644 index 0000000000..02f22ce6b9 --- /dev/null +++ b/guava/src/main/java/org/baeldung/guava/EventListener.java @@ -0,0 +1,31 @@ +package org.baeldung.guava; + +import com.google.common.eventbus.Subscribe; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class EventListener { + + private static int eventsHandled; + private static final Logger LOG = LoggerFactory.getLogger(EventListener.class); + + @Subscribe + public void stringEvent(String event) { + LOG.info("do event [" + event + "]"); + eventsHandled++; + } + + @Subscribe + public void someCustomEvent(CustomEvent customEvent) { + LOG.info("do event [" + customEvent.getAction() + "]"); + eventsHandled++; + } + + public int getEventsHandled() { + return eventsHandled; + } + + public void resetEventsHandled() { + eventsHandled = 0; + } +} diff --git a/guava/src/test/java/org/baeldung/guava/EventListener.java b/guava/src/test/java/org/baeldung/guava/EventListener.java deleted file mode 100644 index 17a3ac093e..0000000000 --- a/guava/src/test/java/org/baeldung/guava/EventListener.java +++ /dev/null @@ -1,33 +0,0 @@ -package org.baeldung.guava; -import com.google.common.eventbus.Subscribe; - -public class EventListener { - - private static int eventsHandled; - - /** - * Handles events of type String * - */ - @Subscribe - public void stringEvent(String event){ - System.out.println("do event ["+event+"]"); - eventsHandled++; - } - - /** - * Handles events of type CustomEvent - */ - @Subscribe - public void someEvent(CustomEvent customEvent){ - System.out.println("do event ["+ customEvent.getAction()+"]"); - eventsHandled++; - } - - public int getEventsHandled() { - return eventsHandled; - } - - public void resetEventsHandled(){ - eventsHandled = 0; - } -} diff --git a/guava/src/test/java/org/baeldung/guava/GuavaEventBusTest.java b/guava/src/test/java/org/baeldung/guava/GuavaEventBusTest.java index bd966187ab..1db361d22c 100644 --- a/guava/src/test/java/org/baeldung/guava/GuavaEventBusTest.java +++ b/guava/src/test/java/org/baeldung/guava/GuavaEventBusTest.java @@ -11,18 +11,18 @@ public class GuavaEventBusTest { private EventListener listener; @Before - public void setUp() throws Exception { + public void setUp() { listener = new EventListener(); EventBusWrapper.register(listener); } @After - public void tearDown() throws Exception { + public void tearDown() { EventBusWrapper.unregister(listener); } @Test - public void givenStringEvent_whenEventHandled_thenSuccess() throws Exception { + public void givenStringEvent_whenEventHandled_thenSuccess() { listener.resetEventsHandled(); EventBusWrapper.post("String Event"); @@ -31,7 +31,7 @@ public class GuavaEventBusTest { } @Test - public void givenCustomEvent_whenEventHandled_thenSuccess() throws Exception { + public void givenCustomEvent_whenEventHandled_thenSuccess() { listener.resetEventsHandled(); CustomEvent customEvent = new CustomEvent("Custom Event"); diff --git a/guava/src/test/java/org/baeldung/guava/GuavaPreConditionsTest.java b/guava/src/test/java/org/baeldung/guava/GuavaPreConditionsTest.java index 55e3c7db00..2d98418d48 100644 --- a/guava/src/test/java/org/baeldung/guava/GuavaPreConditionsTest.java +++ b/guava/src/test/java/org/baeldung/guava/GuavaPreConditionsTest.java @@ -3,7 +3,7 @@ package org.baeldung.guava; import static org.assertj.core.api.Assertions.assertThatThrownBy; import java.util.Arrays; import org.junit.Test; -import static com.google.common.base.Preconditions.*; +import com.google.common.base.*; public class GuavaPreConditionsTest { @@ -11,10 +11,7 @@ public class GuavaPreConditionsTest { public void whenCheckArgumentEvaluatesFalse_throwsException() { int age = -18; - assertThatThrownBy(() -> checkArgument(age > 0)) - .isInstanceOf(IllegalArgumentException.class) - .hasMessage(null) - .hasNoCause(); + assertThatThrownBy(() -> Preconditions.checkArgument(age > 0)).isInstanceOf(IllegalArgumentException.class).hasMessage(null).hasNoCause(); } @Test @@ -22,10 +19,7 @@ public class GuavaPreConditionsTest { final int age = -18; final String message = "Age can't be zero or less than zero"; - assertThatThrownBy(() -> checkArgument(age > 0, message)) - .isInstanceOf(IllegalArgumentException.class) - .hasMessage(message) - .hasNoCause(); + assertThatThrownBy(() -> Preconditions.checkArgument(age > 0, message)).isInstanceOf(IllegalArgumentException.class).hasMessage(message).hasNoCause(); } @Test @@ -33,19 +27,14 @@ public class GuavaPreConditionsTest { final int age = -18; final String message = "Age can't be zero or less than zero, you supplied %s."; - assertThatThrownBy(() -> checkArgument(age > 0, message, age)) - .isInstanceOf(IllegalArgumentException.class) - .hasMessage(message, age) - .hasNoCause(); + assertThatThrownBy(() -> Preconditions.checkArgument(age > 0, message, age)).isInstanceOf(IllegalArgumentException.class).hasMessage(message, age).hasNoCause(); } @Test public void givenArrayOfIntegers_whenCheckElementIndexEvaluatesFalse_throwsException() { final int[] numbers = { 1, 2, 3, 4, 5 }; - assertThatThrownBy(() -> checkElementIndex(6, numbers.length - 1)) - .isInstanceOf(IndexOutOfBoundsException.class) - .hasNoCause(); + assertThatThrownBy(() -> Preconditions.checkElementIndex(6, numbers.length - 1)).isInstanceOf(IndexOutOfBoundsException.class).hasNoCause(); } @Test @@ -53,20 +42,7 @@ public class GuavaPreConditionsTest { final int[] numbers = { 1, 2, 3, 4, 5 }; final String message = "Please check the bound of an array and retry"; - assertThatThrownBy(() -> checkElementIndex(6, numbers.length - 1, message)) - .isInstanceOf(IndexOutOfBoundsException.class) - .hasMessageStartingWith(message) - .hasNoCause(); - } - - @Test - public void givenNullString_whenCheckNotNullCalled_throwsException() { - final String nullObject = null; - - assertThatThrownBy(() -> checkNotNull(nullObject)) - .isInstanceOf(NullPointerException.class) - .hasMessage(null) - .hasNoCause(); + assertThatThrownBy(() -> Preconditions.checkElementIndex(6, numbers.length - 1, message)).isInstanceOf(IndexOutOfBoundsException.class).hasMessageStartingWith(message).hasNoCause(); } @Test @@ -74,10 +50,7 @@ public class GuavaPreConditionsTest { final String nullObject = null; final String message = "Please check the Object supplied, its null!"; - assertThatThrownBy(() -> checkNotNull(nullObject, message)) - .isInstanceOf(NullPointerException.class) - .hasMessage(message) - .hasNoCause(); + assertThatThrownBy(() -> Preconditions.checkNotNull(nullObject, message)).isInstanceOf(NullPointerException.class).hasMessage(message).hasNoCause(); } @Test @@ -85,19 +58,14 @@ public class GuavaPreConditionsTest { final String nullObject = null; final String message = "Please check the Object supplied, its %s!"; - assertThatThrownBy(() -> checkNotNull(nullObject, message, nullObject)) - .isInstanceOf(NullPointerException.class) - .hasMessage(message, nullObject) - .hasNoCause(); + assertThatThrownBy(() -> Preconditions.checkNotNull(nullObject, message, new Object[] { null })).isInstanceOf(NullPointerException.class).hasMessage(message, nullObject).hasNoCause(); } @Test public void givenArrayOfIntegers_whenCheckPositionIndexEvaluatesFalse_throwsException() { final int[] numbers = { 1, 2, 3, 4, 5 }; - assertThatThrownBy(() -> checkPositionIndex(6, numbers.length - 1)) - .isInstanceOf(IndexOutOfBoundsException.class) - .hasNoCause(); + assertThatThrownBy(() -> Preconditions.checkPositionIndex(6, numbers.length - 1)).isInstanceOf(IndexOutOfBoundsException.class).hasNoCause(); } @Test @@ -105,30 +73,14 @@ public class GuavaPreConditionsTest { final int[] numbers = { 1, 2, 3, 4, 5 }; final String message = "Please check the bound of an array and retry"; - assertThatThrownBy(() -> checkPositionIndex(6, numbers.length - 1, message)) - .isInstanceOf(IndexOutOfBoundsException.class) - .hasMessageStartingWith(message) - .hasNoCause(); + assertThatThrownBy(() -> Preconditions.checkPositionIndex(6, numbers.length - 1, message)).isInstanceOf(IndexOutOfBoundsException.class).hasMessageStartingWith(message).hasNoCause(); } @Test public void givenArrayOfIntegers_whenCheckPositionIndexesEvaluatesFalse_throwsException() { final int[] numbers = { 1, 2, 3, 4, 5 }; - assertThatThrownBy(() -> checkPositionIndexes(6, 0, numbers.length - 1)) - .isInstanceOf(IndexOutOfBoundsException.class) - .hasNoCause(); - } - - @Test - public void givenValidStates_whenCheckStateEvaluatesFalse_throwsException() { - final int[] validStates = { -1, 0, 1 }; - final int givenState = 10; - - assertThatThrownBy(() -> checkState(Arrays.binarySearch(validStates, givenState) > 0)) - .isInstanceOf(IllegalStateException.class) - .hasMessage(null) - .hasNoCause(); + assertThatThrownBy(() -> Preconditions.checkPositionIndexes(6, 0, numbers.length - 1)).isInstanceOf(IndexOutOfBoundsException.class).hasNoCause(); } @Test @@ -137,10 +89,7 @@ public class GuavaPreConditionsTest { final int givenState = 10; final String message = "You have entered an invalid state"; - assertThatThrownBy(() -> checkState(Arrays.binarySearch(validStates, givenState) > 0, message)) - .isInstanceOf(IllegalStateException.class) - .hasMessageStartingWith(message) - .hasNoCause(); + assertThatThrownBy(() -> Preconditions.checkState(Arrays.binarySearch(validStates, givenState) > 0, message)).isInstanceOf(IllegalStateException.class).hasMessageStartingWith(message).hasNoCause(); } @Test @@ -149,9 +98,7 @@ public class GuavaPreConditionsTest { final int givenState = 10; final String message = "State can't be %s, It can be one of %s."; - assertThatThrownBy(() -> checkState(Arrays.binarySearch(validStates, givenState) > 0, message, givenState, Arrays.toString(validStates))) - .isInstanceOf(IllegalStateException.class) - .hasMessage(message, givenState, Arrays.toString(validStates)) - .hasNoCause(); + assertThatThrownBy(() -> Preconditions.checkState(Arrays.binarySearch(validStates, givenState) > 0, message, givenState, Arrays.toString(validStates))).isInstanceOf(IllegalStateException.class) + .hasMessage(message, givenState, Arrays.toString(validStates)).hasNoCause(); } -} +} \ No newline at end of file diff --git a/hystrix/pom.xml b/hystrix/pom.xml index 5e050d726e..ba1a596631 100644 --- a/hystrix/pom.xml +++ b/hystrix/pom.xml @@ -11,7 +11,7 @@ org.springframework.boot spring-boot-starter-parent - 1.4.2.RELEASE + 1.4.4.RELEASE diff --git a/jackson/pom.xml b/jackson/pom.xml index 53fb82c61e..881ba8e24c 100644 --- a/jackson/pom.xml +++ b/jackson/pom.xml @@ -67,6 +67,12 @@ ${jackson.version} + + com.fasterxml.jackson.datatype + jackson-datatype-jdk8 + ${jackson.version} + + joda-time joda-time @@ -128,8 +134,8 @@ ${mockito.version} test - - + + org.slf4j @@ -152,7 +158,7 @@ org.slf4j log4j-over-slf4j ${org.slf4j.version} - + @@ -189,7 +195,7 @@ - 2.8.5 + 2.8.6 1.7.21 @@ -198,7 +204,7 @@ 19.0 3.5 - 2.5 + 2.5 2.9.6 2.8.0 4.1 diff --git a/jackson/src/main/java/com/baeldung/jackson/entities/ActorJackson.java b/jackson/src/main/java/com/baeldung/jackson/entities/ActorJackson.java index f6a8b0cf61..76f87d80b3 100644 --- a/jackson/src/main/java/com/baeldung/jackson/entities/ActorJackson.java +++ b/jackson/src/main/java/com/baeldung/jackson/entities/ActorJackson.java @@ -4,6 +4,7 @@ import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; +import java.util.Locale; import java.util.TimeZone; public class ActorJackson { @@ -53,7 +54,7 @@ public class ActorJackson { } private String formatDateOfBirth() { - final DateFormat formatter = new SimpleDateFormat("EEE MMM dd hh:mm:ss zzz yyyy"); + final DateFormat formatter = new SimpleDateFormat("EEE MMM dd hh:mm:ss zzz yyyy", Locale.US); formatter.setTimeZone(TimeZone.getTimeZone("GMT")); return formatter.format(dateOfBirth); } diff --git a/jackson/src/main/java/com/baeldung/jackson/miscellaneous/mixin/Book.java b/jackson/src/main/java/com/baeldung/jackson/miscellaneous/mixin/Book.java new file mode 100644 index 0000000000..fb961efe85 --- /dev/null +++ b/jackson/src/main/java/com/baeldung/jackson/miscellaneous/mixin/Book.java @@ -0,0 +1,25 @@ +package com.baeldung.jackson.miscellaneous.mixin; + +import java.util.Optional; + +public class Book { + + private String title; + private Optional subTitle; + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public Optional getSubTitle() { + return subTitle; + } + + public void setSubTitle(Optional subTitle) { + this.subTitle = subTitle; + } +} diff --git a/jackson/src/test/java/com/baeldung/jackson/deserialization/JacksonDeserializeTest.java b/jackson/src/test/java/com/baeldung/jackson/deserialization/JacksonDeserializeTest.java index 7e91df5332..cb3bccb879 100644 --- a/jackson/src/test/java/com/baeldung/jackson/deserialization/JacksonDeserializeTest.java +++ b/jackson/src/test/java/com/baeldung/jackson/deserialization/JacksonDeserializeTest.java @@ -3,12 +3,10 @@ package com.baeldung.jackson.deserialization; import java.io.IOException; import java.text.DateFormat; import java.text.SimpleDateFormat; - import com.baeldung.jackson.entities.Movie; -import org.junit.Assert; import org.junit.Test; - import com.fasterxml.jackson.databind.ObjectMapper; +import static org.junit.Assert.assertEquals; public class JacksonDeserializeTest { @@ -20,7 +18,7 @@ public class JacksonDeserializeTest { final Movie movie = mapper.readValue(jsonInput, Movie.class); final String expectedOutput = "Movie [imdbId=tt0472043, director=null, actors=[ActorJackson [imdbId=nm2199632, dateOfBirth=Tue Sep 21 11:00:00 GMT 1982, filmography=[Apocalypto, Beatdown, Wind Walkers]]]]"; - Assert.assertEquals(movie.toString(), expectedOutput); + assertEquals(expectedOutput, movie.toString()); } @Test @@ -35,7 +33,7 @@ public class JacksonDeserializeTest { final Movie movie = mapper.readValue(jsonInput, Movie.class); final String expectedOutput = "Movie [imdbId=tt0472043, director=Mel Gibson, actors=[ActorJackson [imdbId=nm2199632, dateOfBirth=Tue Sep 21 11:00:00 GMT 1982, filmography=[Apocalypto, Beatdown, Wind Walkers]]]]"; - Assert.assertEquals(movie.toString(), expectedOutput); + assertEquals(expectedOutput, movie.toString()); } } diff --git a/jackson/src/test/java/com/baeldung/jackson/miscellaneous/mixin/OptionalTypeTest.java b/jackson/src/test/java/com/baeldung/jackson/miscellaneous/mixin/OptionalTypeTest.java new file mode 100644 index 0000000000..c6d51cd57f --- /dev/null +++ b/jackson/src/test/java/com/baeldung/jackson/miscellaneous/mixin/OptionalTypeTest.java @@ -0,0 +1,61 @@ +package com.baeldung.jackson.miscellaneous.mixin; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.datatype.jdk8.Jdk8Module; +import static io.restassured.path.json.JsonPath.from; +import java.io.IOException; +import java.util.Optional; +import static org.assertj.core.api.Assertions.assertThat; +import org.junit.Test; + +public class OptionalTypeTest { + + ObjectMapper mapper = new ObjectMapper().registerModule(new Jdk8Module()); + + @Test + public void givenPresentOptional_whenSerializing_thenValueInJson() throws JsonProcessingException { + + String subTitle = "The Parish Boy's Progress"; + Book book = new Book(); + book.setTitle("Oliver Twist"); + book.setSubTitle(Optional.of(subTitle)); + + String result = mapper.writeValueAsString(book); + + assertThat(from(result).getString("subTitle")).isEqualTo(subTitle); + } + + @Test + public void givenEmptyOptional_whenSerializing_thenNullValue() throws JsonProcessingException { + + Book book = new Book(); + book.setTitle("Oliver Twist"); + book.setSubTitle(Optional.empty()); + + String result = mapper.writeValueAsString(book); + + assertThat(from(result).getString("subTitle")).isNull(); + } + + @Test + public void givenField_whenDeserializingIntoOptional_thenIsPresentWithValue() throws IOException { + + String subTitle = "The Parish Boy's Progress"; + String book = "{ \"title\": \"Oliver Twist\", \"subTitle\": \"" + subTitle + "\" }"; + + Book result = mapper.readValue(book, Book.class); + + assertThat(result.getSubTitle()).isEqualTo(Optional.of(subTitle)); + } + + @Test + public void givenNullField_whenDeserializingIntoOptional_thenIsEmpty() throws IOException { + + String book = "{ \"title\": \"Oliver Twist\", \"subTitle\": null }"; + + Book result = mapper.readValue(book, Book.class); + + assertThat(result.getSubTitle()).isEmpty(); + } +} diff --git a/jjwt/pom.xml b/jjwt/pom.xml index c1332fa2d7..982b24987b 100644 --- a/jjwt/pom.xml +++ b/jjwt/pom.xml @@ -14,7 +14,7 @@ org.springframework.boot spring-boot-starter-parent - 1.4.2.RELEASE + 1.4.4.RELEASE diff --git a/log-mdc/pom.xml b/log-mdc/pom.xml index 28c8bb820e..931e68a178 100644 --- a/log-mdc/pom.xml +++ b/log-mdc/pom.xml @@ -2,9 +2,9 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 com.baeldung - logmdc + log-mdc 0.0.1-SNAPSHOT - logmdc + log-mdc war tutorial on logging with MDC and NDC diff --git a/log-mdc/src/main/java/com/baeldung/mdc/TransactionFactory.java b/log-mdc/src/main/java/com/baeldung/mdc/TransactionFactory.java index ec1887eea6..0904e4603f 100644 --- a/log-mdc/src/main/java/com/baeldung/mdc/TransactionFactory.java +++ b/log-mdc/src/main/java/com/baeldung/mdc/TransactionFactory.java @@ -7,15 +7,15 @@ import java.util.UUID; public class TransactionFactory { - private static final String[] NAMES = {"John", "Susan", "Marc", "Samantha"}; - private static long nextId = 1; - - public Transfer newInstance() { - String transactionId = String.valueOf( nextId++ ); - String owner = NAMES[ (int) floor(random()*NAMES.length) ]; - long amount = (long) (random()*1500 + 500); - Transfer tx = new Transfer(transactionId, owner, amount); - return tx; - } - + private static final String[] NAMES = { "John", "Susan", "Marc", "Samantha" }; + private static long nextId = 1; + + public Transfer newInstance() { + String transactionId = String.valueOf(nextId++); + String owner = NAMES[(int) floor(random() * NAMES.length)]; + long amount = (long) (random() * 1500 + 500); + Transfer tx = new Transfer(transactionId, owner, amount); + return tx; + } + } diff --git a/log-mdc/src/main/java/com/baeldung/mdc/TransferDemo.java b/log-mdc/src/main/java/com/baeldung/mdc/TransferDemo.java index daf256007c..259e9a8c5c 100644 --- a/log-mdc/src/main/java/com/baeldung/mdc/TransferDemo.java +++ b/log-mdc/src/main/java/com/baeldung/mdc/TransferDemo.java @@ -18,11 +18,11 @@ public class TransferDemo { for (int i = 0; i < 10; i++) { Transfer tx = transactionFactory.newInstance(); - - //Runnable task = new Log4JRunnable(tx); - //Runnable task = new Log4J2Runnable(tx); + + // Runnable task = new Log4JRunnable(tx); + // Runnable task = new Log4J2Runnable(tx); Runnable task = new Slf4jRunnable(tx); - + executor.submit(task); } diff --git a/log-mdc/src/main/java/com/baeldung/ndc/controller/JBossLoggingController.java b/log-mdc/src/main/java/com/baeldung/ndc/controller/JBossLoggingController.java index b024f3ec81..e581c45cd3 100644 --- a/log-mdc/src/main/java/com/baeldung/ndc/controller/JBossLoggingController.java +++ b/log-mdc/src/main/java/com/baeldung/ndc/controller/JBossLoggingController.java @@ -13,7 +13,6 @@ import org.springframework.web.bind.annotation.RestController; import com.baeldung.ndc.Investment; import com.baeldung.ndc.service.InvestmentService; - @RestController public class JBossLoggingController { @Autowired diff --git a/log-mdc/src/test/java/com/baeldung/mdc/log4j/Demo.java b/log-mdc/src/test/java/com/baeldung/mdc/log4j/Demo.java index f9a210606f..665168452a 100644 --- a/log-mdc/src/test/java/com/baeldung/mdc/log4j/Demo.java +++ b/log-mdc/src/test/java/com/baeldung/mdc/log4j/Demo.java @@ -17,7 +17,7 @@ public class Demo { TransactionFactory transactionFactory = new TransactionFactory(); for (int i = 0; i < 10; i++) { Transfer tx = transactionFactory.newInstance(); - Runnable task = new Log4JRunnable(tx); + Runnable task = new Log4JRunnable(tx); executor.submit(task); } executor.shutdown(); diff --git a/log-mdc/src/test/java/com/baeldung/mdc/log4j2/Demo.java b/log-mdc/src/test/java/com/baeldung/mdc/log4j2/Demo.java index 3f7c1d37d5..78c48c2a83 100644 --- a/log-mdc/src/test/java/com/baeldung/mdc/log4j2/Demo.java +++ b/log-mdc/src/test/java/com/baeldung/mdc/log4j2/Demo.java @@ -21,7 +21,7 @@ public class Demo { TransactionFactory transactionFactory = new TransactionFactory(); for (int i = 0; i < 10; i++) { Transfer tx = transactionFactory.newInstance(); - Runnable task = new Log4J2Runnable(tx); + Runnable task = new Log4J2Runnable(tx); executor.submit(task); } executor.shutdown(); diff --git a/log-mdc/src/test/java/com/baeldung/mdc/slf4j/Demo.java b/log-mdc/src/test/java/com/baeldung/mdc/slf4j/Demo.java index 98db698f47..de890f9f5d 100644 --- a/log-mdc/src/test/java/com/baeldung/mdc/slf4j/Demo.java +++ b/log-mdc/src/test/java/com/baeldung/mdc/slf4j/Demo.java @@ -21,7 +21,7 @@ public class Demo { TransactionFactory transactionFactory = new TransactionFactory(); for (int i = 0; i < 10; i++) { Transfer tx = transactionFactory.newInstance(); - Runnable task = new Slf4jRunnable(tx); + Runnable task = new Slf4jRunnable(tx); executor.submit(task); } executor.shutdown(); diff --git a/metrics/src/test/java/com/baeldung/metrics/core/MetricsTest.java b/metrics/src/test/java/com/baeldung/metrics/core/MetricsTest.java index f670acfaef..e876de6e65 100644 --- a/metrics/src/test/java/com/baeldung/metrics/core/MetricsTest.java +++ b/metrics/src/test/java/com/baeldung/metrics/core/MetricsTest.java @@ -138,15 +138,15 @@ public class MetricsTest { long elapsed1 = context1.stop(); - assertEquals(5000000000L, elapsed1, 10000000); + assertEquals(5000000000L, elapsed1, 1000000000); assertThat(timer.getCount(), equalTo(1L)); - assertEquals(0.2, timer.getMeanRate(), 0.1); + assertEquals(0.2, timer.getMeanRate(), 0.2); Timer.Context context2 = timer.time(); TimeUnit.SECONDS.sleep(2); context2.close(); assertThat(timer.getCount(), equalTo(2L)); - assertEquals(0.3, timer.getMeanRate(), 0.1); + assertEquals(0.3, timer.getMeanRate(), 0.2); } } diff --git a/pom.xml b/pom.xml index 41235dcc26..2392e2c594 100644 --- a/pom.xml +++ b/pom.xml @@ -11,6 +11,8 @@ UTF-8 + refs/heads/master + false @@ -147,6 +149,7 @@ spring-rest-docs spring-rest spring-security-basic-auth + spring-security-cache-control spring-security-client/spring-security-jsp-authentication spring-security-client/spring-security-jsp-authorize spring-security-client/spring-security-jsp-config @@ -155,7 +158,7 @@ spring-security-client/spring-security-thymeleaf-authorize spring-security-client/spring-security-thymeleaf-config spring-security-core - spring-security-custom-permission + spring-security-mvc-boot spring-security-mvc-custom spring-security-mvc-digest-auth spring-security-mvc-ldap @@ -189,6 +192,15 @@ struts2 - + + + + + com.vackosar.gitflowincrementalbuilder + gitflow-incremental-builder + 3.1 + + + diff --git a/rxjava/pom.xml b/rxjava/pom.xml index 63aa1f127e..b3936bf78d 100644 --- a/rxjava/pom.xml +++ b/rxjava/pom.xml @@ -26,10 +26,25 @@ rxjava ${rx.java.version} + + junit + junit + ${junit.version} + + + org.hamcrest + hamcrest-all + ${hamcrest.version} + test + + + + 4.12 1.2.5 + 1.3 \ No newline at end of file diff --git a/rxjava/src/main/java/com/baelding/rxjava/ColdObservableBackpressure.java b/rxjava/src/main/java/com/baelding/rxjava/ColdObservableBackpressure.java deleted file mode 100644 index 9855123a3b..0000000000 --- a/rxjava/src/main/java/com/baelding/rxjava/ColdObservableBackpressure.java +++ /dev/null @@ -1,38 +0,0 @@ -package com.baelding.rxjava; - -import rx.Observable; -import rx.schedulers.Schedulers; - -public class ColdObservableBackpressure { - public static void main(String[] args) throws InterruptedException { - Observable.range(1, 1_000_000).observeOn(Schedulers.computation()).subscribe(v -> ComputeFunction.compute(v), Throwable::printStackTrace); - - Thread.sleep(10_000); - - // Observable.range(1, 1_000_000) //implementation of reactive pull backpressure on cold observable - // .subscribe(new Subscriber() { - // @Override - // public void onStart() { - // request(1); - // } - // - // public void onNext(Integer v) { - // compute(v); - // - // request(1); - // } - // - // @Override - // public void onError(Throwable ex) { - // ex.printStackTrace(); - // } - // - // @Override - // public void onCompleted() { - // System.out.println("Done!"); - // } - // }); - - } - -} diff --git a/rxjava/src/main/java/com/baelding/rxjava/HotObservableBackpressureBatching.java b/rxjava/src/main/java/com/baelding/rxjava/HotObservableBackpressureBatching.java deleted file mode 100644 index 6acda7eaad..0000000000 --- a/rxjava/src/main/java/com/baelding/rxjava/HotObservableBackpressureBatching.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.baelding.rxjava; - -import rx.schedulers.Schedulers; -import rx.subjects.PublishSubject; - -public class HotObservableBackpressureBatching { - public static void main(String[] args) throws InterruptedException { - PublishSubject source = PublishSubject.create(); - - source.window(500).observeOn(Schedulers.computation()).subscribe(ComputeFunction::compute, Throwable::printStackTrace); - - for (int i = 0; i < 1_000_000; i++) { - source.onNext(i); - } - Thread.sleep(10_000); - } - -} diff --git a/rxjava/src/main/java/com/baelding/rxjava/HotObservableBackpressureBuffering.java b/rxjava/src/main/java/com/baelding/rxjava/HotObservableBackpressureBuffering.java deleted file mode 100644 index 50638f4c8a..0000000000 --- a/rxjava/src/main/java/com/baelding/rxjava/HotObservableBackpressureBuffering.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.baelding.rxjava; - -import rx.schedulers.Schedulers; -import rx.subjects.PublishSubject; - -public class HotObservableBackpressureBuffering { - public static void main(String[] args) throws InterruptedException { - PublishSubject source = PublishSubject.create(); - - source.buffer(1024).observeOn(Schedulers.computation()).subscribe(ComputeFunction::compute, Throwable::printStackTrace); - - for (int i = 0; i < 1_000_000; i++) { - source.onNext(i); - } - Thread.sleep(10_000); - } -} diff --git a/rxjava/src/main/java/com/baelding/rxjava/HotObservableBackpressureSkipping.java b/rxjava/src/main/java/com/baelding/rxjava/HotObservableBackpressureSkipping.java deleted file mode 100644 index f6f8b9f563..0000000000 --- a/rxjava/src/main/java/com/baelding/rxjava/HotObservableBackpressureSkipping.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.baelding.rxjava; - -import rx.schedulers.Schedulers; -import rx.subjects.PublishSubject; - -import java.util.concurrent.TimeUnit; - -public class HotObservableBackpressureSkipping { - public static void main(String[] args) throws InterruptedException { - PublishSubject source = PublishSubject.create(); - - source.sample(100, TimeUnit.MILLISECONDS) - // .throttleFirst(100, TimeUnit.MILLISECONDS) - .observeOn(Schedulers.computation()).subscribe(ComputeFunction::compute, Throwable::printStackTrace); - - for (int i = 0; i < 1_000_000; i++) { - source.onNext(i); - } - Thread.sleep(10_000); - } -} diff --git a/rxjava/src/main/java/com/baelding/rxjava/HotObservableOnBackpressure.java b/rxjava/src/main/java/com/baelding/rxjava/HotObservableOnBackpressure.java deleted file mode 100644 index afef8027bf..0000000000 --- a/rxjava/src/main/java/com/baelding/rxjava/HotObservableOnBackpressure.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.baelding.rxjava; - -import rx.BackpressureOverflow; -import rx.Observable; -import rx.schedulers.Schedulers; - -public class HotObservableOnBackpressure { - public static void main(String[] args) throws InterruptedException { - Observable.range(1, 1_000_000).onBackpressureBuffer(16, () -> { - }, BackpressureOverflow.ON_OVERFLOW_DROP_OLDEST).observeOn(Schedulers.computation()).subscribe(e -> { - }, Throwable::printStackTrace); - - Observable.range(1, 1_000_000).onBackpressureDrop().observeOn(Schedulers.io()).doOnNext(ComputeFunction::compute).subscribe(v -> { - }, Throwable::printStackTrace); - Thread.sleep(10_000); - - } -} diff --git a/rxjava/src/main/java/com/baelding/rxjava/HotObservableWithoutBackpressure.java b/rxjava/src/main/java/com/baelding/rxjava/HotObservableWithoutBackpressure.java deleted file mode 100644 index 7745dbe5c4..0000000000 --- a/rxjava/src/main/java/com/baelding/rxjava/HotObservableWithoutBackpressure.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.baelding.rxjava; - - -import rx.schedulers.Schedulers; -import rx.subjects.PublishSubject; - -public class HotObservableWithoutBackpressure { - public static void main(String[] args) throws InterruptedException { - PublishSubject source = PublishSubject.create(); - - source.observeOn(Schedulers.computation()) - .subscribe(ComputeFunction::compute, Throwable::printStackTrace); - - - for (int i = 0; i < 1_000_000; i++) { - source.onNext(i); - } - Thread.sleep(10_000); - } -} diff --git a/rxjava/src/test/java/com/baeldung/rxjava/RxJavaBackpressureTest.java b/rxjava/src/test/java/com/baeldung/rxjava/RxJavaBackpressureTest.java new file mode 100644 index 0000000000..8a495650b3 --- /dev/null +++ b/rxjava/src/test/java/com/baeldung/rxjava/RxJavaBackpressureTest.java @@ -0,0 +1,130 @@ +package com.baeldung.rxjava; + +import org.junit.Test; +import rx.BackpressureOverflow; +import rx.Observable; +import rx.exceptions.MissingBackpressureException; +import rx.observers.TestSubscriber; +import rx.schedulers.Schedulers; +import rx.subjects.PublishSubject; + +import java.util.List; +import java.util.concurrent.TimeUnit; +import java.util.stream.IntStream; + +import static org.junit.Assert.assertTrue; + +public class RxJavaBackpressureTest { + + @Test + public void givenColdObservable_shouldNotThrowException() { + // given + TestSubscriber testSubscriber = new TestSubscriber<>(); + + // when + Observable.range(1, 1_000_000).observeOn(Schedulers.computation()).subscribe(testSubscriber); + + // then + testSubscriber.awaitTerminalEvent(); + assertTrue(testSubscriber.getOnErrorEvents().size() == 0); + + } + + @Test + public void givenHotObservable_whenBackpressureNotDefined_shouldTrowException() { + // given + TestSubscriber testSubscriber = new TestSubscriber<>(); + PublishSubject source = PublishSubject. create(); + + source.observeOn(Schedulers.computation()).subscribe(testSubscriber); + + // when + IntStream.range(0, 1_000_000).forEach(source::onNext); + + // then + testSubscriber.awaitTerminalEvent(); + testSubscriber.assertError(MissingBackpressureException.class); + } + + @Test + public void givenHotObservable_whenWindowIsDefined_shouldNotThrowException() { + // given + TestSubscriber> testSubscriber = new TestSubscriber<>(); + PublishSubject source = PublishSubject. create(); + + // when + source.window(500).observeOn(Schedulers.computation()).subscribe(testSubscriber); + + IntStream.range(0, 1_000).forEach(source::onNext); + + // then + testSubscriber.awaitTerminalEvent(2, TimeUnit.SECONDS); + assertTrue(testSubscriber.getOnErrorEvents().size() == 0); + + } + + @Test + public void givenHotObservable_whenBufferIsDefined_shouldNotThrowException() { + // given + TestSubscriber> testSubscriber = new TestSubscriber<>(); + PublishSubject source = PublishSubject. create(); + + // when + source.buffer(1024).observeOn(Schedulers.computation()).subscribe(testSubscriber); + + IntStream.range(0, 1_000).forEach(source::onNext); + + // then + testSubscriber.awaitTerminalEvent(2, TimeUnit.SECONDS); + assertTrue(testSubscriber.getOnErrorEvents().size() == 0); + + } + + @Test + public void givenHotObservable_whenSkippingOperationIsDefined_shouldNotThrowException() { + // given + TestSubscriber testSubscriber = new TestSubscriber<>(); + PublishSubject source = PublishSubject. create(); + + // when + source.sample(100, TimeUnit.MILLISECONDS) + // .throttleFirst(100, TimeUnit.MILLISECONDS) + .observeOn(Schedulers.computation()).subscribe(testSubscriber); + + IntStream.range(0, 1_000).forEach(source::onNext); + + // then + testSubscriber.awaitTerminalEvent(2, TimeUnit.SECONDS); + assertTrue(testSubscriber.getOnErrorEvents().size() == 0); + + } + + @Test + public void givenHotObservable_whenOnBackpressureBufferDefined_shouldNotThrowException() { + // given + TestSubscriber testSubscriber = new TestSubscriber<>(); + + // when + Observable.range(1, 1_000_000).onBackpressureBuffer(16, () -> { + }, BackpressureOverflow.ON_OVERFLOW_DROP_OLDEST).observeOn(Schedulers.computation()).subscribe(testSubscriber); + + // then + testSubscriber.awaitTerminalEvent(2, TimeUnit.SECONDS); + assertTrue(testSubscriber.getOnErrorEvents().size() == 0); + + } + + @Test + public void givenHotObservable_whenOnBackpressureDropDefined_shouldNotThrowException() { + // given + TestSubscriber testSubscriber = new TestSubscriber<>(); + + // when + Observable.range(1, 1_000_000).onBackpressureDrop().observeOn(Schedulers.computation()).subscribe(testSubscriber); + + // then + testSubscriber.awaitTerminalEvent(2, TimeUnit.SECONDS); + assertTrue(testSubscriber.getOnErrorEvents().size() == 0); + + } +} diff --git a/rxjava/src/test/java/com/baeldung/rxjava/RxJavaTesting.java b/rxjava/src/test/java/com/baeldung/rxjava/RxJavaTesting.java new file mode 100644 index 0000000000..91c8ed540e --- /dev/null +++ b/rxjava/src/test/java/com/baeldung/rxjava/RxJavaTesting.java @@ -0,0 +1,98 @@ +package com.baeldung.rxjava; + +import org.junit.Test; +import rx.Observable; +import rx.observers.TestSubscriber; +import rx.schedulers.TestScheduler; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.concurrent.TimeUnit; + +import static org.hamcrest.Matchers.*; +import static org.junit.Assert.assertThat; + +public class RxJavaTesting { + @Test + public void givenObservable_whenZip_shouldAssertBlockingInASameThread() { + // given + List letters = Arrays.asList("A", "B", "C", "D", "E"); + List results = new ArrayList<>(); + Observable observable = Observable.from(letters).zipWith(Observable.range(1, Integer.MAX_VALUE), (string, index) -> index + "-" + string); + + // when + observable.subscribe(results::add); + + // then + assertThat(results, notNullValue()); + assertThat(results, hasSize(5)); + assertThat(results, hasItems("1-A", "2-B", "3-C", "4-D", "5-E")); + } + + @Test + public void givenObservable_whenZip_shouldAssertOnTestSubscriber() { + // given + List letters = Arrays.asList("A", "B", "C", "D", "E"); + TestSubscriber subscriber = new TestSubscriber<>(); + + Observable observable = Observable.from(letters).zipWith(Observable.range(1, Integer.MAX_VALUE), ((string, index) -> index + "-" + string)); + + // when + observable.subscribe(subscriber); + + // then + subscriber.assertCompleted(); + subscriber.assertNoErrors(); + subscriber.assertValueCount(5); + assertThat(subscriber.getOnNextEvents(), hasItems("1-A", "2-B", "3-C", "4-D", "5-E")); + } + + @Test + public void givenTestObserver_whenExceptionWasThrowsOnObservable_observerShouldGetError() { + // given + List letters = Arrays.asList("A", "B", "C", "D", "E"); + TestSubscriber subscriber = new TestSubscriber<>(); + + Observable observable = Observable.from(letters).zipWith(Observable.range(1, Integer.MAX_VALUE), ((string, index) -> index + "-" + string)).concatWith(Observable.error(new RuntimeException("error in Observable"))); + + // when + observable.subscribe(subscriber); + + // then + subscriber.assertError(RuntimeException.class); + subscriber.assertNotCompleted(); + } + + @Test + public void givenObservableThatEmitsEventPerSecond_whenUseAdvanceByTime_shouldEmitEventPerSecond() { + // given + List letters = Arrays.asList("A", "B", "C", "D", "E"); + TestScheduler scheduler = new TestScheduler(); + TestSubscriber subscriber = new TestSubscriber<>(); + Observable tick = Observable.interval(1, TimeUnit.SECONDS, scheduler); + + Observable observable = Observable.from(letters).zipWith(tick, (string, index) -> index + "-" + string); + + observable.subscribeOn(scheduler).subscribe(subscriber); + + // expect + subscriber.assertNoValues(); + subscriber.assertNotCompleted(); + + // when + scheduler.advanceTimeBy(1, TimeUnit.SECONDS); + + // then + subscriber.assertNoErrors(); + subscriber.assertValueCount(1); + subscriber.assertValues("0-A"); + + // when + scheduler.advanceTimeTo(6, TimeUnit.SECONDS); + subscriber.assertCompleted(); + subscriber.assertNoErrors(); + subscriber.assertValueCount(5); + assertThat(subscriber.getOnNextEvents(), hasItems("0-A", "1-B", "2-C", "3-D", "4-E")); + } +} diff --git a/spring-all/pom.xml b/spring-all/pom.xml index deb6bd6f6a..f28fe1f10d 100644 --- a/spring-all/pom.xml +++ b/spring-all/pom.xml @@ -11,7 +11,7 @@ org.springframework.boot spring-boot-starter-parent - 1.4.2.RELEASE + 1.4.4.RELEASE diff --git a/spring-all/src/test/java/org/baeldung/properties/external/ExternalPropertiesWithXmlIntegrationTest.java b/spring-all/src/test/java/org/baeldung/properties/external/ExternalPropertiesWithXmlManualTest.java similarity index 95% rename from spring-all/src/test/java/org/baeldung/properties/external/ExternalPropertiesWithXmlIntegrationTest.java rename to spring-all/src/test/java/org/baeldung/properties/external/ExternalPropertiesWithXmlManualTest.java index 2ea2822b9a..a8a7bda91c 100644 --- a/spring-all/src/test/java/org/baeldung/properties/external/ExternalPropertiesWithXmlIntegrationTest.java +++ b/spring-all/src/test/java/org/baeldung/properties/external/ExternalPropertiesWithXmlManualTest.java @@ -13,7 +13,7 @@ import org.springframework.test.context.support.AnnotationConfigContextLoader; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = { ExternalPropertiesWithXmlConfig.class }, loader = AnnotationConfigContextLoader.class) @Ignore("manual only") -public class ExternalPropertiesWithXmlIntegrationTest { +public class ExternalPropertiesWithXmlManualTest { @Autowired private Environment env; diff --git a/spring-all/src/test/java/org/baeldung/springretry/SpringRetryTest.java b/spring-all/src/test/java/org/baeldung/springretry/SpringRetryIntegrationTest.java similarity index 96% rename from spring-all/src/test/java/org/baeldung/springretry/SpringRetryTest.java rename to spring-all/src/test/java/org/baeldung/springretry/SpringRetryIntegrationTest.java index 2f3411957e..d7d0943e6b 100644 --- a/spring-all/src/test/java/org/baeldung/springretry/SpringRetryTest.java +++ b/spring-all/src/test/java/org/baeldung/springretry/SpringRetryIntegrationTest.java @@ -12,7 +12,7 @@ import java.sql.SQLException; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = AppConfig.class, loader = AnnotationConfigContextLoader.class) -public class SpringRetryTest { +public class SpringRetryIntegrationTest { @Autowired private MyService myService; diff --git a/spring-all/src/test/java/org/baeldung/taskscheduler/ThreadPoolTaskSchedulerTest.java b/spring-all/src/test/java/org/baeldung/taskscheduler/ThreadPoolTaskSchedulerIntegrationTest.java similarity index 91% rename from spring-all/src/test/java/org/baeldung/taskscheduler/ThreadPoolTaskSchedulerTest.java rename to spring-all/src/test/java/org/baeldung/taskscheduler/ThreadPoolTaskSchedulerIntegrationTest.java index cc247cb384..d95857d384 100644 --- a/spring-all/src/test/java/org/baeldung/taskscheduler/ThreadPoolTaskSchedulerTest.java +++ b/spring-all/src/test/java/org/baeldung/taskscheduler/ThreadPoolTaskSchedulerIntegrationTest.java @@ -8,7 +8,8 @@ import org.springframework.test.context.support.AnnotationConfigContextLoader; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = { ThreadPoolTaskSchedulerConfig.class }, loader = AnnotationConfigContextLoader.class) -public class ThreadPoolTaskSchedulerTest { +public class ThreadPoolTaskSchedulerIntegrationTest { + @Test public void testThreadPoolTaskSchedulerAnnotation() throws InterruptedException { Thread.sleep(2550); diff --git a/spring-all/src/test/java/org/baeldung/test/IntegrationTestSuite.java b/spring-all/src/test/java/org/baeldung/test/IntegrationTestSuite.java index f9845f42a7..470ae9e538 100644 --- a/spring-all/src/test/java/org/baeldung/test/IntegrationTestSuite.java +++ b/spring-all/src/test/java/org/baeldung/test/IntegrationTestSuite.java @@ -5,7 +5,7 @@ import org.baeldung.properties.basic.PropertiesWithMultipleXmlsIntegrationTest; import org.baeldung.properties.basic.PropertiesWithXmlIntegrationTest; import org.baeldung.properties.external.ExternalPropertiesWithJavaIntegrationTest; import org.baeldung.properties.external.ExternalPropertiesWithMultipleXmlsIntegrationTest; -import org.baeldung.properties.external.ExternalPropertiesWithXmlIntegrationTest; +import org.baeldung.properties.external.ExternalPropertiesWithXmlManualTest; import org.junit.runner.RunWith; import org.junit.runners.Suite; import org.junit.runners.Suite.SuiteClasses; @@ -15,7 +15,7 @@ import org.junit.runners.Suite.SuiteClasses; PropertiesWithXmlIntegrationTest.class, ExternalPropertiesWithJavaIntegrationTest.class, ExternalPropertiesWithMultipleXmlsIntegrationTest.class, - ExternalPropertiesWithXmlIntegrationTest.class, + ExternalPropertiesWithXmlManualTest.class, ExtendedPropertiesWithJavaIntegrationTest.class, PropertiesWithMultipleXmlsIntegrationTest.class, })// @formatter:on diff --git a/spring-boot/pom.xml b/spring-boot/pom.xml index 68a5857865..b6a24b6cb7 100644 --- a/spring-boot/pom.xml +++ b/spring-boot/pom.xml @@ -1,5 +1,5 @@ + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 com.baeldung spring-boot @@ -12,8 +12,8 @@ org.springframework.boot spring-boot-starter-parent - 1.4.2.RELEASE - + 1.5.1.RELEASE + @@ -24,6 +24,26 @@ org.springframework.boot spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-tomcat + + + + + + org.apache.geronimo.specs + geronimo-osgi-locator + 1.1 + test + + + + org.apache.geronimo.components + geronimo-jaspi + 2.0.0 + test @@ -87,6 +107,20 @@ jquery ${jquery.version} + + + org.apache.tomee + arquillian-tomee-embedded + ${arquillian-tomee-embedded.version} + test + + + + org.apache.tomee + javaee-api + ${tomee-javaee-api.version} + provided + @@ -166,7 +200,7 @@ - json + json @@ -186,6 +220,8 @@ 3.1.1 3.3.7-1 3.1.7 + 7.0.2 + 7.0-1 diff --git a/spring-boot/src/main/java/com/baeldung/annotation/servletcomponentscan/JavaEEApp.java b/spring-boot/src/main/java/com/baeldung/annotation/servletcomponentscan/JavaEEApp.java new file mode 100644 index 0000000000..773503c5af --- /dev/null +++ b/spring-boot/src/main/java/com/baeldung/annotation/servletcomponentscan/JavaEEApp.java @@ -0,0 +1,28 @@ +package com.baeldung.annotation.servletcomponentscan; + +import javax.enterprise.context.ApplicationScoped; +import javax.enterprise.context.Initialized; +import javax.enterprise.event.Observes; +import javax.servlet.ServletContext; + +@ApplicationScoped +public class JavaEEApp { + + private ServletContext context; + + /** + * act as a servletContext provider + */ + private void setContext(@Observes @Initialized(ApplicationScoped.class) final ServletContext context) { + if (this.context != null) { + throw new IllegalStateException("app context started twice"); + } + + this.context = context; + } + + public ServletContext getContext() { + return context; + } + +} diff --git a/spring-boot/src/main/java/com/baeldung/annotation/servletcomponentscan/SpringBootAnnotatedApp.java b/spring-boot/src/main/java/com/baeldung/annotation/servletcomponentscan/SpringBootAnnotatedApp.java new file mode 100644 index 0000000000..9fd66ee12a --- /dev/null +++ b/spring-boot/src/main/java/com/baeldung/annotation/servletcomponentscan/SpringBootAnnotatedApp.java @@ -0,0 +1,25 @@ +package com.baeldung.annotation.servletcomponentscan; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.web.servlet.ServletComponentScan; + +/** + * using the following annotations are equivalent: + *
  • + * @ServletComponentScan + *
  • + * @ServletComponentScan(basePackages = "com.baeldung.annotation.servletcomponentscan.javaee") + *
  • + * @ServletComponentScan(basePackageClasses = {AttrListener.class, HelloFilter.class, HelloServlet.class, EchoServlet.class}) + *
+ */ +@SpringBootApplication +@ServletComponentScan("com.baeldung.annotation.servletcomponentscan.javaee") +public class SpringBootAnnotatedApp { + + public static void main(String[] args) { + SpringApplication.run(SpringBootAnnotatedApp.class, args); + } + +} diff --git a/spring-boot/src/main/java/com/baeldung/annotation/servletcomponentscan/SpringBootPlainApp.java b/spring-boot/src/main/java/com/baeldung/annotation/servletcomponentscan/SpringBootPlainApp.java new file mode 100644 index 0000000000..9ce1c296e6 --- /dev/null +++ b/spring-boot/src/main/java/com/baeldung/annotation/servletcomponentscan/SpringBootPlainApp.java @@ -0,0 +1,13 @@ +package com.baeldung.annotation.servletcomponentscan; + +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.ComponentScan; + +@SpringBootApplication +@ComponentScan(basePackages = "com.baeldung.annotation.servletcomponentscan.javaee") +public class SpringBootPlainApp { + + public static void main(String[] args) { + } + +} diff --git a/spring-boot/src/main/java/com/baeldung/annotation/servletcomponentscan/javaee/AttrListener.java b/spring-boot/src/main/java/com/baeldung/annotation/servletcomponentscan/javaee/AttrListener.java new file mode 100644 index 0000000000..321ddd59d1 --- /dev/null +++ b/spring-boot/src/main/java/com/baeldung/annotation/servletcomponentscan/javaee/AttrListener.java @@ -0,0 +1,23 @@ +package com.baeldung.annotation.servletcomponentscan.javaee; + +import javax.servlet.ServletContextEvent; +import javax.servlet.ServletContextListener; +import javax.servlet.annotation.WebListener; + +@WebListener +public class AttrListener implements ServletContextListener { + + @Override + public void contextInitialized(ServletContextEvent servletContextEvent) { + servletContextEvent + .getServletContext() + .setAttribute("servlet-context-attr", "test"); + System.out.println("context init"); + } + + @Override + public void contextDestroyed(ServletContextEvent servletContextEvent) { + System.out.println("context destroy"); + } + +} diff --git a/spring-boot/src/main/java/com/baeldung/annotation/servletcomponentscan/javaee/EchoServlet.java b/spring-boot/src/main/java/com/baeldung/annotation/servletcomponentscan/javaee/EchoServlet.java new file mode 100644 index 0000000000..b9fed314c7 --- /dev/null +++ b/spring-boot/src/main/java/com/baeldung/annotation/servletcomponentscan/javaee/EchoServlet.java @@ -0,0 +1,30 @@ +package com.baeldung.annotation.servletcomponentscan.javaee; + +import javax.servlet.annotation.WebServlet; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.File; +import java.io.IOException; +import java.nio.file.CopyOption; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.StandardCopyOption; + +@WebServlet(name = "echo servlet", urlPatterns = "/echo") +public class EchoServlet extends HttpServlet { + + @Override + public void doPost(HttpServletRequest request, HttpServletResponse response) { + try { + Path path = File + .createTempFile("echo", "tmp") + .toPath(); + Files.copy(request.getInputStream(), path, StandardCopyOption.REPLACE_EXISTING); + Files.copy(path, response.getOutputStream()); + Files.delete(path); + } catch (IOException e) { + e.printStackTrace(); + } + } +} diff --git a/spring-boot/src/main/java/com/baeldung/annotation/servletcomponentscan/javaee/HelloFilter.java b/spring-boot/src/main/java/com/baeldung/annotation/servletcomponentscan/javaee/HelloFilter.java new file mode 100644 index 0000000000..81e90d69ad --- /dev/null +++ b/spring-boot/src/main/java/com/baeldung/annotation/servletcomponentscan/javaee/HelloFilter.java @@ -0,0 +1,32 @@ +package com.baeldung.annotation.servletcomponentscan.javaee; + +import javax.servlet.*; +import javax.servlet.annotation.WebFilter; +import javax.servlet.annotation.WebInitParam; +import java.io.IOException; + +@WebFilter(urlPatterns = "/hello", description = "a filter for hello servlet", initParams = { @WebInitParam(name = "msg", value = "filtering ") }, filterName = "hello filter", servletNames = { "echo servlet" }) +public class HelloFilter implements Filter { + + private FilterConfig filterConfig; + + @Override + public void init(FilterConfig filterConfig) throws ServletException { + System.out.println("filter init"); + this.filterConfig = filterConfig; + } + + @Override + public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { + servletResponse + .getOutputStream() + .print(filterConfig.getInitParameter("msg")); + filterChain.doFilter(servletRequest, servletResponse); + } + + @Override + public void destroy() { + System.out.println("filter destroy"); + } + +} diff --git a/spring-boot/src/main/java/com/baeldung/annotation/servletcomponentscan/javaee/HelloServlet.java b/spring-boot/src/main/java/com/baeldung/annotation/servletcomponentscan/javaee/HelloServlet.java new file mode 100644 index 0000000000..4a46a56107 --- /dev/null +++ b/spring-boot/src/main/java/com/baeldung/annotation/servletcomponentscan/javaee/HelloServlet.java @@ -0,0 +1,33 @@ +package com.baeldung.annotation.servletcomponentscan.javaee; + +import javax.servlet.ServletConfig; +import javax.servlet.ServletException; +import javax.servlet.annotation.WebInitParam; +import javax.servlet.annotation.WebServlet; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; + +@WebServlet(urlPatterns = "/hello", initParams = { @WebInitParam(name = "msg", value = "hello")}) +public class HelloServlet extends HttpServlet { + + private ServletConfig servletConfig; + + @Override + public void init(ServletConfig servletConfig){ + this.servletConfig = servletConfig; + } + + @Override + public void doGet(HttpServletRequest request, HttpServletResponse response) { + try { + response + .getOutputStream() + .write(servletConfig.getInitParameter("msg").getBytes()); + } catch (IOException e) { + e.printStackTrace(); + } + } + +} diff --git a/spring-boot/src/main/java/com/baeldung/TestController.java b/spring-boot/src/main/java/com/baeldung/webjar/TestController.java similarity index 91% rename from spring-boot/src/main/java/com/baeldung/TestController.java rename to spring-boot/src/main/java/com/baeldung/webjar/TestController.java index 0e28ca67f8..e8e7fd5ce9 100644 --- a/spring-boot/src/main/java/com/baeldung/TestController.java +++ b/spring-boot/src/main/java/com/baeldung/webjar/TestController.java @@ -1,4 +1,4 @@ -package com.baeldung; +package com.baeldung.webjar; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; diff --git a/spring-boot/src/main/java/com/baeldung/WebjarsdemoApplication.java b/spring-boot/src/main/java/com/baeldung/webjar/WebjarsdemoApplication.java similarity index 77% rename from spring-boot/src/main/java/com/baeldung/WebjarsdemoApplication.java rename to spring-boot/src/main/java/com/baeldung/webjar/WebjarsdemoApplication.java index 35490131c6..d2135754c9 100644 --- a/spring-boot/src/main/java/com/baeldung/WebjarsdemoApplication.java +++ b/spring-boot/src/main/java/com/baeldung/webjar/WebjarsdemoApplication.java @@ -1,7 +1,8 @@ -package com.baeldung; +package com.baeldung.webjar; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.ComponentScan; @SpringBootApplication public class WebjarsdemoApplication { diff --git a/spring-boot/src/main/java/org/baeldung/common/error/SpringHelloServletRegistrationBean.java b/spring-boot/src/main/java/org/baeldung/common/error/SpringHelloServletRegistrationBean.java index 78680baf7d..723afddd06 100644 --- a/spring-boot/src/main/java/org/baeldung/common/error/SpringHelloServletRegistrationBean.java +++ b/spring-boot/src/main/java/org/baeldung/common/error/SpringHelloServletRegistrationBean.java @@ -1,8 +1,8 @@ package org.baeldung.common.error; -import javax.servlet.Servlet; +import org.springframework.boot.web.servlet.ServletRegistrationBean; -import org.springframework.boot.context.embedded.ServletRegistrationBean; +import javax.servlet.Servlet; public class SpringHelloServletRegistrationBean extends ServletRegistrationBean { diff --git a/spring-boot/src/main/java/org/baeldung/common/properties/MyServletContainerCustomizationBean.java b/spring-boot/src/main/java/org/baeldung/common/properties/MyServletContainerCustomizationBean.java index 97130bed6a..9b5a0aa948 100644 --- a/spring-boot/src/main/java/org/baeldung/common/properties/MyServletContainerCustomizationBean.java +++ b/spring-boot/src/main/java/org/baeldung/common/properties/MyServletContainerCustomizationBean.java @@ -2,7 +2,7 @@ package org.baeldung.common.properties; import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer; import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer; -import org.springframework.boot.context.embedded.ErrorPage; +import org.springframework.boot.web.servlet.ErrorPage; import org.springframework.http.HttpStatus; import org.springframework.stereotype.Component; diff --git a/spring-boot/src/main/java/org/baeldung/controller/GenericEntityController.java b/spring-boot/src/main/java/org/baeldung/controller/GenericEntityController.java index 7d6293056a..7d1ad7d899 100644 --- a/spring-boot/src/main/java/org/baeldung/controller/GenericEntityController.java +++ b/spring-boot/src/main/java/org/baeldung/controller/GenericEntityController.java @@ -54,8 +54,6 @@ public class GenericEntityController { @GetMapping("/entity/findbyversion") public ResponseEntity findByVersion(@Version String version) { - return version != null - ? new ResponseEntity(entityList.stream().findFirst().get(), HttpStatus.OK) - : new ResponseEntity(HttpStatus.NOT_FOUND); + return version != null ? new ResponseEntity(entityList.stream().findFirst().get(), HttpStatus.OK) : new ResponseEntity(HttpStatus.NOT_FOUND); } } diff --git a/spring-boot/src/test/java/com/baeldung/annotation/servletcomponentscan/JavaEEAppIntegrationTest.java b/spring-boot/src/test/java/com/baeldung/annotation/servletcomponentscan/JavaEEAppIntegrationTest.java new file mode 100644 index 0000000000..95106d2dc8 --- /dev/null +++ b/spring-boot/src/test/java/com/baeldung/annotation/servletcomponentscan/JavaEEAppIntegrationTest.java @@ -0,0 +1,85 @@ +package com.baeldung.annotation.servletcomponentscan; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.net.MalformedURLException; +import java.net.URI; +import java.net.URL; + +import javax.inject.Inject; +import javax.servlet.FilterRegistration; +import javax.servlet.ServletContext; +import javax.ws.rs.client.Client; +import javax.ws.rs.client.ClientBuilder; +import javax.ws.rs.client.Entity; +import javax.ws.rs.client.WebTarget; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; + +import org.jboss.arquillian.container.test.api.Deployment; +import org.jboss.arquillian.container.test.api.RunAsClient; +import org.jboss.arquillian.junit.Arquillian; +import org.jboss.arquillian.test.api.ArquillianResource; +import org.jboss.shrinkwrap.api.ShrinkWrap; +import org.jboss.shrinkwrap.api.spec.WebArchive; +import org.junit.Test; +import org.junit.runner.RunWith; + +import com.baeldung.annotation.servletcomponentscan.javaee.AttrListener; +import com.baeldung.annotation.servletcomponentscan.javaee.EchoServlet; +import com.baeldung.annotation.servletcomponentscan.javaee.HelloFilter; +import com.baeldung.annotation.servletcomponentscan.javaee.HelloServlet; + +@RunWith(Arquillian.class) +public class JavaEEAppIntegrationTest { + + @Deployment + public static WebArchive createDeployment() { + return ShrinkWrap.create(WebArchive.class).addClass(JavaEEApp.class).addClasses(AttrListener.class, HelloFilter.class, HelloServlet.class, EchoServlet.class); + } + + @Inject + private ServletContext servletContext; + + @Test + public void givenServletContextListener_whenAccessSpecialAttrs_thenFound() throws MalformedURLException { + assertNotNull(servletContext); + assertNotNull(servletContext.getAttribute("servlet-context-attr")); + assertEquals("test", servletContext.getAttribute("servlet-context-attr")); + } + + @Test + public void givenServletContext_whenCheckHelloFilterMappings_thenCorrect() throws MalformedURLException { + assertNotNull(servletContext); + FilterRegistration filterRegistration = servletContext.getFilterRegistration("hello filter"); + + assertNotNull(filterRegistration); + assertTrue(filterRegistration.getServletNameMappings().contains("echo servlet")); + } + + @ArquillianResource + private URL base; + + @Test + @RunAsClient + public void givenFilterAndServlet_whenGetHello_thenRespondFilteringHello() throws MalformedURLException { + Client client = ClientBuilder.newClient(); + WebTarget target = client.target(URI.create(new URL(base, "hello").toExternalForm())); + Response response = target.request().get(); + + assertEquals("filtering hello", response.readEntity(String.class)); + } + + @Test + @RunAsClient + public void givenFilterAndServlet_whenPostEcho_thenEchoFiltered() throws MalformedURLException { + Client client = ClientBuilder.newClient(); + WebTarget target = client.target(URI.create(new URL(base, "echo").toExternalForm())); + Response response = target.request().post(Entity.entity("echo", MediaType.TEXT_PLAIN_TYPE)); + + assertEquals("filtering echo", response.readEntity(String.class)); + } + +} diff --git a/spring-boot/src/test/java/com/baeldung/annotation/servletcomponentscan/SpringBootWithServletComponentIntegrationTest.java b/spring-boot/src/test/java/com/baeldung/annotation/servletcomponentscan/SpringBootWithServletComponentIntegrationTest.java new file mode 100644 index 0000000000..81ac3c9841 --- /dev/null +++ b/spring-boot/src/test/java/com/baeldung/annotation/servletcomponentscan/SpringBootWithServletComponentIntegrationTest.java @@ -0,0 +1,65 @@ +package com.baeldung.annotation.servletcomponentscan; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.web.client.TestRestTemplate; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.test.context.TestPropertySource; +import org.springframework.test.context.junit4.SpringRunner; + +import javax.servlet.FilterRegistration; +import javax.servlet.ServletContext; + +import static org.junit.Assert.*; + +@RunWith(SpringRunner.class) +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = SpringBootAnnotatedApp.class) +@AutoConfigureMockMvc +@TestPropertySource(properties = { "security.basic.enabled=false", "server.tomcat.additional-tld-skip-patterns=tomee-*.jar,tomcat-*.jar,openejb-*.jar,cxf-*.jar,activemq-*.jar" }) +public class SpringBootWithServletComponentIntegrationTest { + + @Autowired private ServletContext servletContext; + + @Test + public void givenServletContext_whenAccessAttrs_thenFoundAttrsPutInServletListner() { + assertNotNull(servletContext); + assertNotNull(servletContext.getAttribute("servlet-context-attr")); + assertEquals("test", servletContext.getAttribute("servlet-context-attr")); + } + + @Test + public void givenServletContext_whenCheckHelloFilterMappings_thenCorrect() { + assertNotNull(servletContext); + FilterRegistration filterRegistration = servletContext.getFilterRegistration("hello filter"); + + assertNotNull(filterRegistration); + assertTrue(filterRegistration + .getServletNameMappings() + .contains("echo servlet")); + } + + @Autowired private TestRestTemplate restTemplate; + + @Test + public void givenServletFilter_whenGetHello_thenRequestFiltered() { + ResponseEntity responseEntity = this.restTemplate.getForEntity("/hello", String.class); + assertEquals(HttpStatus.OK, responseEntity.getStatusCode()); + assertEquals("filtering hello", responseEntity.getBody()); + } + + @Test + public void givenFilterAndServlet_whenPostEcho_thenEchoFiltered() { + ResponseEntity responseEntity = this.restTemplate.postForEntity("/echo", "echo", String.class); + assertEquals(HttpStatus.OK, responseEntity.getStatusCode()); + assertEquals("filtering echo", responseEntity.getBody()); + } + + + +} + + diff --git a/spring-boot/src/test/java/com/baeldung/annotation/servletcomponentscan/SpringBootWithoutServletComponentIntegrationTest.java b/spring-boot/src/test/java/com/baeldung/annotation/servletcomponentscan/SpringBootWithoutServletComponentIntegrationTest.java new file mode 100644 index 0000000000..b2dea25864 --- /dev/null +++ b/spring-boot/src/test/java/com/baeldung/annotation/servletcomponentscan/SpringBootWithoutServletComponentIntegrationTest.java @@ -0,0 +1,50 @@ +package com.baeldung.annotation.servletcomponentscan; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.web.client.TestRestTemplate; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.test.context.TestPropertySource; +import org.springframework.test.context.junit4.SpringRunner; + +import javax.servlet.FilterRegistration; +import javax.servlet.ServletContext; + +import static org.junit.Assert.*; + +@RunWith(SpringRunner.class) +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = SpringBootPlainApp.class) +@AutoConfigureMockMvc +@TestPropertySource(properties = { "security.basic.enabled=false", "server.tomcat.additional-tld-skip-patterns=tomee-*.jar,tomcat-*.jar,openejb-*.jar,cxf-*.jar,activemq-*.jar" }) +public class SpringBootWithoutServletComponentIntegrationTest { + + @Autowired private ServletContext servletContext; + + @Autowired private TestRestTemplate restTemplate; + + @Test + public void givenServletContext_whenAccessAttrs_thenNotFound() { + assertNull(servletContext.getAttribute("servlet-context-attr")); + } + + @Test + public void givenServletFilter_whenGetHello_thenEndpointNotFound() { + ResponseEntity responseEntity = this.restTemplate.getForEntity("/hello", String.class); + assertEquals(HttpStatus.NOT_FOUND, responseEntity.getStatusCode()); + } + + @Test + public void givenServletContext_whenCheckFilterMappings_thenEmpty() { + assertNotNull(servletContext); + FilterRegistration filterRegistration = servletContext.getFilterRegistration("hello filter"); + + assertNull(filterRegistration); + } + +} + + diff --git a/spring-boot/src/test/java/com/baeldung/intro/AppLiveTest.java b/spring-boot/src/test/java/com/baeldung/intro/AppLiveTest.java index 772709dc30..af46fe0423 100644 --- a/spring-boot/src/test/java/com/baeldung/intro/AppLiveTest.java +++ b/spring-boot/src/test/java/com/baeldung/intro/AppLiveTest.java @@ -6,6 +6,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.http.MediaType; +import org.springframework.test.context.TestPropertySource; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; @@ -17,6 +18,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. @RunWith(SpringRunner.class) @SpringBootTest @AutoConfigureMockMvc +@TestPropertySource(properties = { "security.basic.enabled=false" }) public class AppLiveTest { @Autowired diff --git a/spring-boot/src/test/java/com/baeldung/WebjarsdemoApplicationIntegrationTest.java b/spring-boot/src/test/java/com/baeldung/webjar/WebjarsdemoApplicationIntegrationTest.java similarity index 68% rename from spring-boot/src/test/java/com/baeldung/WebjarsdemoApplicationIntegrationTest.java rename to spring-boot/src/test/java/com/baeldung/webjar/WebjarsdemoApplicationIntegrationTest.java index 3558682b97..d6e71dcf6b 100644 --- a/spring-boot/src/test/java/com/baeldung/WebjarsdemoApplicationIntegrationTest.java +++ b/spring-boot/src/test/java/com/baeldung/webjar/WebjarsdemoApplicationIntegrationTest.java @@ -1,13 +1,13 @@ -package com.baeldung; +package com.baeldung.webjar; import org.junit.Test; import org.junit.runner.RunWith; -import org.springframework.boot.test.SpringApplicationConfiguration; +import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.web.WebAppConfiguration; @RunWith(SpringJUnit4ClassRunner.class) -@SpringApplicationConfiguration(classes = WebjarsdemoApplication.class) +@SpringBootTest(classes = WebjarsdemoApplication.class) @WebAppConfiguration public class WebjarsdemoApplicationIntegrationTest { diff --git a/spring-boot/src/test/java/org/baeldung/SpringBootApplicationIntegrationTest.java b/spring-boot/src/test/java/org/baeldung/SpringBootApplicationIntegrationTest.java index 8cdcdb2216..87c59a4662 100644 --- a/spring-boot/src/test/java/org/baeldung/SpringBootApplicationIntegrationTest.java +++ b/spring-boot/src/test/java/org/baeldung/SpringBootApplicationIntegrationTest.java @@ -9,7 +9,7 @@ import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.SpringApplicationConfiguration; +import org.springframework.boot.test.context.SpringBootTest; import org.springframework.http.MediaType; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.web.WebAppConfiguration; @@ -22,7 +22,7 @@ import org.springframework.web.context.WebApplicationContext; import java.nio.charset.Charset; @RunWith(SpringJUnit4ClassRunner.class) -@SpringApplicationConfiguration(classes = Application.class) +@SpringBootTest(classes = Application.class) @WebAppConfiguration public class SpringBootApplicationIntegrationTest { @Autowired @@ -45,29 +45,22 @@ public class SpringBootApplicationIntegrationTest { public void givenRequestHasBeenMade_whenMeetsFindByDateOfGivenConditions_thenCorrect() throws Exception { MediaType contentType = new MediaType(MediaType.APPLICATION_JSON.getType(), MediaType.APPLICATION_JSON.getSubtype(), Charset.forName("utf8")); - mockMvc.perform(MockMvcRequestBuilders.get("/entity/findbydate/{date}", "2011-12-03T10:15:30")) - .andExpect(MockMvcResultMatchers.status().isOk()) - .andExpect(MockMvcResultMatchers.content().contentType(contentType)) - .andExpect(jsonPath("$.id", equalTo(1))); + mockMvc.perform(MockMvcRequestBuilders.get("/entity/findbydate/{date}", "2011-12-03T10:15:30")).andExpect(MockMvcResultMatchers.status().isOk()).andExpect(MockMvcResultMatchers.content().contentType(contentType)) + .andExpect(jsonPath("$.id", equalTo(1))); } @Test public void givenRequestHasBeenMade_whenMeetsFindByModeOfGivenConditions_thenCorrect() throws Exception { MediaType contentType = new MediaType(MediaType.APPLICATION_JSON.getType(), MediaType.APPLICATION_JSON.getSubtype(), Charset.forName("utf8")); - mockMvc.perform(MockMvcRequestBuilders.get("/entity/findbymode/{mode}", Modes.ALPHA.name())) - .andExpect(MockMvcResultMatchers.status().isOk()) - .andExpect(MockMvcResultMatchers.content().contentType(contentType)) - .andExpect(jsonPath("$.id", equalTo(1))); + mockMvc.perform(MockMvcRequestBuilders.get("/entity/findbymode/{mode}", Modes.ALPHA.name())).andExpect(MockMvcResultMatchers.status().isOk()).andExpect(MockMvcResultMatchers.content().contentType(contentType)).andExpect(jsonPath("$.id", equalTo(1))); } @Test public void givenRequestHasBeenMade_whenMeetsFindByVersionOfGivenConditions_thenCorrect() throws Exception { MediaType contentType = new MediaType(MediaType.APPLICATION_JSON.getType(), MediaType.APPLICATION_JSON.getSubtype(), Charset.forName("utf8")); - mockMvc.perform(MockMvcRequestBuilders.get("/entity/findbyversion").header("Version", "1.0.0")) - .andExpect(MockMvcResultMatchers.status().isOk()) - .andExpect(MockMvcResultMatchers.content().contentType(contentType)) - .andExpect(jsonPath("$.id", equalTo(1))); + mockMvc.perform(MockMvcRequestBuilders.get("/entity/findbyversion").header("Version", "1.0.0")).andExpect(MockMvcResultMatchers.status().isOk()).andExpect(MockMvcResultMatchers.content().contentType(contentType)) + .andExpect(jsonPath("$.id", equalTo(1))); } } diff --git a/spring-boot/src/test/java/org/baeldung/SpringBootJPAIntegrationTest.java b/spring-boot/src/test/java/org/baeldung/SpringBootJPAIntegrationTest.java index 233684bc24..d4b19e6a1d 100644 --- a/spring-boot/src/test/java/org/baeldung/SpringBootJPAIntegrationTest.java +++ b/spring-boot/src/test/java/org/baeldung/SpringBootJPAIntegrationTest.java @@ -5,14 +5,14 @@ import org.baeldung.repository.GenericEntityRepository; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.SpringApplicationConfiguration; +import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; @RunWith(SpringJUnit4ClassRunner.class) -@SpringApplicationConfiguration(classes = Application.class) +@SpringBootTest(classes = Application.class) public class SpringBootJPAIntegrationTest { @Autowired private GenericEntityRepository genericEntityRepository; diff --git a/spring-boot/src/test/java/org/baeldung/SpringBootMailIntegrationTest.java b/spring-boot/src/test/java/org/baeldung/SpringBootMailIntegrationTest.java index cec25f20f9..10e3d6d60b 100644 --- a/spring-boot/src/test/java/org/baeldung/SpringBootMailIntegrationTest.java +++ b/spring-boot/src/test/java/org/baeldung/SpringBootMailIntegrationTest.java @@ -5,7 +5,7 @@ import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.SpringApplicationConfiguration; +import org.springframework.boot.test.context.SpringBootTest; import org.springframework.mail.SimpleMailMessage; import org.springframework.mail.javamail.JavaMailSender; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @@ -23,7 +23,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertThat; @RunWith(SpringJUnit4ClassRunner.class) -@SpringApplicationConfiguration(classes = Application.class) +@SpringBootTest(classes = Application.class) public class SpringBootMailIntegrationTest { @Autowired private JavaMailSender javaMailSender; diff --git a/spring-cloud-data-flow/batch-job/pom.xml b/spring-cloud-data-flow/batch-job/pom.xml index 3d05732027..5e519e9a35 100644 --- a/spring-cloud-data-flow/batch-job/pom.xml +++ b/spring-cloud-data-flow/batch-job/pom.xml @@ -14,7 +14,7 @@ org.springframework.boot spring-boot-starter-parent - 1.4.2.RELEASE + 1.4.4.RELEASE diff --git a/spring-cloud-data-flow/data-flow-server/pom.xml b/spring-cloud-data-flow/data-flow-server/pom.xml index b1f920c94e..1ed2d4fb74 100644 --- a/spring-cloud-data-flow/data-flow-server/pom.xml +++ b/spring-cloud-data-flow/data-flow-server/pom.xml @@ -14,7 +14,7 @@ org.springframework.boot spring-boot-starter-parent - 1.4.2.RELEASE + 1.4.4.RELEASE diff --git a/spring-cloud-data-flow/data-flow-shell/pom.xml b/spring-cloud-data-flow/data-flow-shell/pom.xml index 55fa995052..d3bd297152 100644 --- a/spring-cloud-data-flow/data-flow-shell/pom.xml +++ b/spring-cloud-data-flow/data-flow-shell/pom.xml @@ -14,7 +14,7 @@ org.springframework.boot spring-boot-starter-parent - 1.4.2.RELEASE + 1.4.4.RELEASE diff --git a/spring-cloud-data-flow/log-sink/pom.xml b/spring-cloud-data-flow/log-sink/pom.xml index 6443ed88c8..dc21132b9f 100644 --- a/spring-cloud-data-flow/log-sink/pom.xml +++ b/spring-cloud-data-flow/log-sink/pom.xml @@ -14,7 +14,7 @@ org.springframework.boot spring-boot-starter-parent - 1.4.2.RELEASE + 1.4.4.RELEASE diff --git a/spring-cloud-data-flow/time-processor/pom.xml b/spring-cloud-data-flow/time-processor/pom.xml index d7553b110f..51a66014e0 100644 --- a/spring-cloud-data-flow/time-processor/pom.xml +++ b/spring-cloud-data-flow/time-processor/pom.xml @@ -14,7 +14,7 @@ org.springframework.boot spring-boot-starter-parent - 1.4.2.RELEASE + 1.4.4.RELEASE diff --git a/spring-cloud-data-flow/time-source/pom.xml b/spring-cloud-data-flow/time-source/pom.xml index 2523dfabea..57933c2833 100644 --- a/spring-cloud-data-flow/time-source/pom.xml +++ b/spring-cloud-data-flow/time-source/pom.xml @@ -14,7 +14,7 @@ org.springframework.boot spring-boot-starter-parent - 1.4.2.RELEASE + 1.4.4.RELEASE diff --git a/spring-cloud/spring-cloud-bootstrap/application-config/resource.properties b/spring-cloud/spring-cloud-bootstrap/application-config/book-service.properties similarity index 91% rename from spring-cloud/spring-cloud-bootstrap/application-config/resource.properties rename to spring-cloud/spring-cloud-bootstrap/application-config/book-service.properties index 9fb610d655..e1244a0cf0 100644 --- a/spring-cloud/spring-cloud-bootstrap/application-config/resource.properties +++ b/spring-cloud/spring-cloud-bootstrap/application-config/book-service.properties @@ -1,4 +1,4 @@ -spring.application.name=resource +spring.application.name=book-service server.port=8083 resource.returnString=hello cloud diff --git a/spring-cloud/spring-cloud-bootstrap/application-config/gateway.properties b/spring-cloud/spring-cloud-bootstrap/application-config/gateway.properties index 6f52a59db9..09f7f3bf4a 100644 --- a/spring-cloud/spring-cloud-bootstrap/application-config/gateway.properties +++ b/spring-cloud/spring-cloud-bootstrap/application-config/gateway.properties @@ -6,9 +6,13 @@ eureka.client.registryFetchIntervalSeconds = 5 management.security.sessions=always -zuul.routes.resource.path=/resource/** -zuul.routes.resource.sensitive-headers=Set-Cookie,Authorization -hystrix.command.resource.execution.isolation.thread.timeoutInMilliseconds=600000 +zuul.routes.book-service.path=/book-service/** +zuul.routes.book-service.sensitive-headers=Set-Cookie,Authorization +hystrix.command.book-service.execution.isolation.thread.timeoutInMilliseconds=600000 + +zuul.routes.rating-service.path=/rating-service/** +zuul.routes.rating-service.sensitive-headers=Set-Cookie,Authorization +hystrix.command.rating-service.execution.isolation.thread.timeoutInMilliseconds=600000 zuul.routes.discovery.path=/discovery/** zuul.routes.discovery.sensitive-headers=Set-Cookie,Authorization diff --git a/spring-cloud/spring-cloud-bootstrap/application-config/rating-service.properties b/spring-cloud/spring-cloud-bootstrap/application-config/rating-service.properties new file mode 100644 index 0000000000..4817d12c83 --- /dev/null +++ b/spring-cloud/spring-cloud-bootstrap/application-config/rating-service.properties @@ -0,0 +1,17 @@ +spring.application.name=rating-service +server.port=8084 + +resource.returnString=hello cloud +resource.user.returnString=hello cloud user +resource.admin.returnString=hello cloud admin + +eureka.client.region = default +eureka.client.registryFetchIntervalSeconds = 5 + +management.security.sessions=never + +logging.level.org.springframework.web.=debug +logging.level.org.springframework.security=debug + +spring.redis.host=localhost +spring.redis.port=6379 diff --git a/spring-cloud/spring-cloud-bootstrap/config/pom.xml b/spring-cloud/spring-cloud-bootstrap/config/pom.xml index 24d054a87b..2c8ad65102 100644 --- a/spring-cloud/spring-cloud-bootstrap/config/pom.xml +++ b/spring-cloud/spring-cloud-bootstrap/config/pom.xml @@ -10,7 +10,7 @@ org.springframework.boot spring-boot-starter-parent - 1.4.2.RELEASE + 1.4.4.RELEASE diff --git a/spring-cloud/spring-cloud-bootstrap/discovery/pom.xml b/spring-cloud/spring-cloud-bootstrap/discovery/pom.xml index be3bfbb0be..40a186350c 100644 --- a/spring-cloud/spring-cloud-bootstrap/discovery/pom.xml +++ b/spring-cloud/spring-cloud-bootstrap/discovery/pom.xml @@ -10,7 +10,7 @@ org.springframework.boot spring-boot-starter-parent - 1.4.2.RELEASE + 1.4.4.RELEASE diff --git a/spring-cloud/spring-cloud-bootstrap/gateway/pom.xml b/spring-cloud/spring-cloud-bootstrap/gateway/pom.xml index 9186f12226..044730ba22 100644 --- a/spring-cloud/spring-cloud-bootstrap/gateway/pom.xml +++ b/spring-cloud/spring-cloud-bootstrap/gateway/pom.xml @@ -10,7 +10,7 @@ org.springframework.boot spring-boot-starter-parent - 1.4.2.RELEASE + 1.4.4.RELEASE 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 60dccf9042..9e5c424403 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 @@ -22,14 +22,14 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() - .antMatchers("/resource/hello/cloud").permitAll() + .antMatchers("/book-service/books").permitAll() .antMatchers("/eureka/**").hasRole("ADMIN") .anyRequest().authenticated() .and() .formLogin() .and() .logout().permitAll() - .logoutSuccessUrl("/resource/hello/cloud").permitAll() + .logoutSuccessUrl("/book-service/books").permitAll() .and() .csrf() .disable(); diff --git a/spring-cloud/spring-cloud-bootstrap/gateway/src/test/java/com/baeldung/spring/cloud/bootstrap/gateway/GatewayApplicationLiveTest.java b/spring-cloud/spring-cloud-bootstrap/gateway/src/test/java/com/baeldung/spring/cloud/bootstrap/gateway/GatewayApplicationLiveTest.java index cea431d461..aa39232bb2 100644 --- a/spring-cloud/spring-cloud-bootstrap/gateway/src/test/java/com/baeldung/spring/cloud/bootstrap/gateway/GatewayApplicationLiveTest.java +++ b/spring-cloud/spring-cloud-bootstrap/gateway/src/test/java/com/baeldung/spring/cloud/bootstrap/gateway/GatewayApplicationLiveTest.java @@ -14,12 +14,12 @@ public class GatewayApplicationLiveTest { TestRestTemplate testRestTemplate = new TestRestTemplate(); String testUrl = "http://localhost:8080"; - ResponseEntity response = testRestTemplate.getForEntity(testUrl + "/resource/hello/cloud", String.class); + ResponseEntity response = testRestTemplate.getForEntity(testUrl + "/book-service/books", String.class); Assert.assertEquals(HttpStatus.OK, response.getStatusCode()); - Assert.assertEquals("hello cloud", response.getBody()); + Assert.assertNotNull(response.getBody()); //try the protected resource and confirm the redirect to login - response = testRestTemplate.getForEntity(testUrl + "/resource/hello/user", String.class); + response = testRestTemplate.getForEntity(testUrl + "/book-service/books/1", String.class); Assert.assertEquals(HttpStatus.FOUND, response.getStatusCode()); Assert.assertEquals("http://localhost:8080/login", response.getHeaders().get("Location").get(0)); @@ -36,12 +36,12 @@ public class GatewayApplicationLiveTest { HttpEntity httpEntity = new HttpEntity<>(headers); //request the protected resource - response = testRestTemplate.exchange(testUrl + "/resource/hello/user", HttpMethod.GET, httpEntity, String.class); + response = testRestTemplate.exchange(testUrl + "/book-service/books/1", HttpMethod.GET, httpEntity, String.class); Assert.assertEquals(HttpStatus.OK, response.getStatusCode()); - Assert.assertEquals("hello cloud user", response.getBody()); + Assert.assertNotNull(response.getBody()); //request the admin protected resource to determine it is still protected - response = testRestTemplate.exchange(testUrl + "/resource/hello/admin", HttpMethod.GET, httpEntity, String.class); + response = testRestTemplate.exchange(testUrl + "/rating-service/ratings/all", HttpMethod.GET, httpEntity, String.class); Assert.assertEquals(HttpStatus.FORBIDDEN, response.getStatusCode()); //login as the admin @@ -57,9 +57,9 @@ public class GatewayApplicationLiveTest { httpEntity = new HttpEntity<>(headers); //request the protected resource - response = testRestTemplate.exchange(testUrl + "/resource/hello/admin", HttpMethod.GET, httpEntity, String.class); + response = testRestTemplate.exchange(testUrl + "/rating-service/ratings/all", HttpMethod.GET, httpEntity, String.class); Assert.assertEquals(HttpStatus.OK, response.getStatusCode()); - Assert.assertEquals("hello cloud admin", response.getBody()); + Assert.assertNotNull(response.getBody()); //request the discovery resources as the admin response = testRestTemplate.exchange(testUrl + "/discovery", HttpMethod.GET, httpEntity, String.class); diff --git a/spring-cloud/spring-cloud-bootstrap/pom.xml b/spring-cloud/spring-cloud-bootstrap/pom.xml index 9a1b2e6d0e..ccfbdb9735 100644 --- a/spring-cloud/spring-cloud-bootstrap/pom.xml +++ b/spring-cloud/spring-cloud-bootstrap/pom.xml @@ -14,7 +14,8 @@ config discovery gateway - resource + svc-book + svc-rating diff --git a/spring-cloud/spring-cloud-bootstrap/resource/src/main/java/com/baeldung/spring/cloud/bootstrap/resource/ResourceApplication.java b/spring-cloud/spring-cloud-bootstrap/resource/src/main/java/com/baeldung/spring/cloud/bootstrap/resource/ResourceApplication.java deleted file mode 100644 index accef18a14..0000000000 --- a/spring-cloud/spring-cloud-bootstrap/resource/src/main/java/com/baeldung/spring/cloud/bootstrap/resource/ResourceApplication.java +++ /dev/null @@ -1,41 +0,0 @@ -package com.baeldung.spring.cloud.bootstrap.resource; - -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.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; - -@SpringBootApplication -@EnableEurekaClient -@RestController -public class ResourceApplication { - public static void main(String[] args) { - SpringApplication.run(ResourceApplication.class, args); - } - - @Value("${resource.returnString}") - private String returnString; - - @Value("${resource.user.returnString}") - private String userReturnString; - - @Value("${resource.admin.returnString}") - private String adminReturnString; - - @RequestMapping("/hello/cloud") - public String getString() { - return returnString; - } - - @RequestMapping("/hello/user") - public String getUserString() { - return userReturnString; - } - - @RequestMapping("/hello/admin") - public String getAdminString() { - return adminReturnString; - } -} diff --git a/spring-cloud/spring-cloud-bootstrap/resource/pom.xml b/spring-cloud/spring-cloud-bootstrap/svc-book/pom.xml similarity index 95% rename from spring-cloud/spring-cloud-bootstrap/resource/pom.xml rename to spring-cloud/spring-cloud-bootstrap/svc-book/pom.xml index 1472693de8..9a99054ed5 100644 --- a/spring-cloud/spring-cloud-bootstrap/resource/pom.xml +++ b/spring-cloud/spring-cloud-bootstrap/svc-book/pom.xml @@ -4,13 +4,14 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 - resource + com.baeldung.spring.cloud + svc-book 1.0.0-SNAPSHOT org.springframework.boot spring-boot-starter-parent - 1.4.2.RELEASE + 1.4.4.RELEASE diff --git a/spring-cloud/spring-cloud-bootstrap/svc-book/src/main/java/com/baeldung/spring/cloud/bootstrap/svcbook/Book.java b/spring-cloud/spring-cloud-bootstrap/svc-book/src/main/java/com/baeldung/spring/cloud/bootstrap/svcbook/Book.java new file mode 100644 index 0000000000..e652437454 --- /dev/null +++ b/spring-cloud/spring-cloud-bootstrap/svc-book/src/main/java/com/baeldung/spring/cloud/bootstrap/svcbook/Book.java @@ -0,0 +1,40 @@ +package com.baeldung.spring.cloud.bootstrap.svcbook; + +public class Book { + private Long id; + private String author; + private String title; + + public Book(Long id, String title, String author) { + this.id = id; + this.author = author; + this.title = title; + } + + public Book() { + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getAuthor() { + return author; + } + + public void setAuthor(String author) { + this.author = author; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } +} 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 new file mode 100644 index 0000000000..25ad2a83b2 --- /dev/null +++ b/spring-cloud/spring-cloud-bootstrap/svc-book/src/main/java/com/baeldung/spring/cloud/bootstrap/svcbook/BookServiceApplication.java @@ -0,0 +1,37 @@ +package com.baeldung.spring.cloud.bootstrap.svcbook; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.netflix.eureka.EnableEurekaClient; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.util.Arrays; +import java.util.List; + +@SpringBootApplication +@EnableEurekaClient +@RestController +@RequestMapping("/books") +public class BookServiceApplication { + public static void main(String[] args) { + SpringApplication.run(BookServiceApplication.class, args); + } + + private List bookList = Arrays.asList( + new Book(1L, "Baeldung goes to the market", "Tim Schimandle"), + new Book(2L, "Baeldung goes to the park", "Slavisa") + ); + + @GetMapping("") + public List findAllBooks() { + return bookList; + } + + @GetMapping("/{bookId}") + public Book findBook(@PathVariable Long bookId) { + return bookList.stream().filter(b -> b.getId().equals(bookId)).findFirst().orElse(null); + } +} diff --git a/spring-cloud/spring-cloud-bootstrap/resource/src/main/java/com/baeldung/spring/cloud/bootstrap/resource/SecurityConfig.java b/spring-cloud/spring-cloud-bootstrap/svc-book/src/main/java/com/baeldung/spring/cloud/bootstrap/svcbook/SecurityConfig.java similarity index 83% rename from spring-cloud/spring-cloud-bootstrap/resource/src/main/java/com/baeldung/spring/cloud/bootstrap/resource/SecurityConfig.java rename to spring-cloud/spring-cloud-bootstrap/svc-book/src/main/java/com/baeldung/spring/cloud/bootstrap/svcbook/SecurityConfig.java index 0b0de6ec20..300b4d7c5a 100644 --- a/spring-cloud/spring-cloud-bootstrap/resource/src/main/java/com/baeldung/spring/cloud/bootstrap/resource/SecurityConfig.java +++ b/spring-cloud/spring-cloud-bootstrap/svc-book/src/main/java/com/baeldung/spring/cloud/bootstrap/svcbook/SecurityConfig.java @@ -1,4 +1,4 @@ -package com.baeldung.spring.cloud.bootstrap.resource; +package com.baeldung.spring.cloud.bootstrap.svcbook; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; @@ -22,9 +22,8 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter { http.httpBasic() .disable() .authorizeRequests() - .antMatchers("/hello/cloud").permitAll() - .antMatchers("/hello/user").hasAnyRole("USER", "ADMIN") - .antMatchers("/hello/admin").hasRole("ADMIN") + .antMatchers("/books").permitAll() + .antMatchers("/books/*").hasAnyRole("USER", "ADMIN") .anyRequest().authenticated() .and() .csrf() diff --git a/spring-cloud/spring-cloud-bootstrap/resource/src/main/java/com/baeldung/spring/cloud/bootstrap/resource/SessionConfig.java b/spring-cloud/spring-cloud-bootstrap/svc-book/src/main/java/com/baeldung/spring/cloud/bootstrap/svcbook/SessionConfig.java similarity index 87% rename from spring-cloud/spring-cloud-bootstrap/resource/src/main/java/com/baeldung/spring/cloud/bootstrap/resource/SessionConfig.java rename to spring-cloud/spring-cloud-bootstrap/svc-book/src/main/java/com/baeldung/spring/cloud/bootstrap/svcbook/SessionConfig.java index f11f316826..dbde068069 100644 --- a/spring-cloud/spring-cloud-bootstrap/resource/src/main/java/com/baeldung/spring/cloud/bootstrap/resource/SessionConfig.java +++ b/spring-cloud/spring-cloud-bootstrap/svc-book/src/main/java/com/baeldung/spring/cloud/bootstrap/svcbook/SessionConfig.java @@ -1,4 +1,4 @@ -package com.baeldung.spring.cloud.bootstrap.resource; +package com.baeldung.spring.cloud.bootstrap.svcbook; import org.springframework.context.annotation.Configuration; import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession; diff --git a/spring-cloud/spring-cloud-bootstrap/resource/src/main/resources/bootstrap.properties b/spring-cloud/spring-cloud-bootstrap/svc-book/src/main/resources/bootstrap.properties similarity index 87% rename from spring-cloud/spring-cloud-bootstrap/resource/src/main/resources/bootstrap.properties rename to spring-cloud/spring-cloud-bootstrap/svc-book/src/main/resources/bootstrap.properties index 9ecfb46125..8f3a3261ac 100644 --- a/spring-cloud/spring-cloud-bootstrap/resource/src/main/resources/bootstrap.properties +++ b/spring-cloud/spring-cloud-bootstrap/svc-book/src/main/resources/bootstrap.properties @@ -1,4 +1,4 @@ -spring.cloud.config.name=resource +spring.cloud.config.name=book-service spring.cloud.config.discovery.service-id=config spring.cloud.config.discovery.enabled=true spring.cloud.config.username=configUser diff --git a/spring-cloud/spring-cloud-bootstrap/svc-rating/pom.xml b/spring-cloud/spring-cloud-bootstrap/svc-rating/pom.xml new file mode 100644 index 0000000000..35da8beba8 --- /dev/null +++ b/spring-cloud/spring-cloud-bootstrap/svc-rating/pom.xml @@ -0,0 +1,85 @@ + + + 4.0.0 + + com.baeldung.spring.cloud + svc-rating + 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 + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-security + + + + org.springframework.session + spring-session + + + org.springframework.boot + spring-boot-starter-data-redis + + + + 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 + + + + + + Brixton.SR7 + 3.6.0 + + \ No newline at end of file diff --git a/spring-cloud/spring-cloud-bootstrap/svc-rating/src/main/java/com/baeldung/spring/cloud/bootstrap/svcrating/Rating.java b/spring-cloud/spring-cloud-bootstrap/svc-rating/src/main/java/com/baeldung/spring/cloud/bootstrap/svcrating/Rating.java new file mode 100644 index 0000000000..5dd3572098 --- /dev/null +++ b/spring-cloud/spring-cloud-bootstrap/svc-rating/src/main/java/com/baeldung/spring/cloud/bootstrap/svcrating/Rating.java @@ -0,0 +1,40 @@ +package com.baeldung.spring.cloud.bootstrap.svcrating; + +public class Rating { + private Long id; + private Long bookId; + private int stars; + + public Rating() { + } + + public Rating(Long id, Long bookId, int stars) { + this.id = id; + this.bookId = bookId; + this.stars = stars; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public Long getBookId() { + return bookId; + } + + public void setBookId(Long bookId) { + this.bookId = bookId; + } + + public int getStars() { + return stars; + } + + public void setStars(int stars) { + this.stars = stars; + } +} 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 new file mode 100644 index 0000000000..11fb5f06b6 --- /dev/null +++ b/spring-cloud/spring-cloud-bootstrap/svc-rating/src/main/java/com/baeldung/spring/cloud/bootstrap/svcrating/RatingServiceApplication.java @@ -0,0 +1,41 @@ +package com.baeldung.spring.cloud.bootstrap.svcrating; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.netflix.eureka.EnableEurekaClient; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.stream.Collectors; + +@SpringBootApplication +@EnableEurekaClient +@RestController +@RequestMapping("/ratings") +public class RatingServiceApplication { + public static void main(String[] args) { + SpringApplication.run(RatingServiceApplication.class, args); + } + + private List ratingList = Arrays.asList( + new Rating(1L, 1L, 2), + new Rating(2L, 1L, 3), + new Rating(3L, 2L, 4), + new Rating(4L, 2L, 5) + ); + + @GetMapping("") + public List findRatingsByBookId(@RequestParam Long bookId) { + return bookId == null || bookId.equals(0L) ? Collections.EMPTY_LIST : ratingList.stream().filter(r -> r.getBookId().equals(bookId)).collect(Collectors.toList()); + } + + @GetMapping("/all") + public List findAllRatings() { + return ratingList; + } +} \ No newline at end of file diff --git a/spring-cloud/spring-cloud-bootstrap/svc-rating/src/main/java/com/baeldung/spring/cloud/bootstrap/svcrating/SecurityConfig.java b/spring-cloud/spring-cloud-bootstrap/svc-rating/src/main/java/com/baeldung/spring/cloud/bootstrap/svcrating/SecurityConfig.java new file mode 100644 index 0000000000..371dc810d5 --- /dev/null +++ b/spring-cloud/spring-cloud-bootstrap/svc-rating/src/main/java/com/baeldung/spring/cloud/bootstrap/svcrating/SecurityConfig.java @@ -0,0 +1,32 @@ +package com.baeldung.spring.cloud.bootstrap.svcrating; + +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.EnableWebSecurity; +import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; + +@EnableWebSecurity +@Configuration +public class SecurityConfig extends WebSecurityConfigurerAdapter { + + @Autowired + public void configureGlobal1(AuthenticationManagerBuilder auth) throws Exception { + //try in memory auth with no users to support the case that this will allow for users that are logged in to go anywhere + auth.inMemoryAuthentication(); + } + + @Override + protected void configure(HttpSecurity http) throws Exception { + http.httpBasic() + .disable() + .authorizeRequests() + .antMatchers("/ratings").hasRole("USER") + .antMatchers("/ratings/all").hasRole("ADMIN") + .anyRequest().authenticated() + .and() + .csrf() + .disable(); + } +} diff --git a/spring-cloud/spring-cloud-bootstrap/svc-rating/src/main/java/com/baeldung/spring/cloud/bootstrap/svcrating/SessionConfig.java b/spring-cloud/spring-cloud-bootstrap/svc-rating/src/main/java/com/baeldung/spring/cloud/bootstrap/svcrating/SessionConfig.java new file mode 100644 index 0000000000..62bc701868 --- /dev/null +++ b/spring-cloud/spring-cloud-bootstrap/svc-rating/src/main/java/com/baeldung/spring/cloud/bootstrap/svcrating/SessionConfig.java @@ -0,0 +1,10 @@ +package com.baeldung.spring.cloud.bootstrap.svcrating; + +import org.springframework.context.annotation.Configuration; +import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession; +import org.springframework.session.web.context.AbstractHttpSessionApplicationInitializer; + +@Configuration +@EnableRedisHttpSession +public class SessionConfig extends AbstractHttpSessionApplicationInitializer { +} diff --git a/spring-cloud/spring-cloud-bootstrap/svc-rating/src/main/resources/bootstrap.properties b/spring-cloud/spring-cloud-bootstrap/svc-rating/src/main/resources/bootstrap.properties new file mode 100644 index 0000000000..be5cf7f1e1 --- /dev/null +++ b/spring-cloud/spring-cloud-bootstrap/svc-rating/src/main/resources/bootstrap.properties @@ -0,0 +1,7 @@ +spring.cloud.config.name=rating-service +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/ diff --git a/spring-cloud/spring-cloud-config/pom.xml b/spring-cloud/spring-cloud-config/pom.xml index d25037a49f..56ff377956 100644 --- a/spring-cloud/spring-cloud-config/pom.xml +++ b/spring-cloud/spring-cloud-config/pom.xml @@ -16,7 +16,7 @@ org.springframework.boot spring-boot-starter-parent - 1.4.2.RELEASE + 1.4.4.RELEASE diff --git a/spring-cloud/spring-cloud-ribbon-client/pom.xml b/spring-cloud/spring-cloud-ribbon-client/pom.xml index 8aee41f5c4..d70de51a58 100644 --- a/spring-cloud/spring-cloud-ribbon-client/pom.xml +++ b/spring-cloud/spring-cloud-ribbon-client/pom.xml @@ -11,7 +11,7 @@ org.springframework.boot spring-boot-starter-parent - 1.4.2.RELEASE + 1.4.4.RELEASE diff --git a/spring-core/README.md b/spring-core/README.md index f6aaaf44a0..a32d30939f 100644 --- a/spring-core/README.md +++ b/spring-core/README.md @@ -3,4 +3,4 @@ - [Exploring the Spring BeanFactory API](http://www.baeldung.com/spring-beanfactory) - [How to use the Spring FactoryBean?](http://www.baeldung.com/spring-factorybean) - [Constructor Dependency Injection in Spring](http://www.baeldung.com/constructor-injection-in-spring) -- [Constructor Injection in Spring with Lombok](http://inprogress.baeldung.com/constructor-injection-in-spring-with-lombok) +- [Constructor Injection in Spring with Lombok](http://www.baeldung.com/spring-injection-lombok) diff --git a/spring-cucumber/pom.xml b/spring-cucumber/pom.xml index 644ddff1d8..55c0af670a 100644 --- a/spring-cucumber/pom.xml +++ b/spring-cucumber/pom.xml @@ -14,7 +14,7 @@ org.springframework.boot spring-boot-starter-parent - 1.4.2.RELEASE + 1.4.4.RELEASE diff --git a/spring-data-dynamodb/pom.xml b/spring-data-dynamodb/pom.xml index 0ce9b3a399..8b06c0b36f 100644 --- a/spring-data-dynamodb/pom.xml +++ b/spring-data-dynamodb/pom.xml @@ -11,7 +11,7 @@ spring-boot-starter-parent org.springframework.boot - 1.4.2.RELEASE + 1.4.4.RELEASE diff --git a/spring-data-dynamodb/src/test/java/com/baeldung/spring/data/dynamodb/repository/ProductInfoRepositoryIntegrationTest.java b/spring-data-dynamodb/src/test/java/com/baeldung/spring/data/dynamodb/repository/ProductInfoRepositoryIntegrationTest.java index 8fe3e96940..05badc74b4 100644 --- a/spring-data-dynamodb/src/test/java/com/baeldung/spring/data/dynamodb/repository/ProductInfoRepositoryIntegrationTest.java +++ b/spring-data-dynamodb/src/test/java/com/baeldung/spring/data/dynamodb/repository/ProductInfoRepositoryIntegrationTest.java @@ -44,8 +44,8 @@ public class ProductInfoRepositoryIntegrationTest { private static final String EXPECTED_PRICE = "50"; @Before - @Ignore //TODO Remove Ignore annotations when running locally with Local DynamoDB instance - public void setup() throws Exception { + @Ignore // TODO Remove Ignore annotations when running locally with Local DynamoDB instance + public void setup() throws Exception { try { dynamoDBMapper = new DynamoDBMapper(amazonDynamoDB); @@ -64,7 +64,7 @@ public class ProductInfoRepositoryIntegrationTest { } @Test - @Ignore //TODO Remove Ignore annotations when running locally with Local DynamoDB instance + @Ignore // TODO Remove Ignore annotations when running locally with Local DynamoDB instance public void givenItemWithExpectedCost_whenRunFindAll_thenItemIsFound() { ProductInfo productInfo = new ProductInfo(EXPECTED_COST, EXPECTED_PRICE); diff --git a/spring-data-mongodb/src/main/java/org/baeldung/config/SimpleMongoConfig.java b/spring-data-mongodb/src/main/java/org/baeldung/config/SimpleMongoConfig.java new file mode 100644 index 0000000000..6140382f82 --- /dev/null +++ b/spring-data-mongodb/src/main/java/org/baeldung/config/SimpleMongoConfig.java @@ -0,0 +1,25 @@ +package org.baeldung.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.mongodb.core.MongoTemplate; +import org.springframework.data.mongodb.repository.config.EnableMongoRepositories; + +import com.mongodb.Mongo; +import com.mongodb.MongoClient; + +@Configuration +@EnableMongoRepositories(basePackages = "org.baeldung.repository") +public class SimpleMongoConfig { + + @Bean + public Mongo mongo() throws Exception { + return new MongoClient("localhost"); + } + + @Bean + public MongoTemplate mongoTemplate() throws Exception { + return new MongoTemplate(mongo(), "test"); + } + +} diff --git a/spring-data-mongodb/src/test/java/org/baeldung/mongotemplate/MongoTemplateProjectionLiveTest.java b/spring-data-mongodb/src/test/java/org/baeldung/mongotemplate/MongoTemplateProjectionLiveTest.java index 04e474f71b..61115faede 100644 --- a/spring-data-mongodb/src/test/java/org/baeldung/mongotemplate/MongoTemplateProjectionLiveTest.java +++ b/spring-data-mongodb/src/test/java/org/baeldung/mongotemplate/MongoTemplateProjectionLiveTest.java @@ -1,8 +1,10 @@ package org.baeldung.mongotemplate; -import static org.junit.Assert.*; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; -import org.baeldung.config.MongoConfig; +import org.baeldung.config.SimpleMongoConfig; import org.baeldung.model.User; import org.junit.After; import org.junit.Before; @@ -15,7 +17,7 @@ import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration(classes = MongoConfig.class) +@ContextConfiguration(classes = SimpleMongoConfig.class) public class MongoTemplateProjectionLiveTest { @Autowired @@ -38,7 +40,7 @@ public class MongoTemplateProjectionLiveTest { mongoTemplate.insert(new User("John", 30)); mongoTemplate.insert(new User("Ringo", 35)); - Query query = new Query(); + final Query query = new Query(); query.fields() .include("name"); @@ -55,7 +57,7 @@ public class MongoTemplateProjectionLiveTest { mongoTemplate.insert(new User("John", 30)); mongoTemplate.insert(new User("Ringo", 35)); - Query query = new Query(); + final Query query = new Query(); query.fields() .exclude("_id"); @@ -64,7 +66,7 @@ public class MongoTemplateProjectionLiveTest { assertNull(user.getId()); assertNotNull(user.getAge()); }); - + } } diff --git a/spring-data-rest/pom.xml b/spring-data-rest/pom.xml index da5d844211..4e8001ae7b 100644 --- a/spring-data-rest/pom.xml +++ b/spring-data-rest/pom.xml @@ -14,7 +14,7 @@ org.springframework.boot spring-boot-starter-parent - 1.4.2.RELEASE + 1.4.4.RELEASE diff --git a/spring-hibernate4/.gitignore b/spring-hibernate4/.gitignore index 478cca6dac..d31cc4c619 100644 --- a/spring-hibernate4/.gitignore +++ b/spring-hibernate4/.gitignore @@ -12,3 +12,4 @@ *.war *.ear /target/ +/target/ diff --git a/spring-hibernate4/src/main/java/com/baeldung/hibernate/oneToMany/main/HibernateOneToManyAnnotationMain.java b/spring-hibernate4/src/main/java/com/baeldung/hibernate/oneToMany/main/HibernateOneToManyAnnotationMain.java index 63b6450bf4..2bc5e514f7 100644 --- a/spring-hibernate4/src/main/java/com/baeldung/hibernate/oneToMany/main/HibernateOneToManyAnnotationMain.java +++ b/spring-hibernate4/src/main/java/com/baeldung/hibernate/oneToMany/main/HibernateOneToManyAnnotationMain.java @@ -16,16 +16,15 @@ public class HibernateOneToManyAnnotationMain { public static void main(String[] args) { Cart cart = new Cart(); - cart.setName("MyCart"); - Items item1 = new Items("I10", 10, 1, cart); - Items item2 = new Items("I20", 20, 2, cart); + Items item1 = new Items(cart); + Items item2 = new Items(cart); Set itemsSet = new HashSet(); itemsSet.add(item1); itemsSet.add(item2); cart.setItems(itemsSet); - cart.setTotal(10 * 1 + 20 * 2); + SessionFactory sessionFactory = null; Session session = null; diff --git a/spring-hibernate4/src/main/java/com/baeldung/hibernate/oneToMany/model/Cart.java b/spring-hibernate4/src/main/java/com/baeldung/hibernate/oneToMany/model/Cart.java index 61a32ba43f..b8b991831e 100644 --- a/spring-hibernate4/src/main/java/com/baeldung/hibernate/oneToMany/model/Cart.java +++ b/spring-hibernate4/src/main/java/com/baeldung/hibernate/oneToMany/model/Cart.java @@ -19,11 +19,6 @@ public class Cart { @Column(name = "cart_id") private long id; - @Column(name = "total") - private double total; - - @Column(name = "name") - private String name; @OneToMany(mappedBy = "cart") private Set items; @@ -36,21 +31,6 @@ public class Cart { this.id = id; } - public double getTotal() { - return total; - } - - public void setTotal(double total) { - this.total = total; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } public Set getItems() { return items; diff --git a/spring-hibernate4/src/main/java/com/baeldung/hibernate/oneToMany/model/Items.java b/spring-hibernate4/src/main/java/com/baeldung/hibernate/oneToMany/model/Items.java index 40ee6fdea1..f63a4855b5 100644 --- a/spring-hibernate4/src/main/java/com/baeldung/hibernate/oneToMany/model/Items.java +++ b/spring-hibernate4/src/main/java/com/baeldung/hibernate/oneToMany/model/Items.java @@ -18,14 +18,6 @@ public class Items { @Column(name = "id") private long id; - @Column(name = "item_id") - private String itemId; - - @Column(name = "item_total") - private double itemTotal; - - @Column(name = "quantity") - private int quantity; @ManyToOne @JoinColumn(name = "cart_id", nullable = false) @@ -35,37 +27,10 @@ public class Items { public Items() { } - public Items(String itemId, double total, int qty, Cart c) { - this.itemId = itemId; - this.itemTotal = total; - this.quantity = qty; + public Items(Cart c) { this.cart = c; } - public String getItemId() { - return itemId; - } - - public void setItemId(String itemId) { - this.itemId = itemId; - } - - public double getItemTotal() { - return itemTotal; - } - - public void setItemTotal(double itemTotal) { - this.itemTotal = itemTotal; - } - - public int getQuantity() { - return quantity; - } - - public void setQuantity(int quantity) { - this.quantity = quantity; - } - public Cart getCart() { return cart; } diff --git a/spring-hibernate4/src/main/resources/one_to_many.sql b/spring-hibernate4/src/main/resources/one_to_many.sql index 7887ff7d9c..2eb48fc262 100644 --- a/spring-hibernate4/src/main/resources/one_to_many.sql +++ b/spring-hibernate4/src/main/resources/one_to_many.sql @@ -1,16 +1,11 @@ CREATE TABLE `Cart` ( `cart_id` int(11) unsigned NOT NULL AUTO_INCREMENT, - `total` decimal(10,0) NOT NULL, - `name` varchar(10) DEFAULT NULL, PRIMARY KEY (`cart_id`) ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8; CREATE TABLE `Items` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT, `cart_id` int(11) unsigned NOT NULL, - `item_id` varchar(10) NOT NULL, - `item_total` decimal(10,0) NOT NULL, - `quantity` int(3) NOT NULL, PRIMARY KEY (`id`), KEY `cart_id` (`cart_id`), CONSTRAINT `items_ibfk_1` FOREIGN KEY (`cart_id`) REFERENCES `Cart` (`cart_id`) diff --git a/spring-hibernate4/src/test/java/com/baeldung/hibernate/oneToMany/main/HibernateOneToManyAnnotationMainTest.java b/spring-hibernate4/src/test/java/com/baeldung/hibernate/oneToMany/main/HibernateOneToManyAnnotationMainTest.java index 688329e329..e2d2bf9143 100644 --- a/spring-hibernate4/src/test/java/com/baeldung/hibernate/oneToMany/main/HibernateOneToManyAnnotationMainTest.java +++ b/spring-hibernate4/src/test/java/com/baeldung/hibernate/oneToMany/main/HibernateOneToManyAnnotationMainTest.java @@ -63,9 +63,6 @@ public class HibernateOneToManyAnnotationMainTest { cartItems = cart.getItems(); Assert.assertNull(cartItems); Items item1 = new Items(); - item1.setItemId("I10"); - item1.setItemTotal(10); - item1.setQuantity(1); item1.setCart(cart); assertNotNull(item1); Set itemsSet = new HashSet(); diff --git a/spring-jersey/pom.xml b/spring-jersey/pom.xml index 293850d41e..41ebb9a6b5 100644 --- a/spring-jersey/pom.xml +++ b/spring-jersey/pom.xml @@ -9,7 +9,7 @@ war - 2.25 + 2.25.1 1.7.22 1.1.8 4.12 diff --git a/spring-jersey/src/main/java/com/baeldung/client/rest/RestClient.java b/spring-jersey/src/main/java/com/baeldung/client/rest/RestClient.java index 0e45b68b14..34f7d45601 100644 --- a/spring-jersey/src/main/java/com/baeldung/client/rest/RestClient.java +++ b/spring-jersey/src/main/java/com/baeldung/client/rest/RestClient.java @@ -18,7 +18,7 @@ public class RestClient { } public Employee getJsonEmployee(int id) { - return client.target(REST_URI).path(new Integer(id).toString()).request(MediaType.APPLICATION_JSON).get(Employee.class); + return client.target(REST_URI).path(String.valueOf(id)).request(MediaType.APPLICATION_JSON).get(Employee.class); } public Response createXmlEmployee(Employee emp) { @@ -26,6 +26,6 @@ public class RestClient { } public Employee getXmlEmployee(int id) { - return client.target(REST_URI).path(new Integer(id).toString()).request(MediaType.APPLICATION_XML).get(Employee.class); + return client.target(REST_URI).path(String.valueOf(id)).request(MediaType.APPLICATION_XML).get(Employee.class); } } diff --git a/spring-jooq/pom.xml b/spring-jooq/pom.xml index bf0dffd68c..195c0b8514 100644 --- a/spring-jooq/pom.xml +++ b/spring-jooq/pom.xml @@ -1,215 +1,215 @@ - 4.0.0 - com.baeldung - jooq-spring - 0.0.1-SNAPSHOT + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + 4.0.0 + com.baeldung + jooq-spring + 0.0.1-SNAPSHOT - - - - - org.springframework.boot - spring-boot-dependencies - 1.4.2.RELEASE - pom - import - - - + + + + + org.springframework.boot + spring-boot-dependencies + 1.4.4.RELEASE + pom + import + + + - - - - org.jooq - jooq - + + + + org.jooq + jooq + - - - com.h2database - h2 - + + + com.h2database + h2 + - - - org.springframework - spring-context - - - org.springframework - spring-jdbc - - - org.springframework.boot - spring-boot-starter-jooq - + + + org.springframework + spring-context + + + org.springframework + spring-jdbc + + + org.springframework.boot + spring-boot-starter-jooq + - - - org.slf4j - slf4j-api - runtime - - - ch.qos.logback - logback-classic - runtime - + + + org.slf4j + slf4j-api + runtime + + + ch.qos.logback + logback-classic + runtime + - - - junit - junit - test - - - org.springframework - spring-test - test - - - org.springframework.boot - spring-boot-starter-test - - - + + + junit + junit + test + + + org.springframework + spring-test + test + + + org.springframework.boot + spring-boot-starter-test + - - - - org.codehaus.mojo - properties-maven-plugin - ${properties-maven-plugin.version} - - - initialize - - read-project-properties - - - - src/main/resources/intro_config.properties - - - - - + - - org.codehaus.mojo - sql-maven-plugin - ${sql-maven-plugin.version} - - - initialize - - execute - - - ${db.driver} - ${db.url} - ${db.username} - ${db.password} - - src/main/resources/intro_schema.sql - - - - - - - com.h2database - h2 - ${com.h2database.version} - - - + + + + org.codehaus.mojo + properties-maven-plugin + ${properties-maven-plugin.version} + + + initialize + + read-project-properties + + + + src/main/resources/intro_config.properties + + + + + - - org.jooq - jooq-codegen-maven - ${org.jooq.version} - - - generate-sources - - generate - - - - ${db.driver} - ${db.url} - ${db.username} - ${db.password} - - - - com.baeldung.jooq.introduction.db - src/main/java - - - - - - + + org.codehaus.mojo + sql-maven-plugin + ${sql-maven-plugin.version} + + + initialize + + execute + + + ${db.driver} + ${db.url} + ${db.username} + ${db.password} + + src/main/resources/intro_schema.sql + + + + + + + com.h2database + h2 + ${com.h2database.version} + + + - - org.apache.maven.plugins - maven-compiler-plugin - ${maven-compiler-plugin.version} - - 1.8 - 1.8 - - + + org.jooq + jooq-codegen-maven + ${org.jooq.version} + + + generate-sources + + generate + + + + ${db.driver} + ${db.url} + ${db.username} + ${db.password} + + + + com.baeldung.jooq.introduction.db + src/main/java + + + + + + - - org.apache.maven.plugins - maven-surefire-plugin - - - **/*IntegrationTest.java - **/*LiveTest.java - - - + + org.apache.maven.plugins + maven-compiler-plugin + ${maven-compiler-plugin.version} + + 1.8 + 1.8 + + - - - - - - org.eclipse.m2e - lifecycle-mapping - ${lifecycle-mapping.version} - - - - - - org.jooq - - jooq-codegen-maven - - - [3.7.3,) - - - generate - - - - - - - - - - - - - + + org.apache.maven.plugins + maven-surefire-plugin + + + **/*IntegrationTest.java + **/*LiveTest.java + + + + + + + + + + org.eclipse.m2e + lifecycle-mapping + ${lifecycle-mapping.version} + + + + + + org.jooq + + jooq-codegen-maven + + + [3.7.3,) + + + generate + + + + + + + + + + + + +
@@ -245,21 +245,21 @@ - - - 3.8.6 - 1.4.193 - 4.3.4.RELEASE - 1.7.21 - 1.1.7 - 4.12 - - 3.6.0 + + + 3.8.6 + 1.4.193 + 4.3.4.RELEASE + 1.7.21 + 1.1.7 + 4.12 + + 3.6.0 2.19.1 1.0.0 1.5 - 1.0.0 - - + 1.0.0 + + \ No newline at end of file diff --git a/spring-katharsis/pom.xml b/spring-katharsis/pom.xml index 32f24231a9..25a1c13f6b 100644 --- a/spring-katharsis/pom.xml +++ b/spring-katharsis/pom.xml @@ -8,7 +8,7 @@ org.springframework.boot spring-boot-starter-parent - 1.4.2.RELEASE + 1.4.4.RELEASE diff --git a/spring-mobile/src/main/java/com/baeldung/Application.java b/spring-mobile/src/main/java/com/baeldung/Application.java index d384ab09b3..7275477b8d 100644 --- a/spring-mobile/src/main/java/com/baeldung/Application.java +++ b/spring-mobile/src/main/java/com/baeldung/Application.java @@ -6,9 +6,10 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Application { - public static void main(String[] args) { - SpringApplication.run(Application.class, args); - } + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + + } } diff --git a/spring-mockito/pom.xml b/spring-mockito/pom.xml index ca9bad2bc0..3231faf197 100644 --- a/spring-mockito/pom.xml +++ b/spring-mockito/pom.xml @@ -13,7 +13,7 @@ org.springframework.boot spring-boot-starter-parent - 1.4.2.RELEASE + 1.4.4.RELEASE diff --git a/spring-mvc-email/pom.xml b/spring-mvc-email/pom.xml index 5d72bb4a1c..4a3ef5f83b 100644 --- a/spring-mvc-email/pom.xml +++ b/spring-mvc-email/pom.xml @@ -12,7 +12,7 @@ org.springframework.boot spring-boot-starter-parent - 1.4.2.RELEASE + 1.4.4.RELEASE diff --git a/spring-mvc-forms/pom.xml b/spring-mvc-forms/pom.xml index 31a0c38791..35ed00c0e9 100644 --- a/spring-mvc-forms/pom.xml +++ b/spring-mvc-forms/pom.xml @@ -46,6 +46,7 @@ commons-fileupload ${fileupload.version} + diff --git a/spring-mvc-forms/src/main/java/com/baeldung/springmvcforms/configuration/WebInitializer.java b/spring-mvc-forms/src/main/java/com/baeldung/springmvcforms/configuration/WebInitializer.java index c602ea6454..2eb669da2c 100644 --- a/spring-mvc-forms/src/main/java/com/baeldung/springmvcforms/configuration/WebInitializer.java +++ b/spring-mvc-forms/src/main/java/com/baeldung/springmvcforms/configuration/WebInitializer.java @@ -24,6 +24,7 @@ public class WebInitializer implements WebApplicationInitializer { servlet.setLoadOnStartup(1); servlet.addMapping("/"); + } // @Override // public void onStartup(ServletContext container) { diff --git a/spring-mvc-java/src/main/java/com/baeldung/model/FormDataWithFile.java b/spring-mvc-java/src/main/java/com/baeldung/model/FormDataWithFile.java new file mode 100644 index 0000000000..29925a592d --- /dev/null +++ b/spring-mvc-java/src/main/java/com/baeldung/model/FormDataWithFile.java @@ -0,0 +1,35 @@ +package com.baeldung.model; + +import org.springframework.web.multipart.MultipartFile; + +public class FormDataWithFile { + + private String name; + private String email; + private MultipartFile file; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } + + public MultipartFile getFile() { + return file; + } + + public void setFile(MultipartFile file) { + this.file = file; + } + +} diff --git a/spring-mvc-java/src/main/java/com/baeldung/web/controller/FileUploadController.java b/spring-mvc-java/src/main/java/com/baeldung/web/controller/FileUploadController.java index 61bccb21aa..b357b9270f 100644 --- a/spring-mvc-java/src/main/java/com/baeldung/web/controller/FileUploadController.java +++ b/spring-mvc-java/src/main/java/com/baeldung/web/controller/FileUploadController.java @@ -2,11 +2,14 @@ package com.baeldung.web.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; +import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.multipart.MultipartFile; +import com.baeldung.model.FormDataWithFile; + @Controller public class FileUploadController { @@ -29,4 +32,21 @@ public class FileUploadController { modelMap.addAttribute("files", files); return "fileUploadView"; } + + @RequestMapping(value = "/uploadFileWithAddtionalData", method = RequestMethod.POST) + public String submit(@RequestParam final MultipartFile file, @RequestParam final String name, @RequestParam final String email, final ModelMap modelMap) { + + modelMap.addAttribute("name", name); + modelMap.addAttribute("email", email); + modelMap.addAttribute("file", file); + return "fileUploadView"; + } + + @RequestMapping(value = "/uploadFileModelAttribute", method = RequestMethod.POST) + public String submit(@ModelAttribute final FormDataWithFile formDataWithFile, final ModelMap modelMap) { + + modelMap.addAttribute("formDataWithFile", formDataWithFile); + return "fileUploadView"; + } + } diff --git a/spring-mvc-java/src/main/java/com/baeldung/web/controller/RequestMappingShortcutsController.java b/spring-mvc-java/src/main/java/com/baeldung/web/controller/RequestMappingShortcutsController.java new file mode 100644 index 0000000000..5d4cbe9d78 --- /dev/null +++ b/spring-mvc-java/src/main/java/com/baeldung/web/controller/RequestMappingShortcutsController.java @@ -0,0 +1,47 @@ +package com.baeldung.web.controller; + +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PatchMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.PutMapping; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class RequestMappingShortcutsController { + + @GetMapping("/get") + public @ResponseBody ResponseEntity get() { + return new ResponseEntity("GET Response", HttpStatus.OK); + } + + @GetMapping("/get/{id}") + public @ResponseBody ResponseEntity getById(@PathVariable String id) { + return new ResponseEntity("GET Response : " + id, HttpStatus.OK); + } + + @PostMapping("/post") + public @ResponseBody ResponseEntity post() { + return new ResponseEntity("POST Response", HttpStatus.OK); + } + + @PutMapping("/put") + public @ResponseBody ResponseEntity put() { + return new ResponseEntity("PUT Response", HttpStatus.OK); + } + + @DeleteMapping("/delete") + public @ResponseBody ResponseEntity delete() { + return new ResponseEntity("DELETE Response", HttpStatus.OK); + } + + @PatchMapping("/patch") + public @ResponseBody ResponseEntity patch() { + return new ResponseEntity("PATCH Response", HttpStatus.OK); + } + +} diff --git a/spring-mvc-java/src/main/webapp/WEB-INF/view/fileUploadForm.jsp b/spring-mvc-java/src/main/webapp/WEB-INF/view/fileUploadForm.jsp index 1414b824ff..41b7c09629 100644 --- a/spring-mvc-java/src/main/webapp/WEB-INF/view/fileUploadForm.jsp +++ b/spring-mvc-java/src/main/webapp/WEB-INF/view/fileUploadForm.jsp @@ -49,6 +49,58 @@ + +
+ +

Fill the Form and Select a File (@RequestParam)

+ + + + + + + + + + + + + + + + + + + +
Name
Email
Select a file to upload
+ +
+ +
+ +

Fill the Form and Select a File (@ModelAttribute)

+ + + + + + + + + + + + + + + + + + + +
Name
Email
Select a file to upload
+ +
diff --git a/spring-mvc-java/src/main/webapp/WEB-INF/view/fileUploadView.jsp b/spring-mvc-java/src/main/webapp/WEB-INF/view/fileUploadView.jsp index d6f748c6af..696af4c4aa 100644 --- a/spring-mvc-java/src/main/webapp/WEB-INF/view/fileUploadView.jsp +++ b/spring-mvc-java/src/main/webapp/WEB-INF/view/fileUploadView.jsp @@ -32,5 +32,48 @@ +
+ +

Submitted File with Data (@RequestParam)

+ + + + + + + + + + + + + + + + + +
Name :${name}
Email :${email}
OriginalFileName :${file.originalFilename}
Type :${file.contentType}
+ +
+ +

Submitted File with Data (@ModelAttribute)

+ + + + + + + + + + + + + + + + + +
Name :${formDataWithFile.name}
Email :${formDataWithFile.email}
OriginalFileName :${formDataWithFile.file.originalFilename}
Type :${formDataWithFile.file.contentType}
\ No newline at end of file diff --git a/spring-mvc-java/src/test/java/com/baeldung/handlermappings/BeanNameMappingConfigTest.java b/spring-mvc-java/src/test/java/com/baeldung/handlermappings/BeanNameMappingConfigIntegrationTest.java similarity index 96% rename from spring-mvc-java/src/test/java/com/baeldung/handlermappings/BeanNameMappingConfigTest.java rename to spring-mvc-java/src/test/java/com/baeldung/handlermappings/BeanNameMappingConfigIntegrationTest.java index f58e5cb0a1..f2c2c05f29 100644 --- a/spring-mvc-java/src/test/java/com/baeldung/handlermappings/BeanNameMappingConfigTest.java +++ b/spring-mvc-java/src/test/java/com/baeldung/handlermappings/BeanNameMappingConfigIntegrationTest.java @@ -22,7 +22,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. @RunWith(SpringJUnit4ClassRunner.class) @WebAppConfiguration @ContextConfiguration(classes = BeanNameUrlHandlerMappingConfig.class) -public class BeanNameMappingConfigTest { +public class BeanNameMappingConfigIntegrationTest { @Autowired private WebApplicationContext webAppContext; diff --git a/spring-mvc-java/src/test/java/com/baeldung/handlermappings/ControllerClassNameHandlerMappingTest.java b/spring-mvc-java/src/test/java/com/baeldung/handlermappings/ControllerClassNameHandlerMappingIntegrationTest.java similarity index 96% rename from spring-mvc-java/src/test/java/com/baeldung/handlermappings/ControllerClassNameHandlerMappingTest.java rename to spring-mvc-java/src/test/java/com/baeldung/handlermappings/ControllerClassNameHandlerMappingIntegrationTest.java index 6fdd168d0b..b84998470d 100644 --- a/spring-mvc-java/src/test/java/com/baeldung/handlermappings/ControllerClassNameHandlerMappingTest.java +++ b/spring-mvc-java/src/test/java/com/baeldung/handlermappings/ControllerClassNameHandlerMappingIntegrationTest.java @@ -22,7 +22,7 @@ import com.baeldung.config.ControllerClassNameHandlerMappingConfig; @RunWith(SpringJUnit4ClassRunner.class) @WebAppConfiguration @ContextConfiguration(classes = ControllerClassNameHandlerMappingConfig.class) -public class ControllerClassNameHandlerMappingTest { +public class ControllerClassNameHandlerMappingIntegrationTest { @Autowired private WebApplicationContext webAppContext; diff --git a/spring-mvc-java/src/test/java/com/baeldung/handlermappings/HandlerMappingDefaultConfigTest.java b/spring-mvc-java/src/test/java/com/baeldung/handlermappings/HandlerMappingDefaultConfigIntegrationTest.java similarity index 93% rename from spring-mvc-java/src/test/java/com/baeldung/handlermappings/HandlerMappingDefaultConfigTest.java rename to spring-mvc-java/src/test/java/com/baeldung/handlermappings/HandlerMappingDefaultConfigIntegrationTest.java index 01be65b829..cb89c01fed 100644 --- a/spring-mvc-java/src/test/java/com/baeldung/handlermappings/HandlerMappingDefaultConfigTest.java +++ b/spring-mvc-java/src/test/java/com/baeldung/handlermappings/HandlerMappingDefaultConfigIntegrationTest.java @@ -1,7 +1,10 @@ package com.baeldung.handlermappings; -import com.baeldung.config.HandlerMappingDefaultConfig; -import com.baeldung.config.HandlerMappingPrioritiesConfig; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view; + import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -14,15 +17,12 @@ import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.web.context.WebApplicationContext; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; -import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view; +import com.baeldung.config.HandlerMappingDefaultConfig; @RunWith(SpringJUnit4ClassRunner.class) @WebAppConfiguration @ContextConfiguration(classes = HandlerMappingDefaultConfig.class) -public class HandlerMappingDefaultConfigTest { +public class HandlerMappingDefaultConfigIntegrationTest { @Autowired private WebApplicationContext webAppContext; diff --git a/spring-mvc-java/src/test/java/com/baeldung/handlermappings/HandlerMappingPriorityConfigTest.java b/spring-mvc-java/src/test/java/com/baeldung/handlermappings/HandlerMappingPriorityConfigIntegrationTest.java similarity index 96% rename from spring-mvc-java/src/test/java/com/baeldung/handlermappings/HandlerMappingPriorityConfigTest.java rename to spring-mvc-java/src/test/java/com/baeldung/handlermappings/HandlerMappingPriorityConfigIntegrationTest.java index d6329ca6c1..55007aec28 100644 --- a/spring-mvc-java/src/test/java/com/baeldung/handlermappings/HandlerMappingPriorityConfigTest.java +++ b/spring-mvc-java/src/test/java/com/baeldung/handlermappings/HandlerMappingPriorityConfigIntegrationTest.java @@ -21,7 +21,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. @RunWith(SpringJUnit4ClassRunner.class) @WebAppConfiguration @ContextConfiguration(classes = HandlerMappingPrioritiesConfig.class) -public class HandlerMappingPriorityConfigTest { +public class HandlerMappingPriorityConfigIntegrationTest { @Autowired private WebApplicationContext webAppContext; diff --git a/spring-mvc-java/src/test/java/com/baeldung/handlermappings/SimpleUrlMappingConfigTest.java b/spring-mvc-java/src/test/java/com/baeldung/handlermappings/SimpleUrlMappingConfigIntegrationTest.java similarity index 96% rename from spring-mvc-java/src/test/java/com/baeldung/handlermappings/SimpleUrlMappingConfigTest.java rename to spring-mvc-java/src/test/java/com/baeldung/handlermappings/SimpleUrlMappingConfigIntegrationTest.java index 636339f152..ad35307330 100644 --- a/spring-mvc-java/src/test/java/com/baeldung/handlermappings/SimpleUrlMappingConfigTest.java +++ b/spring-mvc-java/src/test/java/com/baeldung/handlermappings/SimpleUrlMappingConfigIntegrationTest.java @@ -21,7 +21,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. @RunWith(SpringJUnit4ClassRunner.class) @WebAppConfiguration @ContextConfiguration(classes = SimpleUrlHandlerMappingConfig.class) -public class SimpleUrlMappingConfigTest { +public class SimpleUrlMappingConfigIntegrationTest { @Autowired private WebApplicationContext webAppContext; diff --git a/spring-mvc-java/src/test/java/com/baeldung/web/controller/RequestMapingShortcutsUnitTest.java b/spring-mvc-java/src/test/java/com/baeldung/web/controller/RequestMapingShortcutsUnitTest.java new file mode 100644 index 0000000000..d02a7140b5 --- /dev/null +++ b/spring-mvc-java/src/test/java/com/baeldung/web/controller/RequestMapingShortcutsUnitTest.java @@ -0,0 +1,92 @@ +package com.baeldung.web.controller; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.web.WebAppConfiguration; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.ResultMatcher; +import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; +import org.springframework.test.web.servlet.result.MockMvcResultMatchers; +import org.springframework.test.web.servlet.setup.DefaultMockMvcBuilder; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; +import org.springframework.web.context.WebApplicationContext; + +import com.baeldung.spring.web.config.WebConfig; + +@RunWith(SpringJUnit4ClassRunner.class) +@WebAppConfiguration +@ContextConfiguration(classes = WebConfig.class) +public class RequestMapingShortcutsUnitTest { + + @Autowired + private WebApplicationContext ctx; + + private MockMvc mockMvc; + + @Before + public void setup () { + DefaultMockMvcBuilder builder = MockMvcBuilders.webAppContextSetup(this.ctx); + this.mockMvc = builder.build(); + } + + @Test + public void giventUrl_whenGetRequest_thenFindGetResponse() throws Exception { + + MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/get"); + + ResultMatcher contentMatcher = MockMvcResultMatchers.content().string("GET Response"); + + this.mockMvc.perform(builder).andExpect(contentMatcher).andExpect(MockMvcResultMatchers.status().isOk()); + + } + + @Test + public void giventUrl_whenPostRequest_thenFindPostResponse() throws Exception { + + MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.post("/post"); + + ResultMatcher contentMatcher = MockMvcResultMatchers.content().string("POST Response"); + + this.mockMvc.perform(builder).andExpect(contentMatcher).andExpect(MockMvcResultMatchers.status().isOk()); + + } + + @Test + public void giventUrl_whenPutRequest_thenFindPutResponse() throws Exception { + + MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.put("/put"); + + ResultMatcher contentMatcher = MockMvcResultMatchers.content().string("PUT Response"); + + this.mockMvc.perform(builder).andExpect(contentMatcher).andExpect(MockMvcResultMatchers.status().isOk()); + + } + + @Test + public void giventUrl_whenDeleteRequest_thenFindDeleteResponse() throws Exception { + + MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.delete("/delete"); + + ResultMatcher contentMatcher = MockMvcResultMatchers.content().string("DELETE Response"); + + this.mockMvc.perform(builder).andExpect(contentMatcher).andExpect(MockMvcResultMatchers.status().isOk()); + + } + + @Test + public void giventUrl_whenPatchRequest_thenFindPatchResponse() throws Exception { + + MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.patch("/patch"); + + ResultMatcher contentMatcher = MockMvcResultMatchers.content().string("PATCH Response"); + + this.mockMvc.perform(builder).andExpect(contentMatcher).andExpect(MockMvcResultMatchers.status().isOk()); + + } + +} diff --git a/spring-mvc-web-vs-initializer/pom.xml b/spring-mvc-web-vs-initializer/pom.xml index c8bb08cb38..8fe8893903 100644 --- a/spring-mvc-web-vs-initializer/pom.xml +++ b/spring-mvc-web-vs-initializer/pom.xml @@ -10,7 +10,7 @@ org.springframework.boot spring-boot-starter-parent - 1.4.2.RELEASE + 1.4.4.RELEASE diff --git a/spring-protobuf/pom.xml b/spring-protobuf/pom.xml index 84ebeff9ae..566d11132c 100644 --- a/spring-protobuf/pom.xml +++ b/spring-protobuf/pom.xml @@ -9,7 +9,7 @@ org.springframework.boot spring-boot-starter-parent - 1.4.2.RELEASE + 1.4.4.RELEASE diff --git a/spring-quartz/pom.xml b/spring-quartz/pom.xml index 2fb4ab0e47..85b5734ecc 100644 --- a/spring-quartz/pom.xml +++ b/spring-quartz/pom.xml @@ -13,7 +13,7 @@ org.springframework.boot spring-boot-starter-parent - 1.4.2.RELEASE + 1.4.4.RELEASE diff --git a/spring-rest-angular/pom.xml b/spring-rest-angular/pom.xml index e838e2d5e6..099867d19b 100644 --- a/spring-rest-angular/pom.xml +++ b/spring-rest-angular/pom.xml @@ -11,7 +11,7 @@ org.springframework.boot spring-boot-starter-parent - 1.4.2.RELEASE + 1.4.4.RELEASE diff --git a/spring-rest-docs/pom.xml b/spring-rest-docs/pom.xml index 2acb6b4071..6714617d99 100644 --- a/spring-rest-docs/pom.xml +++ b/spring-rest-docs/pom.xml @@ -4,7 +4,7 @@ 4.0.0 com.example - demo + spring-rest-docs 0.0.1-SNAPSHOT jar @@ -14,7 +14,7 @@ org.springframework.boot spring-boot-starter-parent - 1.4.2.RELEASE + 1.4.4.RELEASE @@ -45,13 +45,11 @@ org.springframework.restdocs spring-restdocs-mockmvc - ${restdocs.version} test com.jayway.jsonpath json-path - ${jsonpath.version} diff --git a/spring-security-cache-control/pom.xml b/spring-security-cache-control/pom.xml new file mode 100644 index 0000000000..c30b0cd1aa --- /dev/null +++ b/spring-security-cache-control/pom.xml @@ -0,0 +1,88 @@ + + + 4.0.0 + + com.baeldung + spring-security-cache-control + 1.0-SNAPSHOT + + + org.springframework.boot + spring-boot-starter-parent + 1.4.3.RELEASE + + + + + org.springframework.boot + spring-boot-starter-actuator + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.security + spring-security-core + + + org.springframework.security + spring-security-config + + + org.springframework.security + spring-security-web + + + org.springframework.boot + spring-boot-starter-test + + + javax.servlet + javax.servlet-api + ${javax.servlet-api.version} + + + + junit + junit + test + + + + org.hamcrest + hamcrest-core + test + + + org.hamcrest + hamcrest-library + test + + + + org.mockito + mockito-core + test + + + + org.springframework + spring-test + + + + com.jayway.restassured + rest-assured + ${rest-assured.version} + + + + + 3.1.0 + 2.9.0 + + + \ No newline at end of file diff --git a/spring-security-cache-control/src/main/java/com/baeldung/cachecontrol/AppRunner.java b/spring-security-cache-control/src/main/java/com/baeldung/cachecontrol/AppRunner.java new file mode 100644 index 0000000000..28ff90a570 --- /dev/null +++ b/spring-security-cache-control/src/main/java/com/baeldung/cachecontrol/AppRunner.java @@ -0,0 +1,12 @@ +package com.baeldung.cachecontrol; + + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class AppRunner { + public static void main(String[] args) { + SpringApplication.run(AppRunner.class, args); + } +} \ No newline at end of file diff --git a/spring-security-cache-control/src/main/java/com/baeldung/cachecontrol/ResourceEndpoint.java b/spring-security-cache-control/src/main/java/com/baeldung/cachecontrol/ResourceEndpoint.java new file mode 100644 index 0000000000..038df43165 --- /dev/null +++ b/spring-security-cache-control/src/main/java/com/baeldung/cachecontrol/ResourceEndpoint.java @@ -0,0 +1,48 @@ +package com.baeldung.cachecontrol; + +import com.baeldung.cachecontrol.model.TimestampDto; +import com.baeldung.cachecontrol.model.UserDto; +import org.springframework.http.CacheControl; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; + +import java.time.LocalDateTime; +import java.time.ZoneOffset; +import java.util.concurrent.TimeUnit; + +@Controller +public class ResourceEndpoint { + + @GetMapping(value = "/default/users/{name}") + public ResponseEntity getUserWithDefaultCaching(@PathVariable String name) { + return ResponseEntity.ok(new UserDto(name)); + } + + @GetMapping("/users/{name}") + public ResponseEntity getUser(@PathVariable String name) { + return ResponseEntity + .ok() + .cacheControl(CacheControl.maxAge(60, TimeUnit.SECONDS)) + .body(new UserDto(name)); + } + + @GetMapping("/timestamp") + public ResponseEntity getServerTimestamp() { + return ResponseEntity + .ok() + .cacheControl(CacheControl.noStore()) + .body(new TimestampDto(LocalDateTime + .now() + .toInstant(ZoneOffset.UTC) + .toEpochMilli())); + } + + @GetMapping("/private/users/{name}") + public ResponseEntity getUserNotCached(@PathVariable String name) { + return ResponseEntity + .ok() + .body(new UserDto(name)); + } +} diff --git a/spring-security-cache-control/src/main/java/com/baeldung/cachecontrol/config/SpringSecurityConfig.java b/spring-security-cache-control/src/main/java/com/baeldung/cachecontrol/config/SpringSecurityConfig.java new file mode 100644 index 0000000000..b4127e9b71 --- /dev/null +++ b/spring-security-cache-control/src/main/java/com/baeldung/cachecontrol/config/SpringSecurityConfig.java @@ -0,0 +1,17 @@ +package com.baeldung.cachecontrol.config; + + +import org.springframework.context.annotation.Configuration; +import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity; +import org.springframework.security.config.annotation.web.builders.HttpSecurity; +import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; +import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; + +@Configuration +@EnableWebSecurity +@EnableGlobalMethodSecurity(prePostEnabled = true) +public class SpringSecurityConfig extends WebSecurityConfigurerAdapter { + + @Override + protected void configure(HttpSecurity http) throws Exception {} +} diff --git a/spring-security-cache-control/src/main/java/com/baeldung/cachecontrol/model/TimestampDto.java b/spring-security-cache-control/src/main/java/com/baeldung/cachecontrol/model/TimestampDto.java new file mode 100644 index 0000000000..cb3befacac --- /dev/null +++ b/spring-security-cache-control/src/main/java/com/baeldung/cachecontrol/model/TimestampDto.java @@ -0,0 +1,10 @@ +package com.baeldung.cachecontrol.model; + + +public class TimestampDto { + public final Long timestamp; + + public TimestampDto(Long timestamp) { + this.timestamp = timestamp; + } +} diff --git a/spring-security-cache-control/src/main/java/com/baeldung/cachecontrol/model/UserDto.java b/spring-security-cache-control/src/main/java/com/baeldung/cachecontrol/model/UserDto.java new file mode 100644 index 0000000000..a41cb31d80 --- /dev/null +++ b/spring-security-cache-control/src/main/java/com/baeldung/cachecontrol/model/UserDto.java @@ -0,0 +1,11 @@ +package com.baeldung.cachecontrol.model; + + +public class UserDto { + public final String name; + + public UserDto(String name) { + this.name = name; + } + +} diff --git a/spring-security-cache-control/src/test/java/com/baeldung/cachecontrol/ResourceEndpointTest.java b/spring-security-cache-control/src/test/java/com/baeldung/cachecontrol/ResourceEndpointTest.java new file mode 100644 index 0000000000..6d532f98fc --- /dev/null +++ b/spring-security-cache-control/src/test/java/com/baeldung/cachecontrol/ResourceEndpointTest.java @@ -0,0 +1,72 @@ +package com.baeldung.cachecontrol; + +import com.jayway.restassured.http.ContentType; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.context.embedded.LocalServerPort; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +import static com.jayway.restassured.RestAssured.given; + +@RunWith(SpringRunner.class) +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = AppRunner.class) +public class ResourceEndpointTest { + + @LocalServerPort + private int serverPort; + + @Test + public void whenGetRequestForUser_shouldRespondWithDefaultCacheHeaders() { + given() + .when() + .get(getBaseUrl() + "/default/users/Michael") + .then() + .headers("Cache-Control", "no-cache, no-store, max-age=0, must-revalidate") + .header("Pragma", "no-cache"); + } + + @Test + public void whenGetRequestForUser_shouldRespondMaxAgeCacheControl() { + given() + .when() + .get(getBaseUrl() + "/users/Michael") + .then() + .header("Cache-Control", "max-age=60"); + } + + @Test + public void givenServiceEndpoint_whenGetRequestForUser_shouldResponseWithCacheControlMaxAge() { + given() + .when() + .get(getBaseUrl() + "/users/Michael") + .then() + .contentType(ContentType.JSON).and().statusCode(200).and() + .header("Cache-Control", "max-age=60"); + } + + @Test + public void givenServiceEndpoint_whenGetRequestForNotCacheableContent_shouldResponseWithCacheControlNoCache() { + given() + .when() + .get(getBaseUrl() + "/timestamp") + .then() + .contentType(ContentType.JSON).and().statusCode(200).and() + .header("Cache-Control", "no-store"); + } + + @Test + public void givenServiceEndpoint_whenGetRequestForPrivateUser_shouldResponseWithSecurityDefaultCacheControl() { + given() + .when() + .get(getBaseUrl() + "/private/users/Michael") + .then() + .contentType(ContentType.JSON).and().statusCode(200).and() + .header("Cache-Control", "no-cache, no-store, max-age=0, must-revalidate"); + } + + private String getBaseUrl() { + return String.format("http://localhost:%d", serverPort); + } + +} \ No newline at end of file diff --git a/spring-security-client/spring-security-jsp-authentication/pom.xml b/spring-security-client/spring-security-jsp-authentication/pom.xml index fc5f0cb86a..c021a46c82 100644 --- a/spring-security-client/spring-security-jsp-authentication/pom.xml +++ b/spring-security-client/spring-security-jsp-authentication/pom.xml @@ -13,7 +13,7 @@ org.springframework.boot spring-boot-starter-parent - 1.4.2.RELEASE + 1.4.4.RELEASE diff --git a/spring-security-client/spring-security-jsp-authorize/pom.xml b/spring-security-client/spring-security-jsp-authorize/pom.xml index 8cedbeb713..e3611aba93 100644 --- a/spring-security-client/spring-security-jsp-authorize/pom.xml +++ b/spring-security-client/spring-security-jsp-authorize/pom.xml @@ -13,7 +13,7 @@ org.springframework.boot spring-boot-starter-parent - 1.4.2.RELEASE + 1.4.4.RELEASE diff --git a/spring-security-client/spring-security-jsp-config/pom.xml b/spring-security-client/spring-security-jsp-config/pom.xml index 067e6dff60..94511a2785 100644 --- a/spring-security-client/spring-security-jsp-config/pom.xml +++ b/spring-security-client/spring-security-jsp-config/pom.xml @@ -13,7 +13,7 @@ org.springframework.boot spring-boot-starter-parent - 1.4.2.RELEASE + 1.4.4.RELEASE diff --git a/spring-security-client/spring-security-mvc/pom.xml b/spring-security-client/spring-security-mvc/pom.xml index b6a67d9c2c..f91a186897 100644 --- a/spring-security-client/spring-security-mvc/pom.xml +++ b/spring-security-client/spring-security-mvc/pom.xml @@ -13,7 +13,7 @@ org.springframework.boot spring-boot-starter-parent - 1.4.2.RELEASE + 1.4.4.RELEASE diff --git a/spring-security-client/spring-security-thymeleaf-authentication/pom.xml b/spring-security-client/spring-security-thymeleaf-authentication/pom.xml index 9f819d11c5..1a5e9e61d4 100644 --- a/spring-security-client/spring-security-thymeleaf-authentication/pom.xml +++ b/spring-security-client/spring-security-thymeleaf-authentication/pom.xml @@ -13,7 +13,7 @@ org.springframework.boot spring-boot-starter-parent - 1.4.2.RELEASE + 1.4.4.RELEASE diff --git a/spring-security-client/spring-security-thymeleaf-authorize/pom.xml b/spring-security-client/spring-security-thymeleaf-authorize/pom.xml index 5c13c34dea..964c57b71e 100644 --- a/spring-security-client/spring-security-thymeleaf-authorize/pom.xml +++ b/spring-security-client/spring-security-thymeleaf-authorize/pom.xml @@ -13,7 +13,7 @@ org.springframework.boot spring-boot-starter-parent - 1.4.2.RELEASE + 1.4.4.RELEASE diff --git a/spring-security-client/spring-security-thymeleaf-config/pom.xml b/spring-security-client/spring-security-thymeleaf-config/pom.xml index cc43b2ffb1..42c6ee4f0f 100644 --- a/spring-security-client/spring-security-thymeleaf-config/pom.xml +++ b/spring-security-client/spring-security-thymeleaf-config/pom.xml @@ -13,7 +13,7 @@ org.springframework.boot spring-boot-starter-parent - 1.4.2.RELEASE + 1.4.4.RELEASE diff --git a/spring-security-core/pom.xml b/spring-security-core/pom.xml index 519ee73296..a8ffce84b7 100644 --- a/spring-security-core/pom.xml +++ b/spring-security-core/pom.xml @@ -11,7 +11,7 @@ org.springframework.boot spring-boot-starter-parent - 1.4.2.RELEASE + 1.4.4.RELEASE diff --git a/spring-security-custom-permission/README.MD b/spring-security-mvc-boot/README.MD similarity index 83% rename from spring-security-custom-permission/README.MD rename to spring-security-mvc-boot/README.MD index d59aea97b4..3e789dedad 100644 --- a/spring-security-custom-permission/README.MD +++ b/spring-security-mvc-boot/README.MD @@ -5,3 +5,4 @@ The "REST With Spring" Classes: http://github.learnspringsecurity.com - [A Custom Security Expression with Spring Security](http://www.baeldung.com/spring-security-create-new-custom-security-expression) - [Custom AccessDecisionVoters in Spring Security](http://www.baeldung.com/spring-security-custom-voter) - [Spring Security: Authentication with a Database-backed UserDetailsService](http://www.baeldung.com/spring-security-authentication-with-a-database) +- [Two Login Pages with Spring Security](http://www.baeldung.com/spring-security-two-login-pages) diff --git a/spring-security-custom-permission/WebContent/META-INF/MANIFEST.MF b/spring-security-mvc-boot/WebContent/META-INF/MANIFEST.MF similarity index 100% rename from spring-security-custom-permission/WebContent/META-INF/MANIFEST.MF rename to spring-security-mvc-boot/WebContent/META-INF/MANIFEST.MF diff --git a/spring-security-custom-permission/pom.xml b/spring-security-mvc-boot/pom.xml similarity index 93% rename from spring-security-custom-permission/pom.xml rename to spring-security-mvc-boot/pom.xml index 39dec2805f..591ededccf 100644 --- a/spring-security-custom-permission/pom.xml +++ b/spring-security-mvc-boot/pom.xml @@ -4,17 +4,17 @@ 4.0.0 com.baeldung - spring-security-custom-permission + spring-security-mvc-boot 0.0.1-SNAPSHOT war - spring-security-custom-permission - Spring Security custom permission + spring-security-mvc-boot + Spring Security MVC Boot org.springframework.boot spring-boot-starter-parent - 1.4.2.RELEASE + 1.4.4.RELEASE @@ -128,6 +128,12 @@ spring-security-taglibs ${spring-security-taglibs.version} + + + org.springframework.security + spring-security-core + ${spring-security-core.version} + javax.servlet.jsp.jstl @@ -280,11 +286,15 @@ org.baeldung.Application - UTF-8 + + + UTF-8 1.8 10.13.1.1 1.1.2 4.2.0.RELEASE + 4.2.0.RELEASE + 1.2 2.4.0 1.6.1 diff --git a/spring-security-custom-permission/src/main/java/org/baeldung/Application.java b/spring-security-mvc-boot/src/main/java/org/baeldung/Application.java similarity index 74% rename from spring-security-custom-permission/src/main/java/org/baeldung/Application.java rename to spring-security-mvc-boot/src/main/java/org/baeldung/Application.java index 71d503e678..03de5897f5 100644 --- a/spring-security-custom-permission/src/main/java/org/baeldung/Application.java +++ b/spring-security-mvc-boot/src/main/java/org/baeldung/Application.java @@ -9,7 +9,7 @@ import org.springframework.context.annotation.FilterType; @Configuration @EnableAutoConfiguration -@ComponentScan(excludeFilters = @ComponentScan.Filter(type = FilterType.REGEX, pattern = "org.baeldung.voter.*")) +@ComponentScan(excludeFilters = { @ComponentScan.Filter(type = FilterType.REGEX, pattern = "org.baeldung.voter.*"), @ComponentScan.Filter(type = FilterType.REGEX, pattern = "org.baeldung.multiplelogin.*") }) public class Application extends SpringBootServletInitializer { public static void main(String[] args) { SpringApplication.run(Application.class, args); diff --git a/spring-security-custom-permission/src/main/java/org/baeldung/config/MethodSecurityConfig.java b/spring-security-mvc-boot/src/main/java/org/baeldung/config/MethodSecurityConfig.java similarity index 100% rename from spring-security-custom-permission/src/main/java/org/baeldung/config/MethodSecurityConfig.java rename to spring-security-mvc-boot/src/main/java/org/baeldung/config/MethodSecurityConfig.java diff --git a/spring-security-custom-permission/src/main/java/org/baeldung/config/MvcConfig.java b/spring-security-mvc-boot/src/main/java/org/baeldung/config/MvcConfig.java similarity index 100% rename from spring-security-custom-permission/src/main/java/org/baeldung/config/MvcConfig.java rename to spring-security-mvc-boot/src/main/java/org/baeldung/config/MvcConfig.java diff --git a/spring-security-custom-permission/src/main/java/org/baeldung/config/PersistenceConfig.java b/spring-security-mvc-boot/src/main/java/org/baeldung/config/PersistenceConfig.java similarity index 100% rename from spring-security-custom-permission/src/main/java/org/baeldung/config/PersistenceConfig.java rename to spring-security-mvc-boot/src/main/java/org/baeldung/config/PersistenceConfig.java diff --git a/spring-security-custom-permission/src/main/java/org/baeldung/config/SecurityConfig.java b/spring-security-mvc-boot/src/main/java/org/baeldung/config/SecurityConfig.java similarity index 100% rename from spring-security-custom-permission/src/main/java/org/baeldung/config/SecurityConfig.java rename to spring-security-mvc-boot/src/main/java/org/baeldung/config/SecurityConfig.java diff --git a/spring-security-mvc-boot/src/main/java/org/baeldung/multiplelogin/MultipleLoginApplication.java b/spring-security-mvc-boot/src/main/java/org/baeldung/multiplelogin/MultipleLoginApplication.java new file mode 100644 index 0000000000..836336eb71 --- /dev/null +++ b/spring-security-mvc-boot/src/main/java/org/baeldung/multiplelogin/MultipleLoginApplication.java @@ -0,0 +1,22 @@ +package org.baeldung.multiplelogin; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.context.web.SpringBootServletInitializer; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.builder.SpringApplicationBuilder; + +@SpringBootApplication +@ComponentScan("org.baeldung.multiplelogin") +public class MultipleLoginApplication extends SpringBootServletInitializer { + public static void main(String[] args) { + SpringApplication.run(MultipleLoginApplication.class, args); + } + + @Override + protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { + return application.sources(MultipleLoginApplication.class); + } +} \ No newline at end of file diff --git a/spring-security-mvc-boot/src/main/java/org/baeldung/multiplelogin/MultipleLoginMvcConfig.java b/spring-security-mvc-boot/src/main/java/org/baeldung/multiplelogin/MultipleLoginMvcConfig.java new file mode 100644 index 0000000000..ec76be1909 --- /dev/null +++ b/spring-security-mvc-boot/src/main/java/org/baeldung/multiplelogin/MultipleLoginMvcConfig.java @@ -0,0 +1,45 @@ +package org.baeldung.multiplelogin; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.ViewResolver; +import org.springframework.web.servlet.config.annotation.EnableWebMvc; +import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; +import org.springframework.web.servlet.view.InternalResourceViewResolver; +import org.springframework.web.servlet.view.JstlView; +import org.springframework.context.annotation.ComponentScan; + +@EnableWebMvc +@Configuration +@ComponentScan("org.baeldung.controller") +public class MultipleLoginMvcConfig extends WebMvcConfigurerAdapter { + + public MultipleLoginMvcConfig() { + super(); + } + + // API + + @Override + public void addViewControllers(final ViewControllerRegistry registry) { + super.addViewControllers(registry); + + registry.addViewController("/anonymous.html"); + + registry.addViewController("/login.html"); + registry.addViewController("/homepage.html"); + registry.addViewController("/console.html"); + } + + @Bean + public ViewResolver viewResolver() { + final InternalResourceViewResolver bean = new InternalResourceViewResolver(); + + bean.setViewClass(JstlView.class); + bean.setPrefix("/WEB-INF/view/"); + bean.setSuffix(".jsp"); + + return bean; + } +} \ No newline at end of file diff --git a/spring-security-mvc-boot/src/main/java/org/baeldung/multiplelogin/MultipleLoginSecurityConfig.java b/spring-security-mvc-boot/src/main/java/org/baeldung/multiplelogin/MultipleLoginSecurityConfig.java new file mode 100644 index 0000000000..8327e7e5d3 --- /dev/null +++ b/spring-security-mvc-boot/src/main/java/org/baeldung/multiplelogin/MultipleLoginSecurityConfig.java @@ -0,0 +1,74 @@ +package org.baeldung.multiplelogin; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.annotation.Order; +import org.springframework.security.authentication.AuthenticationProvider; +import org.springframework.security.authentication.TestingAuthenticationProvider; +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.EnableWebSecurity; +import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; +import org.springframework.security.core.userdetails.User; +import org.springframework.security.core.userdetails.UserDetailsService; +import org.springframework.security.provisioning.InMemoryUserDetailsManager; + +@Configuration +@EnableWebSecurity +public class MultipleLoginSecurityConfig { + + @Bean + public UserDetailsService userDetailsService() throws Exception { + InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager(); + manager.createUser(User.withUsername("user").password("userPass").roles("USER").build()); + manager.createUser(User.withUsername("admin").password("adminPass").roles("ADMIN").build()); + return manager; + } + + @Configuration + @Order(1) + public static class App1ConfigurationAdapter extends WebSecurityConfigurerAdapter { + + public App1ConfigurationAdapter() { + super(); + } + + @Override + protected void configure(AuthenticationManagerBuilder auth) throws Exception { + auth.inMemoryAuthentication().withUser("admin").password("admin").roles("ADMIN"); + } + + @Override + protected void configure(HttpSecurity http) throws Exception { + http.antMatcher("/admin*").authorizeRequests().anyRequest().hasRole("ADMIN") + // log in + .and().formLogin().loginPage("/loginAdmin").loginProcessingUrl("/admin_login").failureUrl("/loginAdmin?error=loginError").defaultSuccessUrl("/adminPage") + // logout + .and().logout().logoutUrl("/admin_logout").logoutSuccessUrl("/protectedLinks").deleteCookies("JSESSIONID").and().exceptionHandling().accessDeniedPage("/403").and().csrf().disable(); + } + } + + @Configuration + @Order(2) + public static class App2ConfigurationAdapter extends WebSecurityConfigurerAdapter { + + public App2ConfigurationAdapter() { + super(); + } + + @Override + protected void configure(AuthenticationManagerBuilder auth) throws Exception { + auth.inMemoryAuthentication().withUser("user").password("user").roles("USER"); + } + + protected void configure(HttpSecurity http) throws Exception { + http.antMatcher("/user*").authorizeRequests().anyRequest().hasRole("USER") + // log in + .and().formLogin().loginPage("/loginUser").loginProcessingUrl("/user_login").failureUrl("/loginUser?error=loginError").defaultSuccessUrl("/userPage") + // logout + .and().logout().logoutUrl("/user_logout").logoutSuccessUrl("/protectedLinks").deleteCookies("JSESSIONID").and().exceptionHandling().accessDeniedPage("/403").and().csrf().disable(); + } + } + +} diff --git a/spring-security-mvc-boot/src/main/java/org/baeldung/multiplelogin/UsersController.java b/spring-security-mvc-boot/src/main/java/org/baeldung/multiplelogin/UsersController.java new file mode 100644 index 0000000000..0672a760af --- /dev/null +++ b/spring-security-mvc-boot/src/main/java/org/baeldung/multiplelogin/UsersController.java @@ -0,0 +1,38 @@ +package org.baeldung.multiplelogin; + +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; + +@Controller +public class UsersController { + + @RequestMapping("/protectedLinks") + public String getAnonymousPage() { + return "protectedLinks"; + } + + @RequestMapping("/userPage") + public String getUserPage() { + return "userPage"; + } + + @RequestMapping("/adminPage") + public String getAdminPage() { + return "adminPage"; + } + + @RequestMapping("/loginAdmin") + public String getAdminLoginPage() { + return "loginAdmin"; + } + + @RequestMapping("/loginUser") + public String getUserLoginPage() { + return "loginUser"; + } + + @RequestMapping("/403") + public String getAccessDeniedPage() { + return "403"; + } +} diff --git a/spring-security-custom-permission/src/main/java/org/baeldung/persistence/SetupData.java b/spring-security-mvc-boot/src/main/java/org/baeldung/persistence/SetupData.java similarity index 100% rename from spring-security-custom-permission/src/main/java/org/baeldung/persistence/SetupData.java rename to spring-security-mvc-boot/src/main/java/org/baeldung/persistence/SetupData.java diff --git a/spring-security-custom-permission/src/main/java/org/baeldung/persistence/dao/OrganizationRepository.java b/spring-security-mvc-boot/src/main/java/org/baeldung/persistence/dao/OrganizationRepository.java similarity index 100% rename from spring-security-custom-permission/src/main/java/org/baeldung/persistence/dao/OrganizationRepository.java rename to spring-security-mvc-boot/src/main/java/org/baeldung/persistence/dao/OrganizationRepository.java diff --git a/spring-security-custom-permission/src/main/java/org/baeldung/persistence/dao/PrivilegeRepository.java b/spring-security-mvc-boot/src/main/java/org/baeldung/persistence/dao/PrivilegeRepository.java similarity index 100% rename from spring-security-custom-permission/src/main/java/org/baeldung/persistence/dao/PrivilegeRepository.java rename to spring-security-mvc-boot/src/main/java/org/baeldung/persistence/dao/PrivilegeRepository.java diff --git a/spring-security-custom-permission/src/main/java/org/baeldung/persistence/dao/UserRepository.java b/spring-security-mvc-boot/src/main/java/org/baeldung/persistence/dao/UserRepository.java similarity index 100% rename from spring-security-custom-permission/src/main/java/org/baeldung/persistence/dao/UserRepository.java rename to spring-security-mvc-boot/src/main/java/org/baeldung/persistence/dao/UserRepository.java diff --git a/spring-security-custom-permission/src/main/java/org/baeldung/persistence/model/Foo.java b/spring-security-mvc-boot/src/main/java/org/baeldung/persistence/model/Foo.java similarity index 100% rename from spring-security-custom-permission/src/main/java/org/baeldung/persistence/model/Foo.java rename to spring-security-mvc-boot/src/main/java/org/baeldung/persistence/model/Foo.java diff --git a/spring-security-custom-permission/src/main/java/org/baeldung/persistence/model/Organization.java b/spring-security-mvc-boot/src/main/java/org/baeldung/persistence/model/Organization.java similarity index 100% rename from spring-security-custom-permission/src/main/java/org/baeldung/persistence/model/Organization.java rename to spring-security-mvc-boot/src/main/java/org/baeldung/persistence/model/Organization.java diff --git a/spring-security-custom-permission/src/main/java/org/baeldung/persistence/model/Privilege.java b/spring-security-mvc-boot/src/main/java/org/baeldung/persistence/model/Privilege.java similarity index 100% rename from spring-security-custom-permission/src/main/java/org/baeldung/persistence/model/Privilege.java rename to spring-security-mvc-boot/src/main/java/org/baeldung/persistence/model/Privilege.java diff --git a/spring-security-custom-permission/src/main/java/org/baeldung/persistence/model/User.java b/spring-security-mvc-boot/src/main/java/org/baeldung/persistence/model/User.java similarity index 100% rename from spring-security-custom-permission/src/main/java/org/baeldung/persistence/model/User.java rename to spring-security-mvc-boot/src/main/java/org/baeldung/persistence/model/User.java diff --git a/spring-security-custom-permission/src/main/java/org/baeldung/security/CustomMethodSecurityExpressionHandler.java b/spring-security-mvc-boot/src/main/java/org/baeldung/security/CustomMethodSecurityExpressionHandler.java similarity index 100% rename from spring-security-custom-permission/src/main/java/org/baeldung/security/CustomMethodSecurityExpressionHandler.java rename to spring-security-mvc-boot/src/main/java/org/baeldung/security/CustomMethodSecurityExpressionHandler.java diff --git a/spring-security-custom-permission/src/main/java/org/baeldung/security/CustomMethodSecurityExpressionRoot.java b/spring-security-mvc-boot/src/main/java/org/baeldung/security/CustomMethodSecurityExpressionRoot.java similarity index 100% rename from spring-security-custom-permission/src/main/java/org/baeldung/security/CustomMethodSecurityExpressionRoot.java rename to spring-security-mvc-boot/src/main/java/org/baeldung/security/CustomMethodSecurityExpressionRoot.java diff --git a/spring-security-custom-permission/src/main/java/org/baeldung/security/CustomPermissionEvaluator.java b/spring-security-mvc-boot/src/main/java/org/baeldung/security/CustomPermissionEvaluator.java similarity index 100% rename from spring-security-custom-permission/src/main/java/org/baeldung/security/CustomPermissionEvaluator.java rename to spring-security-mvc-boot/src/main/java/org/baeldung/security/CustomPermissionEvaluator.java diff --git a/spring-security-custom-permission/src/main/java/org/baeldung/security/MySecurityExpressionRoot.java b/spring-security-mvc-boot/src/main/java/org/baeldung/security/MySecurityExpressionRoot.java similarity index 100% rename from spring-security-custom-permission/src/main/java/org/baeldung/security/MySecurityExpressionRoot.java rename to spring-security-mvc-boot/src/main/java/org/baeldung/security/MySecurityExpressionRoot.java diff --git a/spring-security-custom-permission/src/main/java/org/baeldung/security/MyUserDetailsService.java b/spring-security-mvc-boot/src/main/java/org/baeldung/security/MyUserDetailsService.java similarity index 100% rename from spring-security-custom-permission/src/main/java/org/baeldung/security/MyUserDetailsService.java rename to spring-security-mvc-boot/src/main/java/org/baeldung/security/MyUserDetailsService.java diff --git a/spring-security-custom-permission/src/main/java/org/baeldung/security/MyUserPrincipal.java b/spring-security-mvc-boot/src/main/java/org/baeldung/security/MyUserPrincipal.java similarity index 100% rename from spring-security-custom-permission/src/main/java/org/baeldung/security/MyUserPrincipal.java rename to spring-security-mvc-boot/src/main/java/org/baeldung/security/MyUserPrincipal.java diff --git a/spring-security-custom-permission/src/main/java/org/baeldung/voter/MinuteBasedVoter.java b/spring-security-mvc-boot/src/main/java/org/baeldung/voter/MinuteBasedVoter.java similarity index 100% rename from spring-security-custom-permission/src/main/java/org/baeldung/voter/MinuteBasedVoter.java rename to spring-security-mvc-boot/src/main/java/org/baeldung/voter/MinuteBasedVoter.java diff --git a/spring-security-custom-permission/src/main/java/org/baeldung/voter/VoterApplication.java b/spring-security-mvc-boot/src/main/java/org/baeldung/voter/VoterApplication.java similarity index 89% rename from spring-security-custom-permission/src/main/java/org/baeldung/voter/VoterApplication.java rename to spring-security-mvc-boot/src/main/java/org/baeldung/voter/VoterApplication.java index c032a9634e..d2078e6115 100644 --- a/spring-security-custom-permission/src/main/java/org/baeldung/voter/VoterApplication.java +++ b/spring-security-mvc-boot/src/main/java/org/baeldung/voter/VoterApplication.java @@ -4,7 +4,6 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.FilterType; @Configuration @EnableAutoConfiguration diff --git a/spring-security-custom-permission/src/main/java/org/baeldung/voter/VoterMvcConfig.java b/spring-security-mvc-boot/src/main/java/org/baeldung/voter/VoterMvcConfig.java similarity index 100% rename from spring-security-custom-permission/src/main/java/org/baeldung/voter/VoterMvcConfig.java rename to spring-security-mvc-boot/src/main/java/org/baeldung/voter/VoterMvcConfig.java diff --git a/spring-security-custom-permission/src/main/java/org/baeldung/voter/WebSecurityConfig.java b/spring-security-mvc-boot/src/main/java/org/baeldung/voter/WebSecurityConfig.java similarity index 98% rename from spring-security-custom-permission/src/main/java/org/baeldung/voter/WebSecurityConfig.java rename to spring-security-mvc-boot/src/main/java/org/baeldung/voter/WebSecurityConfig.java index 495567c7b2..06fb4d2316 100644 --- a/spring-security-custom-permission/src/main/java/org/baeldung/voter/WebSecurityConfig.java +++ b/spring-security-mvc-boot/src/main/java/org/baeldung/voter/WebSecurityConfig.java @@ -18,8 +18,8 @@ import org.springframework.security.web.util.matcher.AntPathRequestMatcher; import java.util.Arrays; import java.util.List; -//@Configuration -//@EnableWebSecurity +@Configuration +@EnableWebSecurity public class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Autowired public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { diff --git a/spring-security-custom-permission/src/main/java/org/baeldung/voter/XmlSecurityConfig.java b/spring-security-mvc-boot/src/main/java/org/baeldung/voter/XmlSecurityConfig.java similarity index 77% rename from spring-security-custom-permission/src/main/java/org/baeldung/voter/XmlSecurityConfig.java rename to spring-security-mvc-boot/src/main/java/org/baeldung/voter/XmlSecurityConfig.java index 124513d317..8041585f42 100644 --- a/spring-security-custom-permission/src/main/java/org/baeldung/voter/XmlSecurityConfig.java +++ b/spring-security-mvc-boot/src/main/java/org/baeldung/voter/XmlSecurityConfig.java @@ -6,8 +6,8 @@ import org.springframework.context.annotation.ImportResource; /** * Created by ambrusadrianz on 09/10/2016. */ -@Configuration -@ImportResource({ "classpath:spring-security.xml" }) +// @Configuration +// @ImportResource({ "classpath:spring-security-custom-voter.xml" }) public class XmlSecurityConfig { public XmlSecurityConfig() { super(); diff --git a/spring-security-custom-permission/src/main/java/org/baeldung/web/MainController.java b/spring-security-mvc-boot/src/main/java/org/baeldung/web/MainController.java similarity index 100% rename from spring-security-custom-permission/src/main/java/org/baeldung/web/MainController.java rename to spring-security-mvc-boot/src/main/java/org/baeldung/web/MainController.java diff --git a/spring-security-custom-permission/src/main/resources/application.properties b/spring-security-mvc-boot/src/main/resources/application.properties similarity index 87% rename from spring-security-custom-permission/src/main/resources/application.properties rename to spring-security-mvc-boot/src/main/resources/application.properties index 9b140b3c69..d29b5f6bf1 100644 --- a/spring-security-custom-permission/src/main/resources/application.properties +++ b/spring-security-mvc-boot/src/main/resources/application.properties @@ -1,5 +1,5 @@ server.port=8082 -server.context-path=/spring-security-custom-permission +server.context-path=/spring-security-mvc-boot spring.datasource.driver-class-name=org.h2.Driver spring.datasource.url=jdbc:h2:mem:security_permission;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE spring.datasource.username=sa diff --git a/spring-security-custom-permission/src/main/resources/persistence-derby.properties b/spring-security-mvc-boot/src/main/resources/persistence-derby.properties similarity index 100% rename from spring-security-custom-permission/src/main/resources/persistence-derby.properties rename to spring-security-mvc-boot/src/main/resources/persistence-derby.properties diff --git a/spring-security-custom-permission/src/main/resources/spring-security.xml b/spring-security-mvc-boot/src/main/resources/spring-security-custom-voter.xml similarity index 99% rename from spring-security-custom-permission/src/main/resources/spring-security.xml rename to spring-security-mvc-boot/src/main/resources/spring-security-custom-voter.xml index 83bc14dda7..0b334a3694 100644 --- a/spring-security-custom-permission/src/main/resources/spring-security.xml +++ b/spring-security-mvc-boot/src/main/resources/spring-security-custom-voter.xml @@ -4,7 +4,7 @@ xmlns:beans="http://www.springframework.org/schema/beans" xsi:schemaLocation=" http://www.springframework.org/schema/security - http://www.springframework.org/schema/security/spring-security-4.2.xsd + http://www.springframework.org/schema/security/spring-security.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> diff --git a/spring-security-mvc-boot/src/main/resources/templates/403.html b/spring-security-mvc-boot/src/main/resources/templates/403.html new file mode 100644 index 0000000000..20550768cf --- /dev/null +++ b/spring-security-mvc-boot/src/main/resources/templates/403.html @@ -0,0 +1,10 @@ + + + + + + + +You do not have permission to view this page. + + \ No newline at end of file diff --git a/spring-security-mvc-boot/src/main/resources/templates/adminPage.html b/spring-security-mvc-boot/src/main/resources/templates/adminPage.html new file mode 100644 index 0000000000..13aff9b15a --- /dev/null +++ b/spring-security-mvc-boot/src/main/resources/templates/adminPage.html @@ -0,0 +1,13 @@ + + + + +Insert title here + + +Welcome admin! Logout + +

+Back to links + + \ No newline at end of file diff --git a/spring-security-custom-permission/src/main/resources/templates/index.html b/spring-security-mvc-boot/src/main/resources/templates/index.html similarity index 100% rename from spring-security-custom-permission/src/main/resources/templates/index.html rename to spring-security-mvc-boot/src/main/resources/templates/index.html diff --git a/spring-security-mvc-boot/src/main/resources/templates/login.html b/spring-security-mvc-boot/src/main/resources/templates/login.html new file mode 100644 index 0000000000..dd6bd04767 --- /dev/null +++ b/spring-security-mvc-boot/src/main/resources/templates/login.html @@ -0,0 +1,27 @@ + + + + +

Login

+ +
+ + + + + + + + + + + + + + +
User:
Password:
+ +
+ + + \ No newline at end of file diff --git a/spring-security-mvc-boot/src/main/resources/templates/loginAdmin.html b/spring-security-mvc-boot/src/main/resources/templates/loginAdmin.html new file mode 100644 index 0000000000..43d0e73233 --- /dev/null +++ b/spring-security-mvc-boot/src/main/resources/templates/loginAdmin.html @@ -0,0 +1,31 @@ + + + + +Insert title here + + + +

Admin login page

+
+ + + + + + + + + + + + + +
User:
Password:
+ +
+ +

Login failed!

+ + + \ No newline at end of file diff --git a/spring-security-mvc-boot/src/main/resources/templates/loginUser.html b/spring-security-mvc-boot/src/main/resources/templates/loginUser.html new file mode 100644 index 0000000000..bf4ddd48bc --- /dev/null +++ b/spring-security-mvc-boot/src/main/resources/templates/loginUser.html @@ -0,0 +1,30 @@ + + + + +Login + + + +

User login page

+ +
+ + + + + + + + + + + + +
User:
Password:
+ +
+

Login failed!

+ + + \ No newline at end of file diff --git a/spring-security-custom-permission/src/main/resources/templates/private.html b/spring-security-mvc-boot/src/main/resources/templates/private.html similarity index 100% rename from spring-security-custom-permission/src/main/resources/templates/private.html rename to spring-security-mvc-boot/src/main/resources/templates/private.html diff --git a/spring-security-mvc-boot/src/main/resources/templates/protectedLinks.html b/spring-security-mvc-boot/src/main/resources/templates/protectedLinks.html new file mode 100644 index 0000000000..1877464fe5 --- /dev/null +++ b/spring-security-mvc-boot/src/main/resources/templates/protectedLinks.html @@ -0,0 +1,13 @@ + + + + +Insert title here + + + +User page +
+Admin page + + \ No newline at end of file diff --git a/spring-security-mvc-boot/src/main/resources/templates/userPage.html b/spring-security-mvc-boot/src/main/resources/templates/userPage.html new file mode 100644 index 0000000000..894a225871 --- /dev/null +++ b/spring-security-mvc-boot/src/main/resources/templates/userPage.html @@ -0,0 +1,12 @@ + + + + +Insert title here + + +Welcome user! Logout +

+Back to links + + \ No newline at end of file diff --git a/spring-security-custom-permission/src/test/java/org/baeldung/web/CustomUserDetailsServiceIntegrationTest.java b/spring-security-mvc-boot/src/test/java/org/baeldung/web/CustomUserDetailsServiceIntegrationTest.java similarity index 100% rename from spring-security-custom-permission/src/test/java/org/baeldung/web/CustomUserDetailsServiceIntegrationTest.java rename to spring-security-mvc-boot/src/test/java/org/baeldung/web/CustomUserDetailsServiceIntegrationTest.java diff --git a/spring-security-custom-permission/src/test/java/org/baeldung/web/LiveTest.java b/spring-security-mvc-boot/src/test/java/org/baeldung/web/LiveTest.java similarity index 82% rename from spring-security-custom-permission/src/test/java/org/baeldung/web/LiveTest.java rename to spring-security-mvc-boot/src/test/java/org/baeldung/web/LiveTest.java index 47626b814a..d237ff2ec5 100644 --- a/spring-security-custom-permission/src/test/java/org/baeldung/web/LiveTest.java +++ b/spring-security-mvc-boot/src/test/java/org/baeldung/web/LiveTest.java @@ -14,24 +14,24 @@ import com.jayway.restassured.specification.RequestSpecification; public class LiveTest { - private final FormAuthConfig formAuthConfig = new FormAuthConfig("http://localhost:8082/spring-security-custom-permission/login", "username", "password"); + private final FormAuthConfig formAuthConfig = new FormAuthConfig("http://localhost:8082/spring-security-mvc-boot/login", "username", "password"); @Test public void givenUserWithReadPrivilegeAndHasPermission_whenGetFooById_thenOK() { - final Response response = givenAuth("john", "123").get("http://localhost:8082/spring-security-custom-permission/foos/1"); + final Response response = givenAuth("john", "123").get("http://localhost:8082/spring-security-mvc-boot/foos/1"); assertEquals(200, response.getStatusCode()); assertTrue(response.asString().contains("id")); } @Test public void givenUserWithNoWritePrivilegeAndHasPermission_whenPostFoo_thenForbidden() { - final Response response = givenAuth("john", "123").contentType(MediaType.APPLICATION_JSON_VALUE).body(new Foo("sample")).post("http://localhost:8082/spring-security-custom-permission/foos"); + final Response response = givenAuth("john", "123").contentType(MediaType.APPLICATION_JSON_VALUE).body(new Foo("sample")).post("http://localhost:8082/spring-security-mvc-boot/foos"); assertEquals(403, response.getStatusCode()); } @Test public void givenUserWithWritePrivilegeAndHasPermission_whenPostFoo_thenOk() { - final Response response = givenAuth("tom", "111").contentType(MediaType.APPLICATION_JSON_VALUE).body(new Foo("sample")).post("http://localhost:8082/spring-security-custom-permission/foos"); + final Response response = givenAuth("tom", "111").contentType(MediaType.APPLICATION_JSON_VALUE).body(new Foo("sample")).post("http://localhost:8082/spring-security-mvc-boot/foos"); assertEquals(201, response.getStatusCode()); assertTrue(response.asString().contains("id")); } @@ -40,14 +40,14 @@ public class LiveTest { @Test public void givenUserMemberInOrganization_whenGetOrganization_thenOK() { - final Response response = givenAuth("john", "123").get("http://localhost:8082/spring-security-custom-permission/organizations/1"); + final Response response = givenAuth("john", "123").get("http://localhost:8082/spring-security-mvc-boot/organizations/1"); assertEquals(200, response.getStatusCode()); assertTrue(response.asString().contains("id")); } @Test public void givenUserMemberNotInOrganization_whenGetOrganization_thenForbidden() { - final Response response = givenAuth("john", "123").get("http://localhost:8082/spring-security-custom-permission/organizations/2"); + final Response response = givenAuth("john", "123").get("http://localhost:8082/spring-security-mvc-boot/organizations/2"); assertEquals(403, response.getStatusCode()); } @@ -55,7 +55,7 @@ public class LiveTest { @Test public void givenDisabledSecurityExpression_whenGetFooByName_thenError() { - final Response response = givenAuth("john", "123").get("http://localhost:8082/spring-security-custom-permission/foos?name=sample"); + final Response response = givenAuth("john", "123").get("http://localhost:8082/spring-security-mvc-boot/foos?name=sample"); assertEquals(500, response.getStatusCode()); assertTrue(response.asString().contains("method hasAuthority() not allowed")); } diff --git a/spring-security-mvc-ldap/pom.xml b/spring-security-mvc-ldap/pom.xml index 242cc527ac..fc7941863e 100644 --- a/spring-security-mvc-ldap/pom.xml +++ b/spring-security-mvc-ldap/pom.xml @@ -10,7 +10,7 @@ org.springframework.boot spring-boot-starter-parent - 1.4.2.RELEASE + 1.4.4.RELEASE diff --git a/spring-security-mvc-login/src/main/java/org/baeldung/security/CustomAccessDeniedHandler.java b/spring-security-mvc-login/src/main/java/org/baeldung/security/CustomAccessDeniedHandler.java new file mode 100644 index 0000000000..ea4407c5c4 --- /dev/null +++ b/spring-security-mvc-login/src/main/java/org/baeldung/security/CustomAccessDeniedHandler.java @@ -0,0 +1,30 @@ +package org.baeldung.security; + +import java.io.IOException; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.apache.log4j.Logger; +import org.springframework.security.access.AccessDeniedException; +import org.springframework.security.core.Authentication; +import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.security.web.access.AccessDeniedHandler; + +public class CustomAccessDeniedHandler implements AccessDeniedHandler { + + public static final Logger LOG = Logger.getLogger(CustomAccessDeniedHandler.class); + + @Override + public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException exc) throws IOException, ServletException { + Authentication auth = SecurityContextHolder.getContext() + .getAuthentication(); + if (auth != null) { + LOG.warn("User: " + auth.getName() + " attempted to access the protected URL: " + request.getRequestURI()); + } + + response.sendRedirect(request.getContextPath() + "/accessDenied"); + } + +} diff --git a/spring-security-mvc-login/src/main/java/org/baeldung/spring/MvcConfig.java b/spring-security-mvc-login/src/main/java/org/baeldung/spring/MvcConfig.java index 02392df736..b59dbee0cf 100644 --- a/spring-security-mvc-login/src/main/java/org/baeldung/spring/MvcConfig.java +++ b/spring-security-mvc-login/src/main/java/org/baeldung/spring/MvcConfig.java @@ -28,6 +28,7 @@ public class MvcConfig extends WebMvcConfigurerAdapter { registry.addViewController("/login.html"); registry.addViewController("/homepage.html"); registry.addViewController("/admin/adminpage.html"); + registry.addViewController("/accessDenied"); } @Bean diff --git a/spring-security-mvc-login/src/main/java/org/baeldung/spring/SecSecurityConfig.java b/spring-security-mvc-login/src/main/java/org/baeldung/spring/SecSecurityConfig.java index ae41a037cd..7331d7bb18 100644 --- a/spring-security-mvc-login/src/main/java/org/baeldung/spring/SecSecurityConfig.java +++ b/spring-security-mvc-login/src/main/java/org/baeldung/spring/SecSecurityConfig.java @@ -1,5 +1,6 @@ package org.baeldung.spring; +import org.baeldung.security.CustomAccessDeniedHandler; import org.baeldung.security.CustomLogoutSuccessHandler; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -8,6 +9,7 @@ import org.springframework.security.config.annotation.authentication.builders.Au import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; +import org.springframework.security.web.access.AccessDeniedHandler; import org.springframework.security.web.authentication.logout.LogoutSuccessHandler; @Configuration @@ -53,6 +55,9 @@ public class SecSecurityConfig extends WebSecurityConfigurerAdapter { .logoutUrl("/perform_logout") .deleteCookies("JSESSIONID") .logoutSuccessHandler(logoutSuccessHandler()); + //.and() + //.exceptionHandling().accessDeniedPage("/accessDenied"); + //.exceptionHandling().accessDeniedHandler(accessDeniedHandler()); // @formatter:on } @@ -60,5 +65,10 @@ public class SecSecurityConfig extends WebSecurityConfigurerAdapter { public LogoutSuccessHandler logoutSuccessHandler() { return new CustomLogoutSuccessHandler(); } + + @Bean + public AccessDeniedHandler accessDeniedHandler(){ + return new CustomAccessDeniedHandler(); + } } diff --git a/spring-security-mvc-login/src/main/resources/webSecurityConfig.xml b/spring-security-mvc-login/src/main/resources/webSecurityConfig.xml index 9c8fdea9ee..f0fa956934 100644 --- a/spring-security-mvc-login/src/main/resources/webSecurityConfig.xml +++ b/spring-security-mvc-login/src/main/resources/webSecurityConfig.xml @@ -19,10 +19,16 @@ always-use-default-target="true"/> + + + + + + diff --git a/spring-security-mvc-login/src/main/webapp/WEB-INF/view/accessDenied.jsp b/spring-security-mvc-login/src/main/webapp/WEB-INF/view/accessDenied.jsp new file mode 100644 index 0000000000..45820cf43d --- /dev/null +++ b/spring-security-mvc-login/src/main/webapp/WEB-INF/view/accessDenied.jsp @@ -0,0 +1,15 @@ +<%@ page language="java" contentType="text/html; charset=ISO-8859-1" + pageEncoding="ISO-8859-1"%> +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> + + + + +Access Denied + + +

Sorry, you do not have permission to view this page.

+ +Click ">here to go back to the Homepage. + + \ No newline at end of file diff --git a/spring-security-mvc-login/src/main/webapp/WEB-INF/view/homepage.jsp b/spring-security-mvc-login/src/main/webapp/WEB-INF/view/homepage.jsp index 80f27f5466..c9d88cbc9b 100644 --- a/spring-security-mvc-login/src/main/webapp/WEB-INF/view/homepage.jsp +++ b/spring-security-mvc-login/src/main/webapp/WEB-INF/view/homepage.jsp @@ -4,21 +4,23 @@ -

This is the body of the sample view

+

This is the body of the sample view

- - This text is only visible to a user -
-
+ + This text is only visible to a user +

+ ">Restricted Admin Page +

+
- - This text is only visible to an admin -
+ + This text is only visible to an admin +
">Admin Page
-
+
+ + ">Logout - ">Logout - \ No newline at end of file diff --git a/spring-security-mvc-login/src/main/webapp/WEB-INF/web.xml b/spring-security-mvc-login/src/main/webapp/WEB-INF/web.xml index 0a0a340995..eef48ec9b3 100644 --- a/spring-security-mvc-login/src/main/webapp/WEB-INF/web.xml +++ b/spring-security-mvc-login/src/main/webapp/WEB-INF/web.xml @@ -43,8 +43,15 @@ /* + + 403 + /accessDenied + + + + \ No newline at end of file diff --git a/spring-security-openid/pom.xml b/spring-security-openid/pom.xml index 6e5db533d7..ff0b030bd2 100644 --- a/spring-security-openid/pom.xml +++ b/spring-security-openid/pom.xml @@ -13,7 +13,7 @@ org.springframework.boot spring-boot-starter-parent - 1.4.2.RELEASE + 1.4.4.RELEASE diff --git a/spring-security-rest-basic-auth/pom.xml b/spring-security-rest-basic-auth/pom.xml index 7987eebb66..953cfd6b6b 100644 --- a/spring-security-rest-basic-auth/pom.xml +++ b/spring-security-rest-basic-auth/pom.xml @@ -342,8 +342,8 @@ - 4.3.4.RELEASE - 4.2.0.RELEASE + 4.3.6.RELEASE + 4.2.1.RELEASE 5.2.5.Final @@ -351,7 +351,7 @@ 4.4.5 - 4.5.2 + 4.5.3 1.7.21 diff --git a/spring-security-rest-basic-auth/src/main/java/org/baeldung/client/HttpComponentsClientHttpRequestFactoryBasicAuth.java b/spring-security-rest-basic-auth/src/main/java/org/baeldung/client/HttpComponentsClientHttpRequestFactoryBasicAuth.java new file mode 100644 index 0000000000..a2f51d343b --- /dev/null +++ b/spring-security-rest-basic-auth/src/main/java/org/baeldung/client/HttpComponentsClientHttpRequestFactoryBasicAuth.java @@ -0,0 +1,39 @@ +package org.baeldung.client; + +import java.net.URI; + +import org.apache.http.HttpHost; +import org.apache.http.client.AuthCache; +import org.apache.http.client.protocol.HttpClientContext; +import org.apache.http.impl.auth.BasicScheme; +import org.apache.http.impl.client.BasicAuthCache; +import org.apache.http.protocol.BasicHttpContext; +import org.apache.http.protocol.HttpContext; +import org.springframework.http.HttpMethod; +import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; + +public class HttpComponentsClientHttpRequestFactoryBasicAuth extends HttpComponentsClientHttpRequestFactory { + + HttpHost host; + + public HttpComponentsClientHttpRequestFactoryBasicAuth(HttpHost host) { + super(); + this.host = host; + } + + protected HttpContext createHttpContext(HttpMethod httpMethod, URI uri) { + return createHttpContext(); + } + + private HttpContext createHttpContext() { + + AuthCache authCache = new BasicAuthCache(); + + BasicScheme basicAuth = new BasicScheme(); + authCache.put(host, basicAuth); + + BasicHttpContext localcontext = new BasicHttpContext(); + localcontext.setAttribute(HttpClientContext.AUTH_CACHE, authCache); + return localcontext; + } +} \ No newline at end of file diff --git a/spring-security-rest-basic-auth/src/main/java/org/baeldung/client/RestTemplateFactory.java b/spring-security-rest-basic-auth/src/main/java/org/baeldung/client/RestTemplateFactory.java index 0cec0dc5c3..5e15648e9b 100644 --- a/spring-security-rest-basic-auth/src/main/java/org/baeldung/client/RestTemplateFactory.java +++ b/spring-security-rest-basic-auth/src/main/java/org/baeldung/client/RestTemplateFactory.java @@ -1,15 +1,10 @@ package org.baeldung.client; -import org.apache.http.auth.AuthScope; -import org.apache.http.auth.UsernamePasswordCredentials; -import org.apache.http.client.config.RequestConfig; -import org.apache.http.impl.client.BasicCredentialsProvider; -import org.apache.http.impl.client.CloseableHttpClient; -import org.apache.http.impl.client.HttpClientBuilder; +import org.apache.http.HttpHost; import org.springframework.beans.factory.FactoryBean; import org.springframework.beans.factory.InitializingBean; import org.springframework.http.client.ClientHttpRequestFactory; -import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; +import org.springframework.http.client.support.BasicAuthorizationInterceptor; import org.springframework.stereotype.Component; import org.springframework.web.client.RestTemplate; @@ -40,16 +35,10 @@ public class RestTemplateFactory implements FactoryBean, Initializ @Override public void afterPropertiesSet() { - final int timeout = 5; - - final RequestConfig config = RequestConfig.custom().setConnectTimeout(timeout * 1000).setConnectionRequestTimeout(timeout * 1000).setSocketTimeout(timeout * 1000).build(); - - final BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider(); - credentialsProvider.setCredentials(new AuthScope("localhost", 8082, AuthScope.ANY_REALM), new UsernamePasswordCredentials("user1", "user1Pass")); - final CloseableHttpClient client = HttpClientBuilder.create().setDefaultRequestConfig(config).setDefaultCredentialsProvider(credentialsProvider).build(); - - final ClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(client); + HttpHost host = new HttpHost("localhost", 8082, "http"); + final ClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactoryBasicAuth(host); restTemplate = new RestTemplate(requestFactory); + restTemplate.getInterceptors().add(new BasicAuthorizationInterceptor("user1", "user1Pass")); } } \ No newline at end of file diff --git a/spring-security-rest-custom/pom.xml b/spring-security-rest-custom/pom.xml index 4c2fdb4709..d421f8d7b4 100644 --- a/spring-security-rest-custom/pom.xml +++ b/spring-security-rest-custom/pom.xml @@ -10,7 +10,7 @@ org.springframework.boot spring-boot-starter-parent - 1.4.2.RELEASE + 1.4.4.RELEASE diff --git a/spring-security-rest-full/pom.xml b/spring-security-rest-full/pom.xml index e9580d357d..43d14af5ea 100644 --- a/spring-security-rest-full/pom.xml +++ b/spring-security-rest-full/pom.xml @@ -10,7 +10,7 @@ org.springframework.boot spring-boot-starter-parent - 1.4.2.RELEASE + 1.4.4.RELEASE diff --git a/spring-security-x509/pom.xml b/spring-security-x509/pom.xml index 8596223cf4..b76bf4a23f 100644 --- a/spring-security-x509/pom.xml +++ b/spring-security-x509/pom.xml @@ -16,7 +16,7 @@ org.springframework.boot spring-boot-starter-parent - 1.4.2.RELEASE + 1.4.4.RELEASE diff --git a/spring-session/pom.xml b/spring-session/pom.xml index c247d8a065..2c8b15a6eb 100644 --- a/spring-session/pom.xml +++ b/spring-session/pom.xml @@ -11,7 +11,7 @@ org.springframework.boot spring-boot-starter-parent - 1.4.2.RELEASE + 1.4.4.RELEASE diff --git a/spring-social-login/pom.xml b/spring-social-login/pom.xml index 4369ec4e7c..1462611cfc 100644 --- a/spring-social-login/pom.xml +++ b/spring-social-login/pom.xml @@ -9,7 +9,7 @@ org.springframework.boot spring-boot-starter-parent - 1.4.2.RELEASE + 1.4.4.RELEASE diff --git a/spring-zuul/pom.xml b/spring-zuul/pom.xml index 2107892667..50b20b8791 100644 --- a/spring-zuul/pom.xml +++ b/spring-zuul/pom.xml @@ -10,7 +10,7 @@ org.springframework.boot spring-boot-starter-parent - 1.4.2.RELEASE + 1.4.4.RELEASE