Merge remote-tracking branch 'upstream/master' into BAEL-5229
This commit is contained in:
@@ -5,3 +5,4 @@
|
||||
- [How to Check if a Database Table Exists with JDBC](https://www.baeldung.com/jdbc-check-table-exists)
|
||||
- [Inserting Null Into an Integer Column Using JDBC](https://www.baeldung.com/jdbc-insert-null-into-integer-column)
|
||||
- [A Guide to Auto-Commit in JDBC](https://www.baeldung.com/java-jdbc-auto-commit)
|
||||
- [JDBC Connection Status](https://www.baeldung.com/jdbc-connection-status)
|
||||
|
||||
+65
@@ -0,0 +1,65 @@
|
||||
package com.baeldung.connectionstatus;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
|
||||
public class ConnectionValidation
|
||||
{
|
||||
|
||||
public static Connection getConnection()
|
||||
throws Exception
|
||||
{
|
||||
Class.forName("org.h2.Driver");
|
||||
String url = "jdbc:h2:mem:testdb";
|
||||
return DriverManager.getConnection(url, "user", "password");
|
||||
}
|
||||
|
||||
public static void runIfOpened(Connection connection)
|
||||
throws SQLException
|
||||
{
|
||||
if (connection != null && !connection.isClosed()) {
|
||||
// run sql statements
|
||||
}
|
||||
else {
|
||||
// handle closed connection
|
||||
}
|
||||
}
|
||||
|
||||
public static void runIfValid(Connection connection)
|
||||
throws SQLException
|
||||
{
|
||||
// Try to validate connection with a 5 seconds timeout
|
||||
if (connection.isValid(5)) {
|
||||
// run sql statements
|
||||
}
|
||||
else {
|
||||
// handle invalid connection
|
||||
}
|
||||
}
|
||||
|
||||
public static void runIfConnectionValid(Connection connection)
|
||||
{
|
||||
if (isConnectionValid(connection)) {
|
||||
// run sql statements
|
||||
}
|
||||
else {
|
||||
// handle invalid connection
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean isConnectionValid(Connection connection)
|
||||
{
|
||||
try {
|
||||
if (connection != null && !connection.isClosed()) {
|
||||
// Running a simple validation query
|
||||
connection.prepareStatement("SELECT 1");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
catch (SQLException e) {
|
||||
// log some useful data here
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
package com.baeldung.connectionstatus;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.sql.Connection;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
class ConnectionValidationUnitTest
|
||||
{
|
||||
@Test
|
||||
void givenConnectionObject_whenCreated_thenIsNotClosed()
|
||||
throws Exception
|
||||
{
|
||||
Connection connection = ConnectionValidation.getConnection();
|
||||
assertNotNull(connection);
|
||||
assertFalse(connection.isClosed());
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenConnectionObject_whenCreated_thenIsValid()
|
||||
throws Exception
|
||||
{
|
||||
Connection connection = ConnectionValidation.getConnection();
|
||||
assertTrue(connection.isValid(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenConnectionObject_whenValidated_thenIsValid()
|
||||
throws Exception
|
||||
{
|
||||
Connection connection = ConnectionValidation.getConnection();
|
||||
assertTrue(ConnectionValidation.isConnectionValid(connection));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -12,3 +12,4 @@ This module contains articles about MongoDB in Java.
|
||||
- [MongoDB Aggregations Using Java](https://www.baeldung.com/java-mongodb-aggregations)
|
||||
- [BSON to JSON Document Conversion in Java](https://www.baeldung.com/java-convert-bson-to-json)
|
||||
- [How to Check Field Existence in MongoDB?](https://www.baeldung.com/mongodb-check-field-exists)
|
||||
- [Get Last Inserted Document ID in MongoDB With Java Driver](https://www.baeldung.com/java-mongodb-last-inserted-id)
|
||||
|
||||
+6
-6
@@ -32,10 +32,10 @@ public class SpringContextTest {
|
||||
public static final String KEYSPACE_ACTIVATE_QUERY = "USE testKeySpace;";
|
||||
|
||||
public static final String DATA_TABLE_NAME = "book";
|
||||
|
||||
|
||||
@Autowired
|
||||
private CassandraAdminOperations adminTemplate;
|
||||
|
||||
|
||||
@BeforeClass
|
||||
public static void startCassandraEmbedded() throws InterruptedException, TTransportException, ConfigurationException, IOException {
|
||||
EmbeddedCassandraServerHelper.startEmbeddedCassandra();
|
||||
@@ -47,14 +47,14 @@ public class SpringContextTest {
|
||||
}
|
||||
|
||||
@Before
|
||||
public void createTable() throws InterruptedException, TTransportException, ConfigurationException, IOException {
|
||||
adminTemplate.createTable(true, CqlIdentifier.cqlId(DATA_TABLE_NAME), Book.class, new HashMap<String, Object>());
|
||||
public void createTable() {
|
||||
adminTemplate.createTable(true, CqlIdentifier.cqlId(DATA_TABLE_NAME), Book.class, new HashMap<>());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void whenSpringContextIsBootstrapped_thenNoExceptions() {
|
||||
}
|
||||
|
||||
|
||||
@After
|
||||
public void dropTable() {
|
||||
adminTemplate.dropTable(CqlIdentifier.cqlId(DATA_TABLE_NAME));
|
||||
|
||||
+22
-17
@@ -1,17 +1,15 @@
|
||||
package com.baeldung.spring.data.cassandra.repository;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotEquals;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
|
||||
import com.baeldung.spring.data.cassandra.config.CassandraConfig;
|
||||
import com.baeldung.spring.data.cassandra.model.Book;
|
||||
import com.datastax.driver.core.Cluster;
|
||||
import com.datastax.driver.core.Session;
|
||||
import com.datastax.driver.core.utils.UUIDs;
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import org.apache.cassandra.exceptions.ConfigurationException;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.apache.thrift.transport.TTransportException;
|
||||
import com.baeldung.spring.data.cassandra.config.CassandraConfig;
|
||||
import org.cassandraunit.utils.EmbeddedCassandraServerHelper;
|
||||
import org.junit.After;
|
||||
import org.junit.AfterClass;
|
||||
@@ -25,15 +23,24 @@ import org.springframework.data.cassandra.core.CassandraAdminOperations;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import com.datastax.driver.core.Cluster;
|
||||
import com.datastax.driver.core.Session;
|
||||
import com.datastax.driver.core.utils.UUIDs;
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotEquals;
|
||||
|
||||
/**
|
||||
* Live test for Cassandra testing.
|
||||
*
|
||||
* This can be converted to IntegrationTest once cassandra-unit tests can be executed in parallel and
|
||||
* multiple test servers started as part of test suite.
|
||||
*
|
||||
* Open cassandra-unit issue for parallel execution: https://github.com/jsevellec/cassandra-unit/issues/155
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = CassandraConfig.class)
|
||||
public class BookRepositoryIntegrationTest {
|
||||
private static final Log LOGGER = LogFactory.getLog(BookRepositoryIntegrationTest.class);
|
||||
public class BookRepositoryLiveTest {
|
||||
private static final Log LOGGER = LogFactory.getLog(BookRepositoryLiveTest.class);
|
||||
|
||||
public static final String KEYSPACE_CREATION_QUERY = "CREATE KEYSPACE IF NOT EXISTS testKeySpace WITH replication = { 'class': 'SimpleStrategy', 'replication_factor': '3' };";
|
||||
|
||||
@@ -47,8 +54,6 @@ public class BookRepositoryIntegrationTest {
|
||||
@Autowired
|
||||
private CassandraAdminOperations adminTemplate;
|
||||
|
||||
//
|
||||
|
||||
@BeforeClass
|
||||
public static void startCassandraEmbedded() throws InterruptedException, TTransportException, ConfigurationException, IOException {
|
||||
EmbeddedCassandraServerHelper.startEmbeddedCassandra();
|
||||
@@ -62,8 +67,8 @@ public class BookRepositoryIntegrationTest {
|
||||
}
|
||||
|
||||
@Before
|
||||
public void createTable() throws InterruptedException, TTransportException, ConfigurationException, IOException {
|
||||
adminTemplate.createTable(true, CqlIdentifier.cqlId(DATA_TABLE_NAME), Book.class, new HashMap<String, Object>());
|
||||
public void createTable() {
|
||||
adminTemplate.createTable(true, CqlIdentifier.cqlId(DATA_TABLE_NAME), Book.class, new HashMap<>());
|
||||
}
|
||||
|
||||
@Test
|
||||
+28
-23
@@ -1,17 +1,13 @@
|
||||
package com.baeldung.spring.data.cassandra.repository;
|
||||
|
||||
import static junit.framework.TestCase.assertNull;
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
import com.baeldung.spring.data.cassandra.config.CassandraConfig;
|
||||
import com.baeldung.spring.data.cassandra.model.Book;
|
||||
import com.datastax.driver.core.Cluster;
|
||||
import com.datastax.driver.core.Session;
|
||||
import com.datastax.driver.core.querybuilder.QueryBuilder;
|
||||
import com.datastax.driver.core.querybuilder.Select;
|
||||
import com.datastax.driver.core.utils.UUIDs;
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import org.apache.cassandra.exceptions.ConfigurationException;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
@@ -30,17 +26,28 @@ import org.springframework.data.cassandra.core.CassandraOperations;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import com.datastax.driver.core.Cluster;
|
||||
import com.datastax.driver.core.Session;
|
||||
import com.datastax.driver.core.querybuilder.QueryBuilder;
|
||||
import com.datastax.driver.core.querybuilder.Select;
|
||||
import com.datastax.driver.core.utils.UUIDs;
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
import static junit.framework.TestCase.assertNull;
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
/**
|
||||
* Live test for Cassandra testing.
|
||||
*
|
||||
* This can be converted to IntegrationTest once cassandra-unit tests can be executed in parallel and
|
||||
* multiple test servers started as part of test suite.
|
||||
*
|
||||
* Open cassandra-unit issue for parallel execution: https://github.com/jsevellec/cassandra-unit/issues/155
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = CassandraConfig.class)
|
||||
public class CassandraTemplateIntegrationTest {
|
||||
private static final Log LOGGER = LogFactory.getLog(CassandraTemplateIntegrationTest.class);
|
||||
public class CassandraTemplateLiveTest {
|
||||
private static final Log LOGGER = LogFactory.getLog(CassandraTemplateLiveTest.class);
|
||||
|
||||
public static final String KEYSPACE_CREATION_QUERY = "CREATE KEYSPACE IF NOT EXISTS testKeySpace " + "WITH replication = { 'class': 'SimpleStrategy', 'replication_factor': '3' };";
|
||||
|
||||
@@ -54,8 +61,6 @@ public class CassandraTemplateIntegrationTest {
|
||||
@Autowired
|
||||
private CassandraOperations cassandraTemplate;
|
||||
|
||||
//
|
||||
|
||||
@BeforeClass
|
||||
public static void startCassandraEmbedded() throws InterruptedException, TTransportException, ConfigurationException, IOException {
|
||||
EmbeddedCassandraServerHelper.startEmbeddedCassandra();
|
||||
@@ -69,8 +74,8 @@ public class CassandraTemplateIntegrationTest {
|
||||
}
|
||||
|
||||
@Before
|
||||
public void createTable() throws InterruptedException, TTransportException, ConfigurationException, IOException {
|
||||
adminTemplate.createTable(true, CqlIdentifier.cqlId(DATA_TABLE_NAME), Book.class, new HashMap<String, Object>());
|
||||
public void createTable() {
|
||||
adminTemplate.createTable(true, CqlIdentifier.cqlId(DATA_TABLE_NAME), Book.class, new HashMap<>());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -111,7 +116,7 @@ public class CassandraTemplateIntegrationTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenDeletingASelectedBook_thenNotAvailableOnRetrieval() throws InterruptedException {
|
||||
public void whenDeletingASelectedBook_thenNotAvailableOnRetrieval() {
|
||||
final Book javaBook = new Book(UUIDs.timeBased(), "Head First Java", "OReilly Media", ImmutableSet.of("Computer", "Software"));
|
||||
cassandraTemplate.insert(javaBook);
|
||||
cassandraTemplate.delete(javaBook);
|
||||
+28
-26
@@ -1,19 +1,16 @@
|
||||
package com.baeldung.spring.data.cassandra.repository;
|
||||
|
||||
import static junit.framework.TestCase.assertEquals;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.apache.cassandra.exceptions.ConfigurationException;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.apache.thrift.transport.TTransportException;
|
||||
import com.baeldung.spring.data.cassandra.config.CassandraConfig;
|
||||
import com.baeldung.spring.data.cassandra.model.Book;
|
||||
import com.datastax.driver.core.Cluster;
|
||||
import com.datastax.driver.core.Session;
|
||||
import com.datastax.driver.core.querybuilder.Insert;
|
||||
import com.datastax.driver.core.querybuilder.QueryBuilder;
|
||||
import com.datastax.driver.core.querybuilder.Select;
|
||||
import com.datastax.driver.core.utils.UUIDs;
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.cassandraunit.utils.EmbeddedCassandraServerHelper;
|
||||
import org.junit.After;
|
||||
import org.junit.AfterClass;
|
||||
@@ -28,18 +25,25 @@ import org.springframework.data.cassandra.core.CassandraOperations;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
import com.datastax.driver.core.Cluster;
|
||||
import com.datastax.driver.core.Session;
|
||||
import com.datastax.driver.core.querybuilder.Insert;
|
||||
import com.datastax.driver.core.querybuilder.QueryBuilder;
|
||||
import com.datastax.driver.core.querybuilder.Select;
|
||||
import com.datastax.driver.core.utils.UUIDs;
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import static junit.framework.TestCase.assertEquals;
|
||||
|
||||
/**
|
||||
* Live test for Cassandra testing.
|
||||
*
|
||||
* This can be converted to IntegrationTest once cassandra-unit tests can be executed in parallel and
|
||||
* multiple test servers started as part of test suite.
|
||||
*
|
||||
* Open cassandra-unit issue for parallel execution: https://github.com/jsevellec/cassandra-unit/issues/155
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = CassandraConfig.class)
|
||||
public class CqlQueriesIntegrationTest {
|
||||
private static final Log LOGGER = LogFactory.getLog(CqlQueriesIntegrationTest.class);
|
||||
public class CqlQueriesLiveTest {
|
||||
private static final Log LOGGER = LogFactory.getLog(CqlQueriesLiveTest.class);
|
||||
|
||||
public static final String KEYSPACE_CREATION_QUERY = "CREATE KEYSPACE IF NOT EXISTS testKeySpace " + "WITH replication = { 'class': 'SimpleStrategy', 'replication_factor': '3' };";
|
||||
|
||||
@@ -53,10 +57,8 @@ public class CqlQueriesIntegrationTest {
|
||||
@Autowired
|
||||
private CassandraOperations cassandraTemplate;
|
||||
|
||||
//
|
||||
|
||||
@BeforeClass
|
||||
public static void startCassandraEmbedded() throws InterruptedException, TTransportException, ConfigurationException, IOException {
|
||||
public static void startCassandraEmbedded() throws Exception {
|
||||
EmbeddedCassandraServerHelper.startEmbeddedCassandra(25000);
|
||||
final Cluster cluster = Cluster.builder().addContactPoints("127.0.0.1").withPort(9142).build();
|
||||
LOGGER.info("Server Started at 127.0.0.1:9142... ");
|
||||
@@ -68,8 +70,8 @@ public class CqlQueriesIntegrationTest {
|
||||
}
|
||||
|
||||
@Before
|
||||
public void createTable() throws InterruptedException, TTransportException, ConfigurationException, IOException {
|
||||
adminTemplate.createTable(true, CqlIdentifier.cqlId(DATA_TABLE_NAME), Book.class, new HashMap<String, Object>());
|
||||
public void createTable() {
|
||||
adminTemplate.createTable(true, CqlIdentifier.cqlId(DATA_TABLE_NAME), Book.class, new HashMap<>());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -67,6 +67,7 @@
|
||||
<start-class>com.baeldung.boot.Application</start-class>
|
||||
<testcontainers.postgresql.version>1.10.6</testcontainers.postgresql.version>
|
||||
<postgresql.version>42.2.5</postgresql.version>
|
||||
<spring-boot.version>2.6.1</spring-boot.version> <!-- Temp downgrade due to spring-data-jpa issue. Fixing in JAVA-9857 -->
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
@@ -124,7 +124,7 @@
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<spring-tx.version>5.3.13</spring-tx.version>
|
||||
<spring-tx.version>5.3.15</spring-tx.version>
|
||||
<httpclient.version>4.5.2</httpclient.version>
|
||||
<reactor-core.version>3.3.1.RELEASE</reactor-core.version>
|
||||
<embed.mongo.version>3.2.6</embed.mongo.version>
|
||||
|
||||
@@ -78,7 +78,7 @@
|
||||
|
||||
<properties>
|
||||
<!-- Spring -->
|
||||
<org.springframework.version>5.3.13</org.springframework.version>
|
||||
<org.springframework.version>5.3.15</org.springframework.version>
|
||||
<!-- persistence -->
|
||||
<spring-mybatis.version>2.0.6</spring-mybatis.version>
|
||||
<mybatis.version>3.5.2</mybatis.version>
|
||||
|
||||
Reference in New Issue
Block a user