DATAMONGO-454 - Improvements to ServerAddressPropertyEditor.
ServerAddressPropertyEditor now only eventually fails if none of the configured addresses can be parsed correctly. Strengthened the parsing implementation to not fail for host-only parsing or accidental double commas. Cleaned up test dependency setup.
This commit is contained in:
@@ -68,24 +68,6 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>log4j</groupId>
|
<groupId>log4j</groupId>
|
||||||
<artifactId>log4j</artifactId>
|
<artifactId>log4j</artifactId>
|
||||||
<exclusions>
|
|
||||||
<exclusion>
|
|
||||||
<groupId>javax.mail</groupId>
|
|
||||||
<artifactId>mail</artifactId>
|
|
||||||
</exclusion>
|
|
||||||
<exclusion>
|
|
||||||
<groupId>javax.jms</groupId>
|
|
||||||
<artifactId>jms</artifactId>
|
|
||||||
</exclusion>
|
|
||||||
<exclusion>
|
|
||||||
<groupId>com.sun.jdmk</groupId>
|
|
||||||
<artifactId>jmxtools</artifactId>
|
|
||||||
</exclusion>
|
|
||||||
<exclusion>
|
|
||||||
<groupId>com.sun.jmx</groupId>
|
|
||||||
<artifactId>jmxri</artifactId>
|
|
||||||
</exclusion>
|
|
||||||
</exclusions>
|
|
||||||
<scope>runtime</scope>
|
<scope>runtime</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
@@ -95,17 +77,6 @@
|
|||||||
<optional>true</optional>
|
<optional>true</optional>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.mockito</groupId>
|
|
||||||
<artifactId>mockito-all</artifactId>
|
|
||||||
<scope>test</scope>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<dependency>
|
|
||||||
<groupId>junit</groupId>
|
|
||||||
<artifactId>junit</artifactId>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.aspectj</groupId>
|
<groupId>org.aspectj</groupId>
|
||||||
<artifactId>aspectjrt</artifactId>
|
<artifactId>aspectjrt</artifactId>
|
||||||
|
|||||||
@@ -27,47 +27,9 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>log4j</groupId>
|
<groupId>log4j</groupId>
|
||||||
<artifactId>log4j</artifactId>
|
<artifactId>log4j</artifactId>
|
||||||
<exclusions>
|
|
||||||
<exclusion>
|
|
||||||
<groupId>javax.mail</groupId>
|
|
||||||
<artifactId>mail</artifactId>
|
|
||||||
</exclusion>
|
|
||||||
<exclusion>
|
|
||||||
<groupId>javax.jms</groupId>
|
|
||||||
<artifactId>jms</artifactId>
|
|
||||||
</exclusion>
|
|
||||||
<exclusion>
|
|
||||||
<groupId>com.sun.jdmk</groupId>
|
|
||||||
<artifactId>jmxtools</artifactId>
|
|
||||||
</exclusion>
|
|
||||||
<exclusion>
|
|
||||||
<groupId>com.sun.jmx</groupId>
|
|
||||||
<artifactId>jmxri</artifactId>
|
|
||||||
</exclusion>
|
|
||||||
</exclusions>
|
|
||||||
<scope>compile</scope>
|
<scope>compile</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<!-- Test dependencies -->
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.mockito</groupId>
|
|
||||||
<artifactId>mockito-all</artifactId>
|
|
||||||
<scope>test</scope>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.hamcrest</groupId>
|
|
||||||
<artifactId>hamcrest-all</artifactId>
|
|
||||||
<version>1.1</version>
|
|
||||||
<scope>test</scope>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<dependency>
|
|
||||||
<groupId>junit</groupId>
|
|
||||||
<artifactId>junit</artifactId>
|
|
||||||
<scope>test</scope>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
|||||||
@@ -11,9 +11,10 @@
|
|||||||
<properties>
|
<properties>
|
||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
<!-- versions for commonly-used dependencies -->
|
<!-- versions for commonly-used dependencies -->
|
||||||
<junit.version>4.8.1</junit.version>
|
<junit.version>4.10</junit.version>
|
||||||
<log4j.version>1.2.15</log4j.version>
|
<log4j.version>1.2.15</log4j.version>
|
||||||
<org.mockito.version>1.8.4</org.mockito.version>
|
<org.mockito.version>1.8.4</org.mockito.version>
|
||||||
|
<hamcrest.version>1.2.1</hamcrest.version>
|
||||||
<org.slf4j.version>1.5.10</org.slf4j.version>
|
<org.slf4j.version>1.5.10</org.slf4j.version>
|
||||||
<org.codehaus.jackson.version>1.6.1</org.codehaus.jackson.version>
|
<org.codehaus.jackson.version>1.6.1</org.codehaus.jackson.version>
|
||||||
<org.springframework.version.30>3.0.7.RELEASE</org.springframework.version.30>
|
<org.springframework.version.30>3.0.7.RELEASE</org.springframework.version.30>
|
||||||
@@ -289,14 +290,14 @@
|
|||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.mockito</groupId>
|
<groupId>org.mockito</groupId>
|
||||||
<artifactId>mockito-all</artifactId>
|
<artifactId>mockito-core</artifactId>
|
||||||
<version>${org.mockito.version}</version>
|
<version>${org.mockito.version}</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>junit</groupId>
|
<groupId>junit</groupId>
|
||||||
<artifactId>junit</artifactId>
|
<artifactId>junit-dep</artifactId>
|
||||||
<version>${junit.version}</version>
|
<version>${junit.version}</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
@@ -304,19 +305,34 @@
|
|||||||
</dependencies>
|
</dependencies>
|
||||||
</dependencyManagement>
|
</dependencyManagement>
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<!--
|
|
||||||
dependency definitions to be inherited by child poms. any
|
<!-- Test dependencies -->
|
||||||
<dependency> declarations here will automatically show up on child
|
|
||||||
project classpaths. only items that are truly common across all
|
|
||||||
projects (modules and samples) should go here. otherwise, consider
|
|
||||||
<dependencyManagement> above
|
|
||||||
-->
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>log4j</groupId>
|
<groupId>log4j</groupId>
|
||||||
<artifactId>log4j</artifactId>
|
<artifactId>log4j</artifactId>
|
||||||
<version>${log4j.version}</version>
|
<version>${log4j.version}</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.hamcrest</groupId>
|
||||||
|
<artifactId>hamcrest-library</artifactId>
|
||||||
|
<version>${hamcrest.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.mockito</groupId>
|
||||||
|
<artifactId>mockito-core</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>junit</groupId>
|
||||||
|
<artifactId>junit-dep</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
@@ -398,7 +414,7 @@
|
|||||||
<includes>
|
<includes>
|
||||||
<include>**/*Tests.java</include>
|
<include>**/*Tests.java</include>
|
||||||
</includes>
|
</includes>
|
||||||
<junitArtifactName>junit:junit</junitArtifactName>
|
<junitArtifactName>junit:junit-dep</junitArtifactName>
|
||||||
</configuration>
|
</configuration>
|
||||||
</plugin>
|
</plugin>
|
||||||
<plugin>
|
<plugin>
|
||||||
|
|||||||
@@ -91,25 +91,6 @@
|
|||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<!-- Test dependencies -->
|
<!-- Test dependencies -->
|
||||||
<dependency>
|
|
||||||
<groupId>org.mockito</groupId>
|
|
||||||
<artifactId>mockito-all</artifactId>
|
|
||||||
<scope>test</scope>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<dependency>
|
|
||||||
<groupId>org.hamcrest</groupId>
|
|
||||||
<artifactId>hamcrest-all</artifactId>
|
|
||||||
<version>1.1</version>
|
|
||||||
<scope>test</scope>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<dependency>
|
|
||||||
<groupId>junit</groupId>
|
|
||||||
<artifactId>junit</artifactId>
|
|
||||||
<scope>test</scope>
|
|
||||||
</dependency>
|
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>joda-time</groupId>
|
<groupId>joda-time</groupId>
|
||||||
<artifactId>joda-time</artifactId>
|
<artifactId>joda-time</artifactId>
|
||||||
@@ -120,8 +101,8 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.slf4j</groupId>
|
<groupId>org.slf4j</groupId>
|
||||||
<artifactId>slf4j-api</artifactId>
|
<artifactId>slf4j-api</artifactId>
|
||||||
<scope>test</scope>
|
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.slf4j</groupId>
|
<groupId>org.slf4j</groupId>
|
||||||
<artifactId>jcl-over-slf4j</artifactId>
|
<artifactId>jcl-over-slf4j</artifactId>
|
||||||
|
|||||||
@@ -17,7 +17,11 @@ package org.springframework.data.mongodb.config;
|
|||||||
|
|
||||||
import java.beans.PropertyEditorSupport;
|
import java.beans.PropertyEditorSupport;
|
||||||
import java.net.UnknownHostException;
|
import java.net.UnknownHostException;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import org.apache.commons.logging.Log;
|
||||||
|
import org.apache.commons.logging.LogFactory;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
import com.mongodb.ServerAddress;
|
import com.mongodb.ServerAddress;
|
||||||
@@ -30,6 +34,8 @@ import com.mongodb.ServerAddress;
|
|||||||
*/
|
*/
|
||||||
public class ServerAddressPropertyEditor extends PropertyEditorSupport {
|
public class ServerAddressPropertyEditor extends PropertyEditorSupport {
|
||||||
|
|
||||||
|
private static final Log LOG = LogFactory.getLog(ServerAddressPropertyEditor.class);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* (non-Javadoc)
|
* (non-Javadoc)
|
||||||
* @see java.beans.PropertyEditorSupport#setAsText(java.lang.String)
|
* @see java.beans.PropertyEditorSupport#setAsText(java.lang.String)
|
||||||
@@ -38,21 +44,49 @@ public class ServerAddressPropertyEditor extends PropertyEditorSupport {
|
|||||||
public void setAsText(String replicaSetString) {
|
public void setAsText(String replicaSetString) {
|
||||||
|
|
||||||
String[] replicaSetStringArray = StringUtils.commaDelimitedListToStringArray(replicaSetString);
|
String[] replicaSetStringArray = StringUtils.commaDelimitedListToStringArray(replicaSetString);
|
||||||
ServerAddress[] serverAddresses = new ServerAddress[replicaSetStringArray.length];
|
Set<ServerAddress> serverAddresses = new HashSet<ServerAddress>(replicaSetStringArray.length);
|
||||||
|
|
||||||
for (int i = 0; i < replicaSetStringArray.length; i++) {
|
for (String element : replicaSetStringArray) {
|
||||||
|
|
||||||
String[] hostAndPort = StringUtils.delimitedListToStringArray(replicaSetStringArray[i], ":");
|
ServerAddress address = parseServerAddress(element);
|
||||||
|
|
||||||
try {
|
if (address != null) {
|
||||||
serverAddresses[i] = new ServerAddress(hostAndPort[0], Integer.parseInt(hostAndPort[1]));
|
serverAddresses.add(address);
|
||||||
} catch (NumberFormatException e) {
|
|
||||||
throw new IllegalArgumentException("Could not parse port " + hostAndPort[1], e);
|
|
||||||
} catch (UnknownHostException e) {
|
|
||||||
throw new IllegalArgumentException("Could not parse host " + hostAndPort[0], e);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
setValue(serverAddresses);
|
if (serverAddresses.isEmpty()) {
|
||||||
|
throw new IllegalArgumentException(
|
||||||
|
"Could not resolve at least one server of the replica set configuration! Validate your config!");
|
||||||
|
}
|
||||||
|
|
||||||
|
setValue(serverAddresses.toArray(new ServerAddress[serverAddresses.size()]));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parses the given source into a {@link ServerAddress}.
|
||||||
|
*
|
||||||
|
* @param source
|
||||||
|
* @return the
|
||||||
|
*/
|
||||||
|
private ServerAddress parseServerAddress(String source) {
|
||||||
|
|
||||||
|
String[] hostAndPort = StringUtils.delimitedListToStringArray(source.trim(), ":");
|
||||||
|
|
||||||
|
if (!StringUtils.hasText(source) || hostAndPort.length > 2) {
|
||||||
|
LOG.warn(String.format("Could not parse address source '%s'. Check your replica set configuration!", source));
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
return hostAndPort.length == 1 ? new ServerAddress(hostAndPort[0]) : new ServerAddress(hostAndPort[0],
|
||||||
|
Integer.parseInt(hostAndPort[1]));
|
||||||
|
} catch (UnknownHostException e) {
|
||||||
|
LOG.warn(String.format("Could not parse host '%s'. Check your replica set configuration!", hostAndPort[0]));
|
||||||
|
} catch (NumberFormatException e) {
|
||||||
|
LOG.warn(String.format("Could not parse port '%s'. Check your replica set configuration!", hostAndPort[1]));
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2010 the original author or authors.
|
* Copyright 2011-2012 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@@ -16,6 +16,7 @@
|
|||||||
|
|
||||||
package org.springframework.data.mongodb.config;
|
package org.springframework.data.mongodb.config;
|
||||||
|
|
||||||
|
import static org.hamcrest.Matchers.*;
|
||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -29,6 +30,7 @@ import org.springframework.data.mongodb.core.MongoFactoryBean;
|
|||||||
import org.springframework.data.mongodb.core.MongoTemplate;
|
import org.springframework.data.mongodb.core.MongoTemplate;
|
||||||
import org.springframework.test.context.ContextConfiguration;
|
import org.springframework.test.context.ContextConfiguration;
|
||||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||||
|
import org.springframework.test.util.ReflectionTestUtils;
|
||||||
|
|
||||||
import com.mongodb.CommandResult;
|
import com.mongodb.CommandResult;
|
||||||
import com.mongodb.Mongo;
|
import com.mongodb.Mongo;
|
||||||
@@ -36,47 +38,45 @@ import com.mongodb.ServerAddress;
|
|||||||
|
|
||||||
@RunWith(SpringJUnit4ClassRunner.class)
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
@ContextConfiguration
|
@ContextConfiguration
|
||||||
public class MongoNamespaceReplicaSetTests extends NamespaceTestSupport {
|
public class MongoNamespaceReplicaSetTests {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private ApplicationContext ctx;
|
private ApplicationContext ctx;
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
public void testParsingMongoWithReplicaSets() throws Exception {
|
public void testParsingMongoWithReplicaSets() throws Exception {
|
||||||
|
|
||||||
assertTrue(ctx.containsBean("replicaSetMongo"));
|
assertTrue(ctx.containsBean("replicaSetMongo"));
|
||||||
MongoFactoryBean mfb = (MongoFactoryBean) ctx.getBean("&replicaSetMongo");
|
MongoFactoryBean mfb = (MongoFactoryBean) ctx.getBean("&replicaSetMongo");
|
||||||
|
|
||||||
List<ServerAddress> replicaSetSeeds = readField("replicaSetSeeds", mfb);
|
List<ServerAddress> replicaSetSeeds = (List<ServerAddress>) ReflectionTestUtils.getField(mfb, "replicaSetSeeds");
|
||||||
assertNotNull(replicaSetSeeds);
|
|
||||||
|
|
||||||
assertEquals("127.0.0.1", replicaSetSeeds.get(0).getHost());
|
|
||||||
assertEquals(10001, replicaSetSeeds.get(0).getPort());
|
|
||||||
|
|
||||||
assertEquals("localhost", replicaSetSeeds.get(1).getHost());
|
|
||||||
assertEquals(10002, replicaSetSeeds.get(1).getPort());
|
|
||||||
|
|
||||||
|
assertThat(replicaSetSeeds, is(notNullValue()));
|
||||||
|
assertThat(replicaSetSeeds, hasItems(new ServerAddress("127.0.0.1", 10001), new ServerAddress("localhost", 10002)));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
public void testParsingWithPropertyPlaceHolder() throws Exception {
|
public void testParsingWithPropertyPlaceHolder() throws Exception {
|
||||||
|
|
||||||
assertTrue(ctx.containsBean("manyReplicaSetMongo"));
|
assertTrue(ctx.containsBean("manyReplicaSetMongo"));
|
||||||
MongoFactoryBean mfb = (MongoFactoryBean) ctx.getBean("&manyReplicaSetMongo");
|
MongoFactoryBean mfb = (MongoFactoryBean) ctx.getBean("&manyReplicaSetMongo");
|
||||||
|
|
||||||
List<ServerAddress> replicaSetSeeds = readField("replicaSetSeeds", mfb);
|
List<ServerAddress> replicaSetSeeds = (List<ServerAddress>) ReflectionTestUtils.getField(mfb, "replicaSetSeeds");
|
||||||
assertNotNull(replicaSetSeeds);
|
|
||||||
|
|
||||||
assertEquals("192.168.174.130", replicaSetSeeds.get(0).getHost());
|
|
||||||
assertEquals(27017, replicaSetSeeds.get(0).getPort());
|
|
||||||
assertEquals("192.168.174.130", replicaSetSeeds.get(1).getHost());
|
|
||||||
assertEquals(27018, replicaSetSeeds.get(1).getPort());
|
|
||||||
assertEquals("192.168.174.130", replicaSetSeeds.get(2).getHost());
|
|
||||||
assertEquals(27019, replicaSetSeeds.get(2).getPort());
|
|
||||||
|
|
||||||
|
assertThat(replicaSetSeeds, is(notNullValue()));
|
||||||
|
assertThat(replicaSetSeeds, hasSize(3));
|
||||||
|
assertThat(
|
||||||
|
replicaSetSeeds,
|
||||||
|
hasItems(new ServerAddress("192.168.174.130", 27017), new ServerAddress("192.168.174.130", 27018),
|
||||||
|
new ServerAddress("192.168.174.130", 27019)));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Ignore("CI infrastructure does not yet support replica sets")
|
@Ignore("CI infrastructure does not yet support replica sets")
|
||||||
public void testMongoWithReplicaSets() {
|
public void testMongoWithReplicaSets() {
|
||||||
|
|
||||||
Mongo mongo = ctx.getBean(Mongo.class);
|
Mongo mongo = ctx.getBean(Mongo.class);
|
||||||
assertEquals(2, mongo.getAllAddress().size());
|
assertEquals(2, mongo.getAllAddress().size());
|
||||||
List<ServerAddress> servers = mongo.getAllAddress();
|
List<ServerAddress> servers = mongo.getAllAddress();
|
||||||
@@ -88,6 +88,5 @@ public class MongoNamespaceReplicaSetTests extends NamespaceTestSupport {
|
|||||||
MongoTemplate template = new MongoTemplate(mongo, "admin");
|
MongoTemplate template = new MongoTemplate(mongo, "admin");
|
||||||
CommandResult result = template.executeCommand("{replSetGetStatus : 1}");
|
CommandResult result = template.executeCommand("{replSetGetStatus : 1}");
|
||||||
assertEquals("blort", result.getString("set"));
|
assertEquals("blort", result.getString("set"));
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,42 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright 2011 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.mongodb.config;
|
|
||||||
|
|
||||||
import java.lang.reflect.Field;
|
|
||||||
|
|
||||||
public class NamespaceTestSupport {
|
|
||||||
|
|
||||||
@SuppressWarnings({ "unchecked" })
|
|
||||||
public static <T> T readField(String name, Object target) throws Exception {
|
|
||||||
Field field = null;
|
|
||||||
Class<?> clazz = target.getClass();
|
|
||||||
do {
|
|
||||||
try {
|
|
||||||
field = clazz.getDeclaredField(name);
|
|
||||||
} catch (Exception ex) {
|
|
||||||
}
|
|
||||||
|
|
||||||
clazz = clazz.getSuperclass();
|
|
||||||
} while (field == null && !clazz.equals(Object.class));
|
|
||||||
|
|
||||||
if (field == null)
|
|
||||||
throw new IllegalArgumentException("Cannot find field '" + name + "' in the class hierarchy of "
|
|
||||||
+ target.getClass());
|
|
||||||
field.setAccessible(true);
|
|
||||||
return (T) field.get(target);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,80 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2012 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.mongodb.config;
|
||||||
|
|
||||||
|
import static org.hamcrest.Matchers.*;
|
||||||
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
|
import java.net.UnknownHostException;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Collection;
|
||||||
|
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import com.mongodb.ServerAddress;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Unit tests for {@link ServerAddressPropertyEditor}.
|
||||||
|
*
|
||||||
|
* @author Oliver Gierke
|
||||||
|
*/
|
||||||
|
public class ServerAddressPropertyEditorUnitTests {
|
||||||
|
|
||||||
|
ServerAddressPropertyEditor editor;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setUp() {
|
||||||
|
editor = new ServerAddressPropertyEditor();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @see DATAMONGO-454
|
||||||
|
*/
|
||||||
|
@Test(expected = IllegalArgumentException.class)
|
||||||
|
public void rejectsAddressConfigWithoutASingleParsableServerAddress() {
|
||||||
|
|
||||||
|
editor.setAsText("foo, bar");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @see DATAMONGO-454
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void skipsUnparsableAddressIfAtLeastOneIsParsable() throws UnknownHostException {
|
||||||
|
|
||||||
|
editor.setAsText("foo, localhost");
|
||||||
|
assertSingleAddressOfLocalhost(editor.getValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @see DATAMONGO-454
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void handlesEmptyAddressAsParseError() throws UnknownHostException {
|
||||||
|
|
||||||
|
editor.setAsText(", localhost");
|
||||||
|
assertSingleAddressOfLocalhost(editor.getValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void assertSingleAddressOfLocalhost(Object result) throws UnknownHostException {
|
||||||
|
|
||||||
|
assertThat(result, is(instanceOf(ServerAddress[].class)));
|
||||||
|
Collection<ServerAddress> addresses = Arrays.asList((ServerAddress[]) result);
|
||||||
|
assertThat(addresses, hasSize(1));
|
||||||
|
assertThat(addresses, hasItem(new ServerAddress("localhost")));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -907,8 +907,6 @@ public class MongoTemplateTests {
|
|||||||
assertThat(lastMongoAction.getEntityClass().toString(), is(PersonWithIdPropertyOfTypeObjectId.class.toString()));
|
assertThat(lastMongoAction.getEntityClass().toString(), is(PersonWithIdPropertyOfTypeObjectId.class.toString()));
|
||||||
assertThat(lastMongoAction.getMongoActionOperation(), is(MongoActionOperation.UPDATE));
|
assertThat(lastMongoAction.getMongoActionOperation(), is(MongoActionOperation.UPDATE));
|
||||||
assertThat(lastMongoAction.getQuery(), equalTo(q.getQueryObject()));
|
assertThat(lastMongoAction.getQuery(), equalTo(q.getQueryObject()));
|
||||||
assertThat(lastMongoAction.getDocument(), equalTo(u.getUpdateObject()));
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private class FsyncSafeWriteConcernResolver implements WriteConcernResolver {
|
private class FsyncSafeWriteConcernResolver implements WriteConcernResolver {
|
||||||
|
|||||||
@@ -115,7 +115,7 @@ public class MappingMongoConverterUnitTests {
|
|||||||
DBObject dbObject = new BasicDBObject();
|
DBObject dbObject = new BasicDBObject();
|
||||||
converter.write(person, dbObject);
|
converter.write(person, dbObject);
|
||||||
|
|
||||||
assertThat(dbObject.get("birthDate"), is(Date.class));
|
assertThat(dbObject.get("birthDate"), is(instanceOf(Date.class)));
|
||||||
|
|
||||||
Person result = converter.read(Person.class, dbObject);
|
Person result = converter.read(Person.class, dbObject);
|
||||||
assertThat(result.birthDate, is(notNullValue()));
|
assertThat(result.birthDate, is(notNullValue()));
|
||||||
@@ -176,7 +176,7 @@ public class MappingMongoConverterUnitTests {
|
|||||||
dbObject.put("birthDate", new LocalDate());
|
dbObject.put("birthDate", new LocalDate());
|
||||||
dbObject.put(DefaultMongoTypeMapper.DEFAULT_TYPE_KEY, Person.class.getName());
|
dbObject.put(DefaultMongoTypeMapper.DEFAULT_TYPE_KEY, Person.class.getName());
|
||||||
|
|
||||||
assertThat(converter.read(Contact.class, dbObject), is(Person.class));
|
assertThat(converter.read(Contact.class, dbObject), is(instanceOf(Person.class)));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -189,7 +189,7 @@ public class MappingMongoConverterUnitTests {
|
|||||||
dbObject.put("birthDate", new LocalDate());
|
dbObject.put("birthDate", new LocalDate());
|
||||||
dbObject.put(DefaultMongoTypeMapper.DEFAULT_TYPE_KEY, Person.class.getName());
|
dbObject.put(DefaultMongoTypeMapper.DEFAULT_TYPE_KEY, Person.class.getName());
|
||||||
|
|
||||||
assertThat(converter.read(BirthDateContainer.class, dbObject), is(BirthDateContainer.class));
|
assertThat(converter.read(BirthDateContainer.class, dbObject), is(instanceOf(BirthDateContainer.class)));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -217,7 +217,7 @@ public class MappingMongoConverterUnitTests {
|
|||||||
DBObject result = new BasicDBObject();
|
DBObject result = new BasicDBObject();
|
||||||
converter.write(value, result);
|
converter.write(value, result);
|
||||||
|
|
||||||
assertThat(result.get("sampleEnum"), is(String.class));
|
assertThat(result.get("sampleEnum"), is(instanceOf(String.class)));
|
||||||
assertThat(result.get("sampleEnum").toString(), is("FIRST"));
|
assertThat(result.get("sampleEnum").toString(), is("FIRST"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -233,7 +233,7 @@ public class MappingMongoConverterUnitTests {
|
|||||||
DBObject result = new BasicDBObject();
|
DBObject result = new BasicDBObject();
|
||||||
converter.write(value, result);
|
converter.write(value, result);
|
||||||
|
|
||||||
assertThat(result.get("enums"), is(BasicDBList.class));
|
assertThat(result.get("enums"), is(instanceOf(BasicDBList.class)));
|
||||||
|
|
||||||
BasicDBList enums = (BasicDBList) result.get("enums");
|
BasicDBList enums = (BasicDBList) result.get("enums");
|
||||||
assertThat(enums.size(), is(1));
|
assertThat(enums.size(), is(1));
|
||||||
@@ -263,7 +263,7 @@ public class MappingMongoConverterUnitTests {
|
|||||||
|
|
||||||
ClassWithEnumProperty result = converter.read(ClassWithEnumProperty.class, dbObject);
|
ClassWithEnumProperty result = converter.read(ClassWithEnumProperty.class, dbObject);
|
||||||
|
|
||||||
assertThat(result.enums, is(List.class));
|
assertThat(result.enums, is(instanceOf(List.class)));
|
||||||
assertThat(result.enums.size(), is(1));
|
assertThat(result.enums.size(), is(1));
|
||||||
assertThat(result.enums, hasItem(SampleEnum.FIRST));
|
assertThat(result.enums, hasItem(SampleEnum.FIRST));
|
||||||
}
|
}
|
||||||
@@ -328,7 +328,7 @@ public class MappingMongoConverterUnitTests {
|
|||||||
converter.write(wrapper, dbObject);
|
converter.write(wrapper, dbObject);
|
||||||
|
|
||||||
Object result = dbObject.get("contacts");
|
Object result = dbObject.get("contacts");
|
||||||
assertThat(result, is(BasicDBList.class));
|
assertThat(result, is(instanceOf(BasicDBList.class)));
|
||||||
BasicDBList contacts = (BasicDBList) result;
|
BasicDBList contacts = (BasicDBList) result;
|
||||||
DBObject personDbObject = (DBObject) contacts.get(0);
|
DBObject personDbObject = (DBObject) contacts.get(0);
|
||||||
assertThat(personDbObject.get("foo").toString(), is("Oliver"));
|
assertThat(personDbObject.get("foo").toString(), is("Oliver"));
|
||||||
@@ -351,7 +351,7 @@ public class MappingMongoConverterUnitTests {
|
|||||||
assertThat(result.contacts, is(notNullValue()));
|
assertThat(result.contacts, is(notNullValue()));
|
||||||
assertThat(result.contacts.size(), is(1));
|
assertThat(result.contacts.size(), is(1));
|
||||||
Contact contact = result.contacts.get(0);
|
Contact contact = result.contacts.get(0);
|
||||||
assertThat(contact, is(Person.class));
|
assertThat(contact, is(instanceOf(Person.class)));
|
||||||
assertThat(((Person) contact).firstname, is("Oliver"));
|
assertThat(((Person) contact).firstname, is("Oliver"));
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -365,7 +365,7 @@ public class MappingMongoConverterUnitTests {
|
|||||||
converter.write(wrapper, dbObject);
|
converter.write(wrapper, dbObject);
|
||||||
|
|
||||||
Object localeField = dbObject.get("locale");
|
Object localeField = dbObject.get("locale");
|
||||||
assertThat(localeField, is(String.class));
|
assertThat(localeField, is(instanceOf(String.class)));
|
||||||
assertThat((String) localeField, is("en_US"));
|
assertThat((String) localeField, is("en_US"));
|
||||||
|
|
||||||
LocaleWrapper read = converter.read(LocaleWrapper.class, dbObject);
|
LocaleWrapper read = converter.read(LocaleWrapper.class, dbObject);
|
||||||
@@ -473,13 +473,13 @@ public class MappingMongoConverterUnitTests {
|
|||||||
DBObject dbo1 = new BasicDBObject();
|
DBObject dbo1 = new BasicDBObject();
|
||||||
|
|
||||||
converter.write(p1, dbo1);
|
converter.write(p1, dbo1);
|
||||||
assertThat(dbo1.get("_id"), is(String.class));
|
assertThat(dbo1.get("_id"), is(instanceOf(String.class)));
|
||||||
|
|
||||||
PersonPojoStringId p2 = new PersonPojoStringId(new ObjectId().toString(), "Text-1");
|
PersonPojoStringId p2 = new PersonPojoStringId(new ObjectId().toString(), "Text-1");
|
||||||
DBObject dbo2 = new BasicDBObject();
|
DBObject dbo2 = new BasicDBObject();
|
||||||
|
|
||||||
converter.write(p2, dbo2);
|
converter.write(p2, dbo2);
|
||||||
assertThat(dbo2.get("_id"), is(ObjectId.class));
|
assertThat(dbo2.get("_id"), is(instanceOf(ObjectId.class)));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -493,8 +493,8 @@ public class MappingMongoConverterUnitTests {
|
|||||||
|
|
||||||
ClassWithSortedMap result = converter.read(ClassWithSortedMap.class, wrapper);
|
ClassWithSortedMap result = converter.read(ClassWithSortedMap.class, wrapper);
|
||||||
|
|
||||||
assertThat(result, is(ClassWithSortedMap.class));
|
assertThat(result, is(instanceOf(ClassWithSortedMap.class)));
|
||||||
assertThat(result.map, is(SortedMap.class));
|
assertThat(result.map, is(instanceOf(SortedMap.class)));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -760,7 +760,7 @@ public class MappingMongoConverterUnitTests {
|
|||||||
|
|
||||||
assertThat(result.containsField("Foo"), is(true));
|
assertThat(result.containsField("Foo"), is(true));
|
||||||
assertThat(result.get("Foo"), is(notNullValue()));
|
assertThat(result.get("Foo"), is(notNullValue()));
|
||||||
assertThat(result.get("Foo"), is(BasicDBList.class));
|
assertThat(result.get("Foo"), is(instanceOf(BasicDBList.class)));
|
||||||
|
|
||||||
BasicDBList list = (BasicDBList) result.get("Foo");
|
BasicDBList list = (BasicDBList) result.get("Foo");
|
||||||
|
|
||||||
@@ -811,11 +811,11 @@ public class MappingMongoConverterUnitTests {
|
|||||||
converter.write(wrapper, result);
|
converter.write(wrapper, result);
|
||||||
|
|
||||||
Object mapObject = result.get("mapOfObjects");
|
Object mapObject = result.get("mapOfObjects");
|
||||||
assertThat(mapObject, is(BasicDBObject.class));
|
assertThat(mapObject, is(instanceOf(BasicDBObject.class)));
|
||||||
|
|
||||||
DBObject map = (DBObject) mapObject;
|
DBObject map = (DBObject) mapObject;
|
||||||
Object valueObject = map.get("foo");
|
Object valueObject = map.get("foo");
|
||||||
assertThat(valueObject, is(BasicDBList.class));
|
assertThat(valueObject, is(instanceOf(BasicDBList.class)));
|
||||||
|
|
||||||
List<Object> list = (List<Object>) valueObject;
|
List<Object> list = (List<Object>) valueObject;
|
||||||
assertThat(list.size(), is(1));
|
assertThat(list.size(), is(1));
|
||||||
@@ -906,7 +906,7 @@ public class MappingMongoConverterUnitTests {
|
|||||||
converter.write(wrapper, result);
|
converter.write(wrapper, result);
|
||||||
|
|
||||||
Object contacts = result.get("contacts");
|
Object contacts = result.get("contacts");
|
||||||
assertThat(contacts, is(Collection.class));
|
assertThat(contacts, is(instanceOf(Collection.class)));
|
||||||
assertThat(((Collection<?>) contacts).size(), is(2));
|
assertThat(((Collection<?>) contacts).size(), is(2));
|
||||||
assertThat(((Collection<Object>) contacts), hasItem(nullValue()));
|
assertThat(((Collection<Object>) contacts), hasItem(nullValue()));
|
||||||
}
|
}
|
||||||
@@ -969,7 +969,7 @@ public class MappingMongoConverterUnitTests {
|
|||||||
Item read = converter.read(Item.class, result);
|
Item read = converter.read(Item.class, result);
|
||||||
assertThat(read.attributes.size(), is(1));
|
assertThat(read.attributes.size(), is(1));
|
||||||
assertThat(read.attributes.get(0).key, is(attribute.key));
|
assertThat(read.attributes.get(0).key, is(attribute.key));
|
||||||
assertThat(read.attributes.get(0).value, is(Collection.class));
|
assertThat(read.attributes.get(0).value, is(instanceOf(Collection.class)));
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
Collection<String> values = (Collection<String>) read.attributes.get(0).value;
|
Collection<String> values = (Collection<String>) read.attributes.get(0).value;
|
||||||
@@ -1028,11 +1028,11 @@ public class MappingMongoConverterUnitTests {
|
|||||||
address.street = "Foo";
|
address.street = "Foo";
|
||||||
|
|
||||||
Object result = converter.convertToMongoType(Collections.singleton(address));
|
Object result = converter.convertToMongoType(Collections.singleton(address));
|
||||||
assertThat(result, is(BasicDBList.class));
|
assertThat(result, is(instanceOf(BasicDBList.class)));
|
||||||
|
|
||||||
Set<?> readResult = converter.read(Set.class, (BasicDBList) result);
|
Set<?> readResult = converter.read(Set.class, (BasicDBList) result);
|
||||||
assertThat(readResult.size(), is(1));
|
assertThat(readResult.size(), is(1));
|
||||||
assertThat(readResult.iterator().next(), is(Map.class));
|
assertThat(readResult.iterator().next(), is(instanceOf(Map.class)));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -84,7 +84,7 @@ public class QueryMapperUnitTests {
|
|||||||
|
|
||||||
DBObject query = new BasicDBObject("_id", new ObjectId().toString());
|
DBObject query = new BasicDBObject("_id", new ObjectId().toString());
|
||||||
DBObject result = mapper.getMappedObject(query, null);
|
DBObject result = mapper.getMappedObject(query, null);
|
||||||
assertThat(result.get("_id"), is(ObjectId.class));
|
assertThat(result.get("_id"), is(instanceOf(ObjectId.class)));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -114,9 +114,9 @@ public class QueryMapperUnitTests {
|
|||||||
|
|
||||||
DBObject result = mapper.getMappedObject(criteria.getCriteriaObject(), context.getPersistentEntity(Sample.class));
|
DBObject result = mapper.getMappedObject(criteria.getCriteriaObject(), context.getPersistentEntity(Sample.class));
|
||||||
Object object = result.get("_id");
|
Object object = result.get("_id");
|
||||||
assertThat(object, is(DBObject.class));
|
assertThat(object, is(instanceOf(DBObject.class)));
|
||||||
DBObject dbObject = (DBObject) object;
|
DBObject dbObject = (DBObject) object;
|
||||||
assertThat(dbObject.get("$ne"), is(ObjectId.class));
|
assertThat(dbObject.get("$ne"), is(instanceOf(ObjectId.class)));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -128,7 +128,7 @@ public class QueryMapperUnitTests {
|
|||||||
DBObject result = mapper.getMappedObject(query.getQueryObject(), null);
|
DBObject result = mapper.getMappedObject(query.getQueryObject(), null);
|
||||||
|
|
||||||
Object object = result.get("foo");
|
Object object = result.get("foo");
|
||||||
assertThat(object, is(String.class));
|
assertThat(object, is(instanceOf(String.class)));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -137,10 +137,10 @@ public class QueryMapperUnitTests {
|
|||||||
DBObject result = mapper.getMappedObject(query.getQueryObject(), null);
|
DBObject result = mapper.getMappedObject(query.getQueryObject(), null);
|
||||||
|
|
||||||
Object object = result.get("foo");
|
Object object = result.get("foo");
|
||||||
assertThat(object, is(DBObject.class));
|
assertThat(object, is(instanceOf(DBObject.class)));
|
||||||
|
|
||||||
Object ne = ((DBObject) object).get("$ne");
|
Object ne = ((DBObject) object).get("$ne");
|
||||||
assertThat(ne, is(String.class));
|
assertThat(ne, is(instanceOf(String.class)));
|
||||||
assertThat(ne.toString(), is(Enum.INSTANCE.name()));
|
assertThat(ne.toString(), is(Enum.INSTANCE.name()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -151,14 +151,14 @@ public class QueryMapperUnitTests {
|
|||||||
DBObject result = mapper.getMappedObject(query.getQueryObject(), null);
|
DBObject result = mapper.getMappedObject(query.getQueryObject(), null);
|
||||||
|
|
||||||
Object object = result.get("foo");
|
Object object = result.get("foo");
|
||||||
assertThat(object, is(DBObject.class));
|
assertThat(object, is(instanceOf(DBObject.class)));
|
||||||
|
|
||||||
Object in = ((DBObject) object).get("$in");
|
Object in = ((DBObject) object).get("$in");
|
||||||
assertThat(in, is(BasicDBList.class));
|
assertThat(in, is(instanceOf(BasicDBList.class)));
|
||||||
|
|
||||||
BasicDBList list = (BasicDBList) in;
|
BasicDBList list = (BasicDBList) in;
|
||||||
assertThat(list.size(), is(1));
|
assertThat(list.size(), is(1));
|
||||||
assertThat(list.get(0), is(String.class));
|
assertThat(list.get(0), is(instanceOf(String.class)));
|
||||||
assertThat(list.get(0).toString(), is(Enum.INSTANCE.name()));
|
assertThat(list.get(0).toString(), is(Enum.INSTANCE.name()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -77,12 +77,12 @@ public class SpringDataMongodbSerializerUnitTests {
|
|||||||
address.zipCode = "01234";
|
address.zipCode = "01234";
|
||||||
|
|
||||||
DBObject result = serializer.asDBObject("foo", address);
|
DBObject result = serializer.asDBObject("foo", address);
|
||||||
assertThat(result, is(BasicDBObject.class));
|
assertThat(result, is(instanceOf(BasicDBObject.class)));
|
||||||
BasicDBObject dbObject = (BasicDBObject) result;
|
BasicDBObject dbObject = (BasicDBObject) result;
|
||||||
|
|
||||||
Object value = dbObject.get("foo");
|
Object value = dbObject.get("foo");
|
||||||
assertThat(value, is(notNullValue()));
|
assertThat(value, is(notNullValue()));
|
||||||
assertThat(value, is(BasicDBObject.class));
|
assertThat(value, is(instanceOf(BasicDBObject.class)));
|
||||||
|
|
||||||
Object reference = converter.convertToMongoType(address);
|
Object reference = converter.convertToMongoType(address);
|
||||||
assertThat(value, is(reference));
|
assertThat(value, is(reference));
|
||||||
|
|||||||
Reference in New Issue
Block a user