From a1feafef67815de5059e6d89f2eba4b78f711d75 Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Wed, 30 Mar 2016 15:21:17 +0200 Subject: [PATCH] DATACASS-231 - Polishing. Add author. Add/update license headers where needed. Improve JavaDoc. Tweaked test names. Adopted current driver behavior in tests. Original pull request: #41. --- .../CassandraCqlClusterFactoryBean.java | 151 +++++++++++++++--- .../CassandraCqlClusterFactoryBeanTest.java | 43 ----- .../CassandraCqlClusterFactoryBeanTests.java | 73 +++++++++ 3 files changed, 199 insertions(+), 68 deletions(-) delete mode 100644 spring-cql/src/test/java/org/springframework/cassandra/test/unit/config/CassandraCqlClusterFactoryBeanTest.java create mode 100644 spring-cql/src/test/java/org/springframework/cassandra/test/unit/config/CassandraCqlClusterFactoryBeanTests.java diff --git a/spring-cql/src/main/java/org/springframework/cassandra/config/CassandraCqlClusterFactoryBean.java b/spring-cql/src/main/java/org/springframework/cassandra/config/CassandraCqlClusterFactoryBean.java index 48f4bf675..9e0d82c70 100644 --- a/spring-cql/src/main/java/org/springframework/cassandra/config/CassandraCqlClusterFactoryBean.java +++ b/spring-cql/src/main/java/org/springframework/cassandra/config/CassandraCqlClusterFactoryBean.java @@ -1,12 +1,12 @@ /* - * Copyright 2013-2014 the original author or authors. - * + * Copyright 2013-2016 the original author or authors. + * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -53,17 +53,17 @@ import com.datastax.driver.core.policies.RetryPolicy; /** * Convenient factory for configuring a Cassandra Cluster. - * + * * @author Alex Shvid * @author Matthew T. Adams * @author David Webb + * @author Kirk Clemens */ -public class CassandraCqlClusterFactoryBean implements FactoryBean, InitializingBean, DisposableBean, - PersistenceExceptionTranslator { +public class CassandraCqlClusterFactoryBean + implements FactoryBean, InitializingBean, DisposableBean, PersistenceExceptionTranslator { public static final String DEFAULT_CONTACT_POINTS = "localhost"; public static final boolean DEFAULT_METRICS_ENABLED = true; - public static final boolean DEFAULT_DEFERRED_INITIALIZATION = false; public static final boolean DEFAULT_JMX_REPORTING_ENABLED = true; public static final boolean DEFAULT_SSL_ENABLED = false; public static final int DEFAULT_PORT = 9042; @@ -180,7 +180,7 @@ public class CassandraCqlClusterFactoryBean implements FactoryBean, Ini } } - if(protocolVersion != null) { + if (protocolVersion != null) { builder.withProtocolVersion(protocolVersion); } @@ -231,9 +231,9 @@ public class CassandraCqlClusterFactoryBean implements FactoryBean, Ini Iterator i = specs.iterator(); while (i.hasNext()) { KeyspaceActionSpecification spec = (KeyspaceActionSpecification) i.next(); - String cql = (spec instanceof CreateKeyspaceSpecification) ? new CreateKeyspaceCqlGenerator( - (CreateKeyspaceSpecification) spec).toCql() : new DropKeyspaceCqlGenerator( - (DropKeyspaceSpecification) spec).toCql(); + String cql = (spec instanceof CreateKeyspaceSpecification) + ? new CreateKeyspaceCqlGenerator((CreateKeyspaceSpecification) spec).toCql() + : new DropKeyspaceCqlGenerator((DropKeyspaceSpecification) spec).toCql(); template.execute(cql); } @@ -274,86 +274,169 @@ public class CassandraCqlClusterFactoryBean implements FactoryBean, Ini } /** - * Sets a comma-delimited string of the contact points (hosts) to connect to. + * Set a comma-delimited string of the contact points (hosts) to connect to. Default is {@code localhost}, see + * {@link #DEFAULT_CONTACT_POINTS}. + * + * @param contactPoints */ public void setContactPoints(String contactPoints) { this.contactPoints = contactPoints; } + /** + * Set the port for the contact points. Default is {@code 9042}, see {@link #DEFAULT_PORT}. + * + * @param port + */ public void setPort(int port) { this.port = port; } + /** + * Set the {@link CompressionType}. Default is uncompressed. + * + * @param compressionType + */ public void setCompressionType(CompressionType compressionType) { this.compressionType = compressionType; } + /** + * Set the {@link PoolingOptions}. + * + * @param poolingOptions + */ public void setPoolingOptions(PoolingOptions poolingOptions) { this.poolingOptions = poolingOptions; } - public void setProtocolVersion(ProtocolVersion protocolVersion){ + /** + * Set the {@link ProtocolVersion}. + * + * @param protocolVersion + * @since 1.4 + */ + public void setProtocolVersion(ProtocolVersion protocolVersion) { this.protocolVersion = protocolVersion; } + /** + * Set the {@link SocketOptions} containing low-level socket options. + * + * @param socketOptions + */ public void setSocketOptions(SocketOptions socketOptions) { this.socketOptions = socketOptions; } + /** + * Set the {@link AuthProvider}. Default is unauthenticated. + * + * @param authProvider + */ public void setAuthProvider(AuthProvider authProvider) { this.authProvider = authProvider; } + /** + * Set the {@link LoadBalancingPolicy}. + * + * @param loadBalancingPolicy + */ public void setLoadBalancingPolicy(LoadBalancingPolicy loadBalancingPolicy) { this.loadBalancingPolicy = loadBalancingPolicy; } + /** + * Set the {@link ReconnectionPolicy}. + * + * @param reconnectionPolicy + */ public void setReconnectionPolicy(ReconnectionPolicy reconnectionPolicy) { this.reconnectionPolicy = reconnectionPolicy; } + /** + * Set the {@link RetryPolicy}. + * + * @param retryPolicy + */ public void setRetryPolicy(RetryPolicy retryPolicy) { this.retryPolicy = retryPolicy; } + /** + * Set whether metrics are enabled. Default is {@literal true}, see {@link #DEFAULT_METRICS_ENABLED}. + * + * @param metricsEnabled + */ public void setMetricsEnabled(boolean metricsEnabled) { this.metricsEnabled = metricsEnabled; } + /** + * Set a {@link List} of {@link CreateKeyspaceSpecification create keyspace specifications} that are executed when + * this factory is {@link #afterPropertiesSet() initialized}. {@link CreateKeyspaceSpecification Create keyspace + * specifications} are executed on a system session with no keyspace set, before executing + * {@link #setStartupScripts(List)}. + * + * @param specifications + */ public void setKeyspaceCreations(List specifications) { this.keyspaceCreations = specifications; } + /** + * Return a {@link List} of {@link CreateKeyspaceSpecification create keyspace specifications}. + * + * @return + */ public List getKeyspaceCreations() { return keyspaceCreations; } + /** + * Set a {@link List} of {@link DropKeyspaceSpecification drop keyspace specifications} that are executed when this + * factory is {@link #destroy() destroyed}. {@link DropKeyspaceSpecification Drop keyspace specifications} are + * executed on a system session with no keyspace set, before executing {@link #setShutdownScripts(List)}. + * + * @param specifications + */ public void setKeyspaceDrops(List specifications) { this.keyspaceDrops = specifications; } + /** + * Reurn the {@link List} of {@link DropKeyspaceSpecification drop keyspace specifications}. + * + * @return + */ public List getKeyspaceDrops() { return keyspaceDrops; } + /** + * Set a {@link List} of raw {@link String CQL statements} that are executed when this factory is + * {@link #afterPropertiesSet() initialized}. Scripts are executed on a system session with no keyspace set, after + * executing {@link #setKeyspaceCreations(List)}. + * + * @param scripts + */ public void setStartupScripts(List scripts) { this.startupScripts = scripts; } + /** + * Set a {@link List} of raw {@link String CQL statements} that are executed when this factory is {@link #destroy() + * destroyed}. {@link DropKeyspaceSpecification Drop keyspace specifications} are executed on a system session with no + * keyspace set, after executing {@link #setKeyspaceDrops(List)}. + * + * @param scripts + */ public void setShutdownScripts(List scripts) { this.shutdownScripts = scripts; } - private static Compression convertCompressionType(CompressionType type) { - switch (type) { - case NONE: - return Compression.NONE; - case SNAPPY: - return Compression.SNAPPY; - } - throw new IllegalArgumentException("unknown compression type " + type); - } - /** * @return Returns the keyspaceSpecifications. */ @@ -363,7 +446,7 @@ public class CassandraCqlClusterFactoryBean implements FactoryBean, Ini /** * If accumlating is true, we append to the list, otherwise we replace the list. - * + * * @param keyspaceSpecifications The keyspaceSpecifications to set. */ public void setKeyspaceSpecifications(Set> keyspaceSpecifications) { @@ -371,6 +454,8 @@ public class CassandraCqlClusterFactoryBean implements FactoryBean, Ini } /** + * Set the username to use with {@link com.datastax.driver.core.PlainTextAuthProvider}. + * * @param username The username to set. */ public void setUsername(String username) { @@ -378,6 +463,8 @@ public class CassandraCqlClusterFactoryBean implements FactoryBean, Ini } /** + * Set the username to use with {@link com.datastax.driver.core.PlainTextAuthProvider}. + * * @param password The password to set. */ public void setPassword(String password) { @@ -385,6 +472,8 @@ public class CassandraCqlClusterFactoryBean implements FactoryBean, Ini } /** + * Set whether to use JMX reporting. Default is {@literal false}, see {@link #DEFAULT_JMX_REPORTING_ENABLED}. + * * @param jmxReportingEnabled The jmxReportingEnabled to set. */ public void setJmxReportingEnabled(boolean jmxReportingEnabled) { @@ -392,6 +481,8 @@ public class CassandraCqlClusterFactoryBean implements FactoryBean, Ini } /** + * Set whether to use SSL. Default is plain, see {@link #DEFAULT_SSL_ENABLED}. + * * @param sslEnabled The sslEnabled to set. */ public void setSslEnabled(boolean sslEnabled) { @@ -418,4 +509,14 @@ public class CassandraCqlClusterFactoryBean implements FactoryBean, Ini public void setLatencyTracker(LatencyTracker latencyTracker) { this.latencyTracker = latencyTracker; } + + private static Compression convertCompressionType(CompressionType type) { + switch (type) { + case NONE: + return Compression.NONE; + case SNAPPY: + return Compression.SNAPPY; + } + throw new IllegalArgumentException("unknown compression type " + type); + } } diff --git a/spring-cql/src/test/java/org/springframework/cassandra/test/unit/config/CassandraCqlClusterFactoryBeanTest.java b/spring-cql/src/test/java/org/springframework/cassandra/test/unit/config/CassandraCqlClusterFactoryBeanTest.java deleted file mode 100644 index e43edf540..000000000 --- a/spring-cql/src/test/java/org/springframework/cassandra/test/unit/config/CassandraCqlClusterFactoryBeanTest.java +++ /dev/null @@ -1,43 +0,0 @@ -package org.springframework.cassandra.test.unit.config; - -import com.datastax.driver.core.ProtocolVersion; -import org.junit.Assert; -import org.junit.Test; -import org.springframework.cassandra.config.CassandraCqlClusterFactoryBean; - -/** - * JUnit test case for CassandraCqlClusterFactoryBean - * - * @author Kirk Clemens - */ -public class CassandraCqlClusterFactoryBeanTest{ - - @Test - public void testProtocolVersion(){ - final CassandraCqlClusterFactoryBean cassandraCqlClusterFactoryBean = new CassandraCqlClusterFactoryBean(); - cassandraCqlClusterFactoryBean.setProtocolVersion(ProtocolVersion.V2); - try { - cassandraCqlClusterFactoryBean.afterPropertiesSet(); - Assert.assertEquals( - ProtocolVersion.V2, - cassandraCqlClusterFactoryBean.getObject().getConfiguration().getProtocolOptions().getProtocolVersionEnum() - ); - } catch (Exception e) { - Assert.fail("Unable to create CQL cluster bean" + e.getMessage()); - } - } - - @Test - public void testDefaultProtocolVersion(){ - final CassandraCqlClusterFactoryBean cassandraCqlClusterFactoryBean = new CassandraCqlClusterFactoryBean(); - - try { - cassandraCqlClusterFactoryBean.afterPropertiesSet(); - Assert.assertNull( - cassandraCqlClusterFactoryBean.getObject().getConfiguration().getProtocolOptions().getProtocolVersionEnum() - ); - } catch (Exception e) { - Assert.fail("Unable to create CQL cluster bean" + e.getMessage()); - } - } -} diff --git a/spring-cql/src/test/java/org/springframework/cassandra/test/unit/config/CassandraCqlClusterFactoryBeanTests.java b/spring-cql/src/test/java/org/springframework/cassandra/test/unit/config/CassandraCqlClusterFactoryBeanTests.java new file mode 100644 index 000000000..e4ac97a7e --- /dev/null +++ b/spring-cql/src/test/java/org/springframework/cassandra/test/unit/config/CassandraCqlClusterFactoryBeanTests.java @@ -0,0 +1,73 @@ +/* + * Copyright 2016 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.cassandra.test.unit.config; + +import static org.junit.Assert.*; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.springframework.cassandra.config.CassandraCqlClusterFactoryBean; +import org.springframework.cassandra.test.integration.AbstractEmbeddedCassandraIntegrationTest; + +import com.datastax.driver.core.ProtocolVersion; + +/** + * Integration tests for {@link CassandraCqlClusterFactoryBean}. + * + * @author Kirk Clemens + */ +public class CassandraCqlClusterFactoryBeanTests extends AbstractEmbeddedCassandraIntegrationTest { + + private CassandraCqlClusterFactoryBean cassandraCqlClusterFactoryBean; + + @Before + public void setUp() throws Exception { + cassandraCqlClusterFactoryBean = new CassandraCqlClusterFactoryBean(); + } + + @After + public void tearDown() throws Exception { + cassandraCqlClusterFactoryBean.destroy(); + } + + @Test + public void configuredProtocolVersionShouldBeSet() throws Exception { + + cassandraCqlClusterFactoryBean.setProtocolVersion(ProtocolVersion.V2); + cassandraCqlClusterFactoryBean.setPort(CASSANDRA_NATIVE_PORT); + cassandraCqlClusterFactoryBean.afterPropertiesSet(); + + assertEquals(ProtocolVersion.V2, getProtocolVersionEnum(cassandraCqlClusterFactoryBean)); + } + + @Test + public void defaultProtocolVersionShouldBeSet() throws Exception { + + cassandraCqlClusterFactoryBean.setPort(CASSANDRA_NATIVE_PORT); + cassandraCqlClusterFactoryBean.afterPropertiesSet(); + + assertEquals(ProtocolVersion.NEWEST_SUPPORTED, getProtocolVersionEnum(cassandraCqlClusterFactoryBean)); + } + + private ProtocolVersion getProtocolVersionEnum(CassandraCqlClusterFactoryBean cassandraCqlClusterFactoryBean) + throws Exception { + + // initialize connection factory + cassandraCqlClusterFactoryBean.getObject().init(); + return cassandraCqlClusterFactoryBean.getObject().getConfiguration().getProtocolOptions().getProtocolVersionEnum(); + } +}