diff --git a/guest/junit5-example/.gitignore b/guest/junit5-example/.gitignore
new file mode 100644
index 0000000000..60be5b80aa
--- /dev/null
+++ b/guest/junit5-example/.gitignore
@@ -0,0 +1,4 @@
+/target/
+.settings/
+.classpath
+.project
diff --git a/guest/junit5-example/pom.xml b/guest/junit5-example/pom.xml
new file mode 100644
index 0000000000..aec7f9228a
--- /dev/null
+++ b/guest/junit5-example/pom.xml
@@ -0,0 +1,68 @@
+
+ 4.0.0
+ junit5-example
+ junit5-example
+ 0.0.1-SNAPSHOT
+
+
+
+ org.junit.jupiter
+ junit-jupiter-engine
+ 5.0.0-M4
+
+
+ org.junit.jupiter
+ junit-jupiter-params
+ 5.0.0-M4
+
+
+
+ org.junit.vintage
+ junit-vintage-engine
+ 4.12.0-M4
+
+
+
+ com.h2database
+ h2
+ 1.4.195
+
+
+
+ org.apache.logging.log4j
+ log4j-core
+ 2.8.2
+
+
+
+
+
+
+
+ maven-compiler-plugin
+ 3.5
+
+ 1.8
+ 1.8
+
+
+
+ maven-surefire-plugin
+ 2.19.1
+
+
+ org.junit.platform
+ junit-platform-surefire-provider
+ 1.0.0-M4
+
+
+
+
+ math
+
+
+
+
+
+
\ No newline at end of file
diff --git a/guest/junit5-example/src/main/java/com/stackify/daos/UserDAO.java b/guest/junit5-example/src/main/java/com/stackify/daos/UserDAO.java
new file mode 100644
index 0000000000..091d077ef6
--- /dev/null
+++ b/guest/junit5-example/src/main/java/com/stackify/daos/UserDAO.java
@@ -0,0 +1,141 @@
+package com.stackify.daos;
+
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+
+import com.stackify.models.User;
+import com.stackify.utils.ConnectionUtil;
+
+public class UserDAO {
+
+ private Logger logger = LogManager.getLogger(UserDAO.class);
+
+ public void createTable() {
+ try (Connection con = ConnectionUtil.getConnection()) {
+ String createQuery = "CREATE TABLE users(email varchar(50) primary key, name varchar(50))";
+ PreparedStatement pstmt = con.prepareStatement(createQuery);
+
+ pstmt.execute();
+ } catch (SQLException exc) {
+ logger.error(exc.getMessage());
+ }
+
+ }
+
+ public void add(User user) {
+ try (Connection con = ConnectionUtil.getConnection()) {
+
+ String insertQuery = "INSERT INTO users(email,name) VALUES(?,?)";
+ PreparedStatement pstmt = con.prepareStatement(insertQuery);
+ pstmt.setString(1, user.getEmail());
+ pstmt.setString(2, user.getName());
+
+ pstmt.executeUpdate();
+ } catch (SQLException exc) {
+ logger.error(exc.getMessage());
+ }
+ }
+
+ public void update(User user) {
+ try (Connection con = ConnectionUtil.getConnection()) {
+
+ String updateQuery = "UPDATE users SET name=? WHERE email=?";
+ PreparedStatement pstmt = con.prepareStatement(updateQuery);
+ pstmt.setString(1, user.getName());
+ pstmt.setString(2, user.getEmail());
+
+ pstmt.executeUpdate();
+ } catch (SQLException exc) {
+ logger.error(exc.getMessage());
+ }
+ }
+
+ public void delete(User user) {
+ try (Connection con = ConnectionUtil.getConnection()) {
+
+ String deleteQuery = "DELETE FROM users WHERE email=?";
+ PreparedStatement pstmt = con.prepareStatement(deleteQuery);
+ pstmt.setString(1, user.getEmail());
+
+ pstmt.executeUpdate();
+ } catch (SQLException exc) {
+ logger.error(exc.getMessage());
+ }
+ }
+
+ public void delete(String email) {
+ try (Connection con = ConnectionUtil.getConnection()) {
+
+ String deleteQuery = "DELETE FROM users WHERE email=?";
+ PreparedStatement pstmt = con.prepareStatement(deleteQuery);
+ pstmt.setString(1, email);
+
+ pstmt.executeUpdate();
+ } catch (SQLException exc) {
+ logger.error(exc.getMessage());
+ }
+ }
+
+ public User findOne(String email) {
+ User user = null;
+
+ try (Connection con = ConnectionUtil.getConnection()) {
+ String query = "SELECT * FROM users WHERE email=?";
+ PreparedStatement pstmt = con.prepareStatement(query);
+ pstmt.setString(1, email);
+
+ ResultSet rs = pstmt.executeQuery();
+ while (rs.next()) {
+ user = new User();
+ user.setEmail(rs.getString("email"));
+ user.setName(rs.getString("name"));
+ }
+
+ } catch (SQLException exc) {
+ logger.error(exc.getMessage());
+ }
+
+ return user;
+ }
+
+ public List findAll() {
+ List users = new ArrayList<>();
+
+ try (Connection con = ConnectionUtil.getConnection()) {
+ String query = "SELECT * FROM users";
+ PreparedStatement pstmt = con.prepareStatement(query);
+
+ ResultSet rs = pstmt.executeQuery();
+ while (rs.next()) {
+ User user = new User();
+ user.setEmail(rs.getString("email"));
+ user.setName(rs.getString("name"));
+ users.add(user);
+ }
+ } catch (SQLException exc) {
+ logger.error(exc.getMessage());
+ }
+
+ return users;
+ }
+
+ public void deleteAll() {
+ try (Connection con = ConnectionUtil.getConnection()) {
+
+ String deleteQuery = "DELETE FROM users";
+ PreparedStatement pstmt = con.prepareStatement(deleteQuery);
+
+ pstmt.executeUpdate();
+ } catch (SQLException exc) {
+ logger.error(exc.getMessage());
+ }
+ }
+
+}
diff --git a/guest/junit5-example/src/main/java/com/stackify/models/User.java b/guest/junit5-example/src/main/java/com/stackify/models/User.java
new file mode 100644
index 0000000000..d6951102a7
--- /dev/null
+++ b/guest/junit5-example/src/main/java/com/stackify/models/User.java
@@ -0,0 +1,63 @@
+package com.stackify.models;
+
+public class User {
+ private String email;
+ private String name;
+
+ public User() {
+ }
+
+ public User(String email, String name) {
+ super();
+ this.email = email;
+ this.name = name;
+ }
+
+ public String getEmail() {
+ return email;
+ }
+
+ public void setEmail(String email) {
+ this.email = email;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ @Override
+ public int hashCode() {
+ final int prime = 31;
+ int result = 1;
+ result = prime * result + ((email == null) ? 0 : email.hashCode());
+ result = prime * result + ((name == null) ? 0 : name.hashCode());
+ return result;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj)
+ return true;
+ if (obj == null)
+ return false;
+ if (getClass() != obj.getClass())
+ return false;
+ User other = (User) obj;
+ if (email == null) {
+ if (other.email != null)
+ return false;
+ } else if (!email.equals(other.email))
+ return false;
+ if (name == null) {
+ if (other.name != null)
+ return false;
+ } else if (!name.equals(other.name))
+ return false;
+ return true;
+ }
+
+}
diff --git a/guest/junit5-example/src/main/java/com/stackify/test/conditions/DisabledOnEnvironment.java b/guest/junit5-example/src/main/java/com/stackify/test/conditions/DisabledOnEnvironment.java
new file mode 100644
index 0000000000..2c1fa80f2e
--- /dev/null
+++ b/guest/junit5-example/src/main/java/com/stackify/test/conditions/DisabledOnEnvironment.java
@@ -0,0 +1,15 @@
+package com.stackify.test.conditions;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+import org.junit.jupiter.api.extension.ExtendWith;
+
+@Target({ ElementType.METHOD })
+@Retention(RetentionPolicy.RUNTIME)
+@ExtendWith(DisabledOnEnvironmentCondition.class)
+public @interface DisabledOnEnvironment {
+ String[] value();
+}
\ No newline at end of file
diff --git a/guest/junit5-example/src/main/java/com/stackify/test/conditions/DisabledOnEnvironmentCondition.java b/guest/junit5-example/src/main/java/com/stackify/test/conditions/DisabledOnEnvironmentCondition.java
new file mode 100644
index 0000000000..adb9d9c09e
--- /dev/null
+++ b/guest/junit5-example/src/main/java/com/stackify/test/conditions/DisabledOnEnvironmentCondition.java
@@ -0,0 +1,38 @@
+package com.stackify.test.conditions;
+
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.Optional;
+import java.util.Properties;
+
+import org.junit.jupiter.api.extension.ConditionEvaluationResult;
+import org.junit.jupiter.api.extension.TestExecutionCondition;
+import org.junit.jupiter.api.extension.TestExtensionContext;
+import org.junit.platform.commons.support.AnnotationSupport;
+
+import com.stackify.utils.ConnectionUtil;
+
+public class DisabledOnEnvironmentCondition implements TestExecutionCondition {
+
+ @Override
+ public ConditionEvaluationResult evaluate(TestExtensionContext context) {
+ Properties props = new Properties();
+ String env = "";
+ try {
+ props.load(ConnectionUtil.class.getResourceAsStream("/application.properties"));
+ env = props.getProperty("env");
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ Optional disabled = AnnotationSupport.findAnnotation(context.getElement().get(), DisabledOnEnvironment.class);
+ if (disabled.isPresent()) {
+ String[] envs = disabled.get().value();
+ if (Arrays.asList(envs).contains(env)) {
+ return ConditionEvaluationResult.disabled("Disabled on environment " + env);
+ }
+ }
+
+ return ConditionEvaluationResult.enabled("Enabled on environment "+env);
+ }
+
+}
diff --git a/guest/junit5-example/src/main/java/com/stackify/utils/ConnectionUtil.java b/guest/junit5-example/src/main/java/com/stackify/utils/ConnectionUtil.java
new file mode 100644
index 0000000000..bcfe5058a7
--- /dev/null
+++ b/guest/junit5-example/src/main/java/com/stackify/utils/ConnectionUtil.java
@@ -0,0 +1,38 @@
+package com.stackify.utils;
+
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.SQLException;
+import java.util.Properties;
+
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+
+public class ConnectionUtil {
+
+ private static Logger logger = LogManager.getLogger(ConnectionUtil.class);
+
+ public static Connection getConnection() {
+ try {
+ Properties props = new Properties();
+ props.load(ConnectionUtil.class.getResourceAsStream("jdbc.properties"));
+ Class.forName(props.getProperty("jdbc.driver"));
+ Connection con = DriverManager.getConnection(props.getProperty("jdbc.url"), props.getProperty("jdbc.user"), props.getProperty("jdbc.password"));
+ return con;
+ }
+
+ catch (FileNotFoundException exc) {
+ logger.error(exc.getMessage());
+ } catch (IOException exc) {
+ logger.error(exc.getMessage());
+ } catch (ClassNotFoundException exc) {
+ logger.error(exc.getMessage());
+ } catch (SQLException exc) {
+ logger.error(exc.getMessage());
+ }
+ return null;
+ }
+
+}
diff --git a/guest/junit5-example/src/main/resources/application.properties b/guest/junit5-example/src/main/resources/application.properties
new file mode 100644
index 0000000000..601f964ff3
--- /dev/null
+++ b/guest/junit5-example/src/main/resources/application.properties
@@ -0,0 +1 @@
+env=dev
\ No newline at end of file
diff --git a/guest/junit5-example/src/main/resources/com/stackify/utils/jdbc.properties b/guest/junit5-example/src/main/resources/com/stackify/utils/jdbc.properties
new file mode 100644
index 0000000000..2d9a39b157
--- /dev/null
+++ b/guest/junit5-example/src/main/resources/com/stackify/utils/jdbc.properties
@@ -0,0 +1,4 @@
+jdbc.driver=org.h2.Driver
+jdbc.url=jdbc:h2:mem:myDb;DB_CLOSE_DELAY=-1
+jdbc.user=
+jdbc.password=
\ No newline at end of file
diff --git a/guest/junit5-example/src/main/resources/log4j2.xml b/guest/junit5-example/src/main/resources/log4j2.xml
new file mode 100644
index 0000000000..602b5ab490
--- /dev/null
+++ b/guest/junit5-example/src/main/resources/log4j2.xml
@@ -0,0 +1,13 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/guest/junit5-example/src/test/java/com/stackify/test/DatabaseConnectionTest.java b/guest/junit5-example/src/test/java/com/stackify/test/DatabaseConnectionTest.java
new file mode 100644
index 0000000000..d8f13b5715
--- /dev/null
+++ b/guest/junit5-example/src/test/java/com/stackify/test/DatabaseConnectionTest.java
@@ -0,0 +1,19 @@
+package com.stackify.test;
+
+import java.sql.Connection;
+
+import org.junit.jupiter.api.Test;
+
+import com.stackify.utils.ConnectionUtil;
+
+import static org.junit.jupiter.api.Assertions.*;
+
+public interface DatabaseConnectionTest {
+
+ @Test
+ default void testDatabaseConnection() {
+ Connection con = ConnectionUtil.getConnection();
+ assertNotNull(con);
+ }
+
+}
diff --git a/guest/junit5-example/src/test/java/com/stackify/test/DynamicTests.java b/guest/junit5-example/src/test/java/com/stackify/test/DynamicTests.java
new file mode 100644
index 0000000000..39b3b5aac5
--- /dev/null
+++ b/guest/junit5-example/src/test/java/com/stackify/test/DynamicTests.java
@@ -0,0 +1,38 @@
+package com.stackify.test;
+
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.List;
+import java.util.function.Function;
+import java.util.stream.Stream;
+
+import static org.junit.jupiter.api.Assertions.*;
+import org.junit.jupiter.api.DynamicTest;
+import org.junit.jupiter.api.TestFactory;
+import org.junit.jupiter.api.function.ThrowingConsumer;
+
+import com.stackify.daos.UserDAO;
+import com.stackify.models.User;
+
+public class DynamicTests {
+
+ @TestFactory
+ public Collection dynamicTestCollection() {
+ return Arrays.asList(DynamicTest.dynamicTest("Dynamic Test", () -> assertTrue(1 == 1)));
+ }
+
+ @TestFactory
+ public Stream dynamicUserTestCollection() {
+ List inputList = Arrays.asList(new User("john@yahoo.com", "John"), new User("ana@yahoo.com", "Ana"));
+
+ Function displayNameGenerator = (input) -> "Saving user: " + input;
+
+ UserDAO userDAO = new UserDAO();
+ ThrowingConsumer testExecutor = (input) -> {
+ userDAO.add(input);
+ assertNotNull(userDAO.findOne(input.getEmail()));
+ };
+
+ return DynamicTest.stream(inputList.iterator(), displayNameGenerator, testExecutor);
+ }
+}
diff --git a/guest/junit5-example/src/test/java/com/stackify/test/IncrementTest.java b/guest/junit5-example/src/test/java/com/stackify/test/IncrementTest.java
new file mode 100644
index 0000000000..a23a5bf20b
--- /dev/null
+++ b/guest/junit5-example/src/test/java/com/stackify/test/IncrementTest.java
@@ -0,0 +1,25 @@
+package com.stackify.test;
+
+import static org.junit.jupiter.api.Assertions.*;
+
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.RepeatedTest;
+import org.junit.jupiter.api.RepetitionInfo;
+
+public class IncrementTest {
+
+ private static Logger logger = LogManager.getLogger(IncrementTest.class);
+
+ @BeforeEach
+ public void increment() {
+ logger.info("Before Each Test");
+ }
+
+ @RepeatedTest(value = 3, name = RepeatedTest.SHORT_DISPLAY_NAME)
+ public void test(RepetitionInfo info) {
+ assertTrue(1 == 1);
+ logger.info("Repetition #" + info.getCurrentRepetition());
+ }
+}
diff --git a/guest/junit5-example/src/test/java/com/stackify/test/TaggedTest.java b/guest/junit5-example/src/test/java/com/stackify/test/TaggedTest.java
new file mode 100644
index 0000000000..db3c38ceac
--- /dev/null
+++ b/guest/junit5-example/src/test/java/com/stackify/test/TaggedTest.java
@@ -0,0 +1,16 @@
+package com.stackify.test;
+
+import static org.junit.jupiter.api.Assertions.*;
+import org.junit.jupiter.api.Tag;
+import org.junit.jupiter.api.Test;
+
+@Tag("math")
+public class TaggedTest {
+
+ @Test
+ @Tag("arithmetic")
+ public void testEquals() {
+ assertTrue(1 == 1);
+ }
+
+}
diff --git a/guest/junit5-example/src/test/java/com/stackify/test/UsersTest.java b/guest/junit5-example/src/test/java/com/stackify/test/UsersTest.java
new file mode 100644
index 0000000000..88958a8232
--- /dev/null
+++ b/guest/junit5-example/src/test/java/com/stackify/test/UsersTest.java
@@ -0,0 +1,155 @@
+package com.stackify.test;
+
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+import org.junit.jupiter.api.AfterAll;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Disabled;
+import org.junit.jupiter.api.DisplayName;
+import org.junit.jupiter.api.Nested;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.TestInfo;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
+
+import static org.junit.jupiter.api.Assertions.*;
+import static org.junit.jupiter.api.Assumptions.*;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+
+import com.stackify.daos.UserDAO;
+import com.stackify.models.User;
+
+import com.stackify.test.conditions.DisabledOnEnvironment;
+
+public class UsersTest implements DatabaseConnectionTest {
+
+ private static UserDAO userDAO;
+
+ private static Logger logger = LogManager.getLogger(UsersTest.class);
+
+ @BeforeAll
+ public static void addData() {
+ userDAO = new UserDAO();
+ userDAO.createTable();
+
+ User user1 = new User("john@gmail.com", "John");
+ User user2 = new User("ana@gmail.com", "Ana");
+ userDAO.add(user1);
+ userDAO.add(user2);
+ }
+
+ @Test
+ @DisplayName("Test Get Users")
+ public void testGetUsersNumber() {
+ assertEquals(2, userDAO.findAll().size());
+
+ }
+
+ @Test
+ @DisplayName("Test Get Users")
+ public void testGetUsersNumberWithInfo(TestInfo testInfo) {
+ assertEquals(2, userDAO.findAll().size());
+ assertEquals("Test Get Users", testInfo.getDisplayName());
+ assertEquals(UsersTest.class, testInfo.getTestClass().get());
+ logger.info("Running test method:" + testInfo.getTestMethod().get().getName());
+ }
+
+ @Test
+ public void testGetUser() {
+ User user = userDAO.findOne("john@gmail.com");
+ assertEquals("John", user.getName(), "User name:" + user.getName() + " incorrect");
+ }
+
+ @Test
+ public void testClassicAssertions() {
+ User user1 = userDAO.findOne("john@gmail.com");
+ User user2 = userDAO.findOne("john@yahoo.com");
+
+ assertNotNull(user1);
+ assertNull(user2);
+
+ user2 = new User("john@yahoo.com", "John");
+ assertEquals(user1.getName(), user2.getName(), "Names are not equal");
+ assertFalse(user1.getEmail().equals(user2.getEmail()), "Emails are equal");
+ assertNotSame(user1, user2);
+ }
+
+ @Test
+ @Disabled
+ public void testGroupedAssertions() {
+ User user = userDAO.findOne("john@gmail.com");
+ assertAll("user", () -> assertEquals("Johnson", user.getName()), () -> assertEquals("johnson@gmail.com", user.getEmail()));
+ }
+
+ @Test
+ public void testIterableEquals() {
+ User user1 = new User("john@gmail.com", "John");
+ User user2 = new User("ana@gmail.com", "Ana");
+
+ List users = new ArrayList<>();
+ users.add(user1);
+ users.add(user2);
+
+ assertIterableEquals(users, userDAO.findAll());
+ }
+
+ @Test
+ public void testLinesMatch() {
+ List expectedLines = Collections.singletonList("(.*)@(.*)");
+ List emails = Arrays.asList("john@gmail.com");
+ assertLinesMatch(expectedLines, emails);
+ }
+
+ @Test
+ void testThrows() {
+ User user = null;
+ Exception exception = assertThrows(NullPointerException.class, () -> user.getName());
+ logger.info(exception.getMessage());
+ }
+
+ @Test
+ @DisabledOnEnvironment({ "dev", "prod")
+ void testFail() {
+ fail("this test fails");
+ }
+
+ @Test
+ void testAssumptions() {
+ List users = userDAO.findAll();
+ assumeFalse(users == null);
+ assumeTrue(users.size() > 0);
+
+ User user1 = new User("john@gmail.com", "John");
+
+ assumingThat(users.contains(user1), () -> assertTrue(users.size() > 1));
+ }
+
+ @ParameterizedTest
+ @ValueSource(strings = { "john@gmail.com", "ana@gmail.com" })
+ public void testParameterized(String email) {
+ assertNotNull(userDAO.findOne(email));
+ }
+
+ @AfterAll
+ public static void removeData() {
+ userDAO.deleteAll();
+ }
+
+ @Nested
+ class DeleteUsersTest {
+ @Test
+ public void addUser() {
+ User user = new User("bob@gmail.com", "Bob");
+ userDAO.add(user);
+ assertNotNull(userDAO.findOne("bob@gmail.com"));
+
+ userDAO.delete("bob@gmail.com");
+ assertNull(userDAO.findOne("bob@gmail.com"));
+ }
+ }
+
+}
diff --git a/libraries/src/main/java/com/baeldung/opennlp/OpenNLP.java b/libraries/src/main/java/com/baeldung/opennlp/OpenNLP.java
index b2fa8e629b..dd44e96a3a 100644
--- a/libraries/src/main/java/com/baeldung/opennlp/OpenNLP.java
+++ b/libraries/src/main/java/com/baeldung/opennlp/OpenNLP.java
@@ -36,7 +36,7 @@ import opennlp.tools.util.TrainingParameters;
public class OpenNLP {
private final static Logger LOGGER = Logger.getLogger(OpenNLP.class.getName());
- private final static String text = "To get to the south: Go to the store. Buy a compass. Use the compass. Then walk to the south.";
+ private final static String text = buildString();
private final static String sentence[] = new String[] { "James", "Jordan", "live", "in", "Oklahoma", "city", "." };
private DoccatModel docCatModel;
@@ -45,6 +45,16 @@ public class OpenNLP {
new OpenNLP();
}
+ public static String buildString(){
+ StringBuilder sb = new StringBuilder();
+ sb.append("To get to the south:");
+ sb.append(" Go to the store.");
+ sb.append(" Buy a compass.");
+ sb.append(" Use the compass.");
+ sb.append(" Then walk to the south.");
+ return sb.toString();
+ }
+
public OpenNLP() {
try {
sentenceDetector();
@@ -68,7 +78,9 @@ public class OpenNLP {
SentenceModel model = new SentenceModel(is);
SentenceDetectorME sdetector = new SentenceDetectorME(model);
String sentences[] = sdetector.sentDetect(text);
- Arrays.stream(sentences).forEach(LOGGER::info);
+ for (String sentence : sentences) {
+ LOGGER.info(sentence);
+ }
is.close();
}
@@ -77,7 +89,9 @@ public class OpenNLP {
TokenizerModel model = new TokenizerModel(is);
Tokenizer tokenizer = new TokenizerME(model);
String tokens[] = tokenizer.tokenize(text);
- Arrays.stream(tokens).forEach(LOGGER::info);
+ for (String token : tokens) {
+ LOGGER.info(token);
+ }
is.close();
}
@@ -89,6 +103,9 @@ public class OpenNLP {
Span nameSpans[] = nameFinder.find(sentence);
String[] names = Span.spansToStrings(nameSpans, sentence);
Arrays.stream(names).forEach(LOGGER::info);
+ for (String name : names) {
+ LOGGER.info(name);
+ }
}
public static void locationFinder() throws IOException {
@@ -99,6 +116,9 @@ public class OpenNLP {
Span locationSpans[] = nameFinder.find(sentence);
String[] locations = Span.spansToStrings(locationSpans, sentence);
Arrays.stream(locations).forEach(LOGGER::info);
+ for (String location : locations) {
+ LOGGER.info(location);
+ }
}
public void trainDocumentCategorizer() {
@@ -160,7 +180,9 @@ public class OpenNLP {
String[] taggedSentence = new String[] {"Out", "of", "the", "night", "that", "covers", "me"};
String pos[] = new String[] { "IN", "IN", "DT", "NN", "WDT", "VBZ", "PRP"};
String chunks[] = chunkerME.chunk(taggedSentence, pos);
- Arrays.stream(chunks).forEach(LOGGER::info);
+ for (String chunk : chunks) {
+ LOGGER.info(chunk);
+ }
}
}
diff --git a/spring-boot-bootstrap/.gitignore b/spring-boot-bootstrap/.gitignore
new file mode 100644
index 0000000000..2af7cefb0a
--- /dev/null
+++ b/spring-boot-bootstrap/.gitignore
@@ -0,0 +1,24 @@
+target/
+!.mvn/wrapper/maven-wrapper.jar
+
+### STS ###
+.apt_generated
+.classpath
+.factorypath
+.project
+.settings
+.springBeans
+
+### IntelliJ IDEA ###
+.idea
+*.iws
+*.iml
+*.ipr
+
+### NetBeans ###
+nbproject/private/
+build/
+nbbuild/
+dist/
+nbdist/
+.nb-gradle/
\ No newline at end of file
diff --git a/spring-boot-bootstrap/pom.xml b/spring-boot-bootstrap/pom.xml
new file mode 100644
index 0000000000..697a139eb5
--- /dev/null
+++ b/spring-boot-bootstrap/pom.xml
@@ -0,0 +1,75 @@
+
+
+ 4.0.0
+
+ org.baeldung
+ spring-boot-bootstrap
+ 0.0.1-SNAPSHOT
+ jar
+
+ spring-boot-bootstrap
+ Demo project for Spring Boot
+
+
+ org.springframework.boot
+ spring-boot-starter-parent
+ 1.5.3.RELEASE
+
+
+
+
+ UTF-8
+ UTF-8
+ 1.8
+
+
+
+
+ org.springframework.boot
+ spring-boot-starter-web
+
+
+ org.springframework.boot
+ spring-boot-starter-thymeleaf
+
+
+ org.springframework.boot
+ spring-boot-starter-data-jpa
+
+
+ com.h2database
+ h2
+
+
+ org.springframework.boot
+ spring-boot-starter-security
+
+
+
+
+ org.springframework.boot
+ spring-boot-starter-test
+ test
+
+
+
+ io.rest-assured
+ rest-assured
+ 3.0.3
+ test
+
+
+
+
+
+
+
+ org.springframework.boot
+ spring-boot-maven-plugin
+
+
+
+
+
+
diff --git a/spring-boot-bootstrap/src/main/java/org/baeldung/Application.java b/spring-boot-bootstrap/src/main/java/org/baeldung/Application.java
new file mode 100644
index 0000000000..f7e7bb0347
--- /dev/null
+++ b/spring-boot-bootstrap/src/main/java/org/baeldung/Application.java
@@ -0,0 +1,19 @@
+package org.baeldung;
+
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.boot.autoconfigure.domain.EntityScan;
+import org.springframework.context.annotation.ComponentScan;
+import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
+
+@SpringBootApplication
+@ComponentScan("org.baeldung")
+@EnableJpaRepositories("org.baeldung.persistence.repo")
+@EntityScan("org.baeldung.persistence.model")
+public class Application {
+
+ public static void main(String[] args) {
+ SpringApplication.run(Application.class, args);
+ }
+
+}
diff --git a/spring-boot-bootstrap/src/main/java/org/baeldung/persistence/model/Book.java b/spring-boot-bootstrap/src/main/java/org/baeldung/persistence/model/Book.java
new file mode 100644
index 0000000000..02060e17c9
--- /dev/null
+++ b/spring-boot-bootstrap/src/main/java/org/baeldung/persistence/model/Book.java
@@ -0,0 +1,96 @@
+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 Book {
+ @Id
+ @GeneratedValue(strategy = GenerationType.AUTO)
+ private long id;
+
+ @Column(nullable = false, unique = true)
+ private String title;
+
+ @Column(nullable = false)
+ private String author;
+
+ //
+
+ public Book() {
+ super();
+ }
+
+ public Book(String title, String author) {
+ super();
+ this.title = title;
+ this.author = author;
+ }
+
+ public long getId() {
+ return id;
+ }
+
+ public void setId(long id) {
+ this.id = id;
+ }
+
+ public String getTitle() {
+ return title;
+ }
+
+ public void setTitle(String title) {
+ this.title = title;
+ }
+
+ public String getAuthor() {
+ return author;
+ }
+
+ public void setAuthor(String author) {
+ this.author = author;
+ }
+
+ @Override
+ public int hashCode() {
+ final int prime = 31;
+ int result = 1;
+ result = prime * result + ((author == null) ? 0 : author.hashCode());
+ result = prime * result + (int) (id ^ (id >>> 32));
+ result = prime * result + ((title == null) ? 0 : title.hashCode());
+ return result;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj)
+ return true;
+ if (obj == null)
+ return false;
+ if (getClass() != obj.getClass())
+ return false;
+ Book other = (Book) obj;
+ if (author == null) {
+ if (other.author != null)
+ return false;
+ } else if (!author.equals(other.author))
+ return false;
+ if (id != other.id)
+ return false;
+ if (title == null) {
+ if (other.title != null)
+ return false;
+ } else if (!title.equals(other.title))
+ return false;
+ return true;
+ }
+
+ @Override
+ public String toString() {
+ return "Book [id=" + id + ", title=" + title + ", author=" + author + "]";
+ }
+
+}
\ No newline at end of file
diff --git a/spring-boot-bootstrap/src/main/java/org/baeldung/persistence/repo/BookRepository.java b/spring-boot-bootstrap/src/main/java/org/baeldung/persistence/repo/BookRepository.java
new file mode 100644
index 0000000000..ec3a3e25bc
--- /dev/null
+++ b/spring-boot-bootstrap/src/main/java/org/baeldung/persistence/repo/BookRepository.java
@@ -0,0 +1,10 @@
+package org.baeldung.persistence.repo;
+
+import java.util.List;
+
+import org.baeldung.persistence.model.Book;
+import org.springframework.data.repository.CrudRepository;
+
+public interface BookRepository extends CrudRepository {
+ List findByTitle(String title);
+}
diff --git a/spring-boot-bootstrap/src/main/java/org/baeldung/web/BookController.java b/spring-boot-bootstrap/src/main/java/org/baeldung/web/BookController.java
new file mode 100644
index 0000000000..544ac80217
--- /dev/null
+++ b/spring-boot-bootstrap/src/main/java/org/baeldung/web/BookController.java
@@ -0,0 +1,74 @@
+package org.baeldung.web;
+
+import java.util.List;
+
+import org.baeldung.persistence.model.Book;
+import org.baeldung.persistence.repo.BookRepository;
+import org.baeldung.web.exception.BookIdMismatchException;
+import org.baeldung.web.exception.BookNotFoundException;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.http.HttpStatus;
+import org.springframework.web.bind.annotation.DeleteMapping;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.PathVariable;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.PutMapping;
+import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.ResponseStatus;
+import org.springframework.web.bind.annotation.RestController;
+
+@RestController
+@RequestMapping("/api/books")
+public class BookController {
+
+ @Autowired
+ private BookRepository bookRepository;
+
+ @GetMapping
+ public Iterable findAll() {
+ return bookRepository.findAll();
+ }
+
+ @GetMapping("/title/{bookTitle}")
+ public List findByTitle(@PathVariable String bookTitle) {
+ return bookRepository.findByTitle(bookTitle);
+ }
+
+ @GetMapping("/{id}")
+ public Book findOne(@PathVariable Long id) {
+ final Book book = bookRepository.findOne(id);
+ if (book == null) {
+ throw new BookNotFoundException();
+ }
+ return book;
+ }
+
+ @PostMapping
+ @ResponseStatus(HttpStatus.CREATED)
+ public Book create(@RequestBody Book book) {
+ return bookRepository.save(book);
+ }
+
+ @DeleteMapping("/{id}")
+ public void delete(@PathVariable Long id) {
+ final Book book = bookRepository.findOne(id);
+ if (book == null) {
+ throw new BookNotFoundException();
+ }
+ bookRepository.delete(id);
+ }
+
+ @PutMapping("/{id}")
+ public Book updateBook(@RequestBody Book book, @PathVariable Long id) {
+ if (book.getId() != id) {
+ throw new BookIdMismatchException();
+ }
+ final Book old = bookRepository.findOne(id);
+ if (old == null) {
+ throw new BookNotFoundException();
+ }
+ return bookRepository.save(book);
+ }
+
+}
diff --git a/spring-boot-bootstrap/src/main/java/org/baeldung/web/RestExceptionHandler.java b/spring-boot-bootstrap/src/main/java/org/baeldung/web/RestExceptionHandler.java
new file mode 100644
index 0000000000..60153fc92f
--- /dev/null
+++ b/spring-boot-bootstrap/src/main/java/org/baeldung/web/RestExceptionHandler.java
@@ -0,0 +1,32 @@
+package org.baeldung.web;
+
+import org.baeldung.web.exception.BookIdMismatchException;
+import org.baeldung.web.exception.BookNotFoundException;
+import org.hibernate.exception.ConstraintViolationException;
+import org.springframework.dao.DataIntegrityViolationException;
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.ResponseEntity;
+import org.springframework.web.bind.annotation.ControllerAdvice;
+import org.springframework.web.bind.annotation.ExceptionHandler;
+import org.springframework.web.context.request.WebRequest;
+import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
+
+@ControllerAdvice
+public class RestExceptionHandler extends ResponseEntityExceptionHandler {
+
+ public RestExceptionHandler() {
+ super();
+ }
+
+ @ExceptionHandler({ BookNotFoundException.class })
+ protected ResponseEntity