> rows) {
+
+ Assert.notNull(rows);
+ Assert.notEmpty(rows);
+
+ Object[][] values = new Object[rows.size()][];
+ int i = 0;
+ for (List> row : rows) {
+ values[i++] = row.toArray();
+ }
+
+ ingest(cql, values);
+
+ }
+
+ /* (non-Javadoc)
+ * @see org.springframework.cassandra.core.CassandraOperations#execute(java.lang.String, java.lang.Object[][])
+ */
+ @Override
+ public void ingest(String cql, final Object[][] rows) {
+
+ ingest(cql, new RowIterator() {
+
+ int index = 0;
+
+ @Override
+ public Object[] next() {
+ return rows[index++];
+ }
+
+ @Override
+ public boolean hasNext() {
+ return index < rows.length;
+ }
+
+ });
+ }
}
\ No newline at end of file
diff --git a/src/main/java/org/springframework/cassandra/core/CqlParameter.java b/src/main/java/org/springframework/cassandra/core/CqlParameter.java
deleted file mode 100644
index bfd5db193..000000000
--- a/src/main/java/org/springframework/cassandra/core/CqlParameter.java
+++ /dev/null
@@ -1,139 +0,0 @@
-/*
- * Copyright 2011-2013 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.core;
-
-import java.util.LinkedList;
-import java.util.List;
-
-import org.springframework.util.Assert;
-
-import com.datastax.driver.core.DataType;
-
-/**
- * @author David Webb
- *
- */
-public class CqlParameter {
-
- /** The name of the parameter, if any */
- private String name;
-
- /** SQL type constant from {@link DataType} */
- private final DataType type;
-
- /** The scale to apply in case of a NUMERIC or DECIMAL type, if any */
- private Integer scale;
-
- /**
- * Create a new anonymous CqlParameter, supplying the SQL type.
- *
- * @param type Cassandra Data Type of the parameter according to {@link DataType}
- */
- public CqlParameter(DataType type) {
- this.type = type;
- }
-
- /**
- * Create a new anonymous CqlParameter, supplying the SQL type.
- *
- * @param type Cassandra Data Type of the parameter according to {@link DataType}
- * @param scale the number of digits after the decimal point
- */
- public CqlParameter(DataType type, int scale) {
- this.type = type;
- this.scale = scale;
- }
-
- /**
- * Create a new CqlParameter, supplying name and SQL type.
- *
- * @param name name of the parameter, as used in input and output maps
- * @param type Cassandra Data Type of the parameter according to {@link DataType}
- */
- public CqlParameter(String name, DataType type) {
- this.name = name;
- this.type = type;
- }
-
- /**
- * Create a new CqlParameter, supplying name and SQL type.
- *
- * @param name name of the parameter, as used in input and output maps
- * @param type Cassandra Data Type of the parameter according to {@link DataType}
- * @param scale the number of digits after the decimal point (for DECIMAL and NUMERIC types)
- */
- public CqlParameter(String name, DataType type, int scale) {
- this.name = name;
- this.type = type;
- this.scale = scale;
- }
-
- /**
- * Copy constructor.
- *
- * @param otherParam the CqlParameter object to copy from
- */
- public CqlParameter(CqlParameter otherParam) {
- Assert.notNull(otherParam, "CqlParameter object must not be null");
- this.name = otherParam.name;
- this.type = otherParam.type;
- this.scale = otherParam.scale;
- }
-
- /**
- * Return the name of the parameter.
- */
- public String getName() {
- return this.name;
- }
-
- /**
- * Return the SQL type of the parameter.
- */
- public DataType getType() {
- return this.type;
- }
-
- /**
- * Return the scale of the parameter, if any.
- */
- public Integer getScale() {
- return this.scale;
- }
-
- /**
- * Return whether this parameter holds input values that should be set before execution even if they are {@code null}.
- *
- * This implementation always returns {@code true}.
- */
- public boolean isInputValueProvided() {
- return true;
- }
-
- /**
- * Convert a list of JDBC types, as defined in {@code java.sql.Types}, to a List of CqlParameter objects as used in
- * this package.
- */
- public static List sqlTypesToAnonymousParameterList(DataType[] types) {
- List result = new LinkedList();
- if (types != null) {
- for (DataType type : types) {
- result.add(new CqlParameter(type));
- }
- }
- return result;
- }
-}
diff --git a/src/main/java/org/springframework/cassandra/core/CqlParameterValue.java b/src/main/java/org/springframework/cassandra/core/CqlParameterValue.java
deleted file mode 100644
index c2932815f..000000000
--- a/src/main/java/org/springframework/cassandra/core/CqlParameterValue.java
+++ /dev/null
@@ -1,68 +0,0 @@
-/*
- * Copyright 2011-2013 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.core;
-
-import com.datastax.driver.core.DataType;
-
-/**
- * @author David Webb
- *
- */
-public class CqlParameterValue extends CqlParameter {
-
- private final Object value;
-
- /**
- * Create a new CqlParameterValue, supplying the Cassandra DataType.
- *
- * @param type Cassandra Data Type of the parameter according to {@link DataType}
- * @param value the value object
- */
- public CqlParameterValue(DataType type, Object value) {
- super(type);
- this.value = value;
- }
-
- /**
- * Create a new CqlParameterValue, supplying the Cassandra DataType.
- *
- * @param type Cassandra Data Type of the parameter according to {@link DataType}
- * @param scale the number of digits after the decimal point (for DECIMAL and NUMERIC types)
- * @param value the value object
- */
- public CqlParameterValue(DataType type, int scale, Object value) {
- super(type, scale);
- this.value = value;
- }
-
- /**
- * Create a new CqlParameterValue based on the given CqlParameter declaration.
- *
- * @param declaredParam the declared CqlParameter to define a value for
- * @param value the value object
- */
- public CqlParameterValue(CqlParameter declaredParam, Object value) {
- super(declaredParam);
- this.value = value;
- }
-
- /**
- * Return the value object that this parameter value holds.
- */
- public Object getValue() {
- return this.value;
- }
-}
diff --git a/src/main/java/org/springframework/cassandra/core/PreparedStatementCreatorFactory.java b/src/main/java/org/springframework/cassandra/core/PreparedStatementCreatorFactory.java
deleted file mode 100644
index cbf34c58d..000000000
--- a/src/main/java/org/springframework/cassandra/core/PreparedStatementCreatorFactory.java
+++ /dev/null
@@ -1,207 +0,0 @@
-/*
- * Copyright 2011-2013 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.core;
-
-import java.util.Arrays;
-import java.util.Collections;
-import java.util.HashSet;
-import java.util.LinkedList;
-import java.util.List;
-import java.util.Set;
-
-import org.springframework.dao.InvalidDataAccessApiUsageException;
-import org.springframework.util.Assert;
-
-import com.datastax.driver.core.BoundStatement;
-import com.datastax.driver.core.PreparedStatement;
-import com.datastax.driver.core.Session;
-import com.datastax.driver.core.exceptions.DriverException;
-
-/**
- * @author David Webb
- *
- * @deprecated - Pattern from JDBC Template, but DW doesn't like it. Use {@link BoundStatementFactory}
- *
- */
-public class PreparedStatementCreatorFactory {
-
- /**
- * The CQL, which won't change when the parameters change
- */
- private final String cql;
-
- /** List of CqlParameter objects. May not be {@code null}. */
- private final List declaredParameters;
-
- /**
- * Create a new factory.
- */
- public PreparedStatementCreatorFactory(String cql) {
- this.cql = cql;
- this.declaredParameters = new LinkedList();
- }
-
- /**
- * Create a new factory with the given CQL and parameters.
- *
- * @param cql CQL
- * @param declaredParameters list of {@link CqlParameter} objects
- * @see CqlParameter
- */
- public PreparedStatementCreatorFactory(String cql, List declaredParameters) {
- this.cql = cql;
- this.declaredParameters = declaredParameters;
- }
-
- /**
- * Return a new PreparedStatementBinder for the given parameters.
- *
- * @param params list of parameters (may be {@code null})
- */
- public PreparedStatementBinder newPreparedStatementBinder(List params) {
- return new PreparedStatementCreatorImpl(params != null ? params : Collections.emptyList());
- }
-
- /**
- * Return a new PreparedStatementBinder for the given parameters.
- *
- * @param params the parameter array (may be {@code null})
- */
- public PreparedStatementBinder newPreparedStatementBinder(Object[] params) {
- return new PreparedStatementCreatorImpl(params != null ? Arrays.asList(params) : Collections.emptyList());
- }
-
- /**
- * Return a new PreparedStatementCreator for the given parameters.
- *
- * @param params list of parameters (may be {@code null})
- */
- public PreparedStatementCreator newPreparedStatementCreator(List params) {
- return new PreparedStatementCreatorImpl(params != null ? params : Collections.emptyList());
- }
-
- /**
- * Return a new PreparedStatementCreator for the given parameters.
- *
- * @param params the parameter array (may be {@code null})
- */
- public PreparedStatementCreator newPreparedStatementCreator(Object[] params) {
- return new PreparedStatementCreatorImpl(params != null ? Arrays.asList(params) : Collections.emptyList());
- }
-
- /**
- * Return a new PreparedStatementCreator for the given parameters.
- *
- * @param sqlToUse the actual SQL statement to use (if different from the factory's, for example because of named
- * parameter expanding)
- * @param params the parameter array (may be {@code null})
- */
- public PreparedStatementCreator newPreparedStatementCreator(String sqlToUse, Object[] params) {
- return new PreparedStatementCreatorImpl(sqlToUse, params != null ? Arrays.asList(params) : Collections.emptyList());
- }
-
- /**
- * PreparedStatementCreator implementation returned by this class.
- */
- private class PreparedStatementCreatorImpl implements PreparedStatementCreator, PreparedStatementBinder, CqlProvider {
-
- private final String actualCql;
-
- private final List> parameters;
-
- private PreparedStatement preparedStatement;
-
- public PreparedStatementCreatorImpl(List> parameters) {
- this(cql, parameters);
- }
-
- /**
- * @param actualCql
- * @param parameters
- */
- public PreparedStatementCreatorImpl(String actualCql, List> parameters) {
- this.actualCql = actualCql;
- Assert.notNull(parameters, "Parameters List must not be null");
- this.parameters = parameters;
- if (this.parameters.size() != declaredParameters.size()) {
- Set names = new HashSet();
- for (int i = 0; i < parameters.size(); i++) {
- Object param = parameters.get(i);
- if (param instanceof CqlParameterValue) {
- names.add(((CqlParameterValue) param).getName());
- } else {
- names.add("Parameter #" + i);
- }
- }
- if (names.size() != declaredParameters.size()) {
- throw new InvalidDataAccessApiUsageException("CQL [" + cql + "]: given " + names.size()
- + " parameters but expected " + declaredParameters.size());
- }
- }
-
- }
-
- /* (non-Javadoc)
- * @see org.springframework.cassandra.core.PreparedStatementCreator#createPreparedStatement(com.datastax.driver.core.Session)
- */
- @Override
- public PreparedStatement createPreparedStatement(Session session) throws DriverException {
- return session.prepare(this.actualCql);
- }
-
- /* (non-Javadoc)
- * @see org.springframework.cassandra.core.PreparedStatementBinder#bindValues(com.datastax.driver.core.PreparedStatement)
- */
- @Override
- public BoundStatement bindValues(PreparedStatement ps) throws DriverException {
- if (this.parameters == null || this.parameters.size() == 0) {
- return ps.bind();
- }
-
- // Test the type of the first value
- Object v = this.parameters.get(0);
- Object[] values;
- if (v instanceof CqlParameterValue) {
- LinkedList valuesList = new LinkedList();
- for (Object value : this.parameters) {
- valuesList.add(((CqlParameterValue) value).getValue());
- }
- values = valuesList.toArray();
- } else {
- values = this.parameters.toArray();
- }
-
- return ps.bind(values);
- }
-
- /* (non-Javadoc)
- * @see org.springframework.cassandra.core.CqlProvider#getCql()
- */
- @Override
- public String getCql() {
- return cql;
- }
-
- @Override
- public String toString() {
- StringBuilder sb = new StringBuilder();
- sb.append("PreparedStatementCreatorFactory.PreparedStatementCreatorImpl: cql=[");
- sb.append(cql).append("]; parameters=").append(this.parameters);
- return sb.toString();
- }
-
- }
-}
diff --git a/src/main/java/org/springframework/cassandra/core/RowIterator.java b/src/main/java/org/springframework/cassandra/core/RowIterator.java
new file mode 100644
index 000000000..9fb98ce57
--- /dev/null
+++ b/src/main/java/org/springframework/cassandra/core/RowIterator.java
@@ -0,0 +1,29 @@
+/*
+ * Copyright 2011-2013 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.core;
+
+
+/**
+ * @author David Webb
+ *
+ */
+public interface RowIterator {
+
+ Object[] next();
+
+ boolean hasNext();
+
+}
diff --git a/src/test/java/org/springframework/cassandra/test/integration/core/cql/generator/AbstractEmbeddedCassandraIntegrationTest.java b/src/test/java/org/springframework/cassandra/test/integration/AbstractEmbeddedCassandraIntegrationTest.java
similarity index 79%
rename from src/test/java/org/springframework/cassandra/test/integration/core/cql/generator/AbstractEmbeddedCassandraIntegrationTest.java
rename to src/test/java/org/springframework/cassandra/test/integration/AbstractEmbeddedCassandraIntegrationTest.java
index be34c18f3..c636965fc 100644
--- a/src/test/java/org/springframework/cassandra/test/integration/core/cql/generator/AbstractEmbeddedCassandraIntegrationTest.java
+++ b/src/test/java/org/springframework/cassandra/test/integration/AbstractEmbeddedCassandraIntegrationTest.java
@@ -1,4 +1,4 @@
-package org.springframework.cassandra.test.integration.core.cql.generator;
+package org.springframework.cassandra.test.integration;
import java.io.IOException;
import java.util.UUID;
@@ -16,10 +16,14 @@ import com.datastax.driver.core.Session;
public abstract class AbstractEmbeddedCassandraIntegrationTest {
+ protected final static String CASSANDRA_CONFIG = "cassandra.yaml";
+ protected final static String CASSANDRA_HOST = "localhost";
+ protected final static int CASSANDRA_NATIVE_PORT = 9042;
+
@BeforeClass
public static void beforeClass() throws ConfigurationException, TTransportException, IOException,
InterruptedException {
- EmbeddedCassandraServerHelper.startEmbeddedCassandra("cassandra.yaml");
+ EmbeddedCassandraServerHelper.startEmbeddedCassandra(CASSANDRA_CONFIG);
}
/**
@@ -51,7 +55,7 @@ public abstract class AbstractEmbeddedCassandraIntegrationTest {
}
public Cluster cluster() {
- return Cluster.builder().addContactPoint("localhost").withPort(9042).build();
+ return Cluster.builder().addContactPoint(CASSANDRA_HOST).withPort(CASSANDRA_NATIVE_PORT).build();
}
@Before
@@ -69,7 +73,9 @@ public abstract class AbstractEmbeddedCassandraIntegrationTest {
session.execute("CREATE KEYSPACE " + keyspace
+ " WITH replication = {'class': 'SimpleStrategy', 'replication_factor' : 1};");
session.execute("USE " + keyspace + ";");
- } // else keyspace already exists
+ } else {// else keyspace already exists
+ session = cluster.connect(keyspace);
+ }
}
}
}
diff --git a/src/test/java/org/springframework/cassandra/test/integration/core/cql/generator/CreateTableCqlGeneratorIntegrationTests.java b/src/test/java/org/springframework/cassandra/test/integration/core/cql/generator/CreateTableCqlGeneratorIntegrationTests.java
index be7b762d9..82872f163 100644
--- a/src/test/java/org/springframework/cassandra/test/integration/core/cql/generator/CreateTableCqlGeneratorIntegrationTests.java
+++ b/src/test/java/org/springframework/cassandra/test/integration/core/cql/generator/CreateTableCqlGeneratorIntegrationTests.java
@@ -3,6 +3,7 @@ package org.springframework.cassandra.test.integration.core.cql.generator;
import static org.springframework.cassandra.test.integration.core.cql.generator.CqlTableSpecificationAssertions.assertTable;
import org.junit.Test;
+import org.springframework.cassandra.test.integration.AbstractEmbeddedCassandraIntegrationTest;
import org.springframework.cassandra.test.unit.core.cql.generator.CreateTableCqlGeneratorTests.BasicTest;
import org.springframework.cassandra.test.unit.core.cql.generator.CreateTableCqlGeneratorTests.CompositePartitionKeyTest;
import org.springframework.cassandra.test.unit.core.cql.generator.CreateTableCqlGeneratorTests.CreateTableTest;
diff --git a/src/test/java/org/springframework/cassandra/test/integration/core/template/CassandraOperationsTest.java b/src/test/java/org/springframework/cassandra/test/integration/core/template/CassandraOperationsTest.java
new file mode 100644
index 000000000..246d628cb
--- /dev/null
+++ b/src/test/java/org/springframework/cassandra/test/integration/core/template/CassandraOperationsTest.java
@@ -0,0 +1,275 @@
+/*
+ * Copyright 2011-2013 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.integration.core.template;
+
+import static org.junit.Assert.assertNotNull;
+
+import java.util.Collection;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Set;
+
+import junit.framework.Assert;
+
+import org.cassandraunit.CassandraCQLUnit;
+import org.cassandraunit.dataset.cql.ClassPathCQLDataSet;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.cassandra.core.CassandraOperations;
+import org.springframework.cassandra.core.CassandraTemplate;
+import org.springframework.cassandra.core.HostMapper;
+import org.springframework.cassandra.core.PreparedStatementBinder;
+import org.springframework.cassandra.core.ResultSetExtractor;
+import org.springframework.cassandra.core.RingMember;
+import org.springframework.cassandra.core.RowIterator;
+import org.springframework.cassandra.test.integration.AbstractEmbeddedCassandraIntegrationTest;
+import org.springframework.dao.DataAccessException;
+import org.springframework.data.cassandra.test.integration.table.Book;
+
+import com.datastax.driver.core.BoundStatement;
+import com.datastax.driver.core.Host;
+import com.datastax.driver.core.PreparedStatement;
+import com.datastax.driver.core.ResultSet;
+import com.datastax.driver.core.Row;
+import com.datastax.driver.core.exceptions.DriverException;
+
+/**
+ * Unit Tests for CassandraTemplate
+ *
+ * @author David Webb
+ *
+ */
+public class CassandraOperationsTest extends AbstractEmbeddedCassandraIntegrationTest {
+
+ private CassandraOperations cassandraTemplate;
+
+ private static Logger log = LoggerFactory.getLogger(CassandraOperationsTest.class);
+
+ /*
+ * Objects used for test data
+ */
+ final Object[] o1 = new Object[] { "1234", "Moby Dick", "Herman Manville", new Integer(456) };
+ final Object[] o2 = new Object[] { "2345", "War and Peace", "Russian Dude", new Integer(456) };
+ final Object[] o3 = new Object[] { "3456", "Jane Ayre", "Charlotte", new Integer(456) };
+
+ /**
+ * This loads any test specific Cassandra objects
+ */
+ @Rule
+ public CassandraCQLUnit cassandraCQLUnit = new CassandraCQLUnit(new ClassPathCQLDataSet(
+ "cassandraOperationsTest-cql-dataload.cql", this.keyspace), CASSANDRA_CONFIG, CASSANDRA_HOST,
+ CASSANDRA_NATIVE_PORT);
+
+ @Before
+ public void setupTemplate() {
+ cassandraTemplate = new CassandraTemplate(session);
+ }
+
+ @Test
+ public void ringTest() {
+
+ List ring = cassandraTemplate.describeRing();
+
+ /*
+ * There must be 1 node in the cluster if the embedded server is
+ * running.
+ */
+ assertNotNull(ring);
+
+ for (RingMember h : ring) {
+ log.info("ringTest Host -> " + h.address);
+ }
+ }
+
+ @Test
+ public void hostMapperTest() {
+
+ List ring = (List) cassandraTemplate.describeRing(new HostMapper() {
+
+ @Override
+ public Collection mapHosts(Set host) throws DriverException {
+
+ List list = new LinkedList();
+
+ for (Host h : host) {
+ MyHost mh = new MyHost();
+ mh.someName = h.getAddress().getCanonicalHostName();
+ list.add(mh);
+ }
+
+ return list;
+ }
+
+ });
+
+ assertNotNull(ring);
+ Assert.assertTrue(ring.size() > 0);
+
+ for (MyHost h : ring) {
+ log.info("hostMapperTest Host -> " + h.someName);
+ }
+
+ }
+
+ @Test
+ public void ingestionTestListOfList() {
+
+ String cql = "insert into book (isbn, title, author, pages) values (?, ?, ?, ?)";
+
+ List> values = new LinkedList>();
+
+ List l1 = new LinkedList();
+ l1.add("1234");
+ l1.add("Moby Dick");
+ l1.add("Herman Manville");
+ l1.add(new Integer(456));
+
+ values.add(l1);
+
+ List l2 = new LinkedList();
+ l2.add("2345");
+ l2.add("War and Peace");
+ l2.add("Russian Dude");
+ l2.add(new Integer(456));
+
+ values.add(l2);
+
+ // values.add(new Object[] { "3456", "Jane Ayre", "Charlotte", new Integer(456) });
+
+ cassandraTemplate.ingest(cql, values);
+
+ // Assert that the rows were inserted into Cassandra
+ Book b1 = getBook("1234");
+ Book b2 = getBook("2345");
+
+ Assert.assertEquals(b1.getIsbn(), l1.get(0));
+ Assert.assertEquals(b2.getIsbn(), l2.get(0));
+ }
+
+ @Test
+ public void ingestionTestObjectArray() {
+
+ String cql = "insert into book (isbn, title, author, pages) values (?, ?, ?, ?)";
+
+ Object[][] values = new Object[3][];
+ values[0] = o1;
+ values[1] = o2;
+ values[2] = o3;
+
+ cassandraTemplate.ingest(cql, values);
+
+ // Assert that the rows were inserted into Cassandra
+ Book b1 = getBook("1234");
+ Book b2 = getBook("2345");
+ Book b3 = getBook("3456");
+
+ Assert.assertEquals(b1.getIsbn(), values[0][0]);
+ Assert.assertEquals(b2.getTitle(), values[1][1]);
+ Assert.assertEquals(b3.getAuthor(), values[2][2]);
+ }
+
+ /**
+ * This is an implementation of RowIterator for the purposes of testing passing your own Impl to CassandraTemplate
+ *
+ * @author David Webb
+ */
+ final class MyRowIterator implements RowIterator {
+
+ private Object[][] values;
+
+ public MyRowIterator(Object[][] values) {
+ this.values = values;
+ }
+
+ int index = 0;
+
+ /* (non-Javadoc)
+ * @see org.springframework.cassandra.core.RowIterator#next()
+ */
+ @Override
+ public Object[] next() {
+ return values[index++];
+ }
+
+ /* (non-Javadoc)
+ * @see org.springframework.cassandra.core.RowIterator#hasNext()
+ */
+ @Override
+ public boolean hasNext() {
+ return index < values.length;
+ }
+
+ }
+
+ @Test
+ public void ingestionTestRowIterator() {
+
+ String cql = "insert into book (isbn, title, author, pages) values (?, ?, ?, ?)";
+
+ final Object[][] v = new Object[3][];
+ v[0] = o1;
+ v[1] = o2;
+ v[2] = o3;
+ RowIterator ri = new MyRowIterator(v);
+
+ cassandraTemplate.ingest(cql, ri);
+
+ // Assert that the rows were inserted into Cassandra
+ Book b1 = getBook("1234");
+ Book b2 = getBook("2345");
+ Book b3 = getBook("3456");
+
+ Assert.assertEquals(b1.getIsbn(), o1[0]);
+ Assert.assertEquals(b2.getTitle(), o2[1]);
+ Assert.assertEquals(b3.getAuthor(), o3[2]);
+ }
+
+ public Book getBook(final String isbn) {
+
+ Book b = this.cassandraTemplate.query("select * from book where isbn = ?", new PreparedStatementBinder() {
+
+ @Override
+ public BoundStatement bindValues(PreparedStatement ps) throws DriverException {
+ return ps.bind(isbn);
+ }
+ }, new ResultSetExtractor() {
+
+ @Override
+ public Book extractData(ResultSet rs) throws DriverException, DataAccessException {
+ Book b = new Book();
+ Row r = rs.one();
+ b.setIsbn(r.getString("isbn"));
+ b.setTitle(r.getString("title"));
+ b.setAuthor(r.getString("author"));
+ b.setPages(r.getInt("pages"));
+ return b;
+ }
+ });
+
+ return b;
+
+ }
+
+ /**
+ * For testing a HostMapper Implementation
+ */
+ public class MyHost {
+ public String someName;
+ }
+}
diff --git a/src/test/java/org/springframework/data/cassandra/test/integration/config/TestConfig.java b/src/test/java/org/springframework/data/cassandra/test/integration/config/TestConfig.java
index e69e13cd2..415f5393e 100644
--- a/src/test/java/org/springframework/data/cassandra/test/integration/config/TestConfig.java
+++ b/src/test/java/org/springframework/data/cassandra/test/integration/config/TestConfig.java
@@ -22,12 +22,14 @@ import com.datastax.driver.core.Cluster.Builder;
@Configuration
public class TestConfig extends AbstractCassandraConfiguration {
+ public static final String keyspace = "test";
+
/* (non-Javadoc)
* @see org.springframework.data.cassandra.config.AbstractCassandraConfiguration#getKeyspaceName()
*/
@Override
protected String getKeyspaceName() {
- return "test";
+ return keyspace;
}
/* (non-Javadoc)
diff --git a/src/test/java/org/springframework/data/cassandra/test/integration/template/CassandraOperationsTest.java b/src/test/java/org/springframework/data/cassandra/test/integration/template/CassandraOperationsTest.java
deleted file mode 100644
index e850028a1..000000000
--- a/src/test/java/org/springframework/data/cassandra/test/integration/template/CassandraOperationsTest.java
+++ /dev/null
@@ -1,215 +0,0 @@
-/*
- * Copyright 2011-2013 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.data.cassandra.test.integration.template;
-
-import static org.junit.Assert.assertNotNull;
-
-import java.io.IOException;
-import java.util.Collection;
-import java.util.LinkedList;
-import java.util.List;
-import java.util.Set;
-
-import junit.framework.Assert;
-
-import org.apache.cassandra.exceptions.ConfigurationException;
-import org.apache.thrift.transport.TTransportException;
-import org.cassandraunit.CassandraCQLUnit;
-import org.cassandraunit.DataLoader;
-import org.cassandraunit.dataset.cql.ClassPathCQLDataSet;
-import org.cassandraunit.dataset.yaml.ClassPathYamlDataSet;
-import org.cassandraunit.utils.EmbeddedCassandraServerHelper;
-import org.junit.After;
-import org.junit.AfterClass;
-import org.junit.BeforeClass;
-import org.junit.Rule;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.cassandra.core.BoundStatementFactory;
-import org.springframework.cassandra.core.CassandraOperations;
-import org.springframework.cassandra.core.CqlParameter;
-import org.springframework.cassandra.core.CqlParameterValue;
-import org.springframework.cassandra.core.HostMapper;
-import org.springframework.cassandra.core.PreparedStatementCreatorFactory;
-import org.springframework.cassandra.core.ResultSetExtractor;
-import org.springframework.cassandra.core.RingMember;
-import org.springframework.dao.DataAccessException;
-import org.springframework.data.cassandra.test.integration.config.TestConfig;
-import org.springframework.data.cassandra.test.integration.table.Book;
-import org.springframework.test.context.ContextConfiguration;
-import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
-import org.springframework.test.context.support.AnnotationConfigContextLoader;
-
-import com.datastax.driver.core.DataType;
-import com.datastax.driver.core.Host;
-import com.datastax.driver.core.ResultSet;
-import com.datastax.driver.core.Row;
-import com.datastax.driver.core.exceptions.DriverException;
-
-/**
- * Unit Tests for CassandraTemplate
- *
- * @author David Webb
- *
- */
-@RunWith(SpringJUnit4ClassRunner.class)
-@ContextConfiguration(classes = { TestConfig.class }, loader = AnnotationConfigContextLoader.class)
-public class CassandraOperationsTest {
-
- /**
- * @author David Webb
- *
- */
- public class MyHost {
-
- public String someName;
-
- }
-
- @Autowired
- private CassandraOperations cassandraTemplate;
-
- private static Logger log = LoggerFactory.getLogger(CassandraOperationsTest.class);
-
- private final static String CASSANDRA_CONFIG = "cassandra.yaml";
- private final static String KEYSPACE_NAME = "test";
- private final static String CASSANDRA_HOST = "localhost";
- private final static int CASSANDRA_NATIVE_PORT = 9042;
- private final static int CASSANDRA_THRIFT_PORT = 9160;
-
- @Rule
- public CassandraCQLUnit cassandraCQLUnit = new CassandraCQLUnit(new ClassPathCQLDataSet(
- "cassandraOperationsTest-cql-dataload.cql", KEYSPACE_NAME), CASSANDRA_CONFIG, CASSANDRA_HOST,
- CASSANDRA_NATIVE_PORT);
-
- @BeforeClass
- public static void startCassandra() throws IOException, TTransportException, ConfigurationException,
- InterruptedException {
-
- EmbeddedCassandraServerHelper.startEmbeddedCassandra(CASSANDRA_CONFIG);
-
- /*
- * Load data file to creat the test keyspace before we init the template
- */
- DataLoader dataLoader = new DataLoader("Test Cluster", CASSANDRA_HOST + ":" + CASSANDRA_THRIFT_PORT);
- dataLoader.load(new ClassPathYamlDataSet("cassandra-keyspace.yaml"));
- }
-
- @Test
- public void ringTest() {
-
- List ring = cassandraTemplate.describeRing();
-
- /*
- * There must be 1 node in the cluster if the embedded server is
- * running.
- */
- assertNotNull(ring);
-
- for (RingMember h : ring) {
- log.info("ringTest Host -> " + h.address);
- }
- }
-
- @Test
- public void hostMapperTest() {
-
- List ring = (List) cassandraTemplate.describeRing(new HostMapper() {
-
- @Override
- public Collection mapHosts(Set host) throws DriverException {
-
- List list = new LinkedList();
-
- for (Host h : host) {
- MyHost mh = new MyHost();
- mh.someName = h.getAddress().getCanonicalHostName();
- list.add(mh);
- }
-
- return list;
- }
-
- });
-
- assertNotNull(ring);
- Assert.assertTrue(ring.size() > 0);
-
- for (MyHost h : ring) {
- log.info("hostMapperTest Host -> " + h.someName);
- }
-
- }
-
- @Test
- public void preparedStatementFactoryTest() {
-
- String cql = "select * from book where isbn = ?";
-
- List parameters = new LinkedList();
- parameters.add(new CqlParameter("isbn", DataType.text()));
-
- PreparedStatementCreatorFactory factory = new PreparedStatementCreatorFactory(cql, parameters);
-
- List values = new LinkedList();
- values.add(new CqlParameterValue(DataType.text(), "999999999"));
-
- Book b = cassandraTemplate.query(factory.newPreparedStatementCreator(values),
- factory.newPreparedStatementBinder(values), new ResultSetExtractor() {
-
- @Override
- public Book extractData(ResultSet rs) throws DriverException, DataAccessException {
- Row r = rs.one();
- Book b = new Book();
- b.setIsbn(r.getString("isbn"));
- b.setTitle(r.getString("title"));
- b.setAuthor(r.getString("author"));
- b.setPages(r.getInt("pages"));
- return b;
- }
- });
-
- log.info(b.toString());
-
- }
-
- @Test
- public void boundStatementFactoryTest() {
-
- String cql = "insert into book (isbn, title, author, pages) values (?, ?, ?, ?)";
-
- BoundStatementFactory bsf = new BoundStatementFactory(cql);
- bsf.addValues(new Object[] { "1234", "Moby Dick", "Herman Manville", new Integer(456) }, new Object[] { "2345",
- "War and Peace", "Russian Dude", new Integer(456) }, new Object[] { "3456", "Jane Ayre", "Charlotte",
- new Integer(456) });
-
- cassandraTemplate.execute(bsf);
- }
-
- @After
- public void clearCassandra() {
- EmbeddedCassandraServerHelper.cleanEmbeddedCassandra();
-
- }
-
- @AfterClass
- public static void stopCassandra() {
- // EmbeddedCassandraServerHelper.stopEmbeddedCassandra();
- }
-}