Skip to content

Commit ee8a932

Browse files
committed
Refactor Java version check in tests, and ignore a Java 8-specific test when Java version is not at least 7
1 parent 35f8359 commit ee8a932

File tree

7 files changed

+52
-14
lines changed

7 files changed

+52
-14
lines changed

driver-core/src/test/functional/com/mongodb/ClusterFixture.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,18 @@ private static ServerVersion getConnectedServerVersion() {
160160
return clusterDescription.getAny().get(0).getVersion();
161161
}
162162

163+
public static boolean isNotAtLeastJava7() {
164+
return javaVersionStartsWith("1.6");
165+
}
166+
167+
public static boolean isNotAtLeastJava8() {
168+
return isNotAtLeastJava7() || javaVersionStartsWith("1.7");
169+
}
170+
171+
private static boolean javaVersionStartsWith(final String versionPrefix) {
172+
return System.getProperty("java.version").startsWith(versionPrefix + ".");
173+
}
174+
163175
static class ShutdownHook extends Thread {
164176
@Override
165177
public void run() {

driver-core/src/test/functional/com/mongodb/connection/AsyncStreamTimeoutsSpecification.groovy

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import java.util.concurrent.TimeUnit
3333
import static com.mongodb.ClusterFixture.getCredentialList
3434
import static com.mongodb.ClusterFixture.getPrimary
3535
import static com.mongodb.ClusterFixture.getSslSettings
36+
import static com.mongodb.ClusterFixture.isNotAtLeastJava7
3637
import static com.mongodb.connection.CommandHelper.executeCommand
3738

3839
@IgnoreIf({ System.getProperty('ignoreSlowUnitTests') == 'true' })
@@ -42,7 +43,7 @@ class AsyncStreamTimeoutsSpecification extends OperationFunctionalSpecification
4243
static SocketSettings openSocketSettings = SocketSettings.builder().connectTimeout(1, TimeUnit.MILLISECONDS).build();
4344
static SocketSettings readSocketSettings = SocketSettings.builder().readTimeout(5, TimeUnit.SECONDS).build();
4445

45-
@IgnoreIf({ System.getProperty('java.version').startsWith('1.6.') || getSslSettings().isEnabled() })
46+
@IgnoreIf({ isNotAtLeastJava7() || getSslSettings().isEnabled() })
4647
def 'should throw a MongoSocketOpenException when the AsynchronousSocket Stream fails to open'() {
4748
given:
4849
def connection = new InternalStreamConnectionFactory(
@@ -57,7 +58,7 @@ class AsyncStreamTimeoutsSpecification extends OperationFunctionalSpecification
5758
thrown(MongoSocketOpenException)
5859
}
5960

60-
@IgnoreIf({ System.getProperty('java.version').startsWith('1.6.') || getSslSettings().isEnabled() })
61+
@IgnoreIf({ isNotAtLeastJava7() || getSslSettings().isEnabled() })
6162
def 'should throw a MongoSocketReadTimeoutException with the AsynchronousSocket stream'() {
6263
given:
6364
def connection = new InternalStreamConnectionFactory(

driver-core/src/test/functional/com/mongodb/connection/SocketStreamHelperSpecification.groovy

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ import javax.net.ssl.SSLSocket
2828
import javax.net.ssl.SSLSocketFactory
2929

3030
import static com.mongodb.ClusterFixture.getPrimary
31+
import static com.mongodb.ClusterFixture.isNotAtLeastJava7
32+
import static com.mongodb.ClusterFixture.isNotAtLeastJava8
3133
import static java.util.concurrent.TimeUnit.MILLISECONDS
3234
import static java.util.concurrent.TimeUnit.SECONDS
3335

@@ -67,8 +69,8 @@ class SocketStreamHelperSpecification extends Specification {
6769
socket?.close()
6870
}
6971
70-
@IgnoreIf({ !ClusterFixture.sslSettings.enabled || System.getProperty('java.version').startsWith('1.6.') })
71-
def 'should enable SSL options if socket is an instance of SSLSocket'() {
72+
@IgnoreIf({ !ClusterFixture.sslSettings.enabled || isNotAtLeastJava7() })
73+
def 'should enable host name verification if socket is an instance of SSLSocket'() {
7274
given:
7375
SSLSocket socket = SSLSocketFactory.default.createSocket()
7476
@@ -77,7 +79,6 @@ class SocketStreamHelperSpecification extends Specification {
7779
7880
then:
7981
socket.getSSLParameters().endpointIdentificationAlgorithm == (sslSettings.invalidHostNameAllowed ? null : 'HTTPS')
80-
socket.getSSLParameters().getServerNames() == [new SNIHostName(getPrimary().getHost())]
8182
8283
cleanup:
8384
socket?.close()
@@ -88,6 +89,25 @@ class SocketStreamHelperSpecification extends Specification {
8889
SslSettings.builder().enabled(true).invalidHostNameAllowed(true).build()]
8990
}
9091
92+
@IgnoreIf({ !ClusterFixture.sslSettings.enabled || isNotAtLeastJava8() })
93+
def 'should enable SNI if socket is an instance of SSLSocket'() {
94+
given:
95+
SSLSocket socket = SSLSocketFactory.default.createSocket()
96+
97+
when:
98+
SocketStreamHelper.initialize(socket, getPrimary(), SocketSettings.builder().build(), sslSettings)
99+
100+
then:
101+
socket.getSSLParameters().getServerNames() == [new SNIHostName(getPrimary().getHost())]
102+
103+
cleanup:
104+
socket?.close()
105+
106+
where:
107+
sslSettings << [SslSettings.builder().enabled(true).build(),
108+
SslSettings.builder().enabled(false).build()]
109+
}
110+
91111
def 'should throw MongoInternalException is ssl is enabled and the socket is not an instance of SSLSocket'() {
92112
given:
93113
Socket socket = SocketFactory.default.createSocket()

driver-core/src/test/unit/com/mongodb/connection/SslSettingsSpecification.groovy

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import com.mongodb.MongoInternalException
2121
import spock.lang.IgnoreIf
2222
import spock.lang.Specification
2323

24+
import static com.mongodb.ClusterFixture.isNotAtLeastJava7
2425
import static com.mongodb.connection.SslSettings.builder
2526

2627

@@ -30,7 +31,7 @@ class SslSettingsSpecification extends Specification {
3031
!builder().build().enabled
3132
}
3233

33-
@IgnoreIf({ System.getProperty('java.version').startsWith('1.6.') })
34+
@IgnoreIf({ isNotAtLeastJava7() })
3435
def 'should enable'() {
3536
expect:
3637
builder().enabled(true).build().enabled
@@ -70,14 +71,14 @@ class SslSettingsSpecification extends Specification {
7071
!builder().applyConnectionString(new ConnectionString('mongodb://localhost/?ssl=false')).build().invalidHostNameAllowed
7172
}
7273

73-
@IgnoreIf({ System.getProperty('java.version').startsWith('1.6.') })
74+
@IgnoreIf({ isNotAtLeastJava7() })
7475
def 'should apply connection string with ssl'() {
7576
expect:
7677
builder().applyConnectionString(new ConnectionString('mongodb://localhost/?ssl=true')).build().enabled
7778
!builder().applyConnectionString(new ConnectionString('mongodb://localhost/?ssl=true')).build().invalidHostNameAllowed
7879
}
7980

80-
@IgnoreIf({ System.getProperty('java.version').startsWith('1.6.') })
81+
@IgnoreIf({ isNotAtLeastJava7() })
8182
def 'should apply connection string with ssl and sslInvalidHostNameAllowed'() {
8283
expect:
8384
builder().applyConnectionString(new ConnectionString('mongodb://localhost/?ssl=true&sslInvalidHostNameAllowed=true'))
@@ -100,7 +101,7 @@ class SslSettingsSpecification extends Specification {
100101
builder().enabled(true).invalidHostNameAllowed(true).build().hashCode()
101102
}
102103

103-
@IgnoreIf({ System.getProperty('java.version').startsWith('1.6.') })
104+
@IgnoreIf({ isNotAtLeastJava7() })
104105
def 'unequivalent settings should not be equal or have the same hash code'() {
105106
expect:
106107
builder().build() != builder().enabled(true).build()

driver-core/src/test/unit/com/mongodb/internal/SslHelperSpecification.groovy

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@ import spock.lang.Specification
2424
import javax.net.ssl.SNIHostName
2525
import javax.net.ssl.SSLParameters
2626

27+
import static com.mongodb.ClusterFixture.isNotAtLeastJava7
28+
import static com.mongodb.ClusterFixture.isNotAtLeastJava8
2729

28-
@IgnoreIf({ System.getProperty('java.version').startsWith('1.6.') })
30+
@IgnoreIf({ isNotAtLeastJava7() })
2931
class SslHelperSpecification extends Specification {
3032
def 'should enable HTTPS host name verification'() {
3133
given:
@@ -38,7 +40,7 @@ class SslHelperSpecification extends Specification {
3840
sslParameters.getEndpointIdentificationAlgorithm() == 'HTTPS'
3941
}
4042

41-
@IgnoreIf({ System.getProperty('java.version').startsWith('1.7.') })
43+
@IgnoreIf({ isNotAtLeastJava8() })
4244
def 'should enable server name indicator'() {
4345
given:
4446
def serverName = 'server.me'

driver/src/test/unit/com/mongodb/MongoClientOptionsSpecification.groovy

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import spock.lang.Specification
3030
import javax.net.SocketFactory
3131
import javax.net.ssl.SSLSocketFactory
3232

33+
import static com.mongodb.ClusterFixture.isNotAtLeastJava7
3334
import static com.mongodb.CustomMatchers.isTheSameAs
3435
import static java.util.concurrent.TimeUnit.MILLISECONDS
3536
import static java.util.concurrent.TimeUnit.SECONDS
@@ -214,7 +215,7 @@ class MongoClientOptionsSpecification extends Specification {
214215
options.sslSettings == SslSettings.builder().enabled(true).invalidHostNameAllowed(true).build()
215216
}
216217

217-
@IgnoreIf({ System.getProperty('java.version').startsWith('1.6.') })
218+
@IgnoreIf({ isNotAtLeastJava7() })
218219
def 'should get socketFactory based on sslEnabled'() {
219220
when:
220221
MongoClientOptions.Builder builder = MongoClientOptions.builder()

driver/src/test/unit/com/mongodb/MongoClientURISpecification.groovy

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import spock.lang.IgnoreIf
2020
import spock.lang.Specification
2121
import spock.lang.Unroll
2222

23+
import static com.mongodb.ClusterFixture.isNotAtLeastJava7
2324
import static com.mongodb.MongoCredential.createCredential
2425
import static com.mongodb.MongoCredential.createGSSAPICredential
2526
import static com.mongodb.MongoCredential.createMongoCRCredential
@@ -123,7 +124,7 @@ class MongoClientURISpecification extends Specification {
123124
.withWTimeout(5, MILLISECONDS)
124125
}
125126

126-
@IgnoreIf({ System.getProperty('java.version').startsWith('1.6.') })
127+
@IgnoreIf({ isNotAtLeastJava7() })
127128
def 'should correctly parse URI options for #type'() {
128129
given:
129130
def uri = new MongoClientURI('mongodb://localhost/?minPoolSize=5&maxPoolSize=10&waitQueueMultiple=7&waitQueueTimeoutMS=150&'
@@ -248,7 +249,7 @@ class MongoClientURISpecification extends Specification {
248249
options.getConnectionsPerHost() == 250
249250
}
250251

251-
@IgnoreIf({ System.getProperty('java.version').startsWith('1.6.') })
252+
@IgnoreIf({ isNotAtLeastJava7() })
252253
def 'should be equal to another MongoClientURI with the same string values'() {
253254
expect:
254255
uri1 == uri2

0 commit comments

Comments
 (0)