DATACASS-720 - Polishing.

Apply keyspace name quoting also in other places. Add unit test.

Original pull request: #170.
This commit is contained in:
Mark Paluch
2020-01-27 12:08:59 +01:00
parent 3ed752ff39
commit 80e6189484
3 changed files with 63 additions and 4 deletions

View File

@@ -134,7 +134,7 @@ public class CassandraAdminTemplate extends CassandraTemplate implements Cassand
Assert.notNull(tableName, "Table name must not be null");
return Optional.ofNullable(getCqlOperations().execute((SessionCallback<TableMetadata>) session -> session
.getCluster().getMetadata().getKeyspace(keyspace).getTable(tableName.toCql())));
.getCluster().getMetadata().getKeyspace(Metadata.quoteIfNecessary(keyspace)).getTable(tableName.toCql())));
}
/* (non-Javadoc)
@@ -149,8 +149,7 @@ public class CassandraAdminTemplate extends CassandraTemplate implements Cassand
String keyspace = Metadata.quoteIfNecessary(session.getLoggedKeyspace());
KeyspaceMetadata keyspaceMetadata = session.getCluster().getMetadata().getKeyspace(keyspace);
Assert.state(keyspaceMetadata != null,
String.format("Metadata for keyspace [%s] not available", keyspace));
Assert.state(keyspaceMetadata != null, String.format("Metadata for keyspace [%s] not available", keyspace));
return keyspaceMetadata;
});

View File

@@ -21,6 +21,7 @@ import org.springframework.util.Assert;
import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.KeyspaceMetadata;
import com.datastax.driver.core.Metadata;
import com.datastax.driver.core.UserType;
/**
@@ -47,7 +48,7 @@ public class SimpleUserTypeResolver implements UserTypeResolver {
Assert.notNull(cluster, "Cluster must not be null");
Assert.hasText(keyspaceName, "Keyspace must not be null or empty");
this.keyspaceName = keyspaceName;
this.keyspaceName = Metadata.quoteIfNecessary(keyspaceName);
this.cluster = cluster;
}

View File

@@ -0,0 +1,59 @@
/*
* Copyright 2020 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
*
* https://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.core.mapping;
import static org.mockito.Mockito.*;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.data.cassandra.core.cql.CqlIdentifier;
import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.KeyspaceMetadata;
import com.datastax.driver.core.Metadata;
/**
* Unit tests for {@link SimpleUserTypeResolver}.
*
* @author Mark Paluch
*/
@RunWith(MockitoJUnitRunner.class)
public class SimpleUserTypeResolverUnitTests {
@Mock Cluster cluster;
@Mock Metadata metadata;
@Mock KeyspaceMetadata keyspaceMetadata;
@Before
public void before() {
when(cluster.getMetadata()).thenReturn(metadata);
when(metadata.getKeyspace(anyString())).thenReturn(keyspaceMetadata);
}
@Test // DATACASS-720
public void shouldQuoteCaseSensitiveKeyspaceName() {
SimpleUserTypeResolver resolver = new SimpleUserTypeResolver(cluster, "MyKeyspace");
resolver.resolveType(CqlIdentifier.of("user_type"));
verify(metadata).getKeyspace("\"MyKeyspace\"");
}
}