Merge branch 'master' of https://github.com/eugenp/tutorials into sanketmeghani-master
This commit is contained in:
commit
1017089825
@ -60,12 +60,8 @@
|
|||||||
<configuration>
|
<configuration>
|
||||||
<source>1.9</source>
|
<source>1.9</source>
|
||||||
<target>1.9</target>
|
<target>1.9</target>
|
||||||
|
|
||||||
<verbose>true</verbose>
|
<verbose>true</verbose>
|
||||||
<!-- <executable>C:\develop\jdks\jdk-9_ea122\bin\javac</executable>
|
|
||||||
<compilerVersion>1.9</compilerVersion> -->
|
|
||||||
</configuration>
|
</configuration>
|
||||||
|
|
||||||
</plugin>
|
</plugin>
|
||||||
|
|
||||||
<plugin>
|
<plugin>
|
||||||
@ -85,12 +81,7 @@
|
|||||||
|
|
||||||
|
|
||||||
<!-- maven plugins -->
|
<!-- maven plugins -->
|
||||||
<!--
|
<maven-compiler-plugin.version>3.6-jigsaw-SNAPSHOT</maven-compiler-plugin.version>
|
||||||
<maven-war-plugin.version>2.6</maven-war-plugin.version>
|
|
||||||
maven-compiler-plugin.version>3.5.1</maven-compiler-plugin.version> -->
|
|
||||||
<maven-compiler-plugin.version>3.6-jigsaw-SNAPSHOT</maven-compiler-plugin.version>
|
|
||||||
|
|
||||||
|
|
||||||
<maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version>
|
<maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version>
|
||||||
|
|
||||||
<!-- testing -->
|
<!-- testing -->
|
||||||
|
@ -0,0 +1,58 @@
|
|||||||
|
package com.baeldung.java8;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class Java9OptionalsStreamTest {
|
||||||
|
|
||||||
|
private static List<Optional<String>> listOfOptionals = Arrays.asList(Optional.empty(), Optional.of("foo"), Optional.empty(), Optional.of("bar"));
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void filterOutPresentOptionalsWithFilter() {
|
||||||
|
assertEquals(4, listOfOptionals.size());
|
||||||
|
|
||||||
|
List<String> filteredList = listOfOptionals.stream()
|
||||||
|
.filter(Optional::isPresent)
|
||||||
|
.map(Optional::get)
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
|
||||||
|
assertEquals(2, filteredList.size());
|
||||||
|
assertEquals("foo", filteredList.get(0));
|
||||||
|
assertEquals("bar", filteredList.get(1));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void filterOutPresentOptionalsWithFlatMap() {
|
||||||
|
assertEquals(4, listOfOptionals.size());
|
||||||
|
|
||||||
|
List<String> filteredList = listOfOptionals.stream()
|
||||||
|
.flatMap(o -> o.isPresent() ? Stream.of(o.get()) : Stream.empty())
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
assertEquals(2, filteredList.size());
|
||||||
|
|
||||||
|
assertEquals("foo", filteredList.get(0));
|
||||||
|
assertEquals("bar", filteredList.get(1));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void filterOutPresentOptionalsWithJava9() {
|
||||||
|
assertEquals(4, listOfOptionals.size());
|
||||||
|
|
||||||
|
List<String> filteredList = listOfOptionals.stream()
|
||||||
|
.flatMap(Optional::stream)
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
|
||||||
|
assertEquals(2, filteredList.size());
|
||||||
|
assertEquals("foo", filteredList.get(0));
|
||||||
|
assertEquals("bar", filteredList.get(1));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,6 +1,5 @@
|
|||||||
package com.baeldung.java9;
|
package com.baeldung.java9;
|
||||||
|
|
||||||
import static org.junit.Assert.assertTrue;
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.assertSame;
|
import static org.junit.Assert.assertSame;
|
||||||
|
|
||||||
|
@ -0,0 +1,21 @@
|
|||||||
|
package com.baeldung.java9;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
public class OptionalToStreamTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testOptionalToStream() {
|
||||||
|
Optional<String> op = Optional.ofNullable("String value");
|
||||||
|
Stream<String> strOptionalStream = op.stream();
|
||||||
|
Stream<String> filteredStream = strOptionalStream.filter((str) -> {
|
||||||
|
return str != null && str.startsWith("String");
|
||||||
|
});
|
||||||
|
assertEquals(1, filteredStream.count());
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,26 @@
|
|||||||
|
package com.baeldung.java9;
|
||||||
|
|
||||||
|
import java.util.Set;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
public class SetExamplesTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testUnmutableSet() {
|
||||||
|
Set<String> strKeySet = Set.of("key1", "key2", "key3");
|
||||||
|
try {
|
||||||
|
strKeySet.add("newKey");
|
||||||
|
} catch (UnsupportedOperationException uoe) {
|
||||||
|
}
|
||||||
|
assertEquals(strKeySet.size(), 3);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testArrayToSet() {
|
||||||
|
Integer[] intArray = new Integer[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
|
||||||
|
Set<Integer> intSet = Set.of(intArray);
|
||||||
|
assertEquals(intSet.size(), intArray.length);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,65 @@
|
|||||||
|
package org.baeldung.equalshashcode.entities;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
public class ComplexClass {
|
||||||
|
|
||||||
|
private List<?> genericList;
|
||||||
|
private Set<Integer> integerSet;
|
||||||
|
|
||||||
|
public ComplexClass(ArrayList<?> genericArrayList, HashSet<Integer> integerHashSet) {
|
||||||
|
super();
|
||||||
|
this.genericList = genericArrayList;
|
||||||
|
this.integerSet = integerHashSet;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
final int prime = 31;
|
||||||
|
int result = 1;
|
||||||
|
result = prime * result + ((genericList == null) ? 0 : genericList.hashCode());
|
||||||
|
result = prime * result + ((integerSet == null) ? 0 : integerSet.hashCode());
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object obj) {
|
||||||
|
if (this == obj)
|
||||||
|
return true;
|
||||||
|
if (obj == null)
|
||||||
|
return false;
|
||||||
|
if (!(obj instanceof ComplexClass))
|
||||||
|
return false;
|
||||||
|
ComplexClass other = (ComplexClass) obj;
|
||||||
|
if (genericList == null) {
|
||||||
|
if (other.genericList != null)
|
||||||
|
return false;
|
||||||
|
} else if (!genericList.equals(other.genericList))
|
||||||
|
return false;
|
||||||
|
if (integerSet == null) {
|
||||||
|
if (other.integerSet != null)
|
||||||
|
return false;
|
||||||
|
} else if (!integerSet.equals(other.integerSet))
|
||||||
|
return false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected List<?> getGenericList() {
|
||||||
|
return genericList;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void setGenericArrayList(List<?> genericList) {
|
||||||
|
this.genericList = genericList;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected Set<Integer> getIntegerSet() {
|
||||||
|
return integerSet;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void setIntegerSet(Set<Integer> integerSet) {
|
||||||
|
this.integerSet = integerSet;
|
||||||
|
}
|
||||||
|
}
|
@ -11,13 +11,11 @@ public class Rectangle extends Shape {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public double area() {
|
public double area() {
|
||||||
// A = w * l
|
|
||||||
return width * length;
|
return width * length;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public double perimeter() {
|
public double perimeter() {
|
||||||
// P = 2(w + l)
|
|
||||||
return 2 * (width + length);
|
return 2 * (width + length);
|
||||||
}
|
}
|
||||||
|
|
@ -22,11 +22,10 @@ public class ComplexClassTest {
|
|||||||
strArrayListD.add("pqr");
|
strArrayListD.add("pqr");
|
||||||
ComplexClass dObject = new ComplexClass(strArrayListD, new HashSet<Integer>(45, 67));
|
ComplexClass dObject = new ComplexClass(strArrayListD, new HashSet<Integer>(45, 67));
|
||||||
|
|
||||||
// equals()
|
|
||||||
Assert.assertTrue(aObject.equals(bObject) && bObject.equals(aObject));
|
Assert.assertTrue(aObject.equals(bObject) && bObject.equals(aObject));
|
||||||
// hashCode()
|
|
||||||
Assert.assertTrue(aObject.hashCode() == bObject.hashCode());
|
Assert.assertTrue(aObject.hashCode() == bObject.hashCode());
|
||||||
// non-equal objects are not equals() and have different hashCode()
|
|
||||||
Assert.assertFalse(aObject.equals(dObject));
|
Assert.assertFalse(aObject.equals(dObject));
|
||||||
Assert.assertFalse(aObject.hashCode() == dObject.hashCode());
|
Assert.assertFalse(aObject.hashCode() == dObject.hashCode());
|
||||||
}
|
}
|
@ -12,11 +12,10 @@ public class PrimitiveClassTest {
|
|||||||
PrimitiveClass bObject = new PrimitiveClass(false, 2);
|
PrimitiveClass bObject = new PrimitiveClass(false, 2);
|
||||||
PrimitiveClass dObject = new PrimitiveClass(true, 2);
|
PrimitiveClass dObject = new PrimitiveClass(true, 2);
|
||||||
|
|
||||||
// equals()
|
|
||||||
Assert.assertTrue(aObject.equals(bObject) && bObject.equals(aObject));
|
Assert.assertTrue(aObject.equals(bObject) && bObject.equals(aObject));
|
||||||
// hashCode()
|
|
||||||
Assert.assertTrue(aObject.hashCode() == bObject.hashCode());
|
Assert.assertTrue(aObject.hashCode() == bObject.hashCode());
|
||||||
// non-equal objects are not equals() and have different hashCode()
|
|
||||||
Assert.assertFalse(aObject.equals(dObject));
|
Assert.assertFalse(aObject.equals(dObject));
|
||||||
Assert.assertFalse(aObject.hashCode() == dObject.hashCode());
|
Assert.assertFalse(aObject.hashCode() == dObject.hashCode());
|
||||||
}
|
}
|
@ -15,11 +15,10 @@ public class SquareClassTest {
|
|||||||
|
|
||||||
Square dObject = new Square(20, Color.BLUE);
|
Square dObject = new Square(20, Color.BLUE);
|
||||||
|
|
||||||
// equals()
|
|
||||||
Assert.assertTrue(aObject.equals(bObject) && bObject.equals(aObject));
|
Assert.assertTrue(aObject.equals(bObject) && bObject.equals(aObject));
|
||||||
// hashCode()
|
|
||||||
Assert.assertTrue(aObject.hashCode() == bObject.hashCode());
|
Assert.assertTrue(aObject.hashCode() == bObject.hashCode());
|
||||||
// non-equal objects are not equals() and have different hashCode()
|
|
||||||
Assert.assertFalse(aObject.equals(dObject));
|
Assert.assertFalse(aObject.equals(dObject));
|
||||||
Assert.assertFalse(aObject.hashCode() == dObject.hashCode());
|
Assert.assertFalse(aObject.hashCode() == dObject.hashCode());
|
||||||
}
|
}
|
@ -18,18 +18,18 @@ public class ArrayListTest {
|
|||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() {
|
public void setUp() {
|
||||||
List<String> xs = LongStream.range(0, 16)
|
List<String> list = LongStream.range(0, 16)
|
||||||
.boxed()
|
.boxed()
|
||||||
.map(Long::toHexString)
|
.map(Long::toHexString)
|
||||||
.collect(toCollection(ArrayList::new));
|
.collect(toCollection(ArrayList::new));
|
||||||
stringsToSearch = new ArrayList<>(xs);
|
stringsToSearch = new ArrayList<>(list);
|
||||||
stringsToSearch.addAll(xs);
|
stringsToSearch.addAll(list);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenNewArrayList_whenCheckCapacity_thenDefaultValue() {
|
public void givenNewArrayList_whenCheckCapacity_thenDefaultValue() {
|
||||||
List<String> xs = new ArrayList<>();
|
List<String> list = new ArrayList<>();
|
||||||
assertTrue(xs.isEmpty());
|
assertTrue(list.isEmpty());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -37,29 +37,29 @@ public class ArrayListTest {
|
|||||||
Collection<Integer> numbers =
|
Collection<Integer> numbers =
|
||||||
IntStream.range(0, 10).boxed().collect(toSet());
|
IntStream.range(0, 10).boxed().collect(toSet());
|
||||||
|
|
||||||
List<Integer> xs = new ArrayList<>(numbers);
|
List<Integer> list = new ArrayList<>(numbers);
|
||||||
assertEquals(10, xs.size());
|
assertEquals(10, list.size());
|
||||||
assertTrue(numbers.containsAll(xs));
|
assertTrue(numbers.containsAll(list));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenElement_whenAddToArrayList_thenIsAdded() {
|
public void givenElement_whenAddToArrayList_thenIsAdded() {
|
||||||
List<Long> xs = new ArrayList<>();
|
List<Long> list = new ArrayList<>();
|
||||||
|
|
||||||
xs.add(1L);
|
list.add(1L);
|
||||||
xs.add(2L);
|
list.add(2L);
|
||||||
xs.add(1, 3L);
|
list.add(1, 3L);
|
||||||
|
|
||||||
assertThat(Arrays.asList(1L, 3L, 2L), equalTo(xs));
|
assertThat(Arrays.asList(1L, 3L, 2L), equalTo(list));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenCollection_whenAddToArrayList_thenIsAdded() {
|
public void givenCollection_whenAddToArrayList_thenIsAdded() {
|
||||||
List<Long> xs = new ArrayList<>(Arrays.asList(1L, 2L, 3L));
|
List<Long> list = new ArrayList<>(Arrays.asList(1L, 2L, 3L));
|
||||||
LongStream.range(4, 10).boxed()
|
LongStream.range(4, 10).boxed()
|
||||||
.collect(collectingAndThen(toCollection(ArrayList::new), ys -> xs.addAll(0, ys)));
|
.collect(collectingAndThen(toCollection(ArrayList::new), ys -> list.addAll(0, ys)));
|
||||||
|
|
||||||
assertThat(Arrays.asList(4L, 5L, 6L, 7L, 8L, 9L, 1L, 2L, 3L), equalTo(xs));
|
assertThat(Arrays.asList(4L, 5L, 6L, 7L, 8L, 9L, 1L, 2L, 3L), equalTo(list));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -106,27 +106,27 @@ public class ArrayListTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenIndex_whenRemove_thenCorrectElementRemoved() {
|
public void givenIndex_whenRemove_thenCorrectElementRemoved() {
|
||||||
List<Integer> xs = IntStream.range(0, 10).boxed().collect(toCollection(ArrayList::new));
|
List<Integer> list = IntStream.range(0, 10).boxed().collect(toCollection(ArrayList::new));
|
||||||
Collections.reverse(xs);
|
Collections.reverse(list);
|
||||||
|
|
||||||
xs.remove(0);
|
list.remove(0);
|
||||||
assertThat(xs.get(0), equalTo(8));
|
assertThat(list.get(0), equalTo(8));
|
||||||
|
|
||||||
xs.remove(Integer.valueOf(0));
|
list.remove(Integer.valueOf(0));
|
||||||
assertFalse(xs.contains(0));
|
assertFalse(list.contains(0));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenListIterator_whenReverseTraversal_thenRetrieveElementsInOppositeOrder() {
|
public void givenListIterator_whenReverseTraversal_thenRetrieveElementsInOppositeOrder() {
|
||||||
List<Integer> xs = IntStream.range(0, 10).boxed().collect(toCollection(ArrayList::new));
|
List<Integer> list = IntStream.range(0, 10).boxed().collect(toCollection(ArrayList::new));
|
||||||
ListIterator<Integer> it = xs.listIterator(xs.size());
|
ListIterator<Integer> it = list.listIterator(list.size());
|
||||||
List<Integer> result = new ArrayList<>(xs.size());
|
List<Integer> result = new ArrayList<>(list.size());
|
||||||
while (it.hasPrevious()) {
|
while (it.hasPrevious()) {
|
||||||
result.add(it.previous());
|
result.add(it.previous());
|
||||||
}
|
}
|
||||||
|
|
||||||
Collections.reverse(xs);
|
Collections.reverse(list);
|
||||||
assertThat(result, equalTo(xs));
|
assertThat(result, equalTo(list));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -1,63 +0,0 @@
|
|||||||
package org.baeldung.equalshashcode.entities;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.HashSet;
|
|
||||||
|
|
||||||
public class ComplexClass {
|
|
||||||
|
|
||||||
private ArrayList<?> genericArrayList;
|
|
||||||
private HashSet<Integer> integerHashSet;
|
|
||||||
|
|
||||||
public ComplexClass(ArrayList<?> genericArrayList, HashSet<Integer> integerHashSet) {
|
|
||||||
super();
|
|
||||||
this.genericArrayList = genericArrayList;
|
|
||||||
this.integerHashSet = integerHashSet;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int hashCode() {
|
|
||||||
final int prime = 31;
|
|
||||||
int result = 1;
|
|
||||||
result = prime * result + ((genericArrayList == null) ? 0 : genericArrayList.hashCode());
|
|
||||||
result = prime * result + ((integerHashSet == null) ? 0 : integerHashSet.hashCode());
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean equals(Object obj) {
|
|
||||||
if (this == obj)
|
|
||||||
return true;
|
|
||||||
if (obj == null)
|
|
||||||
return false;
|
|
||||||
if (!(obj instanceof ComplexClass))
|
|
||||||
return false;
|
|
||||||
ComplexClass other = (ComplexClass) obj;
|
|
||||||
if (genericArrayList == null) {
|
|
||||||
if (other.genericArrayList != null)
|
|
||||||
return false;
|
|
||||||
} else if (!genericArrayList.equals(other.genericArrayList))
|
|
||||||
return false;
|
|
||||||
if (integerHashSet == null) {
|
|
||||||
if (other.integerHashSet != null)
|
|
||||||
return false;
|
|
||||||
} else if (!integerHashSet.equals(other.integerHashSet))
|
|
||||||
return false;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected ArrayList<?> getGenericArrayList() {
|
|
||||||
return genericArrayList;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected void setGenericArrayList(ArrayList<?> genericArrayList) {
|
|
||||||
this.genericArrayList = genericArrayList;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected HashSet<Integer> getIntegerHashSet() {
|
|
||||||
return integerHashSet;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected void setIntegerHashSet(HashSet<Integer> integerHashSet) {
|
|
||||||
this.integerHashSet = integerHashSet;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,6 +0,0 @@
|
|||||||
=========
|
|
||||||
|
|
||||||
## ElasticSearch
|
|
||||||
|
|
||||||
### Relevant Articles:
|
|
||||||
- [A Guide to ElasticSearch](http://www.baeldung.com/????????)
|
|
@ -1,35 +0,0 @@
|
|||||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
|
||||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
|
||||||
<modelVersion>4.0.0</modelVersion>
|
|
||||||
|
|
||||||
<groupId>com.baeldung</groupId>
|
|
||||||
<artifactId>elasticsearch</artifactId>
|
|
||||||
<version>0.0.1-SNAPSHOT</version>
|
|
||||||
<packaging>jar</packaging>
|
|
||||||
|
|
||||||
<name>elasticsearch</name>
|
|
||||||
<url>http://maven.apache.org</url>
|
|
||||||
|
|
||||||
<properties>
|
|
||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
|
||||||
</properties>
|
|
||||||
|
|
||||||
<dependencies>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.elasticsearch</groupId>
|
|
||||||
<artifactId>elasticsearch</artifactId>
|
|
||||||
<version>2.3.5</version>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>com.alibaba</groupId>
|
|
||||||
<artifactId>fastjson</artifactId>
|
|
||||||
<version>1.2.13</version>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>junit</groupId>
|
|
||||||
<artifactId>junit</artifactId>
|
|
||||||
<version>4.12</version>
|
|
||||||
<scope>test</scope>
|
|
||||||
</dependency>
|
|
||||||
</dependencies>
|
|
||||||
</project>
|
|
@ -22,24 +22,17 @@ public class FrontControllerServlet extends HttpServlet {
|
|||||||
|
|
||||||
private FrontCommand getCommand(HttpServletRequest request) {
|
private FrontCommand getCommand(HttpServletRequest request) {
|
||||||
try {
|
try {
|
||||||
return (FrontCommand) getCommandClass(request)
|
Class type = Class.forName(
|
||||||
.asSubclass(FrontCommand.class)
|
|
||||||
.newInstance();
|
|
||||||
} catch (Exception e) {
|
|
||||||
throw new RuntimeException("Failed to get command!", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private Class getCommandClass(HttpServletRequest request) {
|
|
||||||
try {
|
|
||||||
return Class.forName(
|
|
||||||
String.format(
|
String.format(
|
||||||
"com.baeldung.enterprise.patterns.front.controller.commands.%sCommand",
|
"com.baeldung.enterprise.patterns.front.controller.commands.%sCommand",
|
||||||
request.getParameter("command")
|
request.getParameter("command")
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
} catch (ClassNotFoundException e) {
|
return (FrontCommand) type
|
||||||
return UnknownCommand.class;
|
.asSubclass(FrontCommand.class)
|
||||||
|
.newInstance();
|
||||||
|
} catch (Exception e) {
|
||||||
|
return new UnknownCommand();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,40 +1,15 @@
|
|||||||
package com.baeldung.enterprise.patterns.front.controller.data;
|
package com.baeldung.enterprise.patterns.front.controller.data;
|
||||||
|
|
||||||
public class Book {
|
public interface Book {
|
||||||
private String author;
|
String getAuthor();
|
||||||
private String title;
|
|
||||||
private Double price;
|
|
||||||
|
|
||||||
public Book() {
|
void setAuthor(String author);
|
||||||
}
|
|
||||||
|
|
||||||
public Book(String author, String title, Double price) {
|
String getTitle();
|
||||||
this.author = author;
|
|
||||||
this.title = title;
|
|
||||||
this.price = price;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getAuthor() {
|
void setTitle(String title);
|
||||||
return author;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setAuthor(String author) {
|
Double getPrice();
|
||||||
this.author = author;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getTitle() {
|
void setPrice(Double price);
|
||||||
return title;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setTitle(String title) {
|
|
||||||
this.title = title;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Double getPrice() {
|
|
||||||
return price;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setPrice(Double price) {
|
|
||||||
this.price = price;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,46 @@
|
|||||||
|
package com.baeldung.enterprise.patterns.front.controller.data;
|
||||||
|
|
||||||
|
public class BookImpl implements Book {
|
||||||
|
private String author;
|
||||||
|
private String title;
|
||||||
|
private Double price;
|
||||||
|
|
||||||
|
public BookImpl() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public BookImpl(String author, String title, Double price) {
|
||||||
|
this.author = author;
|
||||||
|
this.title = title;
|
||||||
|
this.price = price;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getAuthor() {
|
||||||
|
return author;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setAuthor(String author) {
|
||||||
|
this.author = author;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getTitle() {
|
||||||
|
return title;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setTitle(String title) {
|
||||||
|
this.title = title;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Double getPrice() {
|
||||||
|
return price;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setPrice(Double price) {
|
||||||
|
this.price = price;
|
||||||
|
}
|
||||||
|
}
|
@ -3,8 +3,8 @@ package com.baeldung.enterprise.patterns.front.controller.data;
|
|||||||
public interface Bookshelf {
|
public interface Bookshelf {
|
||||||
|
|
||||||
default void init() {
|
default void init() {
|
||||||
add(new Book("Wilson, Robert Anton & Shea, Robert", "Illuminati", 9.99));
|
add(new BookImpl("Wilson, Robert Anton & Shea, Robert", "Illuminati", 9.99));
|
||||||
add(new Book("Fowler, Martin", "Patterns of Enterprise Application Architecture", 27.88));
|
add(new BookImpl("Fowler, Martin", "Patterns of Enterprise Application Architecture", 27.88));
|
||||||
}
|
}
|
||||||
|
|
||||||
Bookshelf getInstance();
|
Bookshelf getInstance();
|
||||||
|
Binary file not shown.
Before Width: | Height: | Size: 9.3 KiB After Width: | Height: | Size: 5.2 KiB |
@ -1,5 +1,5 @@
|
|||||||
@startuml
|
@startuml
|
||||||
|
scale 1.5
|
||||||
class Handler {
|
class Handler {
|
||||||
doGet
|
doGet
|
||||||
doPost
|
doPost
|
||||||
|
@ -15,7 +15,13 @@
|
|||||||
<artifactId>log4j</artifactId>
|
<artifactId>log4j</artifactId>
|
||||||
<version>1.2.17</version>
|
<version>1.2.17</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<!-- SLF4J - Log4J dependency
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.slf4j</groupId>
|
||||||
|
<artifactId>slf4j-log4j12</artifactId>
|
||||||
|
<version>1.7.21</version>
|
||||||
|
</dependency>
|
||||||
|
-->
|
||||||
|
|
||||||
<!--log4j2 dependencies-->
|
<!--log4j2 dependencies-->
|
||||||
<dependency>
|
<dependency>
|
||||||
@ -28,22 +34,34 @@
|
|||||||
<artifactId>log4j-core</artifactId>
|
<artifactId>log4j-core</artifactId>
|
||||||
<version>2.6</version>
|
<version>2.6</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<!--disruptor for log4j2 async logging-->
|
||||||
<!--disruptor for log4j2 async logging-->
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.lmax</groupId>
|
<groupId>com.lmax</groupId>
|
||||||
<artifactId>disruptor</artifactId>
|
<artifactId>disruptor</artifactId>
|
||||||
<version>3.3.4</version>
|
<version>3.3.4</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<!-- SLF4J - Log4J2 dependency
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.logging.log4j</groupId>
|
||||||
|
<artifactId>log4j-slf4j-impl</artifactId>
|
||||||
|
<version>2.6.2</version>
|
||||||
|
</dependency>
|
||||||
|
-->
|
||||||
|
|
||||||
<!--logback dependencies-->
|
<!--logback dependencies-->
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>ch.qos.logback</groupId>
|
<groupId>ch.qos.logback</groupId>
|
||||||
<artifactId>logback-classic</artifactId>
|
<artifactId>logback-classic</artifactId>
|
||||||
<version>1.1.7</version>
|
<version>1.1.7</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<!-- SLF4J - JCL dependency
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.slf4j</groupId>
|
||||||
|
<artifactId>slf4j-jcl</artifactId>
|
||||||
|
<version>1.7.21</version>
|
||||||
|
</dependency>
|
||||||
|
-->
|
||||||
</dependencies>
|
</dependencies>
|
||||||
<build>
|
<build>
|
||||||
<plugins>
|
<plugins>
|
||||||
|
20
log4j/src/main/java/com/baeldung/slf4j/Slf4jExample.java
Normal file
20
log4j/src/main/java/com/baeldung/slf4j/Slf4jExample.java
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
package com.baeldung.slf4j;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* To switch between logging frameworks you need only to uncomment needed framework dependencies in pom.xml
|
||||||
|
*/
|
||||||
|
public class Slf4jExample {
|
||||||
|
private static Logger logger = LoggerFactory.getLogger(Slf4jExample.class);
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
logger.debug("Debug log message");
|
||||||
|
logger.info("Info log message");
|
||||||
|
logger.error("Error log message");
|
||||||
|
|
||||||
|
String variable = "Hello John";
|
||||||
|
logger.debug("Printing variable value {} ", variable);
|
||||||
|
}
|
||||||
|
}
|
1
pom.xml
1
pom.xml
@ -91,7 +91,6 @@
|
|||||||
<module>spring-hibernate3</module>
|
<module>spring-hibernate3</module>
|
||||||
<module>spring-hibernate4</module>
|
<module>spring-hibernate4</module>
|
||||||
<module>spring-jpa</module>
|
<module>spring-jpa</module>
|
||||||
<module>spring-jpa-jndi</module>
|
|
||||||
<module>spring-katharsis</module>
|
<module>spring-katharsis</module>
|
||||||
<module>spring-mockito</module>
|
<module>spring-mockito</module>
|
||||||
<module>spring-mvc-java</module>
|
<module>spring-mvc-java</module>
|
||||||
|
36
selenium-junit-testng/pom.xml
Normal file
36
selenium-junit-testng/pom.xml
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<groupId>com.baeldung</groupId>
|
||||||
|
<artifactId>selenium-junit-testng</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
<build>
|
||||||
|
<sourceDirectory>src</sourceDirectory>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<version>3.1</version>
|
||||||
|
<configuration>
|
||||||
|
<source>1.8</source>
|
||||||
|
<target>1.8</target>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.seleniumhq.selenium</groupId>
|
||||||
|
<artifactId>selenium-java</artifactId>
|
||||||
|
<version>2.53.1</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>junit</groupId>
|
||||||
|
<artifactId>junit</artifactId>
|
||||||
|
<version>4.8.1</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.testng</groupId>
|
||||||
|
<artifactId>testng</artifactId>
|
||||||
|
<version>6.9.10</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
</project>
|
@ -0,0 +1,29 @@
|
|||||||
|
package main.java.com.baeldung.selenium;
|
||||||
|
|
||||||
|
import org.openqa.selenium.WebDriver;
|
||||||
|
import org.openqa.selenium.firefox.FirefoxDriver;
|
||||||
|
|
||||||
|
public class SeleniumExample {
|
||||||
|
|
||||||
|
private WebDriver webDriver;
|
||||||
|
private final String url = "http://www.baeldung.com/";
|
||||||
|
private final String expectedTitle = "Baeldung | Java, Spring and Web Development tutorials";
|
||||||
|
|
||||||
|
public SeleniumExample() {
|
||||||
|
webDriver = new FirefoxDriver();
|
||||||
|
webDriver.get(url);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void closeWindow() {
|
||||||
|
webDriver.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getActualTitle() {
|
||||||
|
return webDriver.getTitle();
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getExpectedTitle() {
|
||||||
|
return expectedTitle;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,30 @@
|
|||||||
|
package test.java.com.baeldung.selenium.junit;
|
||||||
|
|
||||||
|
import static org.testng.Assert.assertEquals;
|
||||||
|
import main.java.com.baeldung.selenium.SeleniumExample;
|
||||||
|
|
||||||
|
import org.junit.After;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class TestSeleniumWithJUnit {
|
||||||
|
|
||||||
|
private SeleniumExample seleniumExample;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setUp() {
|
||||||
|
seleniumExample = new SeleniumExample();
|
||||||
|
}
|
||||||
|
|
||||||
|
@After
|
||||||
|
public void tearDown() {
|
||||||
|
seleniumExample.closeWindow();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenPageIsLoaded_thenTitleIsAsPerExpectation() {
|
||||||
|
String expectedTitle = seleniumExample.getExpectedTitle();
|
||||||
|
String actualTitle = seleniumExample.getActualTitle();
|
||||||
|
assertEquals(actualTitle, expectedTitle);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,30 @@
|
|||||||
|
package test.java.com.baeldung.selenium.testng;
|
||||||
|
|
||||||
|
import static org.testng.Assert.assertEquals;
|
||||||
|
import main.java.com.baeldung.selenium.SeleniumExample;
|
||||||
|
|
||||||
|
import org.testng.annotations.AfterSuite;
|
||||||
|
import org.testng.annotations.BeforeSuite;
|
||||||
|
import org.testng.annotations.Test;
|
||||||
|
|
||||||
|
public class TestSeleniumWithTestNG {
|
||||||
|
|
||||||
|
private SeleniumExample seleniumExample;
|
||||||
|
|
||||||
|
@BeforeSuite
|
||||||
|
public void setUp() {
|
||||||
|
seleniumExample = new SeleniumExample();
|
||||||
|
}
|
||||||
|
|
||||||
|
@AfterSuite
|
||||||
|
public void tearDown() {
|
||||||
|
seleniumExample.closeWindow();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenPageIsLoaded_thenTitleIsAsPerExpectation() {
|
||||||
|
String expectedTitle = seleniumExample.getExpectedTitle();
|
||||||
|
String actualTitle = seleniumExample.getActualTitle();
|
||||||
|
assertEquals(actualTitle, expectedTitle);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,34 @@
|
|||||||
|
package com.baeldun.selenium.testng;
|
||||||
|
import static org.testng.Assert.assertEquals;
|
||||||
|
import static org.testng.Assert.assertNotNull;
|
||||||
|
|
||||||
|
import org.openqa.selenium.WebDriver;
|
||||||
|
import org.openqa.selenium.firefox.FirefoxDriver;
|
||||||
|
import org.testng.annotations.AfterSuite;
|
||||||
|
import org.testng.annotations.BeforeSuite;
|
||||||
|
import org.testng.annotations.Test;
|
||||||
|
|
||||||
|
public class TestSeleniumWithTestNG {
|
||||||
|
|
||||||
|
private WebDriver webDriver;
|
||||||
|
private final String url = "http://www.baeldung.com/";
|
||||||
|
private final String expectedTitle = "Baeldung | Java, Spring and Web Development tutorials";
|
||||||
|
|
||||||
|
@BeforeSuite
|
||||||
|
public void setUp() {
|
||||||
|
webDriver = new FirefoxDriver();
|
||||||
|
webDriver.get(url);
|
||||||
|
}
|
||||||
|
|
||||||
|
@AfterSuite
|
||||||
|
public void tearDown() {
|
||||||
|
webDriver.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenPageIsLoaded_thenTitleIsAsPerExpectation() {
|
||||||
|
String actualTitleReturned = webDriver.getTitle();
|
||||||
|
assertNotNull(actualTitleReturned);
|
||||||
|
assertEquals(expectedTitle, actualTitleReturned);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,34 @@
|
|||||||
|
package com.baeldung.selenium.junit;
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertNotNull;
|
||||||
|
|
||||||
|
import org.junit.After;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.openqa.selenium.WebDriver;
|
||||||
|
import org.openqa.selenium.firefox.FirefoxDriver;
|
||||||
|
|
||||||
|
public class TestSeleniumWithJUnit {
|
||||||
|
|
||||||
|
private WebDriver webDriver;
|
||||||
|
private final String url = "http://www.baeldung.com/";
|
||||||
|
private final String expectedTitle = "Baeldung | Java, Spring and Web Development tutorials";
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setUp() {
|
||||||
|
webDriver = new FirefoxDriver();
|
||||||
|
webDriver.get(url);
|
||||||
|
}
|
||||||
|
|
||||||
|
@After
|
||||||
|
public void tearDown() {
|
||||||
|
webDriver.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenPageIsLoaded_thenTitleIsAsPerExpectation() {
|
||||||
|
String actualTitleReturned = webDriver.getTitle();
|
||||||
|
assertNotNull(actualTitleReturned);
|
||||||
|
assertEquals(expectedTitle, actualTitleReturned);
|
||||||
|
}
|
||||||
|
}
|
27
spring-cloud-config/README.md
Normal file
27
spring-cloud-config/README.md
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
## Spring Cloud Config ##
|
||||||
|
|
||||||
|
To get this example working, you have to initialize a new *Git* repository in
|
||||||
|
the ```client-config``` directory first *and* you have to set the environment variable
|
||||||
|
```CONFIG_REPO``` to an absolute path of that directory.
|
||||||
|
|
||||||
|
```
|
||||||
|
$> cd client-config
|
||||||
|
$> git init
|
||||||
|
$> git add .
|
||||||
|
$> git commit -m 'Initial commit'
|
||||||
|
$> export CONFIG_REPO=$(pwd)
|
||||||
|
```
|
||||||
|
|
||||||
|
Then you're able to run the examples with ```mvn install spring-boot:run```.
|
||||||
|
|
||||||
|
### Docker ###
|
||||||
|
|
||||||
|
To get the *Docker* examples working, you have to repackage the ```spring-cloud-config-server```
|
||||||
|
and ```spring-cloud-config-client``` modules first:
|
||||||
|
|
||||||
|
```
|
||||||
|
$> mvn install spring-boot:repackage
|
||||||
|
```
|
||||||
|
|
||||||
|
Don't forget to download the *Java JCE* package from
|
||||||
|
(Oracle)[http://www.oracle.com/technetwork/java/javase/downloads/jce8-download-2133166.html].
|
6
spring-cloud-config/docker/Dockerfile.client
Normal file
6
spring-cloud-config/docker/Dockerfile.client
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
FROM alpine-java:base
|
||||||
|
MAINTAINER baeldung.com
|
||||||
|
RUN apk --no-cache add netcat-openbsd
|
||||||
|
COPY files/spring-cloud-config-client-1.0.0-SNAPSHOT.jar /opt/spring-cloud/lib/config-client.jar
|
||||||
|
COPY files/config-client-entrypoint.sh /opt/spring-cloud/bin/
|
||||||
|
RUN chmod 755 /opt/spring-cloud/bin/config-client-entrypoint.sh
|
9
spring-cloud-config/docker/Dockerfile.server
Normal file
9
spring-cloud-config/docker/Dockerfile.server
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
FROM alpine-java:base
|
||||||
|
MAINTAINER baeldung.com
|
||||||
|
COPY files/spring-cloud-config-server-1.0.0-SNAPSHOT.jar /opt/spring-cloud/lib/config-server.jar
|
||||||
|
ENV SPRING_APPLICATION_JSON='{"spring": {"cloud": {"config": {"server": \
|
||||||
|
{"git": {"uri": "/var/lib/spring-cloud/config-repo", "clone-on-start": true}}}}}}'
|
||||||
|
ENTRYPOINT ["/usr/bin/java"]
|
||||||
|
CMD ["-jar", "/opt/spring-cloud/lib/config-server.jar"]
|
||||||
|
VOLUME /var/lib/spring-cloud/config-repo
|
||||||
|
EXPOSE 8888
|
2
spring-cloud-config/docker/files/.gitignore
vendored
Normal file
2
spring-cloud-config/docker/files/.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
/UnlimitedJCEPolicyJDK8
|
||||||
|
/*.jar
|
@ -0,0 +1,8 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
|
||||||
|
while ! nc -z config-server 8888 ; do
|
||||||
|
echo "Waiting for upcoming Config Server"
|
||||||
|
sleep 2
|
||||||
|
done
|
||||||
|
|
||||||
|
java -jar /opt/spring-cloud/lib/config-client.jar
|
52
spring-cloud-config/pom.xml
Normal file
52
spring-cloud-config/pom.xml
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<groupId>com.baeldung.spring.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-config</artifactId>
|
||||||
|
<packaging>pom</packaging>
|
||||||
|
|
||||||
|
<modules>
|
||||||
|
<module>spring-cloud-config-server</module>
|
||||||
|
<module>spring-cloud-config-client</module>
|
||||||
|
</modules>
|
||||||
|
|
||||||
|
<parent>
|
||||||
|
<groupId>com.baeldung</groupId>
|
||||||
|
<artifactId>parent-modules</artifactId>
|
||||||
|
<version>1.0.0-SNAPSHOT</version>
|
||||||
|
<relativePath>..</relativePath>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<dependencyManagement>
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-parent</artifactId>
|
||||||
|
<version>1.4.0.RELEASE</version>
|
||||||
|
<type>pom</type>
|
||||||
|
<scope>import</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-dependencies</artifactId>
|
||||||
|
<version>Brixton.SR4</version>
|
||||||
|
<type>pom</type>
|
||||||
|
<scope>import</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
</dependencyManagement>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<pluginManagement>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||||
|
<version>1.4.0.RELEASE</version>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</pluginManagement>
|
||||||
|
</build>
|
||||||
|
</project>
|
@ -69,6 +69,17 @@
|
|||||||
<artifactId>log4j-over-slf4j</artifactId>
|
<artifactId>log4j-over-slf4j</artifactId>
|
||||||
<version>${org.slf4j.version}</version>
|
<version>${org.slf4j.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.elasticsearch</groupId>
|
||||||
|
<artifactId>elasticsearch</artifactId>
|
||||||
|
<version>2.3.5</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.alibaba</groupId>
|
||||||
|
<artifactId>fastjson</artifactId>
|
||||||
|
<version>1.2.13</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
<build>
|
<build>
|
||||||
<plugins>
|
<plugins>
|
||||||
|
@ -1,52 +1,52 @@
|
|||||||
package com.baeldung.elasticsearch;
|
package com.baeldung.elasticsearch;
|
||||||
|
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
|
||||||
public class Person {
|
public class Person {
|
||||||
|
|
||||||
private int age;
|
private int age;
|
||||||
|
|
||||||
private String fullName;
|
private String fullName;
|
||||||
|
|
||||||
private Date dateOfBirth;
|
private Date dateOfBirth;
|
||||||
|
|
||||||
public Person() {
|
public Person() {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Person(int age, String fullName, Date dateOfBirth) {
|
public Person(int age, String fullName, Date dateOfBirth) {
|
||||||
super();
|
super();
|
||||||
this.age = age;
|
this.age = age;
|
||||||
this.fullName = fullName;
|
this.fullName = fullName;
|
||||||
this.dateOfBirth = dateOfBirth;
|
this.dateOfBirth = dateOfBirth;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getAge() {
|
public int getAge() {
|
||||||
return age;
|
return age;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setAge(int age) {
|
public void setAge(int age) {
|
||||||
this.age = age;
|
this.age = age;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getFullName() {
|
public String getFullName() {
|
||||||
return fullName;
|
return fullName;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setFullName(String fullName) {
|
public void setFullName(String fullName) {
|
||||||
this.fullName = fullName;
|
this.fullName = fullName;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Date getDateOfBirth() {
|
public Date getDateOfBirth() {
|
||||||
return dateOfBirth;
|
return dateOfBirth;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setDateOfBirth(Date dateOfBirth) {
|
public void setDateOfBirth(Date dateOfBirth) {
|
||||||
this.dateOfBirth = dateOfBirth;
|
this.dateOfBirth = dateOfBirth;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "Person [age=" + age + ", fullName=" + fullName + ", dateOfBirth=" + dateOfBirth + "]";
|
return "Person [age=" + age + ", fullName=" + fullName + ", dateOfBirth=" + dateOfBirth + "]";
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -5,6 +5,7 @@ import static org.junit.Assert.*;
|
|||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@ -102,13 +103,9 @@ public class ElasticSearchUnitTests {
|
|||||||
try {
|
try {
|
||||||
response2.getHits();
|
response2.getHits();
|
||||||
response3.getHits();
|
response3.getHits();
|
||||||
SearchHit[] searchHits = response.getHits().getHits();
|
List<SearchHit> searchHits = Arrays.asList(response.getHits().getHits());
|
||||||
List<Person> results = new ArrayList<Person>();
|
final List<Person> results = new ArrayList<Person>();
|
||||||
for (SearchHit hit : searchHits) {
|
searchHits.forEach(hit -> results.add(JSON.parseObject(hit.getSourceAsString(), Person.class)));
|
||||||
String sourceAsString = hit.getSourceAsString();
|
|
||||||
Person person = JSON.parseObject(sourceAsString, Person.class);
|
|
||||||
results.add(person);
|
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
isExecutedSuccessfully = false;
|
isExecutedSuccessfully = false;
|
||||||
}
|
}
|
@ -1,7 +1,7 @@
|
|||||||
## Spring Data Neo4j
|
## Spring Data Neo4j
|
||||||
|
|
||||||
### Relevant Articles:
|
### Relevant Articles:
|
||||||
- [Introduction to Spring Data Neo4j](http://www.baeldung.com/spring-data-neo4j-tutorial)
|
- [Introduction to Spring Data Neo4j](http://www.baeldung.com/spring-data-neo4j-intro)
|
||||||
|
|
||||||
### Build the Project with Tests Running
|
### Build the Project with Tests Running
|
||||||
```
|
```
|
||||||
|
13
spring-jpa-jndi/.gitignore
vendored
13
spring-jpa-jndi/.gitignore
vendored
@ -1,13 +0,0 @@
|
|||||||
*.class
|
|
||||||
|
|
||||||
#folders#
|
|
||||||
/target
|
|
||||||
/neoDb*
|
|
||||||
/data
|
|
||||||
/src/main/webapp/WEB-INF/classes
|
|
||||||
*/META-INF/*
|
|
||||||
|
|
||||||
# Packaged files #
|
|
||||||
*.jar
|
|
||||||
*.war
|
|
||||||
*.ear
|
|
@ -1,7 +0,0 @@
|
|||||||
=========
|
|
||||||
|
|
||||||
## Spring JPA using JNDI Project
|
|
||||||
|
|
||||||
|
|
||||||
### Relevant Articles:
|
|
||||||
- [Spring Persistence (Hibernate and JPA) with a JNDI datasource](http://www.baeldung.com/spring-jpa-fndi)
|
|
@ -1,145 +0,0 @@
|
|||||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
|
||||||
<modelVersion>4.0.0</modelVersion>
|
|
||||||
|
|
||||||
<groupId>com.baeldung</groupId>
|
|
||||||
<artifactId>spring-jpa-jndi</artifactId>
|
|
||||||
<version>0.1-SNAPSHOT</version>
|
|
||||||
<packaging>war</packaging>
|
|
||||||
|
|
||||||
<name>spring-jpa-jndi</name>
|
|
||||||
|
|
||||||
<dependencies>
|
|
||||||
|
|
||||||
<!-- Spring -->
|
|
||||||
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework</groupId>
|
|
||||||
<artifactId>spring-orm</artifactId>
|
|
||||||
<version>${org.springframework.version}</version>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework</groupId>
|
|
||||||
<artifactId>spring-context</artifactId>
|
|
||||||
<version>${org.springframework.version}</version>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework</groupId>
|
|
||||||
<artifactId>spring-webmvc</artifactId>
|
|
||||||
<version>${org.springframework.version}</version>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<!-- web -->
|
|
||||||
<dependency>
|
|
||||||
<groupId>javax.servlet</groupId>
|
|
||||||
<artifactId>jstl</artifactId>
|
|
||||||
<version>${javax.servlet.jstl.version}</version>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>javax.servlet</groupId>
|
|
||||||
<artifactId>servlet-api</artifactId>
|
|
||||||
<version>${javax.servlet.servlet-api.version}</version>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<!-- persistence -->
|
|
||||||
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.hibernate</groupId>
|
|
||||||
<artifactId>hibernate-entitymanager</artifactId>
|
|
||||||
<version>${hibernate.version}</version>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>xml-apis</groupId>
|
|
||||||
<artifactId>xml-apis</artifactId>
|
|
||||||
<version>1.4.01</version>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.javassist</groupId>
|
|
||||||
<artifactId>javassist</artifactId>
|
|
||||||
<version>${javassist.version}</version>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.springframework.data</groupId>
|
|
||||||
<artifactId>spring-data-jpa</artifactId>
|
|
||||||
<version>${spring-data-jpa.version}</version>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<!-- validation -->
|
|
||||||
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.hibernate</groupId>
|
|
||||||
<artifactId>hibernate-validator</artifactId>
|
|
||||||
<version>${hibernate-validator.version}</version>
|
|
||||||
</dependency>
|
|
||||||
<dependency>
|
|
||||||
<groupId>javax.el</groupId>
|
|
||||||
<artifactId>javax.el-api</artifactId>
|
|
||||||
<version>2.2.5</version>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
</dependencies>
|
|
||||||
|
|
||||||
<build>
|
|
||||||
<finalName>spring-jpa-jndi</finalName>
|
|
||||||
<resources>
|
|
||||||
<resource>
|
|
||||||
<directory>src/main/resources</directory>
|
|
||||||
<filtering>true</filtering>
|
|
||||||
</resource>
|
|
||||||
</resources>
|
|
||||||
|
|
||||||
<plugins>
|
|
||||||
|
|
||||||
<plugin>
|
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
|
||||||
<artifactId>maven-compiler-plugin</artifactId>
|
|
||||||
<version>${maven-compiler-plugin.version}</version>
|
|
||||||
<configuration>
|
|
||||||
<source>1.8</source>
|
|
||||||
<target>1.8</target>
|
|
||||||
</configuration>
|
|
||||||
</plugin>
|
|
||||||
|
|
||||||
<plugin>
|
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
|
||||||
<artifactId>maven-war-plugin</artifactId>
|
|
||||||
<version>${maven-war-plugin.version}</version>
|
|
||||||
<configuration>
|
|
||||||
<warSourceDirectory>src/main/webapp</warSourceDirectory>
|
|
||||||
<failOnMissingWebXml>false</failOnMissingWebXml>
|
|
||||||
</configuration>
|
|
||||||
</plugin>
|
|
||||||
|
|
||||||
</plugins>
|
|
||||||
|
|
||||||
</build>
|
|
||||||
|
|
||||||
<properties>
|
|
||||||
<!-- Spring -->
|
|
||||||
<org.springframework.version>4.3.2.RELEASE</org.springframework.version>
|
|
||||||
<javassist.version>3.20.0-GA</javassist.version>
|
|
||||||
|
|
||||||
<!-- web -->
|
|
||||||
<javax.servlet.jstl.version>1.2</javax.servlet.jstl.version>
|
|
||||||
<javax.servlet.servlet-api.version>2.5</javax.servlet.servlet-api.version>
|
|
||||||
|
|
||||||
<!-- persistence -->
|
|
||||||
<hibernate.version>4.3.11.Final</hibernate.version>
|
|
||||||
<spring-data-jpa.version>1.8.2.RELEASE</spring-data-jpa.version>
|
|
||||||
<h2.version>1.4.192</h2.version>
|
|
||||||
|
|
||||||
<!-- logging -->
|
|
||||||
<org.slf4j.version>1.7.13</org.slf4j.version>
|
|
||||||
<logback.version>1.1.3</logback.version>
|
|
||||||
|
|
||||||
<!-- various -->
|
|
||||||
<hibernate-validator.version>5.2.2.Final</hibernate-validator.version>
|
|
||||||
|
|
||||||
<!-- maven plugins -->
|
|
||||||
<maven-compiler-plugin.version>3.5.1</maven-compiler-plugin.version>
|
|
||||||
<maven-resources-plugin.version>2.7</maven-resources-plugin.version>
|
|
||||||
<maven-war-plugin.version>2.4</maven-war-plugin.version>
|
|
||||||
<!-- <maven-war-plugin.version>2.6</maven-war-plugin.version> -->
|
|
||||||
|
|
||||||
</properties>
|
|
||||||
|
|
||||||
</project>
|
|
@ -1,22 +0,0 @@
|
|||||||
package org.baeldung.persistence.dao;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import javax.persistence.EntityManager;
|
|
||||||
import javax.persistence.PersistenceContext;
|
|
||||||
|
|
||||||
import org.baeldung.persistence.model.Foo;
|
|
||||||
import org.springframework.stereotype.Repository;
|
|
||||||
|
|
||||||
@Repository
|
|
||||||
public class FooDao {
|
|
||||||
|
|
||||||
@PersistenceContext
|
|
||||||
private EntityManager entityManager;
|
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
public List<Foo> findAll() {
|
|
||||||
return entityManager.createQuery("from " + Foo.class.getName()).getResultList();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,34 +0,0 @@
|
|||||||
package org.baeldung.persistence.model;
|
|
||||||
|
|
||||||
import javax.persistence.Column;
|
|
||||||
import javax.persistence.Entity;
|
|
||||||
import javax.persistence.GeneratedValue;
|
|
||||||
import javax.persistence.GenerationType;
|
|
||||||
import javax.persistence.Id;
|
|
||||||
|
|
||||||
@Entity
|
|
||||||
public class Foo {
|
|
||||||
|
|
||||||
@Id
|
|
||||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
|
||||||
@Column(name = "ID")
|
|
||||||
private long id;
|
|
||||||
@Column(name = "NAME")
|
|
||||||
private String name;
|
|
||||||
|
|
||||||
public long getId() {
|
|
||||||
return id;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setId(final int id) {
|
|
||||||
this.id = id;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getName() {
|
|
||||||
return name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setName(final String name) {
|
|
||||||
this.name = name;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,22 +0,0 @@
|
|||||||
package org.baeldung.persistence.service;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import org.baeldung.persistence.dao.FooDao;
|
|
||||||
import org.baeldung.persistence.model.Foo;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.stereotype.Service;
|
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
|
||||||
|
|
||||||
@Service
|
|
||||||
@Transactional
|
|
||||||
public class FooService {
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
private FooDao dao;
|
|
||||||
|
|
||||||
public List<Foo> findAll() {
|
|
||||||
return dao.findAll();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,20 +0,0 @@
|
|||||||
<configuration>
|
|
||||||
|
|
||||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
|
||||||
<encoder>
|
|
||||||
<pattern>web - %date [%thread] %-5level %logger{36} - %message%n
|
|
||||||
</pattern>
|
|
||||||
</encoder>
|
|
||||||
</appender>
|
|
||||||
|
|
||||||
<logger name="org.springframework" level="WARN" />
|
|
||||||
<logger name="org.springframework.transaction" level="WARN" />
|
|
||||||
|
|
||||||
<!-- in order to debug some marshalling issues, this needs to be TRACE -->
|
|
||||||
<logger name="org.springframework.web.servlet.mvc" level="WARN" />
|
|
||||||
|
|
||||||
<root level="INFO">
|
|
||||||
<appender-ref ref="STDOUT" />
|
|
||||||
</root>
|
|
||||||
|
|
||||||
</configuration>
|
|
@ -4,6 +4,7 @@
|
|||||||
<groupId>com.baeldung</groupId>
|
<groupId>com.baeldung</groupId>
|
||||||
<artifactId>spring-jpa</artifactId>
|
<artifactId>spring-jpa</artifactId>
|
||||||
<version>0.1-SNAPSHOT</version>
|
<version>0.1-SNAPSHOT</version>
|
||||||
|
<packaging>war</packaging>
|
||||||
|
|
||||||
<name>spring-jpa</name>
|
<name>spring-jpa</name>
|
||||||
|
|
||||||
@ -21,6 +22,11 @@
|
|||||||
<artifactId>spring-context</artifactId>
|
<artifactId>spring-context</artifactId>
|
||||||
<version>${org.springframework.version}</version>
|
<version>${org.springframework.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework</groupId>
|
||||||
|
<artifactId>spring-webmvc</artifactId>
|
||||||
|
<version>${org.springframework.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<!-- persistence -->
|
<!-- persistence -->
|
||||||
|
|
||||||
@ -73,6 +79,18 @@
|
|||||||
<artifactId>javax.el-api</artifactId>
|
<artifactId>javax.el-api</artifactId>
|
||||||
<version>2.2.5</version>
|
<version>2.2.5</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<!-- web -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>javax.servlet</groupId>
|
||||||
|
<artifactId>jstl</artifactId>
|
||||||
|
<version>${javax.servlet.jstl.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>javax.servlet</groupId>
|
||||||
|
<artifactId>servlet-api</artifactId>
|
||||||
|
<version>${javax.servlet.servlet-api.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<!-- utils -->
|
<!-- utils -->
|
||||||
|
|
||||||
@ -147,6 +165,16 @@
|
|||||||
<target>1.8</target>
|
<target>1.8</target>
|
||||||
</configuration>
|
</configuration>
|
||||||
</plugin>
|
</plugin>
|
||||||
|
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-war-plugin</artifactId>
|
||||||
|
<version>${maven-war-plugin.version}</version>
|
||||||
|
<configuration>
|
||||||
|
<warSourceDirectory>src/main/webapp</warSourceDirectory>
|
||||||
|
<failOnMissingWebXml>false</failOnMissingWebXml>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
@ -197,6 +225,10 @@
|
|||||||
<mysql-connector-java.version>5.1.38</mysql-connector-java.version>
|
<mysql-connector-java.version>5.1.38</mysql-connector-java.version>
|
||||||
<spring-data-jpa.version>1.10.2.RELEASE</spring-data-jpa.version>
|
<spring-data-jpa.version>1.10.2.RELEASE</spring-data-jpa.version>
|
||||||
<h2.version>1.4.192</h2.version>
|
<h2.version>1.4.192</h2.version>
|
||||||
|
|
||||||
|
<!-- web -->
|
||||||
|
<javax.servlet.jstl.version>1.2</javax.servlet.jstl.version>
|
||||||
|
<javax.servlet.servlet-api.version>2.5</javax.servlet.servlet-api.version>
|
||||||
|
|
||||||
<!-- logging -->
|
<!-- logging -->
|
||||||
<org.slf4j.version>1.7.13</org.slf4j.version>
|
<org.slf4j.version>1.7.13</org.slf4j.version>
|
||||||
@ -224,6 +256,7 @@
|
|||||||
<maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version>
|
<maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version>
|
||||||
<maven-resources-plugin.version>2.7</maven-resources-plugin.version>
|
<maven-resources-plugin.version>2.7</maven-resources-plugin.version>
|
||||||
<cargo-maven2-plugin.version>1.4.18</cargo-maven2-plugin.version>
|
<cargo-maven2-plugin.version>1.4.18</cargo-maven2-plugin.version>
|
||||||
|
<maven-war-plugin.version>2.4</maven-war-plugin.version>
|
||||||
<!-- <maven-war-plugin.version>2.6</maven-war-plugin.version> -->
|
<!-- <maven-war-plugin.version>2.6</maven-war-plugin.version> -->
|
||||||
|
|
||||||
</properties>
|
</properties>
|
||||||
|
@ -36,7 +36,7 @@ public class PersistenceJNDIConfig {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
|
public LocalContainerEntityManagerFactoryBean entityManagerFactory() throws NamingException {
|
||||||
final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
|
final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
|
||||||
em.setDataSource(dataSource());
|
em.setDataSource(dataSource());
|
||||||
em.setPackagesToScan(new String[] { "org.baeldung.persistence.model" });
|
em.setPackagesToScan(new String[] { "org.baeldung.persistence.model" });
|
||||||
@ -46,12 +46,8 @@ public class PersistenceJNDIConfig {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public DataSource dataSource() {
|
public DataSource dataSource() throws NamingException {
|
||||||
try {
|
return (DataSource) new JndiTemplate().lookup(env.getProperty("jdbc.url"));
|
||||||
return (DataSource) new JndiTemplate().lookup(env.getProperty("jdbc.url"));
|
|
||||||
} catch (NamingException e) {
|
|
||||||
throw new IllegalArgumentException("Error looking up JNDI datasource", e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
@ -182,7 +182,6 @@
|
|||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
<artifactId>maven-compiler-plugin</artifactId>
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
<version>${maven-compiler-plugin.version}</version>
|
|
||||||
<configuration>
|
<configuration>
|
||||||
<source>1.8</source>
|
<source>1.8</source>
|
||||||
<target>1.8</target>
|
<target>1.8</target>
|
||||||
@ -192,16 +191,14 @@
|
|||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
<artifactId>maven-war-plugin</artifactId>
|
<artifactId>maven-war-plugin</artifactId>
|
||||||
<version>${maven-war-plugin.version}</version>
|
|
||||||
</plugin>
|
</plugin>
|
||||||
|
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
<artifactId>maven-surefire-plugin</artifactId>
|
<artifactId>maven-surefire-plugin</artifactId>
|
||||||
<version>${maven-surefire-plugin.version}</version>
|
|
||||||
<configuration>
|
<configuration>
|
||||||
<excludes>
|
<excludes>
|
||||||
<!-- <exclude>**/*ProductionTest.java</exclude> -->
|
<exclude>**/*LiveTest.java</exclude>
|
||||||
</excludes>
|
</excludes>
|
||||||
<systemPropertyVariables>
|
<systemPropertyVariables>
|
||||||
<!-- <provPersistenceTarget>h2</provPersistenceTarget> -->
|
<!-- <provPersistenceTarget>h2</provPersistenceTarget> -->
|
||||||
@ -216,7 +213,7 @@
|
|||||||
<configuration>
|
<configuration>
|
||||||
<wait>true</wait>
|
<wait>true</wait>
|
||||||
<container>
|
<container>
|
||||||
<containerId>jetty8x</containerId>
|
<containerId>tomcat8x</containerId>
|
||||||
<type>embedded</type>
|
<type>embedded</type>
|
||||||
<systemProperties>
|
<systemProperties>
|
||||||
<!-- <provPersistenceTarget>cargo</provPersistenceTarget> -->
|
<!-- <provPersistenceTarget>cargo</provPersistenceTarget> -->
|
||||||
@ -234,6 +231,61 @@
|
|||||||
|
|
||||||
</build>
|
</build>
|
||||||
|
|
||||||
|
<profiles>
|
||||||
|
<profile>
|
||||||
|
<id>live</id>
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.codehaus.cargo</groupId>
|
||||||
|
<artifactId>cargo-maven2-plugin</artifactId>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<id>start-server</id>
|
||||||
|
<phase>pre-integration-test</phase>
|
||||||
|
<goals>
|
||||||
|
<goal>start</goal>
|
||||||
|
</goals>
|
||||||
|
</execution>
|
||||||
|
<execution>
|
||||||
|
<id>stop-server</id>
|
||||||
|
<phase>post-integration-test</phase>
|
||||||
|
<goals>
|
||||||
|
<goal>stop</goal>
|
||||||
|
</goals>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-surefire-plugin</artifactId>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<phase>integration-test</phase>
|
||||||
|
<goals>
|
||||||
|
<goal>test</goal>
|
||||||
|
</goals>
|
||||||
|
<configuration>
|
||||||
|
<excludes>
|
||||||
|
<exclude>none</exclude>
|
||||||
|
</excludes>
|
||||||
|
<includes>
|
||||||
|
<include>**/*LiveTest.java</include>
|
||||||
|
</includes>
|
||||||
|
<systemPropertyVariables>
|
||||||
|
<webTarget>cargo</webTarget>
|
||||||
|
</systemPropertyVariables>
|
||||||
|
</configuration>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
</profile>
|
||||||
|
</profiles>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<!-- Spring -->
|
<!-- Spring -->
|
||||||
|
|
||||||
@ -266,7 +318,7 @@
|
|||||||
<maven-compiler-plugin.version>3.5.1</maven-compiler-plugin.version>
|
<maven-compiler-plugin.version>3.5.1</maven-compiler-plugin.version>
|
||||||
<maven-war-plugin.version>2.6</maven-war-plugin.version>
|
<maven-war-plugin.version>2.6</maven-war-plugin.version>
|
||||||
<maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version>
|
<maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version>
|
||||||
<cargo-maven2-plugin.version>1.4.18</cargo-maven2-plugin.version>
|
<cargo-maven2-plugin.version>1.6.0</cargo-maven2-plugin.version>
|
||||||
|
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
|
@ -7,7 +7,7 @@ import org.junit.Test;
|
|||||||
import com.jayway.restassured.RestAssured;
|
import com.jayway.restassured.RestAssured;
|
||||||
|
|
||||||
public class RequestMappingLiveTest {
|
public class RequestMappingLiveTest {
|
||||||
private static String BASE_URI = "http://localhost:8080/spring-rest/ex/";
|
private static String BASE_URI = "http://localhost:8082/spring-rest/ex/";
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenSimplePath_whenGetFoos_thenOk() {
|
public void givenSimplePath_whenGetFoos_thenOk() {
|
||||||
|
@ -21,9 +21,9 @@ import org.springframework.web.client.RestTemplate;
|
|||||||
/**
|
/**
|
||||||
* Integration Test class. Tests methods hits the server's rest services.
|
* Integration Test class. Tests methods hits the server's rest services.
|
||||||
*/
|
*/
|
||||||
public class SpringHttpMessageConvertersIntegrationTestsCase {
|
public class SpringHttpMessageConvertersLiveTest {
|
||||||
|
|
||||||
private static String BASE_URI = "http://localhost:8080/spring-rest/";
|
private static String BASE_URI = "http://localhost:8082/spring-rest/";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Without specifying Accept Header, uses the default response from the
|
* Without specifying Accept Header, uses the default response from the
|
@ -31,9 +31,8 @@ public class SessionTimerInterceptor extends HandlerInterceptorAdapter {
|
|||||||
request.setAttribute("executionTime", startTime);
|
request.setAttribute("executionTime", startTime);
|
||||||
if (UserInterceptor.isUserLogged()) {
|
if (UserInterceptor.isUserLogged()) {
|
||||||
session = request.getSession();
|
session = request.getSession();
|
||||||
log.info("Who is logged in: " + SecurityContextHolder.getContext().getAuthentication().getName());
|
log.info("Time since last request in this session: {} ms",
|
||||||
log.info("Time since last request in this session: "
|
System.currentTimeMillis() - request.getSession().getLastAccessedTime());
|
||||||
+ (System.currentTimeMillis() - request.getSession().getLastAccessedTime()) + " ms");
|
|
||||||
if (System.currentTimeMillis() - session.getLastAccessedTime() > MAX_INACTIVE_SESSION_TIME) {
|
if (System.currentTimeMillis() - session.getLastAccessedTime() > MAX_INACTIVE_SESSION_TIME) {
|
||||||
log.warn("Logging out, due to inactive session");
|
log.warn("Logging out, due to inactive session");
|
||||||
SecurityContextHolder.clearContext();
|
SecurityContextHolder.clearContext();
|
||||||
@ -52,6 +51,6 @@ public class SessionTimerInterceptor extends HandlerInterceptorAdapter {
|
|||||||
final ModelAndView model) throws Exception {
|
final ModelAndView model) throws Exception {
|
||||||
log.info("Post handle method - check execution time of handling");
|
log.info("Post handle method - check execution time of handling");
|
||||||
long startTime = (Long) request.getAttribute("executionTime");
|
long startTime = (Long) request.getAttribute("executionTime");
|
||||||
log.info("Execution time for handling the request was: " + (System.currentTimeMillis() - startTime) + " ms");
|
log.info("Execution time for handling the request was: {} ms", System.currentTimeMillis() - startTime);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -14,6 +14,7 @@ import org.springframework.test.context.web.WebAppConfiguration;
|
|||||||
import org.springframework.test.web.servlet.MockMvc;
|
import org.springframework.test.web.servlet.MockMvc;
|
||||||
import org.springframework.test.web.servlet.request.RequestPostProcessor;
|
import org.springframework.test.web.servlet.request.RequestPostProcessor;
|
||||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
import org.springframework.web.context.WebApplicationContext;
|
import org.springframework.web.context.WebApplicationContext;
|
||||||
|
|
||||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
@ -21,6 +22,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
|||||||
|
|
||||||
@RunWith(SpringJUnit4ClassRunner.class)
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
@WebAppConfiguration
|
@WebAppConfiguration
|
||||||
|
@Transactional
|
||||||
public class CsrfAbstractIntegrationTest {
|
public class CsrfAbstractIntegrationTest {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
|
@ -5,59 +5,69 @@
|
|||||||
<artifactId>spring-thymeleaf</artifactId>
|
<artifactId>spring-thymeleaf</artifactId>
|
||||||
<version>0.1-SNAPSHOT</version>
|
<version>0.1-SNAPSHOT</version>
|
||||||
<packaging>war</packaging>
|
<packaging>war</packaging>
|
||||||
<properties>
|
<properties>
|
||||||
<java-version>1.7</java-version>
|
<java-version>1.8</java-version>
|
||||||
<!-- spring -->
|
<!-- spring -->
|
||||||
<org.springframework-version>4.1.8.RELEASE</org.springframework-version>
|
<org.springframework-version>4.3.3.RELEASE</org.springframework-version>
|
||||||
<javax.servlet-version>3.0.1</javax.servlet-version>
|
<javax.servlet-version>3.0.1</javax.servlet-version>
|
||||||
<!-- logging -->
|
<!-- logging -->
|
||||||
<org.slf4j.version>1.7.12</org.slf4j.version>
|
<org.slf4j.version>1.7.12</org.slf4j.version>
|
||||||
<logback.version>1.1.3</logback.version>
|
<logback.version>1.1.3</logback.version>
|
||||||
<!-- thymeleaf -->
|
<!-- thymeleaf -->
|
||||||
<org.thymeleaf-version>2.1.4.RELEASE</org.thymeleaf-version>
|
<org.thymeleaf-version>3.0.1.RELEASE</org.thymeleaf-version>
|
||||||
<!-- validation -->
|
<!-- validation -->
|
||||||
<javax.validation-version>1.1.0.Final</javax.validation-version>
|
<javax.validation-version>1.1.0.Final</javax.validation-version>
|
||||||
<org.hibernate-version>5.1.2.Final</org.hibernate-version>
|
<org.hibernate-version>5.1.2.Final</org.hibernate-version>
|
||||||
|
|
||||||
<!-- Maven plugins -->
|
<!-- Maven plugins -->
|
||||||
<maven-compiler-plugin.version>3.5.1</maven-compiler-plugin.version>
|
<maven-compiler-plugin.version>3.5.1</maven-compiler-plugin.version>
|
||||||
<maven-war-plugin.version>2.6</maven-war-plugin.version>
|
<maven-war-plugin.version>2.6</maven-war-plugin.version>
|
||||||
<maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version>
|
<maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version>
|
||||||
<cargo-maven2-plugin.version>1.4.18</cargo-maven2-plugin.version>
|
<cargo-maven2-plugin.version>1.4.18</cargo-maven2-plugin.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<!-- Spring -->
|
<!-- Spring -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework</groupId>
|
<groupId>org.springframework</groupId>
|
||||||
<artifactId>spring-context</artifactId>
|
<artifactId>spring-context</artifactId>
|
||||||
<version>${org.springframework-version}</version>
|
<version>${org.springframework-version}</version>
|
||||||
<exclusions>
|
<exclusions>
|
||||||
<!-- Exclude Commons Logging in favor of SLF4j -->
|
<!-- Exclude Commons Logging in favor of SLF4j -->
|
||||||
<exclusion>
|
<exclusion>
|
||||||
<groupId>commons-logging</groupId>
|
<groupId>commons-logging</groupId>
|
||||||
<artifactId>commons-logging</artifactId>
|
<artifactId>commons-logging</artifactId>
|
||||||
</exclusion>
|
</exclusion>
|
||||||
</exclusions>
|
</exclusions>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework</groupId>
|
<groupId>org.springframework</groupId>
|
||||||
<artifactId>spring-webmvc</artifactId>
|
<artifactId>spring-webmvc</artifactId>
|
||||||
<version>${org.springframework-version}</version>
|
<version>${org.springframework-version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<!-- Thymeleaf -->
|
<!-- Spring Security -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.thymeleaf</groupId>
|
<groupId>org.springframework.security</groupId>
|
||||||
<artifactId>thymeleaf</artifactId>
|
<artifactId>spring-security-web</artifactId>
|
||||||
<version>${org.thymeleaf-version}</version>
|
<version>4.1.3.RELEASE</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.thymeleaf</groupId>
|
<groupId>org.springframework.security</groupId>
|
||||||
<artifactId>thymeleaf-spring4</artifactId>
|
<artifactId>spring-security-config</artifactId>
|
||||||
<version>${org.thymeleaf-version}</version>
|
<version>4.1.3.RELEASE</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<!-- Logging -->
|
<!-- Thymeleaf -->
|
||||||
<!-- logging -->
|
<dependency>
|
||||||
|
<groupId>org.thymeleaf</groupId>
|
||||||
|
<artifactId>thymeleaf</artifactId>
|
||||||
|
<version>${org.thymeleaf-version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.thymeleaf</groupId>
|
||||||
|
<artifactId>thymeleaf-spring4</artifactId>
|
||||||
|
<version>${org.thymeleaf-version}</version>
|
||||||
|
</dependency>
|
||||||
|
<!-- Logging -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.slf4j</groupId>
|
<groupId>org.slf4j</groupId>
|
||||||
<artifactId>slf4j-api</artifactId>
|
<artifactId>slf4j-api</artifactId>
|
||||||
@ -80,55 +90,81 @@
|
|||||||
<artifactId>log4j-over-slf4j</artifactId>
|
<artifactId>log4j-over-slf4j</artifactId>
|
||||||
<version>${org.slf4j.version}</version>
|
<version>${org.slf4j.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<!-- Servlet -->
|
<!-- Servlet -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>javax.servlet</groupId>
|
<groupId>javax.servlet</groupId>
|
||||||
<artifactId>javax.servlet-api</artifactId>
|
<artifactId>javax.servlet-api</artifactId>
|
||||||
<version>${javax.servlet-version}</version>
|
<version>${javax.servlet-version}</version>
|
||||||
<scope>provided</scope>
|
<scope>provided</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
<!-- Validation -->
|
<!-- Validation -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>javax.validation</groupId>
|
<groupId>javax.validation</groupId>
|
||||||
<artifactId>validation-api</artifactId>
|
<artifactId>validation-api</artifactId>
|
||||||
<version>${javax.validation-version}</version>
|
<version>${javax.validation-version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.hibernate</groupId>
|
<groupId>org.hibernate</groupId>
|
||||||
<artifactId>hibernate-validator</artifactId>
|
<artifactId>hibernate-validator</artifactId>
|
||||||
<version>${org.hibernate-version}</version>
|
<version>${org.hibernate-version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
<!-- test scoped -->
|
||||||
<build>
|
|
||||||
<plugins>
|
<dependency>
|
||||||
<plugin>
|
<groupId>org.springframework</groupId>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<artifactId>spring-test</artifactId>
|
||||||
<artifactId>maven-compiler-plugin</artifactId>
|
<version>4.1.3.RELEASE</version>
|
||||||
<version>${maven-compiler-plugin.version}</version>
|
<scope>test</scope>
|
||||||
<configuration>
|
</dependency>
|
||||||
<source>${java-version}</source>
|
|
||||||
<target>${java-version}</target>
|
<!-- https://mvnrepository.com/artifact/org.springframework.security/spring-security-test -->
|
||||||
</configuration>
|
<dependency>
|
||||||
</plugin>
|
<groupId>org.springframework.security</groupId>
|
||||||
<plugin>
|
<artifactId>spring-security-test</artifactId>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<version>4.1.3.RELEASE</version>
|
||||||
<artifactId>maven-war-plugin</artifactId>
|
<scope>test</scope>
|
||||||
<version>${maven-war-plugin.version}</version>
|
</dependency>
|
||||||
<configuration>
|
|
||||||
<failOnMissingWebXml>false</failOnMissingWebXml>
|
<!-- https://mvnrepository.com/artifact/junit/junit -->
|
||||||
</configuration>
|
<dependency>
|
||||||
</plugin>
|
<groupId>junit</groupId>
|
||||||
<plugin>
|
<artifactId>junit</artifactId>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<version>4.12</version>
|
||||||
<artifactId>maven-surefire-plugin</artifactId>
|
<scope>test</scope>
|
||||||
<version>${maven-surefire-plugin.version}</version>
|
</dependency>
|
||||||
<configuration>
|
|
||||||
<excludes>
|
</dependencies>
|
||||||
</excludes>
|
|
||||||
<systemPropertyVariables>
|
<build>
|
||||||
</systemPropertyVariables>
|
<plugins>
|
||||||
</configuration>
|
<plugin>
|
||||||
</plugin>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<version>${maven-compiler-plugin.version}</version>
|
||||||
|
<configuration>
|
||||||
|
<source>${java-version}</source>
|
||||||
|
<target>${java-version}</target>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-war-plugin</artifactId>
|
||||||
|
<version>${maven-war-plugin.version}</version>
|
||||||
|
<configuration>
|
||||||
|
<failOnMissingWebXml>false</failOnMissingWebXml>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-surefire-plugin</artifactId>
|
||||||
|
<version>${maven-surefire-plugin.version}</version>
|
||||||
|
<configuration>
|
||||||
|
<excludes>
|
||||||
|
</excludes>
|
||||||
|
<systemPropertyVariables>
|
||||||
|
</systemPropertyVariables>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.codehaus.cargo</groupId>
|
<groupId>org.codehaus.cargo</groupId>
|
||||||
<artifactId>cargo-maven2-plugin</artifactId>
|
<artifactId>cargo-maven2-plugin</artifactId>
|
||||||
@ -148,6 +184,7 @@
|
|||||||
</configuration>
|
</configuration>
|
||||||
</configuration>
|
</configuration>
|
||||||
</plugin>
|
</plugin>
|
||||||
</plugins>
|
</plugins>
|
||||||
</build>
|
</build>
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
|
@ -0,0 +1,11 @@
|
|||||||
|
package com.baeldung.thymeleaf.config;
|
||||||
|
|
||||||
|
import org.springframework.security.web.context.AbstractSecurityWebApplicationInitializer;
|
||||||
|
|
||||||
|
public class InitSecurity extends AbstractSecurityWebApplicationInitializer {
|
||||||
|
|
||||||
|
public InitSecurity() {
|
||||||
|
super(WebMVCSecurity.class);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -9,28 +9,28 @@ import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatche
|
|||||||
*/
|
*/
|
||||||
public class WebApp extends AbstractAnnotationConfigDispatcherServletInitializer {
|
public class WebApp extends AbstractAnnotationConfigDispatcherServletInitializer {
|
||||||
|
|
||||||
public WebApp() {
|
public WebApp() {
|
||||||
super();
|
super();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Class<?>[] getRootConfigClasses() {
|
protected Class<?>[] getRootConfigClasses() {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Class<?>[] getServletConfigClasses() {
|
protected Class<?>[] getServletConfigClasses() {
|
||||||
return new Class<?>[] { WebMVCConfig.class };
|
return new Class<?>[] { WebMVCConfig.class, WebMVCSecurity.class, InitSecurity.class };
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected String[] getServletMappings() {
|
protected String[] getServletMappings() {
|
||||||
return new String[] { "/" };
|
return new String[] { "/" };
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void customizeRegistration(final Dynamic registration) {
|
protected void customizeRegistration(final Dynamic registration) {
|
||||||
super.customizeRegistration(registration);
|
super.customizeRegistration(registration);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,18 +1,25 @@
|
|||||||
package com.baeldung.thymeleaf.config;
|
package com.baeldung.thymeleaf.config;
|
||||||
|
|
||||||
import com.baeldung.thymeleaf.formatter.NameFormatter;
|
import org.springframework.context.ApplicationContext;
|
||||||
|
import org.springframework.context.ApplicationContextAware;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.ComponentScan;
|
import org.springframework.context.annotation.ComponentScan;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.context.annotation.Description;
|
import org.springframework.context.annotation.Description;
|
||||||
import org.springframework.context.support.ResourceBundleMessageSource;
|
import org.springframework.context.support.ResourceBundleMessageSource;
|
||||||
import org.springframework.format.FormatterRegistry;
|
import org.springframework.format.FormatterRegistry;
|
||||||
|
import org.springframework.web.servlet.ViewResolver;
|
||||||
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
||||||
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
|
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
|
||||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
|
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
|
||||||
|
import org.thymeleaf.TemplateEngine;
|
||||||
import org.thymeleaf.spring4.SpringTemplateEngine;
|
import org.thymeleaf.spring4.SpringTemplateEngine;
|
||||||
|
import org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver;
|
||||||
import org.thymeleaf.spring4.view.ThymeleafViewResolver;
|
import org.thymeleaf.spring4.view.ThymeleafViewResolver;
|
||||||
import org.thymeleaf.templateresolver.ServletContextTemplateResolver;
|
import org.thymeleaf.templatemode.TemplateMode;
|
||||||
|
import org.thymeleaf.templateresolver.ITemplateResolver;
|
||||||
|
|
||||||
|
import com.baeldung.thymeleaf.formatter.NameFormatter;
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
@EnableWebMvc
|
@EnableWebMvc
|
||||||
@ -21,35 +28,38 @@ import org.thymeleaf.templateresolver.ServletContextTemplateResolver;
|
|||||||
* Java configuration file that is used for Spring MVC and Thymeleaf
|
* Java configuration file that is used for Spring MVC and Thymeleaf
|
||||||
* configurations
|
* configurations
|
||||||
*/
|
*/
|
||||||
public class WebMVCConfig extends WebMvcConfigurerAdapter {
|
public class WebMVCConfig extends WebMvcConfigurerAdapter implements ApplicationContextAware {
|
||||||
|
|
||||||
@Bean
|
private ApplicationContext applicationContext;
|
||||||
@Description("Thymeleaf Template Resolver")
|
|
||||||
public ServletContextTemplateResolver templateResolver() {
|
|
||||||
ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver();
|
|
||||||
templateResolver.setPrefix("/WEB-INF/views/");
|
|
||||||
templateResolver.setSuffix(".html");
|
|
||||||
templateResolver.setTemplateMode("HTML5");
|
|
||||||
|
|
||||||
return templateResolver;
|
public void setApplicationContext(ApplicationContext applicationContext) {
|
||||||
|
this.applicationContext = applicationContext;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
@Description("Thymeleaf Template Engine")
|
public ViewResolver viewResolver() {
|
||||||
public SpringTemplateEngine templateEngine() {
|
ThymeleafViewResolver resolver = new ThymeleafViewResolver();
|
||||||
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
|
resolver.setTemplateEngine(templateEngine());
|
||||||
templateEngine.setTemplateResolver(templateResolver());
|
resolver.setCharacterEncoding("UTF-8");
|
||||||
|
resolver.setOrder(1);
|
||||||
return templateEngine;
|
return resolver;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
@Description("Thymeleaf View Resolver")
|
public TemplateEngine templateEngine() {
|
||||||
public ThymeleafViewResolver viewResolver() {
|
SpringTemplateEngine engine = new SpringTemplateEngine();
|
||||||
ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
|
engine.setEnableSpringELCompiler(true);
|
||||||
viewResolver.setTemplateEngine(templateEngine());
|
engine.setTemplateResolver(templateResolver());
|
||||||
viewResolver.setOrder(1);
|
return engine;
|
||||||
return viewResolver;
|
}
|
||||||
|
|
||||||
|
private ITemplateResolver templateResolver() {
|
||||||
|
SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
|
||||||
|
resolver.setApplicationContext(applicationContext);
|
||||||
|
resolver.setPrefix("/WEB-INF/views/");
|
||||||
|
resolver.setSuffix(".html");
|
||||||
|
resolver.setTemplateMode(TemplateMode.HTML);
|
||||||
|
return resolver;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
|
@ -0,0 +1,49 @@
|
|||||||
|
package com.baeldung.thymeleaf.config;
|
||||||
|
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.security.authentication.AuthenticationManager;
|
||||||
|
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
|
||||||
|
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.builders.WebSecurity;
|
||||||
|
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
||||||
|
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@EnableWebSecurity
|
||||||
|
@EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true)
|
||||||
|
public class WebMVCSecurity extends WebSecurityConfigurerAdapter {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
@Override
|
||||||
|
public AuthenticationManager authenticationManagerBean() throws Exception {
|
||||||
|
return super.authenticationManagerBean();
|
||||||
|
}
|
||||||
|
|
||||||
|
public WebMVCSecurity() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void configure(final AuthenticationManagerBuilder auth) throws Exception {
|
||||||
|
auth.inMemoryAuthentication().withUser("user1").password("user1Pass").authorities("ROLE_USER");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void configure(final WebSecurity web) throws Exception {
|
||||||
|
web.ignoring().antMatchers("/resources/**");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void configure(final HttpSecurity http) throws Exception {
|
||||||
|
http
|
||||||
|
.authorizeRequests()
|
||||||
|
.anyRequest()
|
||||||
|
.authenticated()
|
||||||
|
.and()
|
||||||
|
.httpBasic()
|
||||||
|
;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -20,50 +20,50 @@ import org.springframework.web.bind.annotation.RequestMethod;
|
|||||||
@Controller
|
@Controller
|
||||||
public class StudentController {
|
public class StudentController {
|
||||||
|
|
||||||
@RequestMapping(value = "/saveStudent", method = RequestMethod.POST)
|
@RequestMapping(value = "/saveStudent", method = RequestMethod.POST)
|
||||||
public String saveStudent(@Valid @ModelAttribute Student student, BindingResult errors, Model model) {
|
public String saveStudent(@Valid @ModelAttribute Student student, BindingResult errors, Model model) {
|
||||||
if (!errors.hasErrors()) {
|
if (!errors.hasErrors()) {
|
||||||
// get mock objects
|
// get mock objects
|
||||||
List<Student> students = buildStudents();
|
List<Student> students = buildStudents();
|
||||||
// add current student
|
// add current student
|
||||||
students.add(student);
|
students.add(student);
|
||||||
model.addAttribute("students", students);
|
model.addAttribute("students", students);
|
||||||
}
|
}
|
||||||
return ((errors.hasErrors()) ? "addStudent" : "listStudents");
|
return ((errors.hasErrors()) ? "addStudent" : "listStudents");
|
||||||
}
|
}
|
||||||
|
|
||||||
@RequestMapping(value = "/addStudent", method = RequestMethod.GET)
|
@RequestMapping(value = "/addStudent", method = RequestMethod.GET)
|
||||||
public String addStudent(Model model) {
|
public String addStudent(Model model) {
|
||||||
model.addAttribute("student", new Student());
|
model.addAttribute("student", new Student());
|
||||||
return "addStudent";
|
return "addStudent";
|
||||||
}
|
}
|
||||||
|
|
||||||
@RequestMapping(value = "/listStudents", method = RequestMethod.GET)
|
@RequestMapping(value = "/listStudents", method = RequestMethod.GET)
|
||||||
public String listStudent(Model model) {
|
public String listStudent(Model model) {
|
||||||
|
|
||||||
model.addAttribute("students", buildStudents());
|
model.addAttribute("students", buildStudents());
|
||||||
|
|
||||||
return "listStudents";
|
return "listStudents";
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<Student> buildStudents() {
|
private List<Student> buildStudents() {
|
||||||
List<Student> students = new ArrayList<Student>();
|
List<Student> students = new ArrayList<Student>();
|
||||||
|
|
||||||
Student student1 = new Student();
|
Student student1 = new Student();
|
||||||
student1.setId(1001);
|
student1.setId(1001);
|
||||||
student1.setName("John Smith");
|
student1.setName("John Smith");
|
||||||
student1.setGender('M');
|
student1.setGender('M');
|
||||||
student1.setPercentage(Float.valueOf("80.45"));
|
student1.setPercentage(Float.valueOf("80.45"));
|
||||||
|
|
||||||
students.add(student1);
|
students.add(student1);
|
||||||
|
|
||||||
Student student2 = new Student();
|
Student student2 = new Student();
|
||||||
student2.setId(1002);
|
student2.setId(1002);
|
||||||
student2.setName("Jane Williams");
|
student2.setName("Jane Williams");
|
||||||
student2.setGender('F');
|
student2.setGender('F');
|
||||||
student2.setPercentage(Float.valueOf("60.25"));
|
student2.setPercentage(Float.valueOf("60.25"));
|
||||||
|
|
||||||
students.add(student2);
|
students.add(student2);
|
||||||
return students;
|
return students;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -12,49 +12,49 @@ import javax.validation.constraints.NotNull;
|
|||||||
*/
|
*/
|
||||||
public class Student implements Serializable {
|
public class Student implements Serializable {
|
||||||
|
|
||||||
private static final long serialVersionUID = -8582553475226281591L;
|
private static final long serialVersionUID = -8582553475226281591L;
|
||||||
|
|
||||||
@NotNull(message = "Student ID is required.")
|
@NotNull(message = "Student ID is required.")
|
||||||
@Min(value = 1000, message = "Student ID must be at least 4 digits.")
|
@Min(value = 1000, message = "Student ID must be at least 4 digits.")
|
||||||
private Integer id;
|
private Integer id;
|
||||||
|
|
||||||
@NotNull(message = "Student name is required.")
|
@NotNull(message = "Student name is required.")
|
||||||
private String name;
|
private String name;
|
||||||
|
|
||||||
@NotNull(message = "Student gender is required.")
|
@NotNull(message = "Student gender is required.")
|
||||||
private Character gender;
|
private Character gender;
|
||||||
|
|
||||||
private Float percentage;
|
private Float percentage;
|
||||||
|
|
||||||
public Integer getId() {
|
public Integer getId() {
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setId(Integer id) {
|
public void setId(Integer id) {
|
||||||
this.id = id;
|
this.id = id;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getName() {
|
public String getName() {
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setName(String name) {
|
public void setName(String name) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Character getGender() {
|
public Character getGender() {
|
||||||
return gender;
|
return gender;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setGender(Character gender) {
|
public void setGender(Character gender) {
|
||||||
this.gender = gender;
|
this.gender = gender;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Float getPercentage() {
|
public Float getPercentage() {
|
||||||
return percentage;
|
return percentage;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setPercentage(Float percentage) {
|
public void setPercentage(Float percentage) {
|
||||||
this.percentage = percentage;
|
this.percentage = percentage;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,12 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<form action="http://localhost:8080/spring-thymeleaf/saveStudent" method="post">
|
||||||
|
<input type="hidden" name="payload" value="CSRF attack!"/>
|
||||||
|
<input type="submit" />
|
||||||
|
</form>
|
||||||
|
</body>
|
||||||
|
</html>
|
@ -0,0 +1,63 @@
|
|||||||
|
package org.baeldung.security.csrf;
|
||||||
|
|
||||||
|
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf;
|
||||||
|
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.user;
|
||||||
|
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
||||||
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||||
|
|
||||||
|
import javax.servlet.Filter;
|
||||||
|
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.http.MediaType;
|
||||||
|
import org.springframework.mock.web.MockHttpSession;
|
||||||
|
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.request.RequestPostProcessor;
|
||||||
|
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||||
|
import org.springframework.web.context.WebApplicationContext;
|
||||||
|
|
||||||
|
import com.baeldung.thymeleaf.config.InitSecurity;
|
||||||
|
import com.baeldung.thymeleaf.config.WebApp;
|
||||||
|
import com.baeldung.thymeleaf.config.WebMVCConfig;
|
||||||
|
import com.baeldung.thymeleaf.config.WebMVCSecurity;
|
||||||
|
|
||||||
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
|
@WebAppConfiguration
|
||||||
|
@ContextConfiguration(classes = { WebApp.class, WebMVCConfig.class, WebMVCSecurity.class, InitSecurity.class })
|
||||||
|
public class CsrfEnabledIntegrationTest {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
WebApplicationContext wac;
|
||||||
|
@Autowired
|
||||||
|
MockHttpSession session;
|
||||||
|
|
||||||
|
private MockMvc mockMvc;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private Filter springSecurityFilterChain;
|
||||||
|
|
||||||
|
protected RequestPostProcessor testUser() {
|
||||||
|
return user("user1").password("user1Pass").roles("USER");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setup() {
|
||||||
|
mockMvc = MockMvcBuilders.webAppContextSetup(wac).addFilters(springSecurityFilterChain).build();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void addStudentWithoutCSRF() throws Exception {
|
||||||
|
mockMvc.perform(post("/saveStudent").contentType(MediaType.APPLICATION_JSON).param("id", "1234567").param("name", "Joe").param("gender", "M").with(testUser())).andExpect(status().isForbidden());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void addStudentWithCSRF() throws Exception {
|
||||||
|
mockMvc.perform(post("/saveStudent").contentType(MediaType.APPLICATION_JSON).param("id", "1234567").param("name", "Joe").param("gender", "M").with(testUser()).with(csrf())).andExpect(status().isOk());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user