Skip to content

Commit

Permalink
DATACASS-54 - XML namespace now supporting <repositories> element
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewadams committed Feb 3, 2014
1 parent de6f070 commit df990d2
Show file tree
Hide file tree
Showing 10 changed files with 270 additions and 91 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -51,7 +51,8 @@ public void postProcess(BeanDefinitionBuilder builder, XmlRepositoryConfiguratio

Element element = config.getElement();

ParsingUtils.setPropertyReference(builder, element, CASSANDRA_TEMPLATE_REF, "cassandraTemplate");
ParsingUtils.addOptionalPropertyReference(builder, "cassandraTemplate", element, CASSANDRA_TEMPLATE_REF,
"cassandra-template");
}

@Override
Expand All @@ -64,5 +65,4 @@ public void postProcess(BeanDefinitionBuilder builder, AnnotationRepositoryConfi
builder.addPropertyReference("cassandraTemplate", cassandraTemplateRef);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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[
Expand Down Expand Up @@ -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">
Expand Down Expand Up @@ -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>
Original file line number Diff line number Diff line change
Expand Up @@ -24,40 +24,35 @@
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();
Expand Down Expand Up @@ -93,12 +88,10 @@ public void setUp() throws InterruptedException {
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());
Expand All @@ -107,23 +100,20 @@ public void findsUserById() throws Exception {

}

@Test
public void findsAll() throws Exception {
List<User> result = Lists.newArrayList(repository.findAll());
assertThat(result.size(), is(all.size()));
assertThat(result.containsAll(all), is(true));

}

@Test
public void findsAllWithGivenIds() {

Iterable<User> result = repository.findAll(Arrays.asList(bob.getUsername(), tom.getUsername()));
assertThat(result, hasItems(bob, tom));
assertThat(result, not(hasItems(alice, scott)));
}

@Test
public void deletesUserCorrectly() throws Exception {

repository.delete(tom);
Expand All @@ -134,7 +124,6 @@ public void deletesUserCorrectly() throws Exception {
assertThat(result, not(hasItem(tom)));
}

@Test
public void deletesUserByIdCorrectly() {

repository.delete(tom.getUsername().toString());
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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();
}
}

0 comments on commit df990d2

Please sign in to comment.