DATACASS-54 - XML namespace now supporting <repositories> element

This commit is contained in:
Matthew Adams
2014-02-03 11:20:41 -06:00
parent de6f070bbb
commit df990d2c14
10 changed files with 270 additions and 91 deletions

View File

@@ -16,6 +16,8 @@
package org.springframework.data.cassandra.config.xml;
import org.springframework.beans.factory.xml.NamespaceHandlerSupport;
import org.springframework.data.cassandra.repository.config.CassandraRepositoryConfigurationExtension;
import org.springframework.data.repository.config.RepositoryBeanDefinitionParser;
/**
* Namespace handler for spring-data-cassandra.
@@ -28,6 +30,9 @@ public class CassandraDataNamespaceHandler extends NamespaceHandlerSupport {
@Override
public void init() {
registerBeanDefinitionParser("repositories", new RepositoryBeanDefinitionParser(
new CassandraRepositoryConfigurationExtension()));
registerBeanDefinitionParser("cluster", new CassandraDataClusterParser());
registerBeanDefinitionParser("session", new CassandraDataSessionParser());
registerBeanDefinitionParser("template", new CassandraDataTemplateParser());

View File

@@ -16,9 +16,9 @@
package org.springframework.data.cassandra.repository.config;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.cassandra.config.xml.ParsingUtils;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.data.cassandra.repository.support.CassandraRepositoryFactoryBean;
import org.springframework.data.config.ParsingUtils;
import org.springframework.data.repository.config.AnnotationRepositoryConfigurationSource;
import org.springframework.data.repository.config.RepositoryConfigurationExtension;
import org.springframework.data.repository.config.RepositoryConfigurationExtensionSupport;
@@ -51,7 +51,8 @@ public class CassandraRepositoryConfigurationExtension extends RepositoryConfigu
Element element = config.getElement();
ParsingUtils.setPropertyReference(builder, element, CASSANDRA_TEMPLATE_REF, "cassandraTemplate");
ParsingUtils.addOptionalPropertyReference(builder, "cassandraTemplate", element, CASSANDRA_TEMPLATE_REF,
"cassandra-template");
}
@Override
@@ -64,5 +65,4 @@ public class CassandraRepositoryConfigurationExtension extends RepositoryConfigu
builder.addPropertyReference("cassandraTemplate", cassandraTemplateRef);
}
}
}

View File

@@ -2,12 +2,20 @@
<xsd:schema xmlns="http://www.springframework.org/schema/data/cassandra"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tool="http://www.springframework.org/schema/tool"
xmlns:tool="http://www.springframework.org/schema/tool" xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:repository="http://www.springframework.org/schema/data/repository"
targetNamespace="http://www.springframework.org/schema/data/cassandra"
elementFormDefault="qualified" attributeFormDefault="unqualified">
<xsd:import namespace="http://www.springframework.org/schema/beans"
schemaLocation="http://www.springframework.org/schema/beans/spring-beans.xsd" />
<xsd:import namespace="http://www.springframework.org/schema/tool"
schemaLocation="http://www.springframework.org/schema/tool/spring-tool.xsd" />
<xsd:import namespace="http://www.springframework.org/schema/context"
schemaLocation="http://www.springframework.org/schema/context/spring-context.xsd" />
<xsd:import namespace="http://www.springframework.org/schema/data/repository"
schemaLocation="http://www.springframework.org/schema/data/repository/spring-repository.xsd" />
<xsd:annotation>
<xsd:documentation><![CDATA[
@@ -444,7 +452,8 @@ Arbitrary CQL script to be executed against the session's keyspace during bean d
]]></xsd:documentation>
</xsd:annotation>
</xsd:element>
<xsd:element name="mapping" type="mappingType" minOccurs="0" maxOccurs="1" />
<xsd:element name="mapping" type="mappingType" minOccurs="0"
maxOccurs="1" />
</xsd:sequence>
<xsd:attribute name="id" type="xsd:ID" use="optional">
@@ -653,4 +662,38 @@ Table name override.
<!-- TODO: allow specification of C* table options here -->
</xsd:complexType>
<xsd:simpleType name="cassandraTemplateRef" final="union">
<xsd:annotation>
<xsd:appinfo>
<tool:annotation kind="ref">
<tool:assignable-to
type="org.springframework.data.cassandra.core.CassandraTemplate" />
</tool:annotation>
</xsd:appinfo>
</xsd:annotation>
<xsd:union memberTypes="xsd:string" />
</xsd:simpleType>
<xsd:attributeGroup name="cassandra-repository-attributes">
<xsd:attribute name="cassandra-template-ref" type="cassandraTemplateRef"
default="cassandra-template">
<xsd:annotation>
<xsd:documentation><![CDATA[
The reference to a cassandraTemplate. Will default to 'cassandra-template'.
]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
</xsd:attributeGroup>
<xsd:element name="repositories">
<xsd:complexType>
<xsd:complexContent>
<xsd:extension base="repository:repositories">
<xsd:attributeGroup ref="cassandra-repository-attributes" />
<xsd:attributeGroup ref="repository:repository-attributes" />
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
</xsd:element>
</xsd:schema>

View File

@@ -24,40 +24,35 @@ import static org.junit.Assert.assertThat;
import java.util.Arrays;
import java.util.List;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.cassandra.core.CassandraOperations;
import org.springframework.data.cassandra.test.integration.AbstractSpringDataEmbeddedCassandraIntegrationTest;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.google.common.collect.Lists;
/**
* Base class for tests for {@link UserRepository}.
* Tests for {@link UserRepository}.
*
* @author Alex Shvid
* @author Matthew T. Adams
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = UserRepositoryIntegrationTestsConfig.class)
public class UserRepositoryIntegrationTests extends AbstractSpringDataEmbeddedCassandraIntegrationTest {
public class UserRepositoryIntegrationTests {
@Autowired
protected UserRepository repository;
UserRepository repository;
@Autowired
protected CassandraOperations template;
CassandraOperations template;
User tom, bob, alice, scott;
List<User> all;
@Before
public UserRepositoryIntegrationTests() {
}
public UserRepositoryIntegrationTests(UserRepository repository, CassandraOperations template) {
this.repository = repository;
this.template = template;
}
public void setUp() throws InterruptedException {
repository.deleteAll();
@@ -93,12 +88,10 @@ public class UserRepositoryIntegrationTests extends AbstractSpringDataEmbeddedCa
all = template.insert(Arrays.asList(tom, bob, alice, scott));
}
@After
public void after() {
repository.deleteAll();
}
@Test
public void findsUserById() throws Exception {
User user = repository.findOne(bob.getUsername());
@@ -107,7 +100,6 @@ public class UserRepositoryIntegrationTests extends AbstractSpringDataEmbeddedCa
}
@Test
public void findsAll() throws Exception {
List<User> result = Lists.newArrayList(repository.findAll());
assertThat(result.size(), is(all.size()));
@@ -115,7 +107,6 @@ public class UserRepositoryIntegrationTests extends AbstractSpringDataEmbeddedCa
}
@Test
public void findsAllWithGivenIds() {
Iterable<User> result = repository.findAll(Arrays.asList(bob.getUsername(), tom.getUsername()));
@@ -123,7 +114,6 @@ public class UserRepositoryIntegrationTests extends AbstractSpringDataEmbeddedCa
assertThat(result, not(hasItems(alice, scott)));
}
@Test
public void deletesUserCorrectly() throws Exception {
repository.delete(tom);
@@ -134,7 +124,6 @@ public class UserRepositoryIntegrationTests extends AbstractSpringDataEmbeddedCa
assertThat(result, not(hasItem(tom)));
}
@Test
public void deletesUserByIdCorrectly() {
repository.delete(tom.getUsername().toString());

View File

@@ -1,41 +0,0 @@
package org.springframework.data.cassandra.test.integration.repository;
import static org.springframework.cassandra.core.keyspace.CreateKeyspaceSpecification.createKeyspace;
import java.util.ArrayList;
import java.util.List;
import org.springframework.cassandra.core.keyspace.CreateKeyspaceSpecification;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.cassandra.config.SchemaAction;
import org.springframework.data.cassandra.repository.config.EnableCassandraRepositories;
import org.springframework.data.cassandra.test.integration.support.AbstractDataTestJavaConfig;
@Configuration
@EnableCassandraRepositories(basePackageClasses = UserRepository.class)
public class UserRepositoryIntegrationTestsConfig extends AbstractDataTestJavaConfig {
@Override
protected String getKeyspaceName() {
return UserRepositoryIntegrationTests.class.getSimpleName();
}
@Override
protected List<CreateKeyspaceSpecification> getKeyspaceCreations() {
List<CreateKeyspaceSpecification> creates = new ArrayList<CreateKeyspaceSpecification>();
creates.add(createKeyspace().name(getKeyspaceName()).withSimpleReplication());
return creates;
}
@Override
public SchemaAction getSchemaAction() {
return SchemaAction.RECREATE;
}
@Override
public String getMappingBasePackage() {
return User.class.getPackage().getName();
}
}

View File

@@ -0,0 +1,119 @@
/*
* 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.repository;
import static org.springframework.cassandra.core.keyspace.CreateKeyspaceSpecification.createKeyspace;
import java.util.ArrayList;
import java.util.List;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cassandra.core.keyspace.CreateKeyspaceSpecification;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.cassandra.config.SchemaAction;
import org.springframework.data.cassandra.core.CassandraOperations;
import org.springframework.data.cassandra.repository.config.EnableCassandraRepositories;
import org.springframework.data.cassandra.test.integration.AbstractSpringDataEmbeddedCassandraIntegrationTest;
import org.springframework.data.cassandra.test.integration.support.AbstractDataTestJavaConfig;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* Base class for Java config tests for {@link UserRepository}.
*
* @author Matthew T. Adams
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class UserRepositoryJavaConfigIntegrationTests extends AbstractSpringDataEmbeddedCassandraIntegrationTest {
@Configuration
@EnableCassandraRepositories(basePackageClasses = UserRepository.class)
public static class Config extends AbstractDataTestJavaConfig {
@Override
protected String getKeyspaceName() {
return UserRepositoryJavaConfigIntegrationTests.class.getSimpleName();
}
@Override
protected List<CreateKeyspaceSpecification> getKeyspaceCreations() {
List<CreateKeyspaceSpecification> creates = new ArrayList<CreateKeyspaceSpecification>();
creates.add(createKeyspace().name(getKeyspaceName()).withSimpleReplication());
return creates;
}
@Override
public SchemaAction getSchemaAction() {
return SchemaAction.RECREATE;
}
@Override
public String getMappingBasePackage() {
return User.class.getPackage().getName();
}
}
@Autowired
protected UserRepository repository;
@Autowired
protected CassandraOperations template;
UserRepositoryIntegrationTests tests;
@Before
public void setUp() throws InterruptedException {
tests = new UserRepositoryIntegrationTests(repository, template);
tests.setUp();
}
@After
public void after() {
tests.after();
}
@Test
public void findsUserById() throws Exception {
tests.findsUserById();
}
@Test
public void findsAll() throws Exception {
tests.findsAll();
}
@Test
public void findsAllWithGivenIds() {
tests.findsAllWithGivenIds();
}
@Test
public void deletesUserCorrectly() throws Exception {
tests.deletesUserCorrectly();
}
@Test
public void deletesUserByIdCorrectly() {
tests.deletesUserByIdCorrectly();
}
}

View File

@@ -0,0 +1,80 @@
/*
* 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.repository;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.cassandra.core.CassandraOperations;
import org.springframework.data.cassandra.test.integration.AbstractSpringDataEmbeddedCassandraIntegrationTest;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* Base class for xml config tests for {@link UserRepository}.
*
* @author Matthew T. Adams
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class UserRepositoryXmlConfigIntegrationTests extends AbstractSpringDataEmbeddedCassandraIntegrationTest {
@Autowired
protected UserRepository repository;
@Autowired
protected CassandraOperations template;
UserRepositoryIntegrationTests tests;
@Before
public void setUp() throws InterruptedException {
tests = new UserRepositoryIntegrationTests(repository, template);
tests.setUp();
}
@After
public void after() {
tests.after();
}
@Test
public void findsUserById() throws Exception {
tests.findsUserById();
}
@Test
public void findsAll() throws Exception {
tests.findsAll();
}
@Test
public void findsAllWithGivenIds() {
tests.findsAllWithGivenIds();
}
@Test
public void deletesUserCorrectly() throws Exception {
tests.deletesUserCorrectly();
}
@Test
public void deletesUserByIdCorrectly() {
tests.deletesUserByIdCorrectly();
}
}

View File

@@ -1,11 +0,0 @@
package org.springframework.data.cassandra.test.integration.table;
import org.springframework.data.cassandra.repository.config.EnableCassandraRepositories;
/**
* Spring Data Cassandra marker interface for use with {@link EnableCassandraRepositories#basePackageClasses()}
*
* @author Matthew T. Adams
*/
public interface Domain {
}

View File

@@ -11,7 +11,7 @@
location="classpath:/org/springframework/data/cassandra/test/integration/repository/cassandra.properties" />
<cassandra:cluster id="cassandra-cluster"
contactPoints="${cassandra.contactPoints}" port="${cassandra.native_transport_port}">
contact-points="${cassandra.contactPoints}" port="${cassandra.native_transport_port}">
<cassandra:local-pooling-options
min-simultaneous-requests="25" max-simultaneous-requests="100"
core-connections="2" max-connections="8" />
@@ -49,14 +49,9 @@
</cassandra:mapping>
</cassandra:session>
<bean id="cassandraTemplate" class="org.springframework.cassandra.core.CqlTemplate">
<constructor-arg ref="cassandra-session" />
</bean>
<cassandra:template id="cassandra-template"
cassandra-converter-ref="cassandra-converter" session-ref="cassandra-session" />
<bean id="cassandraDataTemplate"
class="org.springframework.data.cassandra.core.CassandraTemplate">
<constructor-arg ref="cassandra-session" />
<constructor-arg ref="cassandra-converter" />
<constructor-arg value="${cassandra.keyspace}" />
</bean>
<cassandra:repositories
base-package="org.springframework.data.cassandra.test.integration.repository" />
</beans>

View File

@@ -1,3 +1,3 @@
cassandra.contactPoints=localhost
cassandra.native_transport_port=@build.cassandra.native_transport_port@
cassandra.keyspace=UserRepositoryIntegrationTests
cassandra.keyspace=UserRepositoryXmlConfigIntegrationTests