Merge pull request #14705 from ehsansasanian/java-21464-upgrade-java-cassandra-to-jdk17

java-21464: upgrade java-cassandra to jdk17
This commit is contained in:
Kasra Madadipouya 2023-09-12 00:24:01 +02:00 committed by GitHub
commit 6978371d17
8 changed files with 93 additions and 148 deletions

View File

@ -13,26 +13,11 @@
</parent> </parent>
<dependencies> <dependencies>
<!-- Cassandra -->
<dependency> <dependency>
<groupId>com.datastax.cassandra</groupId> <groupId>com.datastax.cassandra</groupId>
<artifactId>cassandra-driver-core</artifactId> <artifactId>cassandra-driver-core</artifactId>
<version>${cassandra-driver-core.version}</version> <version>${cassandra-driver-core.version}</version>
<optional>true</optional>
<exclusions>
<exclusion>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</exclusion>
</exclusions>
</dependency> </dependency>
<!-- https://mvnrepository.com/artifact/org.cassandraunit/cassandra-unit -->
<dependency>
<groupId>org.cassandraunit</groupId>
<artifactId>cassandra-unit</artifactId>
<version>${cassandra-unit.version}</version>
</dependency>
<!-- DataStax Cassandra -->
<dependency> <dependency>
<groupId>com.datastax.oss</groupId> <groupId>com.datastax.oss</groupId>
<artifactId>java-driver-core</artifactId> <artifactId>java-driver-core</artifactId>
@ -48,32 +33,28 @@
<artifactId>netty-transport</artifactId> <artifactId>netty-transport</artifactId>
<version>${netty-transport-version}</version> <version>${netty-transport-version}</version>
</dependency> </dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>cassandra</artifactId>
<version>${cassandra-testcontainer.version}</version>
<scope>test</scope>
</dependency>
</dependencies> </dependencies>
<profiles> <build>
<profile> <plugins>
<id>integration-lite-first</id> <plugin>
<groupId>org.apache.maven.plugins</groupId>
<build> <artifactId>maven-compiler-plugin</artifactId>
<plugins> </plugin>
<plugin> </plugins>
<groupId>org.apache.maven.plugins</groupId> </build>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<forkCount>1</forkCount>
<reuseForks>true</reuseForks>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
<properties> <properties>
<!-- Cassandra --> <!-- Cassandra -->
<cassandra-driver-core.version>3.1.2</cassandra-driver-core.version> <cassandra-driver-core.version>3.1.2</cassandra-driver-core.version>
<cassandra-unit.version>3.1.1.0</cassandra-unit.version>
<datastax-cassandra.version>4.1.0</datastax-cassandra.version> <datastax-cassandra.version>4.1.0</datastax-cassandra.version>
<cassandra-testcontainer.version>1.15.3</cassandra-testcontainer.version>
<netty-transport-version>4.1.71.Final</netty-transport-version> <netty-transport-version>4.1.71.Final</netty-transport-version>
</properties> </properties>

View File

@ -5,8 +5,6 @@ import com.datastax.oss.driver.api.core.CqlSessionBuilder;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import org.apache.commons.lang3.StringUtils;
public class CassandraConnector { public class CassandraConnector {
private CqlSession session; private CqlSession session;
@ -14,11 +12,11 @@ public class CassandraConnector {
public void connect(final String node, final Integer port, final String dataCenter) { public void connect(final String node, final Integer port, final String dataCenter) {
CqlSessionBuilder builder = CqlSession.builder(); CqlSessionBuilder builder = CqlSession.builder();
builder.addContactPoint(new InetSocketAddress(node, port)); builder.addContactPoint(new InetSocketAddress(node, port));
if (StringUtils.isNotBlank(dataCenter)) { if (dataCenter != null && !dataCenter.isEmpty()) {
builder.withLocalDatacenter(dataCenter); builder.withLocalDatacenter(dataCenter);
} }
session = builder.build(); session = builder.build();
} }
public CqlSession getSession() { public CqlSession getSession() {

View File

@ -1,5 +1,6 @@
package com.baeldung.datastax.cassandra; package com.baeldung.datastax.cassandra;
import com.baeldung.cassandra.batch.repository.ProductRepository;
import com.baeldung.datastax.cassandra.domain.Video; import com.baeldung.datastax.cassandra.domain.Video;
import com.baeldung.datastax.cassandra.repository.KeyspaceRepository; import com.baeldung.datastax.cassandra.repository.KeyspaceRepository;
import com.baeldung.datastax.cassandra.repository.VideoRepository; import com.baeldung.datastax.cassandra.repository.VideoRepository;
@ -30,9 +31,11 @@ public class Application {
keyspaceRepository.useKeyspace("testKeyspace"); keyspaceRepository.useKeyspace("testKeyspace");
VideoRepository videoRepository = new VideoRepository(session); VideoRepository videoRepository = new VideoRepository(session);
ProductRepository productRepository = new ProductRepository(session);
videoRepository.createTable(); videoRepository.createTable();
productRepository.createProductTableByName("testKeyspace");
videoRepository.insertVideo(new Video("Video Title 1", Instant.now())); videoRepository.insertVideo(new Video("Video Title 1", Instant.now()));
videoRepository.insertVideo(new Video("Video Title 2", videoRepository.insertVideo(new Video("Video Title 2",
Instant.now().minus(1, ChronoUnit.DAYS))); Instant.now().minus(1, ChronoUnit.DAYS)));

View File

@ -1,24 +1,5 @@
package com.baeldung.cassandra.batch.epository; package com.baeldung.cassandra.batch.epository;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import org.apache.cassandra.exceptions.ConfigurationException;
import org.apache.thrift.transport.TTransportException;
import org.cassandraunit.utils.EmbeddedCassandraServerHelper;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;
import com.baeldung.cassandra.batch.CassandraConnector;
import com.baeldung.cassandra.batch.domain.Product; import com.baeldung.cassandra.batch.domain.Product;
import com.baeldung.cassandra.batch.repository.KeyspaceRepository; import com.baeldung.cassandra.batch.repository.KeyspaceRepository;
import com.baeldung.cassandra.batch.repository.ProductRepository; import com.baeldung.cassandra.batch.repository.ProductRepository;
@ -26,10 +7,25 @@ import com.datastax.oss.driver.api.core.CqlIdentifier;
import com.datastax.oss.driver.api.core.CqlSession; import com.datastax.oss.driver.api.core.CqlSession;
import com.datastax.oss.driver.api.core.cql.ColumnDefinition; import com.datastax.oss.driver.api.core.cql.ColumnDefinition;
import com.datastax.oss.driver.api.core.cql.ResultSet; import com.datastax.oss.driver.api.core.cql.ResultSet;
import org.junit.Before;
import org.junit.FixMethodOrder;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runners.MethodSorters;
import org.testcontainers.containers.CassandraContainer;
import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
@FixMethodOrder(MethodSorters.NAME_ASCENDING) @FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class ProductRepositoryIntegrationTest { public class ProductRepositoryLiveTest {
@Rule
public CassandraContainer<?> cassandra = new CassandraContainer<>("cassandra:3.11.2");
private KeyspaceRepository schemaRepository; private KeyspaceRepository schemaRepository;
private ProductRepository productRepository; private ProductRepository productRepository;
@ -39,21 +35,20 @@ public class ProductRepositoryIntegrationTest {
private final String KEYSPACE_NAME = "testBaeldungKeyspace"; private final String KEYSPACE_NAME = "testBaeldungKeyspace";
private final String PRODUCT = "product"; private final String PRODUCT = "product";
@BeforeClass
public static void init() throws ConfigurationException, TTransportException, IOException, InterruptedException {
// Start an embedded Cassandra Server
EmbeddedCassandraServerHelper.startEmbeddedCassandra(20000L);
}
@Before @Before
public void connect() { public void connect() {
CassandraConnector client = new CassandraConnector(); cassandra.start();
client.connect("127.0.0.1", 9142,"datacenter1");
session = client.getSession(); this.session = CqlSession
schemaRepository = new KeyspaceRepository(client.getSession()); .builder()
.addContactPoint(new InetSocketAddress(cassandra.getHost(),cassandra.getFirstMappedPort()))
.withLocalDatacenter("datacenter1")
.build();
schemaRepository = new KeyspaceRepository(this.session);
schemaRepository.createKeyspace(KEYSPACE_NAME, 1); schemaRepository.createKeyspace(KEYSPACE_NAME, 1);
schemaRepository.useKeyspace(KEYSPACE_NAME); schemaRepository.useKeyspace(KEYSPACE_NAME);
productRepository = new ProductRepository(client.getSession()); productRepository = new ProductRepository(this.session);
} }
@Test @Test
@ -114,14 +109,7 @@ public class ProductRepositoryIntegrationTest {
assertEquals(productList.get(0).getPrice(), 12f,0f); assertEquals(productList.get(0).getPrice(), 12f,0f);
assertEquals(productList.get(1).getPrice(), 12f,0f); assertEquals(productList.get(1).getPrice(), 12f,0f);
} }
@AfterClass
public static void cleanup() {
EmbeddedCassandraServerHelper.cleanEmbeddedCassandra();
}
private Product getTestProduct() { private Product getTestProduct() {
Product product = new Product(); Product product = new Product();
product.setProductName("Banana"); product.setProductName("Banana");

View File

@ -1,31 +1,24 @@
package com.baeldung.cassandra.java.client.repository; package com.baeldung.cassandra.java.client.repository;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertEquals;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;
import org.apache.cassandra.exceptions.ConfigurationException;
import org.apache.thrift.transport.TTransportException;
import org.cassandraunit.utils.EmbeddedCassandraServerHelper;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import com.baeldung.cassandra.java.client.CassandraConnector;
import com.baeldung.cassandra.java.client.domain.Book; import com.baeldung.cassandra.java.client.domain.Book;
import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.ResultSet; import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.Session; import com.datastax.driver.core.Session;
import com.datastax.driver.core.exceptions.InvalidQueryException; import com.datastax.driver.core.exceptions.InvalidQueryException;
import com.datastax.driver.core.utils.UUIDs; import com.datastax.driver.core.utils.UUIDs;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.testcontainers.containers.CassandraContainer;
public class BookRepositoryIntegrationTest { import java.util.List;
import java.util.stream.Collectors;
private KeyspaceRepository schemaRepository; import static org.junit.Assert.*;
public class BookRepositoryLiveTest {
@Rule
public CassandraContainer<?> cassandra = new CassandraContainer<>("cassandra:3.11.2");
private BookRepository bookRepository; private BookRepository bookRepository;
@ -35,18 +28,18 @@ public class BookRepositoryIntegrationTest {
final String BOOKS = "books"; final String BOOKS = "books";
final String BOOKS_BY_TITLE = "booksByTitle"; final String BOOKS_BY_TITLE = "booksByTitle";
@BeforeClass
public static void init() throws ConfigurationException, TTransportException, IOException, InterruptedException {
// Start an embedded Cassandra Server
EmbeddedCassandraServerHelper.startEmbeddedCassandra(20000L);
}
@Before @Before
public void connect() { public void connect() {
CassandraConnector client = new CassandraConnector(); cassandra.start();
client.connect("127.0.0.1", 9142);
this.session = client.getSession(); this.session = Cluster
schemaRepository = new KeyspaceRepository(session); .builder()
.addContactPoint(cassandra.getHost())
.withPort(cassandra.getMappedPort(CassandraContainer.CQL_PORT))
.build()
.newSession();
KeyspaceRepository schemaRepository = new KeyspaceRepository(session);
schemaRepository.createKeyspace(KEYSPACE_NAME, "SimpleStrategy", 1); schemaRepository.createKeyspace(KEYSPACE_NAME, "SimpleStrategy", 1);
schemaRepository.useKeyspace(KEYSPACE_NAME); schemaRepository.useKeyspace(KEYSPACE_NAME);
bookRepository = new BookRepository(session); bookRepository = new BookRepository(session);
@ -166,9 +159,4 @@ public class BookRepositoryIntegrationTest {
session.execute("SELECT * FROM " + KEYSPACE_NAME + "." + BOOKS + ";"); session.execute("SELECT * FROM " + KEYSPACE_NAME + "." + BOOKS + ";");
} }
@AfterClass
public static void cleanup() {
EmbeddedCassandraServerHelper.cleanEmbeddedCassandra();
}
} }

View File

@ -1,45 +1,38 @@
package com.baeldung.cassandra.java.client.repository; package com.baeldung.cassandra.java.client.repository;
import static org.junit.Assert.assertFalse; import com.datastax.driver.core.Cluster;
import static org.junit.Assert.assertTrue; import com.datastax.driver.core.ResultSet;
import static org.junit.Assert.assertEquals; import com.datastax.driver.core.Session;
import org.junit.Before;
import org.junit.FixMethodOrder;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runners.MethodSorters;
import org.testcontainers.containers.CassandraContainer;
import java.io.IOException;
import java.util.List; import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import org.apache.cassandra.exceptions.ConfigurationException; import static org.junit.Assert.*;
import org.apache.thrift.transport.TTransportException;
import org.cassandraunit.utils.EmbeddedCassandraServerHelper;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;
import com.baeldung.cassandra.java.client.CassandraConnector;
import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.Session;
@FixMethodOrder(MethodSorters.NAME_ASCENDING) @FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class KeyspaceRepositoryIntegrationTest { public class KeyspaceRepositoryLiveTest {
@Rule
public CassandraContainer<?> cassandra = new CassandraContainer<>("cassandra:3.11.2");
private KeyspaceRepository schemaRepository; private KeyspaceRepository schemaRepository;
private Session session; private Session session;
@BeforeClass
public static void init() throws ConfigurationException, TTransportException, IOException, InterruptedException {
// Start an embedded Cassandra Server
EmbeddedCassandraServerHelper.startEmbeddedCassandra(20000L);
}
@Before @Before
public void connect() { public void connect() {
CassandraConnector client = new CassandraConnector(); cassandra.start();
client.connect("127.0.0.1", 9142);
this.session = client.getSession(); this.session = Cluster
.builder()
.addContactPoint(cassandra.getHost())
.withPort(cassandra.getMappedPort(CassandraContainer.CQL_PORT))
.build()
.newSession();
schemaRepository = new KeyspaceRepository(session); schemaRepository = new KeyspaceRepository(session);
} }
@ -62,16 +55,11 @@ public class KeyspaceRepositoryIntegrationTest {
public void whenDeletingAKeyspace_thenDoesNotExist() { public void whenDeletingAKeyspace_thenDoesNotExist() {
String keyspaceName = "testBaeldungKeyspace"; String keyspaceName = "testBaeldungKeyspace";
// schemaRepository.createKeyspace(keyspaceName, "SimpleStrategy", 1); schemaRepository.createKeyspace(keyspaceName, "SimpleStrategy", 1);
schemaRepository.deleteKeyspace(keyspaceName); schemaRepository.deleteKeyspace(keyspaceName);
ResultSet result = session.execute("SELECT * FROM system_schema.keyspaces;"); ResultSet result = session.execute("SELECT * FROM system_schema.keyspaces;");
boolean isKeyspaceCreated = result.all().stream().anyMatch(r -> r.getString(0).equals(keyspaceName.toLowerCase())); boolean isKeyspaceCreated = result.all().stream().anyMatch(r -> r.getString(0).equals(keyspaceName.toLowerCase()));
assertFalse(isKeyspaceCreated); assertFalse(isKeyspaceCreated);
} }
@AfterClass
public static void cleanup() {
EmbeddedCassandraServerHelper.cleanEmbeddedCassandra();
}
} }

View File

@ -35,6 +35,7 @@
<module>hibernate-queries</module> <module>hibernate-queries</module>
<module>hibernate-enterprise</module> <module>hibernate-enterprise</module>
<module>influxdb</module> <module>influxdb</module>
<module>java-cassandra</module>
<module>java-cockroachdb</module> <module>java-cockroachdb</module>
<module>java-jdbi</module> <module>java-jdbi</module>
<module>java-jpa</module> <!-- long running --> <module>java-jpa</module> <!-- long running -->

View File

@ -366,7 +366,6 @@
<module>web-modules/restx</module> <module>web-modules/restx</module>
<module>persistence-modules/deltaspike</module> <!-- delta spike it doesn't support yet the jakarta API--> <module>persistence-modules/deltaspike</module> <!-- delta spike it doesn't support yet the jakarta API-->
<module>persistence-modules/hibernate-ogm</module> <!-- hibernate-ogm wasn't updated because it doesn't support jakarta API --> <module>persistence-modules/hibernate-ogm</module> <!-- hibernate-ogm wasn't updated because it doesn't support jakarta API -->
<module>persistence-modules/java-cassandra</module> <!-- JAVA-21464 cassandra-unit library doesn't support to run with jdk9 and above -->
<module>persistence-modules/spring-data-cassandra-reactive</module> <!--JAVA-21844--> <module>persistence-modules/spring-data-cassandra-reactive</module> <!--JAVA-21844-->
<module>java-nashorn</module> <module>java-nashorn</module>
<module>jeromq</module> <module>jeromq</module>
@ -542,7 +541,6 @@
<module>web-modules/restx</module> <module>web-modules/restx</module>
<module>persistence-modules/deltaspike</module> <!-- delta spike it doesn't support yet the jakarta API--> <module>persistence-modules/deltaspike</module> <!-- delta spike it doesn't support yet the jakarta API-->
<module>persistence-modules/hibernate-ogm</module> <!-- hibernate-ogm wasn't updated because it doesn't support jakarta API --> <module>persistence-modules/hibernate-ogm</module> <!-- hibernate-ogm wasn't updated because it doesn't support jakarta API -->
<module>persistence-modules/java-cassandra</module> <!-- JAVA-21464 cassandra-unit library doesn't support to run with jdk9 and above -->
<module>persistence-modules/spring-data-cassandra-reactive</module> <!--JAVA-21844--> <module>persistence-modules/spring-data-cassandra-reactive</module> <!--JAVA-21844-->
<module>java-nashorn</module> <module>java-nashorn</module>
<module>spring-ejb-modules/ejb-beans</module> <module>spring-ejb-modules/ejb-beans</module>