@ -18,12 +18,15 @@ package org.springframework.data.mongodb.config;
import static org.hamcrest.Matchers.* ;
import static org.hamcrest.Matchers.* ;
import static org.junit.Assert.* ;
import static org.junit.Assert.* ;
import java.net.InetAddress ;
import java.net.UnknownHostException ;
import java.net.UnknownHostException ;
import java.util.Arrays ;
import java.util.Arrays ;
import java.util.Collection ;
import java.util.Collection ;
import org.junit.Before ;
import org.junit.Before ;
import org.junit.Rule ;
import org.junit.Test ;
import org.junit.Test ;
import org.junit.rules.ExpectedException ;
import com.mongodb.ServerAddress ;
import com.mongodb.ServerAddress ;
@ -35,6 +38,8 @@ import com.mongodb.ServerAddress;
* /
* /
public class ServerAddressPropertyEditorUnitTests {
public class ServerAddressPropertyEditorUnitTests {
@Rule public ExpectedException expectedException = ExpectedException . none ( ) ;
ServerAddressPropertyEditor editor ;
ServerAddressPropertyEditor editor ;
@Before
@Before
@ -81,11 +86,111 @@ public class ServerAddressPropertyEditorUnitTests {
assertNull ( editor . getValue ( ) ) ;
assertNull ( editor . getValue ( ) ) ;
}
}
/ * *
* @see DATAMONGO - 808
* /
@Test
public void handleIPv6HostaddressLoopbackShort ( ) throws UnknownHostException {
String hostAddress = "::1" ;
editor . setAsText ( hostAddress ) ;
assertSingleAddressWithPort ( hostAddress , null , editor . getValue ( ) ) ;
}
/ * *
* @see DATAMONGO - 808
* /
@Test
public void handleIPv6HostaddressLoopbackShortWithPort ( ) throws UnknownHostException {
String hostAddress = "::1" ;
int port = 27017 ;
editor . setAsText ( hostAddress + ":" + port ) ;
assertSingleAddressWithPort ( hostAddress , port , editor . getValue ( ) ) ;
}
/ * *
* Here we detect no port since the last segment of the address contains leading zeros .
*
* @see DATAMONGO - 808
* /
@Test
public void handleIPv6HostaddressLoopbackLong ( ) throws UnknownHostException {
String hostAddress = "0000:0000:0000:0000:0000:0000:0000:0001" ;
editor . setAsText ( hostAddress ) ;
assertSingleAddressWithPort ( hostAddress , null , editor . getValue ( ) ) ;
}
/ * *
* @see DATAMONGO - 808
* /
@Test
public void handleIPv6HostaddressLoopbackLongWithBrackets ( ) throws UnknownHostException {
String hostAddress = "[0000:0000:0000:0000:0000:0000:0000:0001]" ;
editor . setAsText ( hostAddress ) ;
assertSingleAddressWithPort ( hostAddress , null , editor . getValue ( ) ) ;
}
/ * *
* We can ' t tell whether the last part of the hostAddress represents a port or not .
*
* @see DATAMONGO - 808
* /
@Test
public void shouldFailToHandleAmbiguousIPv6HostaddressLongWithoutPortAndWithoutBrackets ( ) throws UnknownHostException {
expectedException . expect ( IllegalArgumentException . class ) ;
String hostAddress = "0000:0000:0000:0000:0000:0000:0000:128" ;
editor . setAsText ( hostAddress ) ;
}
/ * *
* @see DATAMONGO - 808
* /
@Test
public void handleIPv6HostaddressExampleAddressWithPort ( ) throws UnknownHostException {
String hostAddress = "0000:0000:0000:0000:0000:0000:0000:0001" ;
int port = 27017 ;
editor . setAsText ( hostAddress + ":" + port ) ;
assertSingleAddressWithPort ( hostAddress , port , editor . getValue ( ) ) ;
}
/ * *
* @see DATAMONGO - 808
* /
@Test
public void handleIPv6HostaddressExampleAddressInBracketsWithPort ( ) throws UnknownHostException {
String hostAddress = "[0000:0000:0000:0000:0000:0000:0000:0001]" ;
int port = 27017 ;
editor . setAsText ( hostAddress + ":" + port ) ;
assertSingleAddressWithPort ( hostAddress , port , editor . getValue ( ) ) ;
}
private static void assertSingleAddressOfLocalhost ( Object result ) throws UnknownHostException {
private static void assertSingleAddressOfLocalhost ( Object result ) throws UnknownHostException {
assertSingleAddressWithPort ( "localhost" , null , result ) ;
}
private static void assertSingleAddressWithPort ( String hostAddress , Integer port , Object result )
throws UnknownHostException {
assertThat ( result , is ( instanceOf ( ServerAddress [ ] . class ) ) ) ;
assertThat ( result , is ( instanceOf ( ServerAddress [ ] . class ) ) ) ;
Collection < ServerAddress > addresses = Arrays . asList ( ( ServerAddress [ ] ) result ) ;
Collection < ServerAddress > addresses = Arrays . asList ( ( ServerAddress [ ] ) result ) ;
assertThat ( addresses , hasSize ( 1 ) ) ;
assertThat ( addresses , hasSize ( 1 ) ) ;
assertThat ( addresses , hasItem ( new ServerAddress ( "localhost" ) ) ) ;
if ( port = = null ) {
assertThat ( addresses , hasItem ( new ServerAddress ( InetAddress . getByName ( hostAddress ) ) ) ) ;
} else {
assertThat ( addresses , hasItem ( new ServerAddress ( InetAddress . getByName ( hostAddress ) , port ) ) ) ;
}
}
}
}
}